Aether3D Game Engine
GameObject.hpp
1 #ifndef GAME_OBJECT_H
2 #define GAME_OBJECT_H
3 
4 #include <string>
5 
6 namespace ae3d
7 {
9  class GameObject
10  {
11  public:
13  static const unsigned InvalidComponentIndex = 99999999;
14 
16  template< class T > void AddComponent()
17  {
18  const unsigned index = GetNextComponentIndex();
19 
20  if (index != InvalidComponentIndex)
21  {
22  components[ index ].handle = T::New();
23  components[ index ].type = T::Type();
24  }
25  }
26 
28  template< class T > void RemoveComponent()
29  {
30  for (unsigned i = 0; i < nextFreeComponentIndex; ++i)
31  {
32  if (components[ i ].type == T::Type())
33  {
34  components[ i ].handle = 0;
35  components[ i ].type = -1;
36  return;
37  }
38  }
39  }
40 
42  template< class T > T* GetComponent() const
43  {
44  for (const auto& component : components)
45  {
46  if (T::Type() == component.type)
47  {
48  return T::Get( component.handle );
49  }
50  }
51 
52  return nullptr;
53  }
54 
56  GameObject() = default;
57 
59  GameObject( const GameObject& other );
60 
62  GameObject& operator=( const GameObject& go );
63 
65  void SetName( const char* aName ) { name = aName; }
66 
68  const std::string& GetName() const { return name; }
69 
71  std::string GetSerialized() const;
72 
73  private:
74  struct ComponentEntry
75  {
76  int type = -1;
77  unsigned handle = 0;
78  };
79 
80  unsigned GetNextComponentIndex();
81 
82  static const int MaxComponents = 10;
83  unsigned nextFreeComponentIndex = 0;
84  ComponentEntry components[ MaxComponents ];
85  std::string name;
86  };
87 }
88 #endif
const std::string & GetName() const
Definition: GameObject.hpp:68
void SetName(const char *aName)
Definition: GameObject.hpp:65
void RemoveComponent()
Remove a component from the game object.
Definition: GameObject.hpp:28
static const unsigned InvalidComponentIndex
Invalid component index.
Definition: GameObject.hpp:13
Definition: AudioClip.hpp:4
GameObject & operator=(const GameObject &go)
void AddComponent()
Adds a component into the game object. There can be multiple components of the same type...
Definition: GameObject.hpp:16
GameObject()=default
Constructor.
std::string GetSerialized() const
GameObject is composed of components that define its behavior.
Definition: GameObject.hpp:9
T * GetComponent() const
Definition: GameObject.hpp:42