1 #ifndef MSP_GAME_ENTITY_H_
2 #define MSP_GAME_ENTITY_H_
5 #include "mspgame_api.h"
13 struct TransformValues;
15 class MSPGAME_API hierarchy_error: public std::logic_error
18 hierarchy_error(): std::logic_error("hierarchy error") { }
21 class MSPGAME_API Entity
24 enum TransformTag { NO_TRANSFORM };
27 Handle<Entity> parent;
28 std::vector<Handle<Component>> components;
29 std::vector<Handle<Entity>> children;
30 Owned<Transform> transform;
33 Entity(Handle<Entity>, TransformTag);
34 Entity(Handle<Entity>, const TransformValues &);
37 void add_component(Handle<Component>);
38 void remove_component(Handle<Component>);
39 const std::vector<Handle<Component>> &get_components() const { return components; }
42 Handle<T> get_component();
44 Handle<Transform> get_transform() const { return transform; }
46 void add_child(Handle<Entity>);
47 void remove_child(Handle<Entity>);
49 Handle<Entity> get_parent() const { return parent; }
50 Handle<Entity> get_root();
51 const std::vector<Handle<Entity>> &get_children() const { return children; }
57 inline Handle<T> Entity::get_component()
59 for(Handle<Component> c: components)
60 if(Handle<T> tc = dynamic_handle_cast<T>(c))
65 inline Handle<Entity> Entity::get_root()
67 Handle<Entity> e = Handle<Entity>::from_object(this);
68 while(Handle<Entity> p = e->get_parent())
73 } // namespace Msp::Game