commit 914eb3332da5509e6d1f43308843b51fefb272a9
parent 781dd09fd08a37660faa7efc919b6d4948cdc419
Author: Amin Mesbah <dev@aminmesbah.com>
Date: Thu, 13 Jun 2019 18:40:49 -0700
Check the tile below a grounded player
This fixes the bug where you walk off a ledge and jump from mid air
because your move_state is still set to 'GROUNDED'.
Diffstat:
1 file changed, 4 insertions(+), 24 deletions(-)
diff --git a/src/game.c b/src/game.c
@@ -263,34 +263,18 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
player->velocity.x = player->velocity.x * friction;
}
- bool player_is_strictly_within_room = true;
- {
- rect player_room_bounds = math_minkowski_diff_rect(
- (rect) {.min = {0.0f, 0.0f}, .max = {ROOM_TILE_DIM_X, ROOM_TILE_DIM_Y}},
- player->dimensions);
- player_is_strictly_within_room = math_in_interval_open(player->pos.local.x, player_room_bounds.min.x, player_room_bounds.max.x)
- && math_in_interval_open(player->pos.local.y, player_room_bounds.min.y, player_room_bounds.max.y);
- }
rect player_rect = math_rect_from_center_dim(player->pos.local, player->dimensions);
- // TODO: tune these
+ // TODO: tune these acceleration rates
switch(player->move_state)
{
case MOVE_STATE_FALLING:
player->acceleration.y = -acceleration_rate;
break;
case MOVE_STATE_GROUNDED:
-#if 1
- if (game_input->key_jump)
- {
- player->acceleration.y = acceleration_rate;
- player->move_state = MOVE_STATE_JUMPING;
- }
-#else
- // TODO: do a tile check
- if (player_is_strictly_within_room
- && entity_is_adjacent_to_solid_tiles(player_rect, current_room->tiles, DIR_DOWN))
+ if(entity_is_adjacent_to_solid_tiles(game_state->world, player->pos, player_rect, DIR_DOWN))
{
+ player->acceleration.y = 0.0f;
if (game_input->key_jump)
{
player->acceleration.y = acceleration_rate;
@@ -301,11 +285,9 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
{
player->move_state = MOVE_STATE_FALLING;
}
-#endif
break;
case MOVE_STATE_CLIMBING:
- if (player_is_strictly_within_room
- && player->facing == previous_facing_dir
+ if (player->facing == previous_facing_dir
&& entity_is_adjacent_to_solid_tiles(game_state->world, player->pos, player_rect, player->facing))
{
if (game_input->key_up)
@@ -454,12 +436,10 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
{
if (edges_collided[RECT_EDGE_TOP] && player->move_state == MOVE_STATE_FALLING)
{
- printf("Noo! I am stuck on the ground!");
player->move_state = MOVE_STATE_GROUNDED;
}
else if (edges_collided[RECT_EDGE_LEFT] || edges_collided[RECT_EDGE_RIGHT])
{
- printf("Yay! I can climb now!");
player->move_state = MOVE_STATE_CLIMBING;
}
}