]> git.tdb.fi Git - libs/game.git/blob - source/game/entity.h
c28cbe5075b94567709ba562f33698097e959e29
[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 #include "owned.h"
6
7 namespace Msp::Game {
8
9 class Component;
10 class Stage;
11 class Transform;
12 struct TransformValues;
13
14 class hierarchy_error: public std::logic_error
15 {
16 public:
17         hierarchy_error(): std::logic_error("hierarchy error") { }
18 };
19
20 class Entity
21 {
22 public:
23         enum TransformTag { NO_TRANSFORM };
24
25 private:
26         Handle<Entity> parent;
27         std::vector<Handle<Component>> components;
28         std::vector<Handle<Entity>> children;
29         Owned<Transform> transform;
30
31 public:
32         Entity(Handle<Entity>, TransformTag);
33         Entity(Handle<Entity>, const TransformValues &);
34         virtual ~Entity();
35
36         void add_component(Handle<Component>);
37         void remove_component(Handle<Component>);
38
39         void add_child(Handle<Entity>);
40         void remove_child(Handle<Entity>);
41
42         Handle<Entity> get_parent() const { return parent; }
43         Handle<Entity> get_root();
44         const std::vector<Handle<Entity>> &get_children() const { return children; }
45         Stage &get_stage();
46         Handle<Transform> get_transform() const { return transform; }
47 };
48
49
50 inline Handle<Entity> Entity::get_root()
51 {
52         Handle<Entity> e = Handle<Entity>::from_object(this);
53         while(Handle<Entity> p = e->get_parent())
54                 e = p;
55         return e;
56 }
57
58 } // namespace Msp::Game
59
60 #endif