The Table Engine
gameobject.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <atomic>
4 #include <vector>
5 
6 #include "component.h"
7 #include "script.h"
8 #include "scene.h"
9 
10 #include "SDL2/SDL.h"
11 
20 class GameObject {
21  friend class SceneNode;
22 
23  private:
24  std::string m_id;
25  std::string m_tag;
26 
27  std::vector<Component*> m_components;
28  std::vector<Script*> m_scripts;
29 
31 
35  ~GameObject();
36 
37  public:
42  GameObject(std::string tag);
43 
48  std::string getId();
49 
54  std::string getTag();
55 
61  template <typename T>
62  T* getComponent();
63 
69  template <typename T>
70  void addComponent(T* component);
71 
77  template <typename T>
78  T* getScript();
79 
85  template <typename T>
86  void addScript(T* script);
87 
93 
98  void setSceneNode(SceneNode* sceneNode);
99 
104  std::vector<GameObject*> getChildren();
105 
109  void input();
110 
114  void update();
115 
119  void render();
120 };
121 
122 template <typename T>
124  for (auto component : m_components) {
125  if (dynamic_cast<T*>(component) != nullptr) {
126  return static_cast<T*>(component);
127  }
128  }
129  return nullptr;
130 }
131 
132 template <typename T>
133 void GameObject::addComponent(T* component) {
134  auto base_component = static_cast<Component*>(component);
135  GameObject::m_components.push_back(base_component);
136  component->setOwner(this);
137 }
138 
139 template <typename T>
141  for (auto script : m_scripts) {
142  if (dynamic_cast<T*>(script) != nullptr) {
143  return static_cast<T*>(script);
144  }
145  }
146  return nullptr;
147 }
148 
149 template <typename T>
150 void GameObject::addScript(T* script) {
151  auto base_script = static_cast<Script*>(script);
152  m_scripts.push_back(base_script);
153  base_script->setOwner(this);
154 }
Base class for all game components, providing basic component functionalities.
Definition: component.h:25
Core class representing an entity in the game world.
Definition: gameobject.h:20
std::vector< Component * > m_components
List of attached components.
Definition: gameobject.h:27
std::string m_id
Unique identifier for the GameObject.
Definition: gameobject.h:24
std::vector< Script * > m_scripts
List of attached scripts.
Definition: gameobject.h:28
void render()
Renders this GameObject and its components.
Definition: gameobject.cpp:75
T * getScript()
Gets a script of the specified type.
Definition: gameobject.h:140
SceneNode * getSceneNode()
Gets the SceneNode associated with this GameObject.
Definition: gameobject.cpp:36
std::string getTag()
Gets the tag identifier of the GameObject.
Definition: gameobject.cpp:32
T * getComponent()
Gets a component of the specified type.
Definition: gameobject.h:123
void input()
Processes input for this GameObject and its components.
Definition: gameobject.cpp:57
std::vector< GameObject * > getChildren()
Gets all child GameObjects.
Definition: gameobject.cpp:44
GameObject(std::string tag)
Constructs a new GameObject with the specified tag.
Definition: gameobject.cpp:7
~GameObject()
Destructor that cleans up the GameObject and its SceneNode.
Definition: gameobject.cpp:14
std::string m_tag
Category identifier (e.g., "Enemy", "Projectile")
Definition: gameobject.h:25
void setSceneNode(SceneNode *sceneNode)
Sets the SceneNode for this GameObject.
Definition: gameobject.cpp:40
void addComponent(T *component)
Adds a component to the GameObject.
Definition: gameobject.h:133
SceneNode * m_sceneNode
Node in the scene graph hierarchy.
Definition: gameobject.h:30
std::string getId()
Gets the unique identifier of the GameObject.
Definition: gameobject.cpp:28
void update()
Updates this GameObject and its components.
Definition: gameobject.cpp:66
void addScript(T *script)
Adds a script to the GameObject.
Definition: gameobject.h:150
Represents a node in the scene graph, encapsulating a game object and its hierarchy.
Definition: scene.h:25
Base class for scripting custom behaviors in game objects.
Definition: script.h:22
Defines the Component base class for all components in the game engine.
Defines the SceneNode and SceneTree classes for managing game objects within scenes.
Defines the Script class for attaching behavior to game objects.