The Table Engine
scene.cpp
Go to the documentation of this file.
1 #include <vector>
2 #include <functional>
3 #include <iostream>
4 
5 #include "scene.h"
6 #include "health.h"
7 #include "gameobject.h"
8 
9 // Scene Node
10 
12  m_gameObject = gameObject;
13  if (m_gameObject != nullptr) {
15  }
16  m_parent = nullptr;
17  m_isBackground = false;
18 }
19 
21  // delete all children nodes
22  for (auto child : m_children) {
23  delete child;
24  }
25  // remove this node from parent's children list
26  if (m_parent && !m_parent->m_children.empty()) {
27  for (auto it = m_parent->m_children.begin(); it != m_parent->m_children.end(); ++it) {
28  if (*it == this) {
29  m_parent->m_children.erase(it);
30  break;
31  }
32  }
33  }
34  // delete game object
35  m_parent = nullptr;
36  m_children.clear();
37  delete m_gameObject;
38 }
39 
40 void SceneNode::addChild(GameObject* child, bool isBackground) {
41  if (child == nullptr) return;
42 
43  SceneNode* childNode = new SceneNode(child);
44  childNode->setIsBackground(isBackground);
45  m_children.push_back(childNode);
46  childNode->m_parent = this;
47 }
48 
49 const std::vector<SceneNode*> SceneNode::getChildren() {
50  return m_children;
51 }
52 
54  return m_parent;
55 }
56 
58  return m_gameObject;
59 }
60 
62  return m_destroy;
63 }
64 
65 void SceneNode::setDestroy(bool destroy) {
66  m_destroy = destroy;
67 }
68 
70  return m_isBackground;
71 }
72 
73 void SceneNode::setIsBackground(bool isBackground) {
75 }
76 
77 // Scene Tree
78 
80  m_root = new SceneNode(nullptr);
81 }
82 
84  delete m_root;
85 }
86 
87 void SceneTree::addChild(GameObject* child, bool isBackground) {
88  m_root->addChild(child, isBackground);
89 }
90 
91 void SceneTree::traverseTree(std::function<void(SceneNode*)> callback) {
92  traverseTree(m_root, callback);
93 }
94 
95 void SceneTree::traverseTree(SceneNode* node, std::function<void(SceneNode*)> callback) {
96  if (node == nullptr) return;
97 
98  callback(node);
99  for (auto child : node->getChildren()) {
100  traverseTree(child, callback);
101  }
102 }
103 
105  GameObject* idGameObject = nullptr;
106  traverseTree(m_root, [&idGameObject, id](SceneNode* node) {
107  if (node && node->getGameObject() && node->getGameObject()->getId() == id) {
108  idGameObject = node->getGameObject();
109  }
110  });
111  return idGameObject;
112 }
113 
114 std::vector<GameObject*> SceneTree::findGameObjectsByTag(std::string tag) {
115  if (m_cachedGameObjects.find(tag) != m_cachedGameObjects.end()) {
116  return {m_cachedGameObjects[tag]};
117  }
119  return m_cachedGameObjectsInFrame[tag];
120  }
121 
122  std::vector<GameObject*> taggedGameObjects;
123  traverseTree(m_root, [&taggedGameObjects, tag](SceneNode* node) {
124  if (node && node->getGameObject() && node->getGameObject()->getTag() == tag) {
125  taggedGameObjects.push_back(node->getGameObject());
126  }
127  });
128 
129  // cache specific game objects
130  if (tag == "Player" || tag == "Bow") {
131  m_cachedGameObjects[tag] = taggedGameObjects[0];
132  }
133  else
134  m_cachedGameObjectsInFrame[tag] = taggedGameObjects;
135 
136  return taggedGameObjects;
137 }
138 
139 std::vector<GameObject*>& SceneTree::findCachedGameObjectsInFrame(std::string tag) {
140  return m_cachedGameObjectsInFrame[tag];
141 }
142 
145 }
146 
148  // check if player is dead
149  auto players = SceneTree::findGameObjectsByTag("Player");
150  auto health = players[0]->getComponent<Health>();
151  if (health->getHealth() <= 0) {
152  return -1;
153  }
154  // check if all enemies and keys are collected
155  auto enemies = SceneTree::findGameObjectsByTag("Warrior");
156  auto keys = SceneTree::findGameObjectsByTag("Key");
157  if (enemies.empty() && keys.empty()) {
158  return 1;
159  }
160  // game is still running
161  return 0;
162 }
Core class representing an entity in the game world.
Definition: gameobject.h:20
std::string getTag()
Gets the tag identifier of the GameObject.
Definition: gameobject.cpp:32
void setSceneNode(SceneNode *sceneNode)
Sets the SceneNode for this GameObject.
Definition: gameobject.cpp:40
std::string getId()
Gets the unique identifier of the GameObject.
Definition: gameobject.cpp:28
A component for managing health of game objects.
Definition: health.h:22
Represents a node in the scene graph, encapsulating a game object and its hierarchy.
Definition: scene.h:25
bool m_isBackground
Flag to indicate whether this node is part of the background.
Definition: scene.h:91
void addChild(GameObject *child, bool isBackground=false)
Adds a child game object to this node.
Definition: scene.cpp:40
void setIsBackground(bool isBackground)
Sets whether this node is part of the background.
Definition: scene.cpp:73
~SceneNode()
Destructor, ensures proper cleanup of node resources. This destructor is private to ensure that Scene...
Definition: scene.cpp:20
bool isBackground()
Checks if this node is part of the background.
Definition: scene.cpp:69
GameObject * m_gameObject
Pointer to the GameObject this node represents.
Definition: scene.h:87
SceneNode * m_parent
Pointer to the parent node in the scene graph.
Definition: scene.h:86
void setDestroy(bool destroy)
Sets the destruction state of this node.
Definition: scene.cpp:65
bool m_destroy
Flag to indicate whether this node is scheduled for destruction.
Definition: scene.h:90
bool readyToDestroy()
Checks if this node is marked for destruction.
Definition: scene.cpp:61
std::vector< SceneNode * > m_children
List of child nodes.
Definition: scene.h:88
SceneNode(GameObject *gameObject)
Constructor that assigns a game object to this node.
Definition: scene.cpp:11
const std::vector< SceneNode * > getChildren()
Returns the children of this node.
Definition: scene.cpp:49
SceneNode * getParent()
Gets the parent of this node.
Definition: scene.cpp:53
GameObject * getGameObject()
Gets the GameObject associated with this node.
Definition: scene.cpp:57
std::unordered_map< std::string, std::vector< GameObject * > > m_cachedGameObjectsInFrame
Cache for faster lookup of GameObjects by tag within a frame.
Definition: scene.h:177
std::unordered_map< std::string, GameObject * > m_cachedGameObjects
Cache for faster lookup of GameObjects by ID.
Definition: scene.h:176
GameObject * findGameObjectById(std::string id)
Finds a GameObject by its ID.
Definition: scene.cpp:104
SceneNode * m_root
Root node of the scene tree.
Definition: scene.h:174
SceneTree()
Constructor for creating a root node of the scene tree.
Definition: scene.cpp:79
void addChild(GameObject *child, bool isBackground=false)
Adds a child GameObject to the scene tree.
Definition: scene.cpp:87
void resetCachedGameObjectsInFrame()
Resets the cached game objects for a new frame, clearing the current frame's cache.
Definition: scene.cpp:143
~SceneTree()
Destructor, cleans up the tree resources.
Definition: scene.cpp:83
void traverseTree(std::function< void(SceneNode *)> callback)
Traverses the entire tree, executing a callback on each node.
Definition: scene.cpp:91
std::vector< GameObject * > & findCachedGameObjectsInFrame(std::string tag)
Retrieves the game objects cached by tag.
Definition: scene.cpp:139
int gameStatus()
Retrieves the current game status.
Definition: scene.cpp:147
std::vector< GameObject * > findGameObjectsByTag(std::string tag)
Finds GameObjects by a tag.
Definition: scene.cpp:114
Defines the Health class, a component for managing the health of game objects.
Defines the SceneNode and SceneTree classes for managing game objects within scenes.