CSEngine
Loading...
Searching...
No Matches
SCloneFactory.cpp
1//
2// Created by ounols on 19. 2. 13.
3//
4
5#include "SCloneFactory.h"
6#include "SceneMgr.h"
7#include "../Object/SScene.h"
8#include "../Component/TransformComponent.h"
9#include "EngineCore.h"
10#include "../Object/SGameObjectFromSPrefab.h"
11
12#include <map>
13
14using namespace CSE;
15
16static void CloningObjects(SGameObject* object, std::map<SComponent*, SComponent*>& clone_comp,
17 std::map<SGameObject*, SGameObject*>& clone_obj, SGameObject* parent);
18
19
20SCloneFactory::SCloneFactory() = default;
21
22SCloneFactory::~SCloneFactory() = default;
23
24SComponent* SCloneFactory::Clone(SComponent* component, SGameObject* parent) {
25 if(parent == nullptr) {
26 Scene* currentScene = CORE->GetCore(SceneMgr)->GetCurrentScene();
27 if(!dynamic_cast<SScene*>(currentScene)) return nullptr;
28
29 parent = static_cast<SScene*>(currentScene)->GetRoot();
30 }
31
32 SComponent* clone_component = component->Clone(parent);
33 clone_component->CopyReference(component, std::map<SGameObject*, SGameObject*>(), std::map<SComponent*, SComponent*>());
34
35 return clone_component;
36}
37
38SGameObject* SCloneFactory::Clone(SGameObject* object, SGameObject* parent) {
39 if(parent == nullptr) {
40 Scene* currentScene = CORE->GetCore(SceneMgr)->GetCurrentScene();
41 if(!dynamic_cast<SScene*>(currentScene)) return nullptr;
42
43 parent = static_cast<SScene*>(currentScene)->GetRoot();
44 }
45
46
47 std::map<SComponent*, SComponent*> clone_component;
48 std::map<SGameObject*, SGameObject*> clone_object;
49
50 CloningObjects(object, clone_component, clone_object, parent);
51
52 SGameObject* cloneObject_root = clone_object[object];
53
54
55 for(const auto& component_pair : clone_component) {
56 SComponent* component = component_pair.second;
57 component->CopyReference(component_pair.first, clone_object, clone_component);
58 }
59
60 return cloneObject_root;
61}
62
63
64void CloningObjects(SGameObject* object, std::map<SComponent*, SComponent*>& clone_comp,
65 std::map<SGameObject*, SGameObject*>& clone_obj, SGameObject* parent) {
66 if(object == nullptr) return;
67
68 //게임 오브젝트 복사
69 SGameObject* cloneObject = nullptr;
70 const auto& objectFromPrefab = dynamic_cast<SGameObjectFromSPrefab*>(object);
71 if(objectFromPrefab != nullptr) {
72 const auto& cloneFromPrefab = new SGameObjectFromSPrefab(object->GetName() + "");
73 cloneFromPrefab->SetRefHash(objectFromPrefab->GetRefHash());
74 cloneObject = cloneFromPrefab;
75 } else {
76 cloneObject = new SGameObject(object->GetName() + "");
77 }
78 clone_obj[object] = cloneObject;
79 cloneObject->SetIsEnable(object->GetIsEnable());
80 cloneObject->SetIsPrefab(object->isPrefab(true));
81 cloneObject->SetResourceID(object->GetResourceID());
82
83
84 //게임 컴포넌트 복사
85 const auto& target_component = object->GetComponents();
86 for(const auto& component : target_component) {
87 if(dynamic_cast<TransformComponent*>(component)) {
88 auto transform_copy = static_cast<TransformComponent*>(cloneObject->GetTransform());
89 TransformInterface* transform_src = static_cast<TransformComponent*>(component);
90 clone_comp[component] = transform_copy;
91
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);
95 continue;
96 }
97 const auto& copy_comp = component->Clone(cloneObject);
98 clone_comp[component] = copy_comp;
99 copy_comp->SetIsEnable(component->GetIsEnable());
100// cloneObject->AddComponent(copy_comp);
101 }
102
103 auto children = object->GetChildren();
104 for(const auto& child : children) {
105 CloningObjects(child, clone_comp, clone_obj, cloneObject);
106 }
107 parent->AddChild(cloneObject);
108}