From 50a4b42284846ad85d54493b93ac4f1970eedc31 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sun, 5 Mar 2023 21:44:06 -0500 Subject: [PATCH] Using serializer for the scene copy --- .../Scene/Components/WrenScriptComponent.h | 19 ++++++++ Nuake/src/Scene/Entities/Entity.h | 10 +++-- Nuake/src/Scene/Scene.cpp | 45 +++---------------- Nuake/src/Scene/Systems/TransformSystem.cpp | 1 - 4 files changed, 30 insertions(+), 45 deletions(-) diff --git a/Nuake/src/Scene/Components/WrenScriptComponent.h b/Nuake/src/Scene/Components/WrenScriptComponent.h index ba098273..36ddbe19 100644 --- a/Nuake/src/Scene/Components/WrenScriptComponent.h +++ b/Nuake/src/Scene/Components/WrenScriptComponent.h @@ -11,10 +11,22 @@ namespace Nuake { unsigned int mModule = 0; Ref mWrenScript; + void LoadScript(const std::string& path) + { + Ref nuakeFile = Nuake::FileSystem::GetFile(Script); + mWrenScript = CreateRef(nuakeFile, true); + auto modules = mWrenScript->GetModules(); + if (modules.size() > 0) + { + mModule = 0; + } + } + json Serialize() { BEGIN_SERIALIZE(); SERIALIZE_VAL(Script); + SERIALIZE_VAL(mModule) END_SERIALIZE(); } @@ -22,8 +34,15 @@ namespace Nuake { { BEGIN_DESERIALIZE(); if (j.contains("Script")) + { Script = j["Script"]; + LoadScript(Script); + } + if (j.contains("mModule")) + { + mModule = j["mModule"]; + } return true; } diff --git a/Nuake/src/Scene/Entities/Entity.h b/Nuake/src/Scene/Entities/Entity.h index 19ed979f..de18739c 100644 --- a/Nuake/src/Scene/Entities/Entity.h +++ b/Nuake/src/Scene/Entities/Entity.h @@ -36,18 +36,20 @@ namespace Nuake } template - void RemoveComponent() { + void RemoveComponent() + { m_Scene->m_Registry.remove(m_EntityHandle); } template - T& GetComponent() { + T& GetComponent() + { T& component = m_Scene->m_Registry.get(m_EntityHandle); return component; } - void Destroy() { - Logger::Log("Deleted eneity :" + std::to_string((int)m_EntityHandle)); + void Destroy() + { m_Scene->m_Registry.destroy(m_EntityHandle); } diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 6faf4fd8..9e8e88d2 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -214,7 +214,9 @@ namespace Nuake { ParentComponent& parentC = entity.GetComponent(); std::vector copyChildrens = parentC.Children; - if (parentC.HasParent) { // Remove self from parents children lists. + if (parentC.HasParent) + { + // Remove self from parents children lists. ParentComponent& parent = parentC.Parent.GetComponent(); parent.RemoveChildren(entity); } @@ -306,47 +308,10 @@ namespace Nuake { Ref Scene::Copy() { Ref sceneCopy = CreateRef(); - sceneCopy->Path = this->Path; - sceneCopy->Name = this->Name; - - sceneCopy->m_EditorCamera = this->m_EditorCamera->Copy(); - - sceneCopy->SetEnvironment(this->GetEnvironment()->Copy()); - auto& srcRegistry = this->m_Registry; - auto& dstRegistry = sceneCopy->m_Registry; - auto idView = srcRegistry.view(); - for (auto e : idView) - { - NameComponent& nameComponent = srcRegistry.get(e); - int id = nameComponent.ID; - std::string& name = nameComponent.Name; - sceneCopy->CreateEntity(name, id); - } + json serializedScene = Serialize(); - CopyComponent(sceneCopy->m_Registry, this->m_Registry); - - /*auto parentView = m_Registry.view(); - for (auto e : parentView) - { - auto& [parentComponent, nameComponent] = m_Registry.get(e); - - if (parentComponent.HasParent) - { - int id = parentComponent.ParentID; - - auto newEnt = sceneCopy->GetEntityByID(nameComponent.ID); - sceneCopy->GetEntityByID(id).AddChild(newEnt); - } - }*/ - - CopyComponent(sceneCopy->m_Registry, this->m_Registry); - CopyComponent(sceneCopy->m_Registry, this->m_Registry); - CopyComponent(sceneCopy->m_Registry, this->m_Registry); - CopyComponent(sceneCopy->m_Registry, this->m_Registry); - CopyComponent(sceneCopy->m_Registry, this->m_Registry); - CopyComponent(sceneCopy->m_Registry, this->m_Registry); - CopyComponent(sceneCopy->m_Registry, this->m_Registry); + sceneCopy->Deserialize(serializedScene.dump()); return sceneCopy; } diff --git a/Nuake/src/Scene/Systems/TransformSystem.cpp b/Nuake/src/Scene/Systems/TransformSystem.cpp index e4e7b1ae..a62a9ebd 100644 --- a/Nuake/src/Scene/Systems/TransformSystem.cpp +++ b/Nuake/src/Scene/Systems/TransformSystem.cpp @@ -103,7 +103,6 @@ namespace Nuake { globalScale *= transformComponent.GetLocalScale(); NameComponent& nameComponent = parentComponent.Parent.GetComponent(); - //std::cout << "Name:" << nameComponent.Name << std::endl; parentComponent = parentComponent.Parent.GetComponent(); }