Mario: prepartations for multiple moving objects

This commit is contained in:
2021-08-05 00:32:17 +02:00
parent 37f7bab63e
commit bfe658618e
19 changed files with 321 additions and 249 deletions
+37 -37
View File
@@ -15,8 +15,8 @@
#include "objectids.hpp"
#include "blocks.hpp"
#include "maploader.hpp"
#include "mario_visitor.hpp"
#include "mario.hpp"
#include "visitors/visitor_generator.hpp"
bool quit = false;
bool update = false;
@@ -27,6 +27,8 @@ std::shared_ptr< SDLPP::TextRenderer > fps = nullptr;
std::shared_ptr< SDLPP::TextRenderer > coins = nullptr;
int coin_count = 0;
std::vector< std::shared_ptr< MarioBlock > > moving_objects = {};
std::mutex render_mutex;
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
@@ -51,8 +53,9 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
!scene.getRenderer().getRenderColiders() );
break;
case SDLK_f:
if(fps)
fps->setHidden(!fps->getHidden());
if ( fps ) {
fps->setHidden( !fps->getHidden() );
}
default:
break;
}
@@ -82,8 +85,9 @@ void pollEvents( SDLPP::Scene &scene ) {
quit = true;
break;
case SDL_KEYDOWN:
if ( !event.key.repeat )
if ( !event.key.repeat ) {
handleKeyDown( event.key.keysym.sym, scene );
}
break;
case SDL_KEYUP:
handleKeyUp( event.key.keysym.sym );
@@ -130,24 +134,17 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
while ( true ) {
SDL_framerateDelay( &gFPS );
pollEvents( *scene );
std::lock_guard<std::mutex> lock(render_mutex);
std::lock_guard< std::mutex > lock( render_mutex );
scene->updateScene();
MarioVisitor mv(mario->getMovement().getY() < 0);
scene->visitCollisions( *mario, mv );
if ( mv.isDead() ) {
quit = true;
}
mario->handleVisitor(mv);
if ( mv.hasCoin() ) {
coin_count++;
coins->changeText(std::to_string(coin_count) + " COINS");
}
if ( mv.hasCoinBlock() ) {
auto coin = mv.getCoinBlock();
scene->addObject(coin);
scene->setZIndex(coin, 1);
}
auto prev_coin_count = coin_count;
// TODO visit all moving objects
auto visitor = getVisitor(*mario, *scene, quit, coin_count, moving_objects);
scene->visitCollisions( *mario, *visitor );
mario->handleVisitor( *visitor );
if(coin_count != prev_coin_count) {
coins->changeText( std::to_string( coin_count ) + " COINS" );
}
// if player is > 0.7 of playground, move everything left
auto playerX = mario->getRect().x;
auto width = scene->getWidth();
@@ -189,12 +186,11 @@ int main() {
scene->addObject( bg );
g_mario_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture =
std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png",
MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture->setAlpha( 100 );
mario = std::make_shared< Mario >(renderer);
scene->addObject(mario);
mario = std::make_shared< Mario >( renderer );
scene->addObject( mario );
auto defeat =
std::make_shared< SDLPP::RectangleRender >( 0, 1.01, 0, 0, renderer );
@@ -221,18 +217,22 @@ int main() {
loadMap( scene, mario, "test_binary2.bin" );
auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 );
fps = std::make_shared<SDLPP::TextRenderer>(0.2, 0, 0.78, 0.1, renderer, font, "0fps", "#FFFFFF", "#000000", 0.1, SDLPP_TEXT_RIGHT);
fps->setAlignment(SDLPP::OBJ_END, SDLPP::OBJ_START);
fps->setId(0);
fps = std::make_shared< SDLPP::TextRenderer >(
0.2, 0, 0.78, 0.1, renderer, font, "0fps", "#FFFFFF", "#000000", 0.1,
SDLPP_TEXT_RIGHT );
fps->setAlignment( SDLPP::OBJ_END, SDLPP::OBJ_START );
fps->setId( 0 );
fps->setPermanent();
fps->setHidden(true);
scene->addObject(fps);
fps->setHidden( true );
scene->addObject( fps );
coins = std::make_shared<SDLPP::TextRenderer>(0.2, 0, 0.78, 0.1, renderer, font, "0 COINS", "#FFFFFF", "#000000", 0.1, SDLPP_TEXT_RIGHT);
coins->setAlignment(SDLPP::OBJ_START, SDLPP::OBJ_START);
coins->setId(0);
coins = std::make_shared< SDLPP::TextRenderer >(
0.2, 0, 0.78, 0.1, renderer, font, "0 COINS", "#FFFFFF", "#000000", 0.1,
SDLPP_TEXT_RIGHT );
coins->setAlignment( SDLPP::OBJ_START, SDLPP::OBJ_START );
coins->setId( 0 );
coins->setPermanent();
scene->addObject(coins);
scene->addObject( coins );
FPSmanager gFPS;
SDL_initFramerate( &gFPS );
@@ -247,9 +247,9 @@ int main() {
while ( !quit ) {
SDL_PumpEvents();
SDL_framerateDelay( &gFPS );
std::lock_guard<std::mutex> lock(render_mutex);
std::lock_guard< std::mutex > lock( render_mutex );
mario->setStanding();
if(update) {
if ( update ) {
scene->updateSizeAndPosition();
update = false;
}
@@ -257,7 +257,7 @@ int main() {
renderer->presentRenderer();
frames++;
if ( SDL_GetTicks() - base >= 1000 ) {
fps->changeText(std::to_string(frames) + " fps");
fps->changeText( std::to_string( frames ) + " fps" );
frames = 0;
base = SDL_GetTicks();
}