Mario: destructible blocks

This commit is contained in:
zv0n 2021-05-24 00:07:40 +02:00
parent bea479bf72
commit b75b44201a
4 changed files with 28 additions and 19 deletions

View File

@ -8,9 +8,11 @@
MarioBlock::MarioBlock( int x, int y, MarioBlock::MarioBlock( int x, int y,
std::shared_ptr< SDLPP::Renderer > renderer, std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Texture > texture, std::shared_ptr< SDLPP::Texture > texture,
SDL_Rect src ) SDL_Rect src, bool destructible )
: RectangleRender( x * BLOCK_SIZE, 1 - ( 16 - y ) * BLOCK_SIZE, : 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 ) { void MarioBlock::visit( SDLPP::Visitor &visitor ) {
if ( !_tool && _terrain && if ( !_tool && _terrain &&
visitor.getVisitorType() == VisitorType::Terrain ) { visitor.getVisitorType() == VisitorType::Terrain ) {
@ -20,6 +22,9 @@ void MarioBlock::visit( SDLPP::Visitor &visitor ) {
visitor.getVisitorType() == VisitorType::Modifier ) { visitor.getVisitorType() == VisitorType::Modifier ) {
destroy(); destroy();
} }
if(visitor.getFromId() == MARIO_TOP_DETECT && _destructible) {
destroy();
}
visitor.visit( *this ); visitor.visit( *this );
} }
void MarioBlock::setTool( bool tool ) { 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 > std::shared_ptr< SDLPP::RectangleRender >
createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
std::shared_ptr< SDLPP::Texture > &texture, const SDL_Rect &src, std::shared_ptr< SDLPP::Texture > &texture, const SDL_Rect &src,
uint64_t id, bool collision = false ) { uint64_t id, bool collision = false, bool destructible = false ) {
auto block = std::make_shared< MarioBlock >( x, y, renderer, texture, src ); auto block = std::make_shared< MarioBlock >( x, y, renderer, texture, src, destructible );
block->setId( id ); block->setId( id );
block->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); block->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
block->setStatic(); block->setStatic();
@ -195,34 +200,34 @@ std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type, createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
std::shared_ptr< SDLPP::Texture > texture, std::shared_ptr< SDLPP::Texture > texture,
bool collision ) { bool collision, bool destructible ) {
return createBlock( renderer, x, y, texture, return createBlock( renderer, x, y, texture,
getSourceRectByID( block_id, type ), block_id, getSourceRectByID( block_id, type ), block_id,
collision ); collision, destructible );
} }
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type, createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, std::shared_ptr< SDLPP::Renderer > &renderer,
std::shared_ptr< SDLPP::Texture > texture, std::shared_ptr< SDLPP::Texture > texture,
bool collision ) { bool collision, bool destructible ) {
return createTerrainBlock( block_id, type, renderer, 0, 0, texture, return createTerrainBlock( block_id, type, renderer, 0, 0, texture,
collision ); collision, destructible );
} }
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type, createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
bool collision ) { bool collision, bool destructible ) {
return createTerrainBlock( block_id, type, renderer, x, y, return createTerrainBlock( block_id, type, renderer, x, y,
g_terrain_texture, collision ); g_terrain_texture, collision, destructible );
} }
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type, createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, std::shared_ptr< SDLPP::Renderer > &renderer,
bool collision ) { bool collision, bool destructible ) {
return createTerrainBlock( block_id, type, renderer, g_terrain_texture, return createTerrainBlock( block_id, type, renderer, g_terrain_texture,
collision ); collision, destructible );
} }
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >

View File

@ -7,7 +7,7 @@
class MarioBlock : public SDLPP::RectangleRender { class MarioBlock : public SDLPP::RectangleRender {
public: public:
MarioBlock( int x, int y, std::shared_ptr< SDLPP::Renderer > renderer, 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 visit( SDLPP::Visitor &visitor ) override;
void setTool( bool tool = true ); void setTool( bool tool = true );
void setTerrain( bool terrain = true ); void setTerrain( bool terrain = true );
@ -15,6 +15,7 @@ public:
private: private:
bool _tool = false; bool _tool = false;
bool _terrain = true; bool _terrain = true;
bool _destructible = false;
}; };
extern const std::vector< uint64_t > possibleBlocks; extern const std::vector< uint64_t > possibleBlocks;
@ -35,21 +36,21 @@ struct BlockRole {
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type, createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, std::shared_ptr< SDLPP::Renderer > &renderer,
bool collision = false ); bool collision = false, bool destructible = false );
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type, createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
bool collision = false ); bool collision = false, bool destructible = false );
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type, createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, std::shared_ptr< SDLPP::Renderer > &renderer,
std::shared_ptr< SDLPP::Texture > texture, std::shared_ptr< SDLPP::Texture > texture,
bool collision = false ); bool collision = false, bool destructible = false );
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type, createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
std::shared_ptr< SDLPP::Texture > texture, std::shared_ptr< SDLPP::Texture > texture,
bool collision = false ); bool collision = false, bool destructible = false );
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createMario( LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer, createMario( LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer,
int x, int y ); int x, int y );

View File

@ -41,13 +41,16 @@ void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
} }
} }
bool collision = false; bool collision = false;
bool destructible = false;
if ( id == FLOOR_ID || id == BRICK_ID || id == BRICK_TOP_ID ) { if ( id == FLOOR_ID || id == BRICK_ID || id == BRICK_TOP_ID ) {
collision = true; collision = true;
if(id != FLOOR_ID)
destructible = true;
} }
// TODO add modifiers to createTerrainBlock // TODO add modifiers to createTerrainBlock
auto obj = auto obj =
createTerrainBlock( id, static_cast< LandType::Value >( type ), createTerrainBlock( id, static_cast< LandType::Value >( type ),
renderer, i, j, collision ); renderer, i, j, collision, destructible );
if ( obj != nullptr ) if ( obj != nullptr )
scene->addObject( obj ); scene->addObject( obj );
if ( character ) { if ( character ) {

View File

@ -18,7 +18,7 @@ Mario::Mario(const std::shared_ptr< SDLPP::Renderer > &renderer) : SDLPP::Rectan
addCollision( addCollision(
SDLPP::RectColider( 0.9, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) ); SDLPP::RectColider( 0.9, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) );
addCollision( 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 ); setStatic( false );
} }
void Mario::walkLeft() { void Mario::walkLeft() {