variable jump height

This commit is contained in:
KikooDX 2021-12-17 11:00:29 +01:00
parent 6f3eda4353
commit 1b3f7df1ec
3 changed files with 26 additions and 2 deletions

View File

@ -11,3 +11,4 @@
#define AIR_RESISTANCE 0.02f
#define GRAVITY 0.2f
#define JUMP_SPEED -4.0f
#define JUMP_BREAK 3

View File

@ -1,9 +1,12 @@
#pragma once
#include "vec.h"
enum AirState { AS_NEUTRAL, AS_RISING, AS_BREAKING };
struct Player {
struct Vec pos;
struct VecF spd, rem;
enum AirState air_state;
};
void player_init(struct Player *);

View File

@ -18,6 +18,7 @@ player_init(struct Player *p)
p->pos.x = TILE_SIZE;
p->pos.y = TILE_SIZE;
reset_speed(p, 1, 1);
p->air_state = AS_NEUTRAL;
}
void
@ -28,6 +29,7 @@ player_update(struct Player *p)
const int k_right = input_down(K_RIGHT);
// const int k_up = input_down(K_UP);
// const int k_down = input_down(K_DOWN);
const int k_jump = input_down(K_JUMP);
const int kp_jump = input_pressed(K_JUMP);
const int dir_x = k_right - k_left;
@ -38,10 +40,28 @@ player_update(struct Player *p)
/* air resistance & gravity */
p->spd.y *= (1 - AIR_RESISTANCE);
p->spd.y += GRAVITY;
p->spd.y +=
(p->air_state == AS_BREAKING) ? (GRAVITY * JUMP_BREAK) : (GRAVITY);
/* air state machine */
/* state is set to AS_RISING when jumping */
switch (p->air_state) {
case AS_RISING:
if (!k_jump) p->air_state = AS_BREAKING;
/* fallthrough */
case AS_BREAKING:
if (p->spd.y > 0) p->air_state = AS_NEUTRAL;
break;
case AS_NEUTRAL:
default:
break;
}
/* jump */
if (on_ground && kp_jump) p->spd.y = JUMP_SPEED;
if (on_ground && kp_jump) {
p->spd.y = JUMP_SPEED;
p->air_state = AS_RISING;
}
player_move(p, update_rem(p));
}