1 #ifndef MSP_GAME_ENTITY_H_
2 #define MSP_GAME_ENTITY_H_
12 struct TransformValues;
14 class hierarchy_error: public std::logic_error
17 hierarchy_error(): std::logic_error("hierarchy error") { }
23 enum TransformTag { NO_TRANSFORM };
26 Handle<Entity> parent;
27 std::vector<Handle<Component>> components;
28 std::vector<Handle<Entity>> children;
29 Owned<Transform> transform;
32 Entity(Handle<Entity>, TransformTag);
33 Entity(Handle<Entity>, const TransformValues &);
36 void add_component(Handle<Component>);
37 void remove_component(Handle<Component>);
40 Handle<T> get_component();
42 Handle<Transform> get_transform() const { return transform; }
44 void add_child(Handle<Entity>);
45 void remove_child(Handle<Entity>);
47 Handle<Entity> get_parent() const { return parent; }
48 Handle<Entity> get_root();
49 const std::vector<Handle<Entity>> &get_children() const { return children; }
55 inline Handle<T> Entity::get_component()
57 for(Handle<Component> c: components)
58 if(Handle<T> tc = dynamic_handle_cast<T>(c))
63 inline Handle<Entity> Entity::get_root()
65 Handle<Entity> e = Handle<Entity>::from_object(this);
66 while(Handle<Entity> p = e->get_parent())
71 } // namespace Msp::Game