commit 05b02e41bad2755dfca4325ed29859582f2f99a5
parent c4891b75f41e2a026a825a72c941e4e9a975b420
Author: amin <dev@aminmesbah.com>
Date: Fri, 14 Jun 2019 01:45:41 +0000
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'.
FossilOrigin-Name: ec8883e2c3aae7449ebee5c9c02a37fd530d03248980a468d362e71284b4779e
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;
}
}