Added 'destroy_children' parameter to game_world_destroy_entity().

This commit is contained in:
2025-12-18 12:10:25 +01:00
parent 4e807afa3c
commit 199afff820
4 changed files with 42 additions and 13 deletions

View File

@@ -91,21 +91,32 @@ bool game_world_create_entity(Game_World *world, Entity_Type type, uint16_t x, u
return true;
}
void game_world_destroy_entity(Game_World *world, Entity_ID entity_id) {
void game_world_destroy_entity(Game_World *world, Entity_ID entity_id, bool destroy_children) {
assert(world != NULL);
assert(entity_id != INVALID_ENTITY_ID);
Entity *entity = game_world_try_get_entity_by_id(
world,
entity_id
);
assert(entity != NULL);
memset(&entity, 0, sizeof(Entity));
Entity_ID id_to_remove = entity_id;
while(id_to_remove != INVALID_ENTITY_ID) {
Entity *e = game_world_try_get_entity_by_id(
world,
id_to_remove
);
assert(e != NULL);
Grid_Cell *cell = grid_get_cell(&world->grid, entity->x, entity->y);
assert(cell != NULL);
assert(grid_try_remove_entity_from_cell(cell));
Grid_Cell *cell = grid_get_cell(&world->grid, e->x, e->y);
assert(cell != NULL);
assert(grid_try_remove_entity_from_cell(cell));
squeue_push(&world->entity_id_queue, (void *)&entity_id);
squeue_push(&world->entity_id_queue, (void *)&e->id);
id_to_remove = e->child;
memset(e, 0, sizeof(Entity));
if(!destroy_children) {
break;
}
}
}
Entity *game_world_try_get_entity_by_id(Game_World *world, Entity_ID id) {

View File

@@ -23,7 +23,7 @@ Game_World *game_world_create(uint32_t seed, uint16_t level_width, uint16_t leve
void game_world_destroy(Game_World *world);
bool game_world_create_entity(Game_World *world, Entity_Type type, uint16_t x, uint16_t y, Entity_ID *out_entity_id);
void game_world_destroy_entity(Game_World *world, Entity_ID entity_id);
void game_world_destroy_entity(Game_World *world, Entity_ID entity_id, bool destroy_children);
// TODO: SS - "void game_world_spawn_player(Game_World *world, ..)"

View File

@@ -82,7 +82,7 @@ void simulation_world_tick(Simulation_World *simulation_world) {
assert(player->active);
player->active = false;
game_world_destroy_entity(simulation_world->game_world, player->entity_id);
game_world_destroy_entity(simulation_world->game_world, player->entity_id, true);
break;
}