diff --git a/mario/blocks.cpp b/mario/blocks.cpp index 2d97c7f..5049871 100644 --- a/mario/blocks.cpp +++ b/mario/blocks.cpp @@ -8,9 +8,11 @@ MarioBlock::MarioBlock( int x, int y, std::shared_ptr< SDLPP::Renderer > renderer, std::shared_ptr< SDLPP::Texture > texture, - SDL_Rect src ) + SDL_Rect src, bool destructible ) : RectangleRender( x * BLOCK_SIZE, 1 - ( 16 - y ) * BLOCK_SIZE, - BLOCK_SIZE, BLOCK_SIZE, renderer, texture, src ) {} + BLOCK_SIZE, BLOCK_SIZE, renderer, texture, src ) { + _destructible = destructible; +} void MarioBlock::visit( SDLPP::Visitor &visitor ) { if ( !_tool && _terrain && visitor.getVisitorType() == VisitorType::Terrain ) { @@ -20,6 +22,9 @@ void MarioBlock::visit( SDLPP::Visitor &visitor ) { visitor.getVisitorType() == VisitorType::Modifier ) { destroy(); } + if(visitor.getFromId() == MARIO_TOP_DETECT && _destructible) { + destroy(); + } visitor.visit( *this ); } void MarioBlock::setTool( bool tool ) { @@ -157,8 +162,8 @@ const std::unordered_map< uint64_t, const SDL_Rect * > block_mapping = { std::shared_ptr< SDLPP::RectangleRender > createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, std::shared_ptr< SDLPP::Texture > &texture, const SDL_Rect &src, - uint64_t id, bool collision = false ) { - auto block = std::make_shared< MarioBlock >( x, y, renderer, texture, src ); + uint64_t id, bool collision = false, bool destructible = false ) { + auto block = std::make_shared< MarioBlock >( x, y, renderer, texture, src, destructible ); block->setId( id ); block->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); block->setStatic(); @@ -195,34 +200,34 @@ std::shared_ptr< SDLPP::RectangleRender > createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, std::shared_ptr< SDLPP::Texture > texture, - bool collision ) { + bool collision, bool destructible ) { return createBlock( renderer, x, y, texture, getSourceRectByID( block_id, type ), block_id, - collision ); + collision, destructible ); } std::shared_ptr< SDLPP::RectangleRender > createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, std::shared_ptr< SDLPP::Texture > texture, - bool collision ) { + bool collision, bool destructible ) { return createTerrainBlock( block_id, type, renderer, 0, 0, texture, - collision ); + collision, destructible ); } std::shared_ptr< SDLPP::RectangleRender > createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, - bool collision ) { + bool collision, bool destructible ) { return createTerrainBlock( block_id, type, renderer, x, y, - g_terrain_texture, collision ); + g_terrain_texture, collision, destructible ); } std::shared_ptr< SDLPP::RectangleRender > createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, - bool collision ) { + bool collision, bool destructible ) { return createTerrainBlock( block_id, type, renderer, g_terrain_texture, - collision ); + collision, destructible ); } std::shared_ptr< SDLPP::RectangleRender > diff --git a/mario/blocks.hpp b/mario/blocks.hpp index 70b06b5..3fdd416 100644 --- a/mario/blocks.hpp +++ b/mario/blocks.hpp @@ -7,7 +7,7 @@ class MarioBlock : public SDLPP::RectangleRender { public: MarioBlock( int x, int y, std::shared_ptr< SDLPP::Renderer > renderer, - std::shared_ptr< SDLPP::Texture > texture, SDL_Rect src ); + std::shared_ptr< SDLPP::Texture > texture, SDL_Rect src, bool destructible = false ); void visit( SDLPP::Visitor &visitor ) override; void setTool( bool tool = true ); void setTerrain( bool terrain = true ); @@ -15,6 +15,7 @@ public: private: bool _tool = false; bool _terrain = true; + bool _destructible = false; }; extern const std::vector< uint64_t > possibleBlocks; @@ -35,21 +36,21 @@ struct BlockRole { std::shared_ptr< SDLPP::RectangleRender > createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, - bool collision = false ); + bool collision = false, bool destructible = false ); std::shared_ptr< SDLPP::RectangleRender > createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, - bool collision = false ); + bool collision = false, bool destructible = false ); std::shared_ptr< SDLPP::RectangleRender > createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, std::shared_ptr< SDLPP::Texture > texture, - bool collision = false ); + bool collision = false, bool destructible = false ); std::shared_ptr< SDLPP::RectangleRender > createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, std::shared_ptr< SDLPP::Texture > texture, - bool collision = false ); + bool collision = false, bool destructible = false ); std::shared_ptr< SDLPP::RectangleRender > createMario( LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y ); diff --git a/mario/maploader.cpp b/mario/maploader.cpp index 9eeffd3..7662036 100644 --- a/mario/maploader.cpp +++ b/mario/maploader.cpp @@ -41,13 +41,16 @@ void loadMap( std::shared_ptr< SDLPP::Scene > &scene, } } bool collision = false; + bool destructible = false; if ( id == FLOOR_ID || id == BRICK_ID || id == BRICK_TOP_ID ) { collision = true; + if(id != FLOOR_ID) + destructible = true; } // TODO add modifiers to createTerrainBlock auto obj = createTerrainBlock( id, static_cast< LandType::Value >( type ), - renderer, i, j, collision ); + renderer, i, j, collision, destructible ); if ( obj != nullptr ) scene->addObject( obj ); if ( character ) { diff --git a/mario/mario.cpp b/mario/mario.cpp index 0852ab7..a07f3e4 100644 --- a/mario/mario.cpp +++ b/mario/mario.cpp @@ -18,7 +18,7 @@ Mario::Mario(const std::shared_ptr< SDLPP::Renderer > &renderer) : SDLPP::Rectan addCollision( SDLPP::RectColider( 0.9, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) ); addCollision( - SDLPP::RectColider( 0.21, 0, 0.65, 0.15, MARIO_TOP_DETECT ) ); + SDLPP::RectColider( 0.45, 0, 0.1, 0.15, MARIO_TOP_DETECT ) ); setStatic( false ); } void Mario::walkLeft() {