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  GetComponent< T >()->gameObject = this;
25  }
26  }
27 
29  template< class T > void RemoveComponent()
30  {
31  for (unsigned i = 0; i < nextFreeComponentIndex; ++i)
32  {
33  if (components[ i ].type == T::Type())
34  {
35  GetComponent< T >()->gameObject = nullptr;
36  components[ i ].handle = 0;
37  components[ i ].type = -1;
38  return;
39  }
40  }
41  }
42 
44  template< class T > T* GetComponent() const
45  {
46  for (const auto& component : components)
47  {
48  if (T::Type() == component.type)
49  {
50  return T::Get( component.handle );
51  }
52  }
53 
54  return nullptr;
55  }
56 
58  GameObject() = default;
59 
61  GameObject( const GameObject& other );
62 
64  GameObject& operator=( const GameObject& go );
65 
67  void SetName( const char* aName ) { name = aName; }
68 
70  void SetEnabled( bool enabled ) { isEnabled = enabled; }
71 
73  bool IsEnabled() const;
74 
76  const std::string& GetName() const { return name; }
77 
79  void SetLayer( unsigned aLayer ) { layer = aLayer; }
80 
82  unsigned GetLayer() const { return layer; }
83 
85  std::string GetSerialized() const;
86 
87  private:
88  struct ComponentEntry
89  {
90  int type = -1;
91  unsigned handle = 0;
92  };
93 
94  unsigned GetNextComponentIndex();
95 
96  static const int MaxComponents = 10;
97  unsigned nextFreeComponentIndex = 0;
98  ComponentEntry components[ MaxComponents ];
99  std::string name;
100  unsigned layer = 1;
101  bool isEnabled = true;
102  };
103 }
104 #endif
const std::string & GetName() const
Definition: GameObject.hpp:76
void SetName(const char *aName)
Definition: GameObject.hpp:67
void RemoveComponent()
Remove a component from the game object.
Definition: GameObject.hpp:29
unsigned GetLayer() const
Definition: GameObject.hpp:82
static const unsigned InvalidComponentIndex
Invalid component index.
Definition: GameObject.hpp:13
Definition: AudioClip.hpp:4
GameObject & operator=(const GameObject &go)
void SetLayer(unsigned aLayer)
Definition: GameObject.hpp:79
void AddComponent()
Adds a component into the game object. There can be multiple components of the same type...
Definition: GameObject.hpp:16
void SetEnabled(bool enabled)
Definition: GameObject.hpp:70
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:44
bool IsEnabled() const