From fa58a3239c86b5a669bba10e74e10827d9bfcdee Mon Sep 17 00:00:00 2001 From: En Yi Date: Tue, 17 Jan 2023 19:30:08 +0800 Subject: [PATCH] Improve collision and render functions Changelog: - Render entity after tile, display numbers after entity - Add array to avoid re-checking collision against the same entity - Increase maximum entity --- mempool.h | 2 +- scene_impl.c | 35 ++++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/mempool.h b/mempool.h index c7da3fa..71e2a20 100644 --- a/mempool.h +++ b/mempool.h @@ -2,7 +2,7 @@ #define __MEMPOOL_H #include "entity.h" #include "components.h" -#define MAX_COMP_POOL_SIZE 256 +#define MAX_COMP_POOL_SIZE 1024 void init_memory_pools(void); void free_memory_pools(void); diff --git a/scene_impl.c b/scene_impl.c index de0f20a..c39cb61 100644 --- a/scene_impl.c +++ b/scene_impl.c @@ -214,16 +214,11 @@ static void level_scene_render_func(Scene_t* scene) if (tilemap.tiles[i].solid) { DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK); - DrawText(buffer, x, y, 10, WHITE); } - else + else if (tilemap.tiles[i].water_level > 0) { // Draw water tile - if (tilemap.tiles[i].water_level > 0) - { - DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLUE); - } - DrawText(buffer, x, y, 10, BLACK); + DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLUE); } } @@ -246,6 +241,24 @@ static void level_scene_render_func(Scene_t* scene) DrawRectangle(p_ct->position.x, p_ct->position.y, p_bbox->size.x, p_bbox->size.y, colour); } + for (size_t i=0; iscene_data; TileGrid_t tilemap = data->tilemap; @@ -632,11 +648,16 @@ static void tile_collision_system(Scene_t *scene) else { unsigned int other_ent_idx; + memset(checked_entities, 0, sizeof(checked_entities)); // TODO: check against the entities of each involved tiles sc_map_foreach_key(&tilemap.tiles[tile_idx].entities_set, other_ent_idx) { if (other_ent_idx == ent_idx) continue; + if (checked_entities[other_ent_idx]) continue; + + checked_entities[other_ent_idx] = true; Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx); + if (p_other_ent->m_tag < p_ent->m_tag) continue; // To only allow one way collision check CBBox_t *p_other_bbox = get_component(&scene->ent_manager, p_other_ent, CBBOX_COMP_T); if (p_other_bbox == NULL || !p_other_bbox->solid) continue;