5#include "SCloneFactory.h"
7#include "../Object/SScene.h"
8#include "../Component/TransformComponent.h"
10#include "../Object/SGameObjectFromSPrefab.h"
16static void CloningObjects(
SGameObject*
object, std::map<SComponent*, SComponent*>& clone_comp,
17 std::map<SGameObject*, SGameObject*>& clone_obj,
SGameObject* parent);
20SCloneFactory::SCloneFactory() =
default;
22SCloneFactory::~SCloneFactory() =
default;
25 if(parent ==
nullptr) {
26 Scene* currentScene = CORE->GetCore(
SceneMgr)->GetCurrentScene();
27 if(!
dynamic_cast<SScene*
>(currentScene))
return nullptr;
29 parent =
static_cast<SScene*
>(currentScene)->GetRoot();
32 SComponent* clone_component = component->Clone(parent);
33 clone_component->CopyReference(component, std::map<SGameObject*, SGameObject*>(), std::map<SComponent*, SComponent*>());
35 return clone_component;
39 if(parent ==
nullptr) {
40 Scene* currentScene = CORE->GetCore(
SceneMgr)->GetCurrentScene();
41 if(!
dynamic_cast<SScene*
>(currentScene))
return nullptr;
43 parent =
static_cast<SScene*
>(currentScene)->GetRoot();
47 std::map<SComponent*, SComponent*> clone_component;
48 std::map<SGameObject*, SGameObject*> clone_object;
50 CloningObjects(
object, clone_component, clone_object, parent);
52 SGameObject* cloneObject_root = clone_object[object];
55 for(
const auto& component_pair : clone_component) {
57 component->CopyReference(component_pair.first, clone_object, clone_component);
60 return cloneObject_root;
64void CloningObjects(
SGameObject*
object, std::map<SComponent*, SComponent*>& clone_comp,
65 std::map<SGameObject*, SGameObject*>& clone_obj,
SGameObject* parent) {
66 if(
object ==
nullptr)
return;
71 if(objectFromPrefab !=
nullptr) {
73 cloneFromPrefab->SetRefHash(objectFromPrefab->GetRefHash());
74 cloneObject = cloneFromPrefab;
76 cloneObject =
new SGameObject(object->GetName() +
"");
78 clone_obj[object] = cloneObject;
79 cloneObject->SetIsEnable(object->GetIsEnable());
80 cloneObject->SetIsPrefab(object->isPrefab(
true));
81 cloneObject->SetResourceID(object->GetResourceID());
85 const auto& target_component =
object->GetComponents();
86 for(
const auto& component : target_component) {
90 clone_comp[component] = transform_copy;
92 transform_copy->m_position =
vec3(transform_src->m_position);
93 transform_copy->m_rotation =
Quaternion(transform_src->m_rotation);
94 transform_copy->m_scale =
vec3(transform_src->m_scale);
97 const auto& copy_comp = component->Clone(cloneObject);
98 clone_comp[component] = copy_comp;
99 copy_comp->SetIsEnable(component->GetIsEnable());
103 auto children =
object->GetChildren();
104 for(
const auto& child : children) {
105 CloningObjects(child, clone_comp, clone_obj, cloneObject);
107 parent->AddChild(cloneObject);