Mario: add goomba into moving_objects

This commit is contained in:
zvon 2021-08-08 21:45:50 +02:00
parent edeeadb232
commit 28d7e208fc

View File

@ -1,5 +1,6 @@
#include "../sdlpp/sdlpp.hpp" #include "../sdlpp/sdlpp.hpp"
#include "sprites.hpp" #include "sprites.hpp"
#include <memory>
#ifdef _WIN32 #ifdef _WIN32
#include "../sdlpp/SDL2/SDL2_framerate.h" #include "../sdlpp/SDL2/SDL2_framerate.h"
#include <ctime> #include <ctime>
@ -125,6 +126,7 @@ void pollEvents( SDLPP::Scene &scene ) {
auto prev_mario_pos = mario->getAbsolutePos(); auto prev_mario_pos = mario->getAbsolutePos();
scene.updateSizeAndPosition(); scene.updateSizeAndPosition();
moveToMarioPosition( scene, prev_mario_pos ); moveToMarioPosition( scene, prev_mario_pos );
update = true;
} }
default: default:
break; break;
@ -154,7 +156,7 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
moving_objects[i]->handleVisitor( *visitor ); moving_objects[i]->handleVisitor( *visitor );
auto rightmost_pos = moving_objects[i]->getAbsolutePos().getX() + auto rightmost_pos = moving_objects[i]->getAbsolutePos().getX() +
moving_objects[i]->getDoubleRect().second.getX(); moving_objects[i]->getDoubleRect().second.getX();
if(rightmost_pos < 0) { if(rightmost_pos < 0 && moving_objects[i] != mario) {
moving_objects[i]->destroy(); moving_objects[i]->destroy();
} }
} }
@ -175,14 +177,14 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
// if player is > 0.7 of playground, move everything left // if player is > 0.7 of playground, move everything left
auto playerX = mario->getRect().x; auto playerX = mario->getRect().x;
auto width = scene->getWidth(); auto width = scene->getWidth();
auto rightBarrier = width * 0.7; auto rightBarrier = width * 0.5;
auto rightmostX = auto rightmostX =
scene->rightmost()->getRect().x + scene->rightmost()->getRect().w; scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
scene->moveEverything( scene->moveEverything(
( playerX > rightBarrier && rightmostX > width ) * ( playerX > rightBarrier && rightmostX > width ) *
( rightBarrier - playerX ) / width, ( rightBarrier - playerX ) / width,
0 ); 0 );
update = playerX > rightBarrier && rightmostX > width; update = update || (playerX > rightBarrier && rightmostX > width);
global_frames++; global_frames++;
} }
} }
@ -203,6 +205,8 @@ int main() {
// prepare global vars // prepare global vars
g_terrain_texture = std::make_shared< SDLPP::Texture >( g_terrain_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
g_enemies_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/enemies.png", MARIO_OVERWORLD_COLORKEY );
auto scene = std::make_shared< SDLPP::Scene >( renderer ); auto scene = std::make_shared< SDLPP::Scene >( renderer );
g_playground = scene; g_playground = scene;
@ -268,10 +272,8 @@ int main() {
auto base = SDL_GetTicks(); auto base = SDL_GetTicks();
int frames = 0; int frames = 0;
std::thread inputThread( doInput, scene );
scene->moveEverything( -mario->getDoubleRect().first.getX() + 0.2, 0 ); scene->moveEverything( -mario->getDoubleRect().first.getX() + 0.2, 0 );
update = true; update = true;
moving_objects.push_back(mario);
std::unordered_set<uint64_t> background_ids = { std::unordered_set<uint64_t> background_ids = {
HILL_INCLINE_ID, HILL_DECLINE_ID, HILL_DOTS_RIGHT_ID, HILL_DOTS_LEFT_ID, HILL_INCLINE_ID, HILL_DECLINE_ID, HILL_DOTS_RIGHT_ID, HILL_DOTS_LEFT_ID,
@ -283,9 +285,25 @@ int main() {
scene->setBackgroundObjectIDs(background_ids); scene->setBackgroundObjectIDs(background_ids);
scene->updateBackgroundObjectZIndex(); scene->updateBackgroundObjectZIndex();
// we want mario to be first because visiting isn't perfect
moving_objects.push_back(mario);
std::unordered_set<int> moving_object_ids = {
GOOMBA_ID,
};
for(auto &obj : scene->getObjects(moving_object_ids)) {
moving_objects.push_back(std::dynamic_pointer_cast<MarioBlock>(obj));
}
std::thread inputThread( doInput, scene );
SDL_PumpEvents();
scene->updateSizeAndPosition();
scene->renderScene();
renderer->presentRenderer();
update = true;
while ( !quit ) { while ( !quit ) {
SDL_PumpEvents();
SDL_framerateDelay( &gFPS ); SDL_framerateDelay( &gFPS );
SDL_PumpEvents();
std::lock_guard< std::mutex > lock( render_mutex ); std::lock_guard< std::mutex > lock( render_mutex );
mario->setStanding(); mario->setStanding();
if ( update ) { if ( update ) {