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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include "GameObject.hpp"
#include "Level.hpp"
#include "Player.hpp"
#include "SceneThings.hpp"
#include "World.hpp"
#include <Metadata.hpp>
#include <RapidJsonHelper.hpp>
#include <rapidjson/document.h>
#include <cassert>
#include <string_view>
#include <utility>
using namespace std::literals;
namespace ProjectBrussel_UNITY_ID {
GameObject* CreateGameObject(GameObject::Kind kind, GameWorld* world) {
using enum Tags::GameObjectKind;
switch (kind) {
case KD_Generic: return new GameObject(world);
case KD_SimpleGeometry: return new SimpleGeometryObject(world);
case KD_Building: return new BuildingObject(world);
case KD_LevelWrapper: return new LevelWrapperObject(world);
default: break;
}
return nullptr;
}
bool ValidateGameObjectChild(GameObject* parent, GameObject* child) {
return parent->GetWorld() == child->GetWorld();
}
} // namespace ProjectBrussel_UNITY_ID
void GameObject::FreeRecursive(GameObject* obj) {
if (!obj->mStopFreePropagation) {
for (auto child : obj->GetChildren()) {
FreeRecursive(obj);
}
}
delete obj;
}
GameObject::GameObject(GameWorld* world)
: GameObject(KD_Generic, world) {
}
GameObject::GameObject(Kind kind, GameWorld* world)
: mEditorAttachment{ nullptr }
, mWorld{ world }
, mParent{ nullptr }
, mRot(1.0f, 0.0f, 0.0f, 0.0f)
, mPos(0.0f, 0.0f, 0.0f)
, mScale(1.0f, 1.0f, 1.0f)
, mKind{ kind } {
}
GameObject::~GameObject() {
RemoveAllChildren();
if (mParent) {
mParent->RemoveChild(this);
// NOTE: from this point on, mParent will be nullptr
}
}
GameObject::Kind GameObject::GetKind() const {
return mKind;
}
GameWorld* GameObject::GetWorld() const {
return mWorld;
}
GameObject* GameObject::GetParent() const {
return mParent;
}
const PodVector<GameObject*>& GameObject::GetChildren() const {
return mChildren;
}
void GameObject::AddChild(GameObject* child) {
using namespace ProjectBrussel_UNITY_ID;
if (child->mParent) {
return;
}
if (!ValidateGameObjectChild(this, child)) {
return;
}
mChildren.push_back(child);
child->SetParent(this);
}
GameObject* GameObject::RemoveChild(int index) {
if (index < 0 || index >= mChildren.size()) {
return nullptr;
}
auto it = mChildren.begin() + index;
auto child = *it;
// cancelUpdate(ret);
std::swap(*it, mChildren.back());
mChildren.pop_back();
child->SetParent(nullptr);
return child;
}
GameObject* GameObject::RemoveChild(GameObject* child) {
if (child) {
for (auto it = mChildren.begin(); it != mChildren.end(); ++it) {
if (*it == child) {
// cancelUpdate(child);
std::swap(*it, mChildren.back());
mChildren.pop_back();
child->SetParent(nullptr);
return child;
}
}
}
return nullptr;
}
void GameObject::RemoveSelfFromParent() {
if (mParent) {
mParent->RemoveChild(this);
}
}
PodVector<GameObject*> GameObject::RemoveAllChildren() {
for (auto& child : mChildren) {
child->SetParent(nullptr);
}
auto result = std::move(mChildren);
// Moving from STL object leaves it in a valid but _unspecified_ state, call std::vector::clear() to guarantee it's empty
// NOTE: even though we have the source code of PodVector<T>, we still do this to follow convention
mChildren.clear();
return result;
}
const glm::vec3& GameObject::GetPos() const {
return mPos;
}
void GameObject::SetPos(const glm::vec3& pos) {
mPos = pos;
}
const glm::quat& GameObject::GetRotation() const {
return mRot;
}
void GameObject::SetRotation(const glm::quat& rotation) {
mRot = rotation;
}
const glm::vec3& GameObject::GetScale() const {
return mScale;
}
void GameObject::SetScale(const glm::vec3& scale) {
mScale = scale;
}
std::span<const RenderObject> GameObject::GetRenderObjects() const {
return {};
}
void GameObject::OnInitialized() {
}
void GameObject::Awaken() {
}
void GameObject::Resleep() {
}
void GameObject::Update() {
}
rapidjson::Value GameObject::Serialize(GameObject* obj, rapidjson::Document& root) {
rapidjson::Value result(rapidjson::kObjectType);
result.AddMember("Type", rapidjson::StringRef(Metadata::EnumToString(obj->GetKind())), root.GetAllocator());
rapidjson::Value rvValue(rapidjson::kObjectType);
obj->WriteSaveFormat(rvValue, root);
result.AddMember("Value", rvValue, root.GetAllocator());
return result;
}
std::unique_ptr<GameObject> GameObject::Deserialize(const rapidjson::Value& value, GameWorld* world) {
using namespace ProjectBrussel_UNITY_ID;
auto rvType = rapidjson::GetProperty(value, rapidjson::kStringType, "Type"sv);
if (!rvType) return nullptr;
auto rvValue = rapidjson::GetProperty(value, rapidjson::kObjectType, "Value"sv);
if (!rvValue) return nullptr;
auto kind = Metadata::EnumFromString<Kind>(rapidjson::AsStringView(*rvType));
assert(kind.has_value());
auto obj = std::unique_ptr<GameObject>(CreateGameObject(kind.value(), world));
if (!obj) return nullptr;
obj->ReadSaveFormat(*rvValue);
return obj;
}
void GameObject::ReadSaveFormat(const rapidjson::Value& value) {
}
void GameObject::WriteSaveFormat(rapidjson::Value& value, rapidjson::Document& root) {
}
void GameObject::SetParent(GameObject* parent) {
if (mParent != parent) {
mParent = parent;
// needUpdate();
}
}
#include <generated/GameObject.gs.inl>
|