a-game

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

commit 736f928759772e12ea59a63b826d60464b39affb
parent d6aafe61abef841b65217c212df68249fe3a5dc2
Author: amin <dev@aminmesbah.com>
Date:   Wed, 12 Jun 2019 04:14:09 +0000

Add a basic wall climb

Watch out! You can keep climbing after you reach the top of a wall!

FossilOrigin-Name: e22cfd3c6e77503ae15a95d272478c40d7e65318c2a3493bb5b60e6914219c1d
Diffstat:
Msrc/game.c | 94++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Msrc/game.h | 19++++++++++++-------
2 files changed, 69 insertions(+), 44 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -6,20 +6,20 @@ #include "render.c" #include "world.c" -internal void physical_status_print(enum PhysicalStatus s) +internal void move_state_print(enum MoveState s) { switch(s) { - case PHYSICAL_STATUS_FALLING: + case MOVE_STATE_FALLING: printf("FALLING\n"); break; - case PHYSICAL_STATUS_GROUNDED: + case MOVE_STATE_GROUNDED: printf("GROUNDED\n"); break; - case PHYSICAL_STATUS_JUMPING: + case MOVE_STATE_JUMPING: printf("JUMPING\n"); break; - case PHYSICAL_STATUS_CLIMBING: + case MOVE_STATE_CLIMBING: printf("CLIMBING\n"); break; default: @@ -42,7 +42,8 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) // In Knytt, the player is 9 by 14 texels and a tile is 24 by 24 texels. // These dimensions are relative to a square 'meter', one tile game_state->player.dimensions = (v2) {0.375f, 0.583f}; - game_state->player.physical_status = PHYSICAL_STATUS_FALLING; + game_state->player.move_state = MOVE_STATE_FALLING; + game_state->player.facing = DIR_RIGHT; mem_st_init( &game_state->world_allocator, @@ -194,7 +195,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga // game_update_player { struct Entity *player = &game_state->player; - physical_status_print(player->physical_status); + move_state_print(player->move_state); f32 dt = game_input->dt; if (dt >= 0.5f) { @@ -208,53 +209,67 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga f32 max_meters_per_second = 5.0f; f32 acceleration_rate = 50.0f; f32 friction = 0.7f; + enum Direction previous_facing_dir = player->facing; - if (player->physical_status == PHYSICAL_STATUS_FALLING) + if (game_input->key_left) + { + player->facing = DIR_LEFT; + player->acceleration.x = -acceleration_rate; + } + else if (game_input->key_right) + { + player->facing = DIR_RIGHT; + player->acceleration.x = acceleration_rate; + } + else + { + player->acceleration.x = 0.0f; + player->velocity.x = player->velocity.x * friction; + } + + // TODO: tune these + if (player->move_state == MOVE_STATE_FALLING) { player->acceleration.y = -acceleration_rate; } - else if (player->physical_status == PHYSICAL_STATUS_CLIMBING) + else if (player->move_state == MOVE_STATE_CLIMBING) { - if (game_input->key_up) - { - player->acceleration.y = acceleration_rate; - } - else if (game_input->key_down) + if (player->facing == previous_facing_dir) { - player->acceleration.y = -acceleration_rate; + if (game_input->key_up) + { + player->acceleration.y = acceleration_rate; + } + else if (game_input->key_down) + { + player->acceleration.y = -acceleration_rate; + } + else + { + player->acceleration.y = -acceleration_rate; + player->velocity.y = player->velocity.y * friction; + } } else { - player->acceleration.y = 0.0f; - player->velocity.y = player->velocity.y * friction; + player->move_state = MOVE_STATE_FALLING; } } - else if (player->physical_status == PHYSICAL_STATUS_GROUNDED || player->physical_status == PHYSICAL_STATUS_JUMPING) + else if (player->move_state == MOVE_STATE_GROUNDED + || player->move_state == MOVE_STATE_CLIMBING + || player->move_state == MOVE_STATE_JUMPING) { if (game_input->key_jump) { player->acceleration.y = acceleration_rate; - player->physical_status = PHYSICAL_STATUS_JUMPING; + player->move_state = MOVE_STATE_JUMPING; } else { - player->physical_status = PHYSICAL_STATUS_FALLING; + player->move_state = MOVE_STATE_FALLING; } } - if (game_input->key_left) - { - player->acceleration.x = -acceleration_rate; - } - else if (game_input->key_right) - { - player->acceleration.x = acceleration_rate; - } - else - { - player->acceleration.x = 0.0f; - player->velocity.x = player->velocity.x * friction; - } // Semi implicit Euler integration: https://gafferongames.com/post/integration_basics/ player->velocity = math_v2_a(player->velocity, math_v2f_m(player->acceleration, dt)); @@ -363,11 +378,16 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga } } - if (collision_occurred - && collided_edge == RECT_EDGE_TOP - && player->physical_status == PHYSICAL_STATUS_FALLING) + if (collision_occurred) { - player->physical_status = PHYSICAL_STATUS_GROUNDED; + if (collided_edge == RECT_EDGE_TOP && player->move_state == MOVE_STATE_FALLING) + { + player->move_state = MOVE_STATE_GROUNDED; + } + else if (collided_edge == RECT_EDGE_LEFT || collided_edge == RECT_EDGE_RIGHT) + { + player->move_state = MOVE_STATE_CLIMBING; + } } v2 delta_to_nearest_collision = math_v2f_m(player_delta, smallest_distance_scale_factor); diff --git a/src/game.h b/src/game.h @@ -37,14 +37,18 @@ struct AbsolutePos v2 local; }; -enum PhysicalStatus +enum MoveState { - PHYSICAL_STATUS_FALLING, - PHYSICAL_STATUS_GROUNDED, - PHYSICAL_STATUS_JUMPING, - PHYSICAL_STATUS_CLIMBING, + MOVE_STATE_FALLING, + MOVE_STATE_GROUNDED, + MOVE_STATE_CLIMBING, + MOVE_STATE_JUMPING, +}; - MAX_PHYSICAL_STATUS, +enum Direction +{ + DIR_RIGHT, + DIR_LEFT, }; struct Entity @@ -53,7 +57,8 @@ struct Entity v2 acceleration; v2 velocity; v2 dimensions; - enum PhysicalStatus physical_status; + enum Direction facing; + enum MoveState move_state; }; struct GameState