More fun jumping

This commit is contained in:
zvon 2020-08-21 19:04:58 +02:00
parent ea7dff32a2
commit 1b0a35cf5a

View File

@ -13,7 +13,7 @@ class Player : public SDLPP::RectangleRender {
public:
Player(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r, double _max_gravity = 1.0, uint32_t _max_gravity_time = 1000) : SDLPP::RectangleRender(x,y,w,h,r) {
max_gravity = _max_gravity;
jump_speed = 1.5*max_gravity;
jump_speed = 2.2*max_gravity;
max_gravity_time = _max_gravity_time;
cur_gravity_time = max_gravity_time;
}
@ -50,7 +50,19 @@ public:
auto grav = gravity_enabled * max_gravity * (max_gravity_time - cur_gravity_time)/max_gravity_time;
if( grav > max_gravity)
grav = max_gravity;
auto jump_ = jumping * jump_speed * cur_gravity_time/max_gravity_time;
// percentage of how close we are to maximum velocity of gravity (0 = we've reached max velocity, 1 = we've just started accelerating)
double division = static_cast<double>(cur_gravity_time)/static_cast<double>(max_gravity_time);
// current jump speed
auto jump_ = jumping * jump_speed * division;
if(division < 0.75 && division >= 0.72) {
// in this time frame, just hover in same place
jump_ = jumping * grav;
} else if(division < 0.72) {
// fall slowly
jump_ = jumping * jump_speed * division * 0.15;
if(jump_ > grav)
jump_ = grav;
}
if(jump_ < 0 || jump_ > jump_speed)
jump_ = 0;
y_ += grav * time_portion;
@ -200,7 +212,6 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
pollEvents(*scene);
if(pause)
continue;
std::cout << "NOOO" << std::endl;
scene->movement();
bool gravity = true;
for( auto &x : scene->getCollisions(*player) ) {