From 38a2478325d606e6018c49056c8deae393d2041b Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 6 Aug 2024 17:58:52 -0500 Subject: [PATCH] fix updates not ran on new entities added from game commands --- src/entity.c | 22 ++++++++++++---------- src/game.c | 6 +++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/entity.c b/src/entity.c index f355193f..1dde5560 100644 --- a/src/entity.c +++ b/src/entity.c @@ -214,20 +214,22 @@ void entity_link(struct entity *parent, struct entity *child) { struct entity_store *store = entity_get_store(parent); - struct entity *first_child = entity_from_handle(store, parent->first); - struct entity *last_child = entity_from_handle(store, parent->last); + struct entity_handle child_handle = child->handle; + struct entity_handle parent_handle = parent->handle; - child->prev = last_child->handle; - child->parent = parent->handle; + child->parent = parent_handle; + if (!parent->first.gen) { + parent->first = child_handle; + } + + struct entity_handle last_child_handle = parent->last; + struct entity *last_child = entity_from_handle(store, last_child_handle); if (last_child->valid) { - last_child->next = child->handle; - } - parent->last = child->handle; - - if (!first_child->valid) { - parent->first = child->handle; + child->prev = last_child_handle; + last_child->next = child_handle; } + parent->last = child_handle; } void entity_unlink(struct entity *ent) diff --git a/src/game.c b/src/game.c index 2d02a60a..7dcbb6c8 100644 --- a/src/game.c +++ b/src/game.c @@ -222,7 +222,6 @@ INTERNAL void game_update(void) G.world.tick_ts = sys_timestamp(); G.world.dt = max_f64(0.0, (1.0 / GAME_FPS) * G.world.timescale); G.world.time += G.world.dt; - struct entity_array entities_array = entity_store_as_array(G.world.entity_store); /* ========================== * * Process game cmds @@ -245,8 +244,8 @@ INTERNAL void game_update(void) /* Clear level */ case GAME_CMD_KIND_CLEAR_ALL: { logf_info("Clearing level"); - for (u64 i = 0; i < entities_array.count; ++i) { - struct entity *ent = &entities_array.entities[i]; + for (u64 i = 0; i < G.world.entity_store->count; ++i) { + struct entity *ent = &G.world.entity_store->entities[i]; if (ent->valid && !ent->root) { entity_release(G.world.entity_store, ent); } @@ -272,6 +271,7 @@ INTERNAL void game_update(void) /* ---------------------------------------------------------------------- */ struct entity_handle root_handle = G.world.entity_store->root; + struct entity_array entities_array = entity_store_as_array(G.world.entity_store); /* ========================== * * Update sprite