Fix jump spam and recovery

Changelog:
- Hold to jump is removed. Need to release jump action to recover jump
- Fix jump recovery issue when interacting with crates and ground
scene_man
En Yi 2023-02-14 23:05:31 +08:00
parent 399c78c200
commit cf38949956
3 changed files with 15 additions and 13 deletions

View File

@ -55,8 +55,8 @@ typedef struct _CJump_t
unsigned int max_jumps; unsigned int max_jumps;
int jump_speed; int jump_speed;
bool jumped; bool jumped;
bool jump_ready;
bool short_hop; bool short_hop;
unsigned int cooldown_timer;
}CJump_t; }CJump_t;
typedef enum PlayerState typedef enum PlayerState

View File

@ -109,8 +109,6 @@ static void level_scene_render_func(Scene_t* scene)
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 15, 12, BLACK); DrawText(buffer, tilemap.width * TILE_SIZE + 1, 15, 12, BLACK);
sprintf(buffer, "Jumps: %u", p_cjump->jumps); sprintf(buffer, "Jumps: %u", p_cjump->jumps);
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 60, 12, BLACK); DrawText(buffer, tilemap.width * TILE_SIZE + 1, 60, 12, BLACK);
sprintf(buffer, "Cooldown: %u", p_cjump->cooldown_timer);
DrawText(buffer, (tilemap.width + 3) * TILE_SIZE + 1, 60, 12, BLACK);
sprintf(buffer, "Crouch: %u", p_pstate->is_crouch); sprintf(buffer, "Crouch: %u", p_pstate->is_crouch);
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 90, 12, BLACK); DrawText(buffer, tilemap.width * TILE_SIZE + 1, 90, 12, BLACK);
sprintf(buffer, "Water: %s", p_mstate->water_state & 1? "YES":"NO"); sprintf(buffer, "Water: %s", p_mstate->water_state & 1? "YES":"NO");
@ -150,6 +148,7 @@ static void spawn_player(Scene_t *scene)
p_cjump->jump_speed = 680; p_cjump->jump_speed = 680;
p_cjump->jumps = 1; p_cjump->jumps = 1;
p_cjump->max_jumps = 1; p_cjump->max_jumps = 1;
p_cjump->jump_ready = true;
add_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T); add_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
add_component(&scene->ent_manager, p_ent, CTILECOORD_COMP_T); add_component(&scene->ent_manager, p_ent, CTILECOORD_COMP_T);
add_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T); add_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T);

View File

@ -178,12 +178,16 @@ void player_movement_input_system(Scene_t* scene)
} }
// Short Hop // Short Hop
if (p_cjump->jumped && p_ctransform->velocity.y < 0) //if (p_cjump->jumped)
{ {
if (!p_pstate->jump_pressed && !p_cjump->short_hop)
if (!p_pstate->jump_pressed)
{ {
p_cjump->short_hop = true; if (!p_cjump->short_hop && p_ctransform->velocity.y < 0)
p_ctransform->velocity.y /= 2; {
p_cjump->short_hop = true;
p_ctransform->velocity.y /= 2;
}
} }
} }
@ -206,11 +210,10 @@ void player_movement_input_system(Scene_t* scene)
if (!(p_mstate->ground_state & 1)) p_pstate->is_crouch &= 0b01; if (!(p_mstate->ground_state & 1)) p_pstate->is_crouch &= 0b01;
p_pstate->is_crouch >>= 1; p_pstate->is_crouch >>= 1;
if (p_cjump->cooldown_timer > 0) p_cjump->cooldown_timer--;
// Jumps is possible as long as you have a jump // Jumps is possible as long as you have a jump
// Check if possible to jump when jump is pressed // Check if possible to jump when jump is pressed
if (p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->cooldown_timer == 0) if (p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->jump_ready)
{ {
p_cjump->jumps--; p_cjump->jumps--;
if (!in_water) if (!in_water)
@ -223,7 +226,7 @@ void player_movement_input_system(Scene_t* scene)
} }
p_cjump->jumped = true; p_cjump->jumped = true;
p_cjump->cooldown_timer = 15; p_cjump->jump_ready = false;
} }
} }
@ -617,18 +620,18 @@ void player_ground_air_transition_system(Scene_t* scene)
{ {
CJump_t* p_cjump = get_component(&scene->ent_manager, p_player, CJUMP_COMP_T); CJump_t* p_cjump = get_component(&scene->ent_manager, p_player, CJUMP_COMP_T);
CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_player, CMOVEMENTSTATE_T); CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_player, CMOVEMENTSTATE_T);
CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_player, CPLAYERSTATE_T);
// Handle Ground<->Air Transition // Handle Ground<->Air Transition
bool in_water = (p_mstate->water_state & 1); bool in_water = (p_mstate->water_state & 1);
// Landing or in water // Landing or in water
if (p_mstate->ground_state == 0b01 || in_water) if ((p_mstate->ground_state & 1 || in_water) && !p_pstate->jump_pressed )
{ {
// Recover jumps // Recover jumps
p_cjump->jumps = p_cjump->max_jumps; p_cjump->jumps = p_cjump->max_jumps;
p_cjump->jumped = false; p_cjump->jumped = false;
p_cjump->short_hop = false; p_cjump->short_hop = false;
if (!in_water) p_cjump->jump_ready = true;
p_cjump->cooldown_timer = 0;
} }
else if (p_mstate->water_state == 0b10 || p_mstate->ground_state == 0b10) else if (p_mstate->water_state == 0b10 || p_mstate->ground_state == 0b10)
{ {