a-game

2D platformer written from scratch.
Log | Files | Refs | README | LICENSE

commit a9cdd9ac36ae6978e4b5130f3031e28f8669b152
parent b3a5599f20db1d635b64eaa3173918dc6bff4005
Author: Amin Mesbah <dev@aminmesbah.com>
Date:   Sun,  9 Jun 2019 17:19:47 -0700

Stop player at world edge

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