From 954e2110f10e0dbde3e5d6c511bb88b9925e1c53 Mon Sep 17 00:00:00 2001 From: antopilo Date: Sat, 21 Sep 2024 12:52:56 -0400 Subject: [PATCH] Fixed prefab serialization and instancing into scene --- Nuake/src/Resource/Prefab.cpp | 46 +++++++++----------- Nuake/src/Scene/Components/PrefabComponent.h | 2 + Nuake/src/Scene/Entities/Entity.cpp | 5 ++- Nuake/src/Scene/Scene.cpp | 4 +- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/Nuake/src/Resource/Prefab.cpp b/Nuake/src/Resource/Prefab.cpp index 0e3ebf5b..a33e3c08 100644 --- a/Nuake/src/Resource/Prefab.cpp +++ b/Nuake/src/Resource/Prefab.cpp @@ -85,25 +85,21 @@ namespace Nuake Entity newEntity = { entity.GetScene()->m_Registry.create(), entity.GetScene() }; newEntity.Deserialize(e); // Id gets overriden by serialized id. - if (newEntity.GetComponent().HasParent) + ParentComponent& parentComponent = newEntity.GetComponent(); + if (parentComponent.HasParent && parentComponent.ParentID != j["Root"]) { - auto pId = newEntity.GetComponent().ParentID; - newEntity.GetComponent().ParentID = newIdsLut[pId]; - auto nc = entity.GetScene()->GetEntityByID(pId); - nc.GetComponent().RemoveChildren(newEntity); - newEntity.GetComponent().Parent = entity.GetScene()->GetEntityByID(newIdsLut[pId]); - entity.GetScene()->GetEntityByID(newIdsLut[pId]).AddChild(newEntity); - - + //auto pId = newEntity.GetComponent().ParentID; + //newEntity.GetComponent().ParentID = newIdsLut[pId]; + //auto nc = entity.GetScene()->GetEntityByID(pId).GetComponent().RemoveChildren(newEntity); + //newEntity.GetComponent().Parent = entity.GetScene()->GetEntityByID(newIdsLut[pId]); + //entity.GetScene()->GetEntityByID(newIdsLut[pId]).AddChild(newEntity); } - - if (newEntity.GetComponent().ParentID == j["Root"]) + else if (newEntity.GetComponent().ParentID == j["Root"]) { - entity.AddChild(newEntity); + //entity.AddChild(newEntity); } auto& nameComponent = newEntity.GetComponent(); - uint32_t oldId = nameComponent.ID; uint32_t newId = OS::GetTime(); nameComponent.Name = nameComponent.Name; @@ -117,18 +113,18 @@ namespace Nuake // Set reference to the parent entity to children for (auto& e : newPrefab->Entities) { - auto& parentC = e.GetComponent(); - auto parent = entity.GetScene()->GetEntityByID(newIdsLut[parentC.ParentID]); - - Logger::Log(parent.GetComponent().Name); - if (parentC.ParentID == j["Root"]) - { - entity.AddChild(e); - } - else - { - parent.AddChild(e); - } + //auto& parentC = e.GetComponent(); + //auto parent = entity.GetScene()->GetEntityByID(newIdsLut[parentC.ParentID]); + // + //Logger::Log(parent.GetComponent().Name); + //if (parentC.ParentID == j["Root"]) + //{ + // entity.AddChild(e); + //} + //else + //{ + // parent.AddChild(e); + //} } // Since the bones point to an entity, and we are instancing a prefab, the new skeleton is gonna be pointing to the wrong diff --git a/Nuake/src/Scene/Components/PrefabComponent.h b/Nuake/src/Scene/Components/PrefabComponent.h index f29ce47a..95b3008e 100644 --- a/Nuake/src/Scene/Components/PrefabComponent.h +++ b/Nuake/src/Scene/Components/PrefabComponent.h @@ -9,6 +9,8 @@ namespace Nuake { Ref PrefabInstance; std::string Path; + bool isInitialized = false; + void SetPrefab(Ref prefab) { PrefabInstance = prefab; diff --git a/Nuake/src/Scene/Entities/Entity.cpp b/Nuake/src/Scene/Entities/Entity.cpp index 425ba3c1..773a2080 100644 --- a/Nuake/src/Scene/Entities/Entity.cpp +++ b/Nuake/src/Scene/Entities/Entity.cpp @@ -136,8 +136,9 @@ namespace Nuake } } - Prefab::InstanceOntoRoot(Entity{(entt::entity)GetHandle(), m_Scene}, prefabPath); - + prefabComp.PrefabInstance = Prefab::InstanceOntoRoot(Entity{(entt::entity)GetHandle(), m_Scene}, prefabPath); + prefabComp.Path = prefabPath; + DeserializeComponents(rootJson); } else diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index f1e15dc1..1d9045e8 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -318,7 +318,7 @@ namespace Nuake } Ref file = FileSystem::GetFile(filePath); - if (file->GetHasBeenModified()) + if (file->GetHasBeenModified() || !prefabComponent.isInitialized) { prefabToReimport[filePath] = file; } @@ -342,7 +342,6 @@ namespace Nuake continue; } - for (auto& ent : prefabInstance->Entities) { // Destroy all children, not the root! @@ -358,6 +357,7 @@ namespace Nuake }); prefabInstance->ReInstance(); + prefabComponent.isInitialized = true; } for (auto& [path, prefabFile] : prefabToReimport)