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 );
pauseAnimation();
setMovement( 0, 0 );
setMovementSpeed( 0.4 );
setMovementSpeed( 1 );
addCollision(
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.25, MARIO_FLOOR_DETECT ) );
addCollision(
@ -46,26 +46,25 @@ void Mario::setStanding() {
void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position) {
// handle gravity
// TODO - https://haydnscarlett.wordpress.com/2020/07/30/marios-jump/
if (jumping) {
/* 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 if(!jumping) {
if(!jumping && on_ground) {
resetMovementY();
setPos(getPos().getX(), visitor.getGroundY() - BLOCK_SIZE);
// 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);
}
}
// make sure Mario isn't stuck inside a wall
if ( visitor.isStopped() ||
@ -78,16 +77,37 @@ void Mario::handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_p
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 );
addMovement( 0, -jump_movement );
ticks_till_gravity = base_gravity_ticks;
}
void Mario::stopJump() {
std::cout << "STOP JUMP!" << std::endl;
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 "mario_visitor.hpp"
#include "sprites.hpp"
class Mario : public SDLPP::RectangleRender {
public:
@ -13,17 +14,22 @@ public:
void handleVisitor(MarioVisitor &visitor, SDLPP::Vec2D<double> previous_position);
void jump();
void stopJump();
virtual void custom_move( int ticks ) override;
private:
bool faces_right = true;
double side_movement = 0.8;
double fall_movement = 1;
double jump_movement = 1.5;
double side_movement = 0.32;
double jump_movement = 1.0;
bool jumping = false;
bool stop_jump = false;
double max_jump = 0;
double min_jump = 0;
double slow_jump = 0;
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