Mario: proper position when standing on ground

This commit is contained in:
zv0n 2021-05-22 22:36:53 +02:00
parent d04cbea02d
commit 0358aa36c7
3 changed files with 11 additions and 4 deletions

View File

@ -156,6 +156,7 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
mario->setMovement( mario->getMovement().getX(), FALL_MOVEMENT ); mario->setMovement( mario->getMovement().getX(), FALL_MOVEMENT );
} else { } else {
mario->resetMovementY(); mario->resetMovementY();
mario->setPos(mario->getPos().getX(), mv.getGroundY() - BLOCK_SIZE);
} }
if ( mv.isStopped() || if ( mv.isStopped() ||
( !mv.canGoLeft() && prevPos.getX() > mario->getPos().getX() ) || ( !mv.canGoLeft() && prevPos.getX() > mario->getPos().getX() ) ||
@ -211,7 +212,7 @@ int main() {
mario->setMovement( 0, 0 ); mario->setMovement( 0, 0 );
mario->setMovementSpeed( 0.4 ); mario->setMovementSpeed( 0.4 );
mario->addCollision( mario->addCollision(
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.16, MARIO_FLOOR_DETECT ) ); SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) );
mario->addCollision( mario->addCollision(
SDLPP::RectColider( 0, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) ); SDLPP::RectColider( 0, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) );
mario->addCollision( mario->addCollision(

View File

@ -6,12 +6,14 @@ 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:
if ( from == MARIO_FLOOR_DETECT ) if ( from == MARIO_FLOOR_DETECT ) {
onGround = true; onGround = true;
else if ( from == MARIO_LEFT_SIDE_DETECT ) groundY = obj.getPos().getY();
} else if ( from == MARIO_LEFT_SIDE_DETECT ) {
left = true; left = true;
else if (from == MARIO_RIGHT_SIDE_DETECT ) } else if (from == MARIO_RIGHT_SIDE_DETECT ) {
right = true; right = true;
}
break; break;
case DEATH_ID: case DEATH_ID:
death = true; death = true;

View File

@ -38,9 +38,13 @@ public:
bool canGoRight() { bool canGoRight() {
return !right; return !right;
} }
double getGroundY() {
return groundY;
}
private: private:
bool onGround = false; bool onGround = false;
double groundY = 0;
bool death = false; bool death = false;
bool stop = false; bool stop = false;
double newX; double newX;