]> git.tdb.fi Git - libs/game.git/blob - source/game/entity.h
Adjust some things to make header dependencies easier to manage
[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 class Stage;
10
11 class hierarchy_error: public std::logic_error
12 {
13 public:
14         hierarchy_error(): std::logic_error("hierarchy error") { }
15 };
16
17 class Entity
18 {
19 private:
20         Handle<Entity> parent;
21         std::vector<Handle<Component>> components;
22         std::vector<Handle<Entity>> children;
23
24 public:
25         Entity(Handle<Entity>);
26         virtual ~Entity();
27
28         void add_component(Handle<Component>);
29         void remove_component(Handle<Component>);
30
31         void add_child(Handle<Entity>);
32         void remove_child(Handle<Entity>);
33
34         Handle<Entity> get_parent() const { return parent; }
35         Handle<Entity> get_root();
36         Stage &get_stage();
37 };
38
39
40 inline Handle<Entity> Entity::get_root()
41 {
42         Handle<Entity> e = Handle<Entity>::from_object(this);
43         while(Handle<Entity> p = e->get_parent())
44                 e = p;
45         return e;
46 }
47
48 } // namespace Msp::Game
49
50 #endif