From 199afff8201d30f1012876123d283df5875f2e86 Mon Sep 17 00:00:00 2001 From: samstalhandske Date: Thu, 18 Dec 2025 12:10:25 +0100 Subject: [PATCH] Added 'destroy_children' parameter to game_world_destroy_entity(). --- src/game/shared/game_world.c | 33 +++++++++++++++++--------- src/game/shared/game_world.h | 2 +- src/game/simulation/simulation_world.c | 2 +- src/todo.md | 18 ++++++++++++++ 4 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 src/todo.md diff --git a/src/game/shared/game_world.c b/src/game/shared/game_world.c index 12ede47..2386c92 100644 --- a/src/game/shared/game_world.c +++ b/src/game/shared/game_world.c @@ -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) { diff --git a/src/game/shared/game_world.h b/src/game/shared/game_world.h index 888a8d5..25ed6ed 100644 --- a/src/game/shared/game_world.h +++ b/src/game/shared/game_world.h @@ -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, ..)" diff --git a/src/game/simulation/simulation_world.c b/src/game/simulation/simulation_world.c index fbc9364..dad6279 100644 --- a/src/game/simulation/simulation_world.c +++ b/src/game/simulation/simulation_world.c @@ -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; } diff --git a/src/todo.md b/src/todo.md new file mode 100644 index 0000000..6e659c6 --- /dev/null +++ b/src/todo.md @@ -0,0 +1,18 @@ +# TODO +- Add player-death. + - [ ] Went out of bounds. + - [ ] Hit other snake's head. + - [ ] Hit yours/other snake's body. +- Spawn players at a random position. + - [ ] When initially joining. + - [ ] When dead and should respawn. +- Spawn food every N tick(?) + - [ ] Spawn food at a random position every N tick. + - Do we want a "max food on the board" value? + - Why? + - This can easily be implemented later. + - If we have that much food on the board, don't spawn new food. + - Otherwise, spawn new food at a random position. +- [ ] When a player disconnects, all children should recursively be destroyed too. Not just the head. +- [ ] Sound effects + music. +- [ ] Customize the session. \ No newline at end of file