Jumping experiment

This commit is contained in:
zv0n 2021-05-23 23:32:15 +02:00
parent 930875ccf8
commit 088fb4d15a
2 changed files with 42 additions and 16 deletions

View File

@ -10,7 +10,7 @@ Mario::Mario(const std::shared_ptr< SDLPP::Renderer > &renderer) : SDLPP::Rectan
setAnimationSpeed( 12.5 ); setAnimationSpeed( 12.5 );
pauseAnimation(); pauseAnimation();
setMovement( 0, 0 ); setMovement( 0, 0 );
setMovementSpeed( 0.4 ); setMovementSpeed( 1 );
addCollision( addCollision(
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) ); SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) );
addCollision( addCollision(
@ -46,27 +46,26 @@ void Mario::setStanding() {
void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position) { void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position) {
// handle gravity // handle gravity
// TODO - https://haydnscarlett.wordpress.com/2020/07/30/marios-jump/ // TODO - https://haydnscarlett.wordpress.com/2020/07/30/marios-jump/
if (jumping) { /* if (jumping) {
if(stop_jump && getPos().getY() - min_jump < 0.05) { if(stop_jump && getPos().getY() - min_jump < 0.05) {
jumping = false; jumping = false;
resetMovementY(); resetMovementY();
} else if( getPos().getY() - max_jump < 0.05 ) { } 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; jumping = false;
resetMovementY(); resetMovementY();
} else if ( getPos().getY() - slow_jump < 0.05 && getMovement().getY() < -1*jump_movement/5 ) { } 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); addMovement(0, jump_movement/50);
} }
} }*/
on_ground = visitor.isOnGround(); on_ground = visitor.isOnGround();
if ( !on_ground && !jumping ) { if(!jumping && on_ground) {
setMovement( getMovement().getX(), fall_movement );
} else if(!jumping) {
resetMovementY(); resetMovementY();
// for some reason falling of the edge causes on_ground to be true, but
// visitor ground_y is 0
if(visitor.getGroundY() != 0) {
setPos(getPos().getX(), visitor.getGroundY() - BLOCK_SIZE); setPos(getPos().getX(), visitor.getGroundY() - BLOCK_SIZE);
} }
}
// 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() ) ||
@ -78,16 +77,37 @@ void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_p
void Mario::jump() { void Mario::jump() {
if(!on_ground) if(!on_ground)
return; return;
std::cout << "JUMP!" << std::endl;
jumping = true; jumping = true;
stop_jump = false; stop_jump = false;
max_jump = getPos().getY() - 3*BLOCK_SIZE; max_jump = getPos().getY() - 3*BLOCK_SIZE;
min_jump = getPos().getY() - 1*BLOCK_SIZE; min_jump = getPos().getY() - 1*BLOCK_SIZE;
slow_jump = getPos().getY() - 2*BLOCK_SIZE; slow_jump = getPos().getY() - 2*BLOCK_SIZE;
setMovement( getMovement().getX(), -jump_movement ); addMovement( 0, -jump_movement );
ticks_till_gravity = base_gravity_ticks;
} }
void Mario::stopJump() { void Mario::stopJump() {
std::cout << "STOP JUMP!" << std::endl;
stop_jump = true; stop_jump = true;
} }
void Mario::custom_move( int ticks ) {
if(!jumping && on_ground)
return;
if(getMovement().getY() >= 1.0625 * jump_movement)
return;
ticks_till_gravity -= ticks;
if(ticks_till_gravity < 0) {
if(getMovement().getY() > 0) {
jumping = false;
addMovement(0, gravity_add_jumping);
} else {
if(stop_jump) {
addMovement(0, gravity_add_falling);
} else {
addMovement(0, gravity_add_jumping);
}
}
ticks_till_gravity += base_gravity_ticks;
}
std::cout << getMovement().getY() << std::endl;
}

View File

@ -3,6 +3,7 @@
#include "../sdlpp/sdlpp_rectrenderer.hpp" #include "../sdlpp/sdlpp_rectrenderer.hpp"
#include "mario_visitor.hpp" #include "mario_visitor.hpp"
#include "sprites.hpp"
class Mario : public SDLPP::RectangleRender { class Mario : public SDLPP::RectangleRender {
public: public:
@ -13,17 +14,22 @@ public:
void handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position); void handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position);
void jump(); void jump();
void stopJump(); void stopJump();
virtual void custom_move( int ticks ) override;
private: private:
bool faces_right = true; bool faces_right = true;
double side_movement = 0.8; double side_movement = 0.32;
double fall_movement = 1; double jump_movement = 1.0;
double jump_movement = 1.5;
bool jumping = false; bool jumping = false;
bool stop_jump = false; bool stop_jump = false;
double max_jump = 0; double max_jump = 0;
double min_jump = 0; double min_jump = 0;
double slow_jump = 0; double slow_jump = 0;
bool on_ground = true; bool on_ground = true;
int ticks_till_gravity = 0;
// gravity should be added every frame in 60fps game
const int base_gravity_ticks = 1000/60;
const double gravity_add_jumping = jump_movement/32.0;
const double gravity_add_falling = jump_movement/(64.0/7.0);
}; };
#endif #endif