forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.cpp
More file actions
152 lines (137 loc) · 3.11 KB
/
Copy pathGameObject.cpp
File metadata and controls
152 lines (137 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "PCH.h"
#include "GameObject.h"
#include "Component/Component.h"
#include "Core/SObjectManager.h"
namespace sh::game
{
SH_GAME_API GameObject::GameObject(World& world, const std::string& name) :
world(world),
bInit(false), bEnable(true), activeSelf(bEnable)
{
transform = AddComponent<Transform>();
SetName(name);
}
SH_GAME_API GameObject::GameObject(GameObject&& other) noexcept :
SObject(std::move(other)),
world(other.world), transform(other.transform),
bInit(other.bInit), bEnable(other.bEnable), activeSelf(bEnable),
components(std::move(other.components))
{
other.transform = nullptr;
}
SH_GAME_API GameObject::~GameObject()
{
SH_INFO_FORMAT("~GameObject: {}", GetName());
}
SH_GAME_API void GameObject::Awake()
{
for (auto& Component : components)
{
if (Component->active)
Component->Awake();
}
bInit = true;
}
SH_GAME_API void GameObject::Start()
{
for (auto& Component : components)
{
if (Component->active)
Component->Start();
}
}
SH_GAME_API void GameObject::OnEnable()
{
for (auto& Component : components)
{
if(Component->active)
Component->OnEnable();
}
}
SH_GAME_API void GameObject::Destroy()
{
for (auto component : components)
{
component->Destroy();
}
components.clear();
Super::Destroy();
}
SH_GAME_API void GameObject::BeginUpdate()
{
for (auto& Component : components)
{
if (Component->active)
Component->BeginUpdate();
}
}
SH_GAME_API void GameObject::FixedUpdate()
{
for (auto& Component : components)
{
if (Component->active)
Component->FixedUpdate();
}
}
SH_GAME_API void GameObject::Update()
{
for (auto& Component : components)
{
if (Component->active)
Component->Update();
}
}
SH_GAME_API void GameObject::LateUpdate()
{
for (auto& Component : components)
{
if (Component->active)
Component->LateUpdate();
}
}
SH_GAME_API void GameObject::SetActive(bool b)
{
if (!bEnable && b == true)
{
if (!bInit)
Awake();
OnEnable();
}
bEnable = b;
}
SH_GAME_API auto GameObject::GetComponents() const -> const std::vector<Component*>&
{
return components;
}
SH_GAME_API void GameObject::AddComponent(Component* component)
{
if (!core::IsValid(component) || &component->gameObject != this)
return;
components.push_back(std::move(component));
components.back()->SetActive(true);
}
SH_GAME_API auto GameObject::Serialize() const -> core::Json
{
core::Json mainJson = Super::Serialize();
core::Json componentJsons = core::Json::array();
for (auto component : components)
{
if (!core::IsValid(component))
continue;
componentJsons.push_back(component->Serialize());
}
mainJson["Components"] = componentJsons;
return mainJson;
}
SH_GAME_API void GameObject::Deserialize(const core::Json& json)
{
Super::Deserialize(json);
core::SObjectManager* objManager = core::SObjectManager::GetInstance();
for (auto& compJson : json["Components"])
{
std::string uuid = compJson["uuid"].get<std::string>();
Component* comp = static_cast<Component*>(objManager->GetSObject(uuid));
comp->Deserialize(compJson);
}
}
}