a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit 55d0fbafa4e9176cdbd489a01cc1607d8a22deca
parent 84c9e47123a850e272600411821f1741b2a57aaa
Author: amin <dev@aminmesbah.com>
Date:   Tue, 24 Sep 2019 06:04:19 +0000

Try doing actual jumping

FossilOrigin-Name: f5faa06bd9ea530c04c576680a010ff31a25d2067c193e44a2f9d7170f3ff834
Diffstat:
Msrc/game.c | 28+++++++++++++---------------
Msrc/input.c | 8++++++++
2 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -286,7 +286,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga // game_update_player { struct Entity *player = &game_state->player; - //move_mode_print(player->move_mode); + move_mode_print(player->move_mode); u32 button_states = game_input->button_states; if (dt >= 0.5f) { @@ -297,7 +297,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga dt = 1.0f / 60.0f; } - f32 max_meters_per_second = 5.0f; + v2 max_velocity = {5.0f, 5.0f}; f32 acceleration_rate = 50.0f; f32 friction = 0.7f; enum Direction previous_facing_dir = player->facing; @@ -342,9 +342,9 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga if(entity_is_adjacent_to_solid_tiles(game_state->world, player->pos, player_rect, DIR_DOWN)) { player->acceleration.y = 0.0f; - if (btn_is_down(button_states, BTN_JUMP)) + if (btn_was_just_pressed(game_input, BTN_JUMP)) { - player->acceleration.y = acceleration_rate; + player->acceleration.y = 13.0f * acceleration_rate; player->move_mode = MOVE_MODE_JUMPING; } } @@ -377,18 +377,16 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga break; case MOVE_MODE_JUMPING: { - // TODO: give this a real timer - bool jump_timed_out = false; - if (!jump_timed_out && btn_is_down(button_states, BTN_JUMP)) - { - player->acceleration.y = acceleration_rate; - player->move_mode = MOVE_MODE_JUMPING; - } - else + if (btn_was_just_released(game_input, BTN_JUMP)) { - player->acceleration.y = -acceleration_rate; + if (player->velocity.y > 0.0f) + { + player->velocity.y = 0.0f; + } player->move_mode = MOVE_MODE_FALLING; } + + player->acceleration.y = -acceleration_rate; break; } case MOVE_MODE_FLOATING: @@ -416,8 +414,8 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga // Semi implicit Euler integration: https://gafferongames.com/post/integration_basics/ player->velocity = math_v2_a(player->velocity, math_v2f_m(player->acceleration, dt)); // TODO: clamp the length of the velocity vector, not each of its components - math_clamp(&player->velocity.x, -max_meters_per_second, max_meters_per_second); - math_clamp(&player->velocity.y, -max_meters_per_second, max_meters_per_second); + math_clamp(&player->velocity.x, -max_velocity.x, max_velocity.x); + //math_clamp(&player->velocity.y, -max_velocity.y, max_velocity.y); // game_detect_collisions { diff --git a/src/input.c b/src/input.c @@ -12,3 +12,11 @@ internal bool btn_was_just_pressed(struct GameInput *game_input, enum GameButton bool result = game_input->button_downs & (1 << b); return result; } + +internal bool btn_was_just_released(struct GameInput *game_input, enum GameButton b) +{ + assert(game_input); + assert(b < NUM_GAME_BUTTONS); + bool result = game_input->button_ups & (1 << b); + return result; +}