From c5c9b84154ed8d51b92f8ed5b21c24609f91a332 Mon Sep 17 00:00:00 2001 From: En Yi Date: Sat, 4 Feb 2023 12:24:50 +0800 Subject: [PATCH] Fix bbox shifting when resizing Changelog: - Check for collision before shifting bbox - This fix the odd bbox shifting when exiting water - Hack a way to force crouch when exiting water --- engine/game_systems.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/engine/game_systems.c b/engine/game_systems.c index 4dc3394..22a0b3a 100644 --- a/engine/game_systems.c +++ b/engine/game_systems.c @@ -183,7 +183,7 @@ void player_movement_input_system(Scene_t* scene) unsigned int tile_x1 = (p_ctransform->position.x) / TILE_SIZE; unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x - 1) / TILE_SIZE; unsigned int tile_y = (p_ctransform->position.y) / TILE_SIZE; - if (p_pstate->is_crouch == 0b01 && tile_y > 0) tile_y--; + if (p_bbox->size.y < TILE_SIZE && tile_y > 0) tile_y--; // hack to detect small bbox state for(unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++) { @@ -251,6 +251,9 @@ void player_bbox_update_system(Scene_t *scene) CBBox_t* p_bbox = get_component(&scene->ent_manager, p_player, CBBOX_COMP_T); CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_player, CPLAYERSTATE_T); CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_player, CMOVEMENTSTATE_T); + + Vector2 new_bbox; + Vector2 offset; if (p_mstate->ground_state & 1) { AnchorPoint_t anchor = AP_BOT_CENTER; @@ -273,7 +276,6 @@ void player_bbox_update_system(Scene_t *scene) break; } } - Vector2 new_bbox; if(p_pstate->is_crouch & 1) { new_bbox.x = PLAYER_C_WIDTH; @@ -284,27 +286,29 @@ void player_bbox_update_system(Scene_t *scene) new_bbox.x = PLAYER_WIDTH; new_bbox.y = PLAYER_HEIGHT; } - Vector2 offset = shift_bbox(p_bbox->size, new_bbox, anchor); - set_bbox(p_bbox, new_bbox.x, new_bbox.y); - p_ctransform->position = Vector2Add(p_ctransform->position, offset); + offset = shift_bbox(p_bbox->size, new_bbox, anchor); } else { if (p_mstate->water_state & 1) { - Vector2 new_bbox = {PLAYER_C_WIDTH, PLAYER_C_HEIGHT}; - Vector2 offset = shift_bbox(p_bbox->size, new_bbox, AP_MID_CENTER); - set_bbox(p_bbox, PLAYER_C_WIDTH, PLAYER_C_HEIGHT); - p_ctransform->position = Vector2Add(p_ctransform->position, offset); + new_bbox.x = PLAYER_C_WIDTH; + new_bbox.y = PLAYER_C_HEIGHT; + offset = shift_bbox(p_bbox->size, new_bbox, AP_MID_CENTER); } else { - Vector2 new_bbox = {PLAYER_WIDTH, PLAYER_HEIGHT}; - Vector2 offset = shift_bbox(p_bbox->size, new_bbox, AP_MID_CENTER); - set_bbox(p_bbox, PLAYER_WIDTH, PLAYER_HEIGHT); - p_ctransform->position = Vector2Add(p_ctransform->position, offset); + new_bbox.x = PLAYER_WIDTH; + new_bbox.y = PLAYER_HEIGHT; + offset = shift_bbox(p_bbox->size, new_bbox, AP_MID_CENTER); } } + + if (!check_collision_at(p_ctransform->position, new_bbox, &tilemap, offset)) + { + set_bbox(p_bbox, new_bbox.x, new_bbox.y); + p_ctransform->position = Vector2Add(p_ctransform->position, offset); + } } }