diff --git a/inc/conf.h b/inc/conf.h index 62885b3..6082fc3 100644 --- a/inc/conf.h +++ b/inc/conf.h @@ -11,3 +11,4 @@ #define AIR_RESISTANCE 0.02f #define GRAVITY 0.2f #define JUMP_SPEED -4.0f +#define JUMP_BREAK 3 diff --git a/inc/player.h b/inc/player.h index 5eebc2c..75b11b6 100644 --- a/inc/player.h +++ b/inc/player.h @@ -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 *); diff --git a/src/player.c b/src/player.c index 8d52c90..60cd907 100644 --- a/src/player.c +++ b/src/player.c @@ -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)); }