Fix grid entities set update bug for OOB entities

When entities are marked for deletion, the tilecoord components
still exists, which can update the grid entities set, even if the
entity is not alive
scene_man
En Yi 2023-08-15 19:58:14 +08:00
parent e1afdf696a
commit cd8ec0c7ed
2 changed files with 9 additions and 5 deletions

View File

@ -4,12 +4,15 @@
void remove_entity_from_tilemap(EntityManager_t *p_manager, TileGrid_t* tilemap, Entity_t* p_ent) void remove_entity_from_tilemap(EntityManager_t *p_manager, TileGrid_t* tilemap, Entity_t* p_ent)
{ {
CTileCoord_t* p_tilecoord = get_component(p_ent, CTILECOORD_COMP_T); CTileCoord_t* p_tilecoord = get_component(p_ent, CTILECOORD_COMP_T);
for (size_t i = 0;i < p_tilecoord->n_tiles; ++i) if (p_tilecoord != NULL)
{ {
// Use previously store tile position for (size_t i = 0;i < p_tilecoord->n_tiles; ++i)
// Clear from those positions {
unsigned int tile_idx = p_tilecoord->tiles[i]; // Use previously store tile position
sc_map_del_64v(&(tilemap->tiles[tile_idx].entities_set), p_ent->m_id); // Clear from those positions
unsigned int tile_idx = p_tilecoord->tiles[i];
sc_map_del_64v(&(tilemap->tiles[tile_idx].entities_set), p_ent->m_id);
}
} }
remove_entity(p_manager, p_ent->m_id); remove_entity(p_manager, p_ent->m_id);
} }

View File

@ -1305,6 +1305,7 @@ void update_tilemap_system(Scene_t* scene)
sc_map_foreach(&scene->ent_manager.component_map[CTILECOORD_COMP_T], ent_idx, p_tilecoord) sc_map_foreach(&scene->ent_manager.component_map[CTILECOORD_COMP_T], ent_idx, p_tilecoord)
{ {
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx); Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
if (!p_ent->m_alive) continue;
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T); CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
if (p_ctransform == NULL) continue; if (p_ctransform == NULL) continue;
CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T); CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T);