]> git.tdb.fi Git - libs/game.git/blob - source/game/entity.h
922f552f4aaac7f642f0cfeda14de22009614da4
[libs/game.git] / source / game / entity.h
1 #ifndef MSP_GAME_ENTITY_H_
2 #define MSP_GAME_ENTITY_H_
3
4 #include "handle.h"
5
6 namespace Msp::Game {
7
8 class Component;
9
10 class hierarchy_error: public std::logic_error
11 {
12 public:
13         hierarchy_error(): std::logic_error("hierarchy error") { }
14 };
15
16 class Entity
17 {
18 private:
19         Handle<Entity> parent;
20         std::vector<Handle<Component>> components;
21         std::vector<Handle<Entity>> children;
22
23 public:
24         Entity(Handle<Entity>);
25         virtual ~Entity();
26
27         void add_component(Handle<Component>);
28         void remove_component(Handle<Component>);
29
30         void add_child(Handle<Entity>);
31         void remove_child(Handle<Entity>);
32
33         Handle<Entity> get_parent() const { return parent; }
34         Handle<Entity> get_root();
35 };
36
37
38 inline Handle<Entity> Entity::get_root()
39 {
40         Handle<Entity> e = Handle<Entity>::from_object(this);
41         while(Handle<Entity> p = e->get_parent())
42                 e = p;
43         return e;
44 }
45
46 } // namespace Msp::Game
47
48 #endif