Skip to content

Commit c76cc5b

Browse files
committed
Scene GameObject management
1 parent 7c0de8a commit c76cc5b

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/private/Core/Scene/Scene.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,55 @@
33
Scene::Scene(std::string name) {
44
this->name = name;
55
this->m_go = new GameObject("SampleObject");
6-
this->m_gameObjects.push_back(this->m_go);
6+
this->AddGameObject(m_go);
7+
8+
this->m_editorCamera = new Camera("EditorCamera");
9+
this->AddGameObject(this->m_editorCamera);
710
}
811

912
void Scene::Init() {
10-
for (GameObject* object : this->m_gameObjects) {
11-
object->Init();
13+
for (std::pair<std::string, GameObject*> object : this->m_gameObjects) {
14+
object.second->Init();
1215
}
1316
}
1417

1518
void Scene::Update() {
16-
for (GameObject* object : this->m_gameObjects) {
17-
object->Update();
19+
for (std::pair<std::string, GameObject*> object : this->m_gameObjects) {
20+
object.second->Update();
1821
}
1922
}
2023

2124
void Scene::Render() {
22-
for (GameObject* object : this->m_gameObjects) {
23-
object->Render();
25+
for (std::pair<std::string, GameObject*> object : this->m_gameObjects) {
26+
object.second->Render();
2427
}
2528
}
2629

30+
bool Scene::ObjectExists(std::string name) {
31+
if (this->m_gameObjects.size() > 0) {
32+
if (this->m_gameObjects.count(name) > 0)
33+
return true;
34+
else
35+
spdlog::error("Scene#{0}: GameObject {1} not found in the scene.", this->name, name);
36+
}
37+
else {
38+
spdlog::error("Scene#{0}: No GameObjects added on the Scene.", this->name);
39+
}
40+
41+
return false;
42+
}
43+
2744
void Scene::AddGameObject(GameObject* object) {
28-
this->m_gameObjects.push_back(object);
45+
if (!object)
46+
return;
47+
48+
if (this->ObjectExists(object->m_name)) {
49+
spdlog::error("Scene#{0}: A GameObject with the name {1} already exists in the scene.", this->name, object->m_name);
50+
return;
51+
}
52+
53+
if (Camera* cam = dynamic_cast<Camera*>(object))
54+
this->m_cameras[object->m_name] = cam;
55+
56+
this->m_gameObjects[object->m_name] = object;
2957
}

src/public/Core/GameObject/GameObject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "Core/Input/Input.h"
1010

1111
class GameObject {
12+
friend class Scene;
1213
private:
1314
std::string m_name;
1415

src/public/Core/Scene/Scene.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22
#include <iostream>
33
#include <vector>
44
#include <map>
5+
#include <spdlog/spdlog.h>
56

67
#include "Core/GameObject/GameObject.h"
8+
#include "Core/GameObject/Camera/Camera.h"
79

810
class Scene {
911
private:
10-
std::vector<GameObject*> m_gameObjects;
1112
GameObject* m_go;
13+
14+
std::map<std::string, GameObject*> m_gameObjects;
15+
std::map<std::string, Camera*> m_cameras;
16+
Camera* m_currentCamera;
17+
18+
Camera* m_editorCamera;
1219
public:
1320
std::string name;
1421

@@ -20,4 +27,5 @@ class Scene {
2027
void Render();
2128

2229
void AddGameObject(GameObject* object);
30+
bool ObjectExists(std::string name);
2331
};

0 commit comments

Comments
 (0)