From 19088cd805cdd695d4021a042288afe75c630152 Mon Sep 17 00:00:00 2001 From: Honeybunch Date: Tue, 11 Jun 2024 17:36:18 -0700 Subject: [PATCH] Fixing parenting issues --- source/tb_rigidbody_component.cpp | 4 ++-- source/tb_scene.c | 36 +++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/source/tb_rigidbody_component.cpp b/source/tb_rigidbody_component.cpp index 5ebef3d8..9014e04a 100644 --- a/source/tb_rigidbody_component.cpp +++ b/source/tb_rigidbody_component.cpp @@ -253,7 +253,7 @@ void tb_on_rigidbody_set(flecs::entity ent, TbRigidbodyComponent &rb) { auto body = JPH::BodyID(rb); bodies.SetPositionAndRotation(body, position, rotation, - JPH::EActivation::DontActivate); + JPH::EActivation::Activate); } void tb_on_rigidbody_removed(flecs::entity ent, TbRigidbodyComponent &rb) { @@ -516,7 +516,7 @@ bool tb_load_rigidbody_comp(ecs_world_t *_ecs, ecs_entity_t ent, body_settings.mMassPropertiesOverride.mMass = 1.0f; JPH::BodyID body = - bodies.CreateAndAddBody(body_settings, JPH::EActivation::DontActivate); + bodies.CreateAndAddBody(body_settings, JPH::EActivation::Activate); TbRigidbodyComponent comp = { body.GetIndexAndSequenceNumber(), diff --git a/source/tb_scene.c b/source/tb_scene.c index ebca2c86..3b9ad4b6 100644 --- a/source/tb_scene.c +++ b/source/tb_scene.c @@ -44,7 +44,9 @@ ECS_TAG_DECLARE(TbSceneParsing); ECS_TAG_DECLARE(TbSceneParsed); ECS_TAG_DECLARE(TbSceneLoading); ECS_TAG_DECLARE(TbSceneLoaded); +ECS_TAG_DECLARE(TbSceneReady); ECS_TAG_DECLARE(TbComponentsReady); +ECS_TAG_DECLARE(TbEntityEnabled); ECS_TAG_DECLARE(TbEntityReady); typedef struct TbSceneParsedArgs { @@ -282,7 +284,9 @@ ecs_entity_t tb_load_entity(ecs_world_t *ecs, const char *source_path, } } - ecs_enable(ecs, ent, enabled); + if (enabled) { + ecs_add(ecs, ent, TbEntityEnabled); + } return ent; } @@ -407,7 +411,6 @@ void tb_ready_check_entities(ecs_iter_t *it) { tb_auto filter_it = ecs_filter_iter(ecs, filter); while (ecs_filter_next(&filter_it)) { for (int32_t ent_idx = 0; ent_idx < filter_it.count; ++ent_idx) { - tb_auto entity = filter_it.entities[ent_idx]; // Entity already ready @@ -447,6 +450,31 @@ void tb_ready_check_entities(ecs_iter_t *it) { } } +void tb_ready_check_scenes(ecs_iter_t *it) { + tb_auto ecs = it->world; + for (int32_t scene_idx = 0; scene_idx < it->count; ++scene_idx) { + TbScene scene = it->entities[scene_idx]; + + // We're here because all entities are loaded + // Mark everything as ready! + tb_auto filter = ecs_filter(ecs, {.terms = { + {.id = ecs_id(TbSceneRef)}, + }}); + tb_auto filter_it = ecs_filter_iter(ecs, filter); + while (ecs_filter_next(&filter_it)) { + for (int32_t ent_idx = 0; ent_idx < filter_it.count; ++ent_idx) { + tb_auto entity = filter_it.entities[ent_idx]; + if (ecs_has(ecs, entity, TbTransformComponent)) { + tb_transform_mark_dirty(ecs, entity); + } + } + } + + ecs_remove(ecs, scene, TbSceneLoaded); + ecs_add(ecs, scene, TbSceneReady); + } +} + void tb_register_scene_sys(TbWorld *world) { tb_auto ecs = world->ecs; ECS_COMPONENT_DEFINE(ecs, TbEntityTaskQueue); @@ -460,7 +488,9 @@ void tb_register_scene_sys(TbWorld *world) { ECS_TAG_DEFINE(ecs, TbSceneParsed); ECS_TAG_DEFINE(ecs, TbSceneLoading); ECS_TAG_DEFINE(ecs, TbSceneLoaded); + ECS_TAG_DEFINE(ecs, TbSceneReady); ECS_TAG_DEFINE(ecs, TbComponentsReady); + ECS_TAG_DEFINE(ecs, TbEntityEnabled); ECS_TAG_DEFINE(ecs, TbEntityReady); // This is a no-readonly system because we are adding entities @@ -482,6 +512,8 @@ void tb_register_scene_sys(TbWorld *world) { ECS_SYSTEM(ecs, tb_ready_check_entities, EcsPostLoad, [in] TbSceneEntityCount, [out] TbSceneEntReadyCounter, TbSceneLoading); + + ECS_SYSTEM(ecs, tb_ready_check_scenes, EcsPostLoad, TbSceneLoaded); } void tb_unregister_scene_sys(TbWorld *world) { (void)world; }