Mario: top collision detection

This commit is contained in:
zv0n 2021-05-23 23:57:29 +02:00
parent 8672830db8
commit bea479bf72
4 changed files with 12 additions and 1 deletions

View File

@ -41,7 +41,7 @@ void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
} }
} }
bool collision = false; bool collision = false;
if ( id == FLOOR_ID ) { if ( id == FLOOR_ID || id == BRICK_ID || id == BRICK_TOP_ID ) {
collision = true; collision = true;
} }
// TODO add modifiers to createTerrainBlock // TODO add modifiers to createTerrainBlock

View File

@ -73,6 +73,9 @@ void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_p
setPos(getPos().getX(), visitor.getGroundY() - BLOCK_SIZE); setPos(getPos().getX(), visitor.getGroundY() - BLOCK_SIZE);
} }
} }
if(visitor.topBlock() && getMovement().getY() < 0) {
resetMovementY();
}
// make sure Mario isn't stuck inside a wall // make sure Mario isn't stuck inside a wall
if ( visitor.isStopped() || if ( visitor.isStopped() ||
( !visitor.canGoLeft() && previous_position.getX() > getPos().getX() ) || ( !visitor.canGoLeft() && previous_position.getX() > getPos().getX() ) ||

View File

@ -6,6 +6,8 @@ void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
auto id = obj.getId(); auto id = obj.getId();
switch ( id ) { switch ( id ) {
case FLOOR_ID: case FLOOR_ID:
case BRICK_ID:
case BRICK_TOP_ID:
if ( from == MARIO_FLOOR_DETECT ) { if ( from == MARIO_FLOOR_DETECT ) {
onGround = true; onGround = true;
groundY = obj.getPos().getY(); groundY = obj.getPos().getY();
@ -13,6 +15,8 @@ void MarioVisitor::visit( const SDLPP::RenderObject &obj ) {
left = true; left = true;
} else if (from == MARIO_RIGHT_SIDE_DETECT ) { } else if (from == MARIO_RIGHT_SIDE_DETECT ) {
right = true; right = true;
} else if (from == MARIO_TOP_DETECT) {
top_hit = true;
} }
break; break;
case DEATH_ID: case DEATH_ID:

View File

@ -41,6 +41,9 @@ public:
double getGroundY() { double getGroundY() {
return groundY; return groundY;
} }
bool topBlock() {
return top_hit;
}
private: private:
bool onGround = false; bool onGround = false;
@ -52,6 +55,7 @@ private:
bool left = false; bool left = false;
bool right = false; bool right = false;
uint64_t _type = 0; uint64_t _type = 0;
bool top_hit = false;
}; };
#endif #endif