1#include "scene/Scene.h"
2#include "scene/components/AnimationComponent.h"
3#include "scene/components/CameraComponent.h"
4#include "scene/components/LightComponent.h"
5#include "scene/components/MeshComponent.h"
6#include "scene/components/PhysicsComponent.h"
7#include "scene/components/PlayerControllerComponent.h"
8#include "scene/components/AudioComponent.h"
9#include "scene/components/AudioListenerComponent.h"
19 AnimationComponent::Register();
20 CameraComponent::Register();
21 LightComponent::Register();
22 MeshComponent::Register();
23 PhysicsComponent::Register();
24 PlayerControllerComponent::Register();
25 AudioComponent::Register();
26 AudioListenerComponent::Register();
27 SpriteComponent::Register();
33 std::remove_if(m_objects.begin(), m_objects.end(),
34 [](
const std::unique_ptr<GameObject>& obj) { return !obj->IsAlive(); }),
38 for (
auto& obj : m_objectsToAdd)
42 m_objectsToAdd.clear();
45 for (
auto it = m_objects.begin(); it != m_objects.end();)
49 (*it)->Update(deltaTime);
54 it = m_objects.erase(it);
72 m_objectsToAdd.push_back({ obj, parent });
90 m_objectsToAdd.push_back({ obj, parent });
105 if (parent ==
nullptr)
107 if (currentParent !=
nullptr)
109 auto it = std::find_if(
110 currentParent->m_children.begin(),
111 currentParent->m_children.end(),
112 [obj](
const std::unique_ptr<GameObject>& el) {
113 return el.get() == obj;
117 if (it != currentParent->m_children.end())
119 m_objects.push_back(std::move(*it));
130 auto it = std::find_if(
133 [obj](
const std::unique_ptr<GameObject>& el) {
134 return el.get() == obj;
138 if (it == m_objects.end())
140 std::unique_ptr<GameObject> objHolder(obj);
141 m_objects.push_back(std::move(objHolder));
149 if (currentParent !=
nullptr)
151 auto it = std::find_if(
152 currentParent->m_children.begin(),
153 currentParent->m_children.end(),
154 [obj](
const std::unique_ptr<GameObject>& el) {
155 return el.get() == obj;
159 if (it != currentParent->m_children.end())
162 auto currentElement = parent;
163 while (currentElement)
165 if (currentElement == obj)
170 currentElement = currentElement->
GetParent();
187 auto it = std::find_if(
190 [obj](
const std::unique_ptr<GameObject>& el) {
191 return el.get() == obj;
196 if (it == m_objects.end())
198 std::unique_ptr<GameObject> objHolder(obj);
199 parent->
m_children.push_back(std::move(objHolder));
206 auto currentElement = parent;
207 while (currentElement)
209 if (currentElement == obj)
214 currentElement = currentElement->
GetParent();
233 m_mainCamera = camera;
243 std::vector<LightData> lights;
244 for (
auto& obj : m_objects)
246 CollectLightsRecursive(obj.get(), lights);
254 if (contents.empty())
259 auto json = nlohmann::json::parse(contents);
265 auto result = std::make_shared<Scene>();
267 const std::string sceneName = json.value(
"name",
"noname");
269 if (json.contains(
"objects") && json[
"objects"].is_array())
271 const auto& objects = json[
"objects"];
273 for (
const auto& obj : objects)
275 result->LoadObject(obj,
nullptr);
279 if (json.contains(
"camera"))
281 std::string cameraObjName = json.value(
"camera",
"");
282 for (
const auto& child : result->m_objects)
284 if (
auto object = child->FindChildByName(cameraObjName))
286 result->SetMainCamera(
object);
295 void Scene::CollectLightsRecursive(
GameObject* obj, std::vector<LightData>& out)
300 data.
color = light->GetColor();
305 for (
auto& child : obj->m_children)
307 CollectLightsRecursive(child.get(), out);
311 void Scene::LoadObject(
const nlohmann::json& jsonObject, GameObject* parent)
313 const std::string name = jsonObject.value(
"name",
"Object");
315 GameObject* gameObject =
nullptr;
317 if (jsonObject.contains(
"type"))
319 const std::string type = jsonObject.value(
"type",
"");
322 std::string path = jsonObject.value(
"path",
"");
326 gameObject->SetParent(parent);
327 gameObject->SetName(name);
346 if (jsonObject.contains(
"position"))
348 auto posObj = jsonObject[
"position"];
350 pos.x = posObj.value(
"x", 0.0f);
351 pos.y = posObj.value(
"y", 0.0f);
352 pos.z = posObj.value(
"z", 0.0f);
353 gameObject->SetPosition(pos);
356 if (jsonObject.contains(
"rotation"))
358 auto rotObj = jsonObject[
"rotation"];
360 rot.x = rotObj.value(
"x", 0.0f);
361 rot.y = rotObj.value(
"y", 0.0f);
362 rot.z = rotObj.value(
"z", 0.0f);
363 rot.w = rotObj.value(
"w", 1.0f);
364 gameObject->SetRotation(rot);
367 if (jsonObject.contains(
"scale"))
369 auto scaleObj = jsonObject[
"scale"];
371 scale.x = scaleObj.value(
"x", 1.0f);
372 scale.y = scaleObj.value(
"y", 1.0f);
373 scale.z = scaleObj.value(
"z", 1.0f);
374 gameObject->SetScale(scale);
377 gameObject->LoadProperties(jsonObject);
379 if (jsonObject.contains(
"components") && jsonObject[
"components"].is_array())
381 const auto& components = jsonObject[
"components"];
382 for (
const auto& comp : components)
384 const std::string type = comp.value(
"type",
"");
389 gameObject->AddComponent(component);
394 if (jsonObject.contains(
"children") && jsonObject[
"children"].is_array())
396 const auto& children = jsonObject[
"children"];
397 for (
const auto& child : children)
399 LoadObject(child, gameObject);
static ComponentFactory & GetInstance()
Component * CreateComponent(const std::string &name)
virtual void LoadProperties(const nlohmann::json &json)
FileSystem & GetFileSystem()
static Engine & GetInstance()
std::string LoadAssetFileText(const std::string &relativePath)
static GameObjectFactory & GetInstance()
GameObject * CreateGameObject(const std::string &typeName)
static GameObject * LoadGLTF(const std::string &path, Scene *scene)
glm::vec3 GetWorldPosition() const
void SetName(const std::string &name)
std::vector< std::unique_ptr< GameObject > > m_children
std::vector< LightData > CollectLights()
void SetMainCamera(GameObject *camera)
static std::shared_ptr< Scene > Load(const std::string &path)
void Update(float deltaTime)
GameObject * CreateObject(const std::string &name, GameObject *parent=nullptr)
bool SetParent(GameObject *obj, GameObject *parent)
GameObject * GetMainCamera()
static void RegisterTypes()