a-game

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

commit 4123538cf3d61f523768b99adee70d4322e7eb4f
parent e8a45e744e5ee4adcbd8aa9b62ba329d4c5ea20c
Author: amin <dev@aminmesbah.com>
Date:   Mon, 10 Jun 2019 00:19:46 +0000

Stop player at world edge

FossilOrigin-Name: 659afc3bbadc1b810a7588d30e8803b417377ec750a4cde92704f9389870d798
Diffstat:
Msrc/game.c | 30+++++++++++++++++++++++++++---
1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -423,10 +423,9 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga .min = {tile_x, tile_y}, .max = {tile_x + TILE_SIZE, tile_y + TILE_SIZE}, }; - - rect tile_player_sum = math_minkowski_sum_rect_rect(tile_aabb, player->dimensions); RENDER_COLLISION_DEBUG_QUAD(tile_aabb, ((v3) {0.8f, 0.4f, 0.4f})); + rect tile_player_sum = math_minkowski_sum_rect_rect(tile_aabb, player->dimensions); segment tile_sum_b = math_rect_get_edge(tile_player_sum, RECT_EDGE_BOTTOM); segment tile_sum_t = math_rect_get_edge(tile_player_sum, RECT_EDGE_TOP); segment tile_sum_l = math_rect_get_edge(tile_player_sum, RECT_EDGE_LEFT); @@ -546,7 +545,32 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga } #undef RENDER_COLLISION_DEBUG_QUAD - player->pos = world_pos_recanonicalize(player->pos); + struct AbsolutePos new_player_p = world_pos_recanonicalize(player->pos); + + if (world_room_get(game_state->world, new_player_p.room)) + { + player->pos = new_player_p; + } + else + { + if(player->pos.local.x < 0.0f && !world_room_get(game_state->world, (v2i) {current_room_i.x - 1, current_room_i.y})) + { + player->pos.local.x = 0.0f; + } + else if (player->pos.local.x > ROOM_TILE_DIM_X && !world_room_get(game_state->world, (v2i) {current_room_i.x + 1, current_room_i.y})) + { + player->pos.local.x = ROOM_TILE_DIM_X; + } + if(player->pos.local.y < 0.0f && !world_room_get(game_state->world, (v2i) {current_room_i.x, current_room_i.y - 1})) + { + player->pos.local.y = 0.0f; + } + else if (player->pos.local.y > ROOM_TILE_DIM_Y && !world_room_get(game_state->world, (v2i) {current_room_i.x, current_room_i.y + 1})) + { + player->pos.local.y = ROOM_TILE_DIM_Y; + } + player->pos = world_pos_recanonicalize(player->pos); + } } // game_render_player