Mario: simple jump

This commit is contained in:
zv0n 2021-05-22 23:54:01 +02:00
parent d9a88f2de2
commit 930875ccf8
3 changed files with 47 additions and 3 deletions

View File

@ -35,6 +35,7 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
break;
case SDLK_SPACE:
case SDLK_w:
mario->jump();
break;
case SDLK_s:
break;
@ -54,8 +55,9 @@ void handleKeyUp( SDL_Keycode key ) {
case SDLK_d:
mario->walkLeft();
break;
case SDLK_SPACE:
case SDLK_w:
case SDLK_s:
mario->stopJump();
default:
break;
}

View File

@ -45,9 +45,25 @@ void Mario::setStanding() {
void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position) {
// handle gravity
if ( !visitor.isOnGround() ) {
// TODO - https://haydnscarlett.wordpress.com/2020/07/30/marios-jump/
if (jumping) {
if(stop_jump && getPos().getY() - min_jump < 0.05) {
jumping = false;
resetMovementY();
} else if( getPos().getY() - max_jump < 0.05 ) {
std::cout << "jumping end" << std::endl;
std::cout << "MAX: " << max_jump << ", MY: " << getPos().getY() << std::endl;
jumping = false;
resetMovementY();
} else if ( getPos().getY() - slow_jump < 0.05 && getMovement().getY() < -1*jump_movement/5 ) {
std::cout << "Slowing down" << std::endl;
addMovement(0, jump_movement/50);
}
}
on_ground = visitor.isOnGround();
if ( !on_ground && !jumping ) {
setMovement( getMovement().getX(), fall_movement );
} else {
} else if(!jumping) {
resetMovementY();
setPos(getPos().getX(), visitor.getGroundY() - BLOCK_SIZE);
}
@ -58,3 +74,20 @@ void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_p
setPos( previous_position.getX(), getPos().getY() );
}
}
void Mario::jump() {
if(!on_ground)
return;
std::cout << "JUMP!" << std::endl;
jumping = true;
stop_jump = false;
max_jump = getPos().getY() - 3*BLOCK_SIZE;
min_jump = getPos().getY() - 1*BLOCK_SIZE;
slow_jump = getPos().getY() - 2*BLOCK_SIZE;
setMovement( getMovement().getX(), -jump_movement );
}
void Mario::stopJump() {
std::cout << "STOP JUMP!" << std::endl;
stop_jump = true;
}

View File

@ -11,10 +11,19 @@ public:
void walkRight();
void setStanding();
void handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position);
void jump();
void stopJump();
private:
bool faces_right = true;
double side_movement = 0.8;
double fall_movement = 1;
double jump_movement = 1.5;
bool jumping = false;
bool stop_jump = false;
double max_jump = 0;
double min_jump = 0;
double slow_jump = 0;
bool on_ground = true;
};
#endif