Mario: check before bouncing

This commit is contained in:
zvon 2021-05-26 18:24:09 +02:00
parent 734b0b58cb
commit 6e61eb03b9
6 changed files with 56 additions and 2 deletions

View File

@ -30,7 +30,17 @@ void MarioBlock::visit( SDLPP::Visitor &visitor ) {
if( _destructible ) { if( _destructible ) {
destroy(); destroy();
} else { } else {
// TODO check if anything above BounceVisitor bv;
bv.setVisitorType(VisitorType::Terrain);
setPos(getPos() - SDLPP::Vec2D<double>(0, BLOCK_SIZE));
if(getCollisions().size() < 2)
addCollision(SDLPP::RectColider(0.1, 0.1, 0.8, 0.8, 69));
updateSizeAndPosition();
g_playground->visitCollisions(*this, bv);
setPos(getPos() + SDLPP::Vec2D<double>(0, BLOCK_SIZE));
updateSizeAndPosition();
if(bv.canBounce())
bounce(); bounce();
} }
} }

View File

@ -4,3 +4,4 @@
std::shared_ptr< SDLPP::Texture > g_terrain_texture{}; std::shared_ptr< SDLPP::Texture > g_terrain_texture{};
std::shared_ptr< SDLPP::Texture > g_mario_texture{}; std::shared_ptr< SDLPP::Texture > g_mario_texture{};
std::shared_ptr< SDLPP::Texture > g_translucent_terrain_texture{}; std::shared_ptr< SDLPP::Texture > g_translucent_terrain_texture{};
std::shared_ptr< SDLPP::Scene > g_playground{};

View File

@ -6,5 +6,6 @@
extern std::shared_ptr< SDLPP::Texture > g_terrain_texture; extern std::shared_ptr< SDLPP::Texture > g_terrain_texture;
extern std::shared_ptr< SDLPP::Texture > g_mario_texture; extern std::shared_ptr< SDLPP::Texture > g_mario_texture;
extern std::shared_ptr< SDLPP::Texture > g_translucent_terrain_texture; extern std::shared_ptr< SDLPP::Texture > g_translucent_terrain_texture;
extern std::shared_ptr< SDLPP::Scene > g_playground;
#endif #endif

View File

@ -166,6 +166,7 @@ int main() {
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
auto scene = std::make_shared< SDLPP::Scene >( renderer ); auto scene = std::make_shared< SDLPP::Scene >( renderer );
g_playground = scene;
auto bg = std::make_shared< SDLPP::RectangleRender >( auto bg = std::make_shared< SDLPP::RectangleRender >(
0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true ); 0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true );
bg->setPermanent(); bg->setPermanent();

View File

@ -38,3 +38,17 @@ void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
break; break;
} }
} }
void BounceVisitor::visit( const SDLPP::RenderObject &obj ) {
auto id = obj.getId();
switch ( id ) {
case FLOOR_ID:
case BRICK_ID:
case BRICK_TOP_ID:
if(from == 69) {
hits += 1;
}
default:
break;
}
}

View File

@ -80,4 +80,31 @@ private:
SDLPP::Vec2D<double> movement_blockage; SDLPP::Vec2D<double> movement_blockage;
}; };
class BounceVisitor : public SDLPP::Visitor {
public:
BounceVisitor() {}
virtual void visit( const SDLPP::RenderObject &obj ) override;
virtual void setFromId( uint64_t id ) override {
from = id;
}
virtual uint64_t getFromId() override {
return from;
}
virtual void setVisitorType( uint64_t type ) override {
_type = type;
}
virtual uint64_t getVisitorType() override {
return _type;
}
bool canBounce() {
return hits <= 2;
}
private:
int hits = 0;
uint64_t from;
uint64_t _type;
};
#endif #endif