The Table Engine
scene.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <vector>
5 #include <string>
6 #include <unordered_map>
7 class GameObject;
8 
24 class SceneNode
25 {
26  friend class SceneTree;
27  friend class SceneManager;
28 
29 public:
34  SceneNode(GameObject* gameObject);
35 
41  void addChild(GameObject* child, bool isBackground = false);
42 
47  const std::vector<SceneNode*> getChildren();
48 
54 
60 
65  bool readyToDestroy();
66 
71  void setDestroy(bool destroy);
72 
77  bool isBackground();
78 
83  void setIsBackground(bool isBackground);
84 
85 private:
88  std::vector<SceneNode*> m_children;
89 
90  bool m_destroy = false;
91  bool m_isBackground = false;
92 
98  ~SceneNode();
99 };
100 
109 {
110 public:
114  SceneTree();
115 
119  ~SceneTree();
120 
126  void addChild(GameObject* child, bool isBackground = false);
127 
132  void traverseTree(std::function<void(SceneNode*)> callback);
133 
139  void traverseTree(SceneNode* node, std::function<void(SceneNode*)> callback);
140 
146  GameObject* findGameObjectById(std::string id);
147 
153  std::vector<GameObject*> findGameObjectsByTag(std::string tag);
154 
160  std::vector<GameObject*>& findCachedGameObjectsInFrame(std::string tag);
161 
166 
171  int gameStatus();
172 
173 private:
175 
176  std::unordered_map<std::string, GameObject*> m_cachedGameObjects;
177  std::unordered_map<std::string, std::vector<GameObject*>> m_cachedGameObjectsInFrame;
178 };
Core class representing an entity in the game world.
Definition: gameobject.h:20
Singleton class that manages scenes and their transitions.
Definition: scenemanager.h:27
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
Manages a tree structure of SceneNodes, facilitating scene graph operations.
Definition: scene.h:109
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