-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscene.cpp
190 lines (156 loc) · 5.12 KB
/
scene.cpp
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
#include "scene.h"
#include "world.h"
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
Scene::Scene(World* world)
: mWorld(world)
{
}
Scene::~Scene()
{
mWorld = nullptr;
}
void Scene::initBlankScene()
{
auto entityManager = mWorld->getEntityManager();
// Editor cam
auto camera = entityManager->createEntity("EditorCam");
auto [editorTrans, editorCam, editorInput] = entityManager->addComponent<TransformComponent, CameraComponent, InputComponent>(camera);
editorTrans.setPosition(gsl::vec3{-1.f, 3.f, 10.f});
editorTrans.setRotation({1.f, .27f, .1f, 0.f});
editorCam.isEditorCamera = true;
editorCam.yaw = 10.f;
editorCam.pitch = 30.f;
for(auto& info : entityManager->getEntityInfos())
{
if(info.entityId == camera)
{
info.shouldShowInEditor = false;
}
}
// Game cam
camera = entityManager->createEntity("GameCam");
auto [gameTrans, gameCam, gameMesh] = entityManager->addComponent<TransformComponent, CameraComponent, MeshComponent>(camera);
gameTrans.setPosition(gsl::vec3{0.f, 0.f, 5.f});
if(auto meshData = ResourceManager::instance().getMesh("camera"))
{
gameMesh.meshData = *meshData;
gameMesh.isVisible = true;
}
// Light
auto entity = entityManager->createEntity("light");
entityManager->addComponent<TransformComponent, DirectionalLightComponent>(entity);
}
bool Scene::LoadFromFile(const std::string& path)
{
QFile file(QString::fromStdString(path));
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "ERROR Scene.load(): Failed to open file at specified path!";
return false;
}
QFileInfo info(QString::fromStdString(path));
auto baseName = info.baseName();
if(baseName.contains("temp"))
baseName = baseName.replace("temp", "");
name = baseName.toStdString();
QJsonDocument document = QJsonDocument::fromJson(file.readAll());
if(!document.isObject())
{
qDebug() << "Load Error: Wrong formatting";
return false;
}
QJsonObject mainObject = document.object();
if(mainObject.isEmpty())
{
qDebug() << "Load Error: File is empty.";
return false;
}
filePath = path;
mWorld->clearEntities();
auto entityManager = mWorld->getEntityManager();
// Iterate all entities
for(auto entityRef : mainObject["Entities"].toArray())
{
// Current entity
auto entityObj = entityRef.toObject();
// Create the entity
auto entity = entityManager->createEntity(entityObj["Name"].toString().toStdString());
auto shouldShowInEditor = entityObj["ShowInEditor"].toBool();
if(!shouldShowInEditor)
{
for(auto& info : entityManager->getEntityInfos())
{
if(info.entityId == entity)
{
info.shouldShowInEditor = false;
}
}
}
// Iterate all components
for(auto compRef : entityObj["Components"].toArray())
{
// Current component
auto compObject = compRef.toObject();
// Add the component
auto comp = entityManager->addComponent(entity, static_cast<ComponentType>(compObject["ComponentType"].toInt()));
// Inititalize the component
comp->fromJSON(compObject);
}
}
return true;
}
void Scene::SaveToFile(const std::string& path)
{
QFile file(QString::fromStdString(path));
if(!file.open(QIODevice::WriteOnly))
{
qDebug() << "Save error: Failed to open file at specified path!";
return;
}
filePath = path;
const auto& entityManager = mWorld->getEntityManager();
const auto& entityInfos = entityManager->getEntityInfos();
QJsonObject mainObject;
QJsonArray entityArray;
// Iterate all entities
for(auto& entityInfo : entityInfos)
{
// Get entity
QJsonArray compArray;
for(auto comp : entityManager->getAllComponents(entityInfo.entityId))
{
compArray.push_back(comp->toJSON());
}
QJsonObject entityObject;
entityObject.insert("Components", compArray);
entityObject.insert("Name", QJsonValue(entityInfo.name.c_str()));
entityObject.insert("ShowInEditor", QJsonValue(entityInfo.shouldShowInEditor));
entityArray.push_back(entityObject);
}
mainObject.insert("Entities", entityArray);
QJsonDocument document(mainObject);
file.write(document.toJson());
file.close();
}
// *** Derived scenes *** //
TestScene::TestScene(World* world)
: Scene(world)
{
}
void TestScene::initCustomObjects()
{
auto entityManager = mWorld->getEntityManager();
for(int i = 0; i < 10; ++i)
{
auto entity = entityManager->createEntity();
auto [transform, render] = entityManager->addComponent<TransformComponent, MeshComponent>(entity);
if(auto meshData = ResourceManager::instance().getMesh("suzanne"))
{
render.meshData = *meshData;
render.isVisible = true;
}
transform.setPosition(gsl::vec3(i*2.f, 0, 0));
}
}