a-game

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

commit e232b9fef7f563f4d3bad962963914b628e083e7
parent 52a4c62c5f688cd670043e034ab7532068df5ee8
Author: amin <dev@aminmesbah.com>
Date:   Sat,  9 Nov 2019 06:03:54 +0000

Improve jumping, climbing, sliding, falling

FossilOrigin-Name: bae310ee95e70487fbccfa257f73e47529452de8528798ab8cb57e817ed80027
Diffstat:
Msrc/game.c | 79++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 54 insertions(+), 25 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -223,7 +223,6 @@ 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); u32 button_states = game_input->button_states; if (dt >= 0.5f) { @@ -234,7 +233,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga dt = 1.0f / 60.0f; } - v2 max_velocity = {5.0f, 5.0f}; + v2 max_velocity = {6.0f, 10.0f}; f32 acceleration_rate = 50.0f; f32 friction = 0.7f; enum Direction previous_facing_dir = player->facing; @@ -269,19 +268,16 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga rect player_rect = math_rect_from_center_dim(player->pos.local, player->dimensions); - // TODO: tune these acceleration rates + // determine this frame's movement mode switch(player->move_mode) { case MOVE_MODE_FALLING: - player->acceleration.y = -acceleration_rate; break; case MOVE_MODE_GROUNDED: if(entity_is_adjacent_to_solid_tiles(game_state->world, player->pos, player_rect, DIR_DOWN)) { - player->acceleration.y = 0.0f; if (btn_was_just_pressed(game_input, BTN_JUMP)) { - player->acceleration.y = 13.0f * acceleration_rate; player->move_mode = MOVE_MODE_JUMPING; } } @@ -294,40 +290,74 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga if (player->facing == previous_facing_dir && entity_is_adjacent_to_solid_tiles(game_state->world, player->pos, player_rect, player->facing)) { - if (btn_is_down(button_states, BTN_UP)) - { - player->acceleration.y = acceleration_rate; - } - else if (btn_is_down(button_states, BTN_DOWN)) - { - player->acceleration.y = -acceleration_rate; - } - else + if (btn_was_just_pressed(game_input, BTN_JUMP)) { - player->acceleration.y = -acceleration_rate; + player->move_mode = MOVE_MODE_JUMPING; } } else { + player->acceleration.y = 0.0f; + player->velocity.y = 0.0f; player->move_mode = MOVE_MODE_FALLING; } break; case MOVE_MODE_JUMPING: - { if (btn_was_just_released(game_input, BTN_JUMP)) { - if (player->velocity.y > 0.0f) - { - player->velocity.y = 0.0f; - } player->move_mode = MOVE_MODE_FALLING; } + break; + case MOVE_MODE_FLOATING: + break; + default: + assert(false); + break; + } + move_mode_print(player->move_mode); + + // TODO: tune these acceleration rates + // simulate movement mode + switch(player->move_mode) + { + case MOVE_MODE_FALLING: player->acceleration.y = -acceleration_rate; break; - } + case MOVE_MODE_GROUNDED: + player->acceleration.y = 0.0f; + player->velocity.y = 0.0f; + break; + case MOVE_MODE_CLIMBING: + if (btn_is_down(button_states, BTN_DOWN)) + { + player->acceleration.y = -acceleration_rate; + } + else + { + if (player->velocity.y < 0.0f) + { + player->acceleration.y = 0.0f; + player->velocity.y = 0.0f; + } + + if (btn_is_down(button_states, BTN_UP)) + { + player->acceleration.y = acceleration_rate; + } + else + { + player->acceleration.y = 0.0f; + player->velocity.y = -1.0f; + } + } + break; + case MOVE_MODE_JUMPING: + max_velocity.y = 100.0f; + player->acceleration.y = 13.0f * acceleration_rate; + player->move_mode = MOVE_MODE_FALLING; + break; case MOVE_MODE_FLOATING: - { if (btn_is_down(button_states, BTN_UP)) { player->acceleration.y = acceleration_rate; @@ -342,7 +372,6 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga player->velocity.y = player->velocity.y * friction; } break; - } default: assert(false); break; @@ -352,7 +381,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga 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_velocity.x, max_velocity.x); - //math_clamp(&player->velocity.y, -max_velocity.y, max_velocity.y); + math_clamp(&player->velocity.y, -max_velocity.y, max_velocity.y); // game_detect_collisions {