33Scene::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
912void 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
1518void 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
2124void 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+
2744void 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}
0 commit comments