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>);
38 const std::vector<Handle<Component>> &get_components() const { return components; }
41 Handle<T> get_component();
43 Handle<Transform> get_transform() const { return transform; }
45 void add_child(Handle<Entity>);
46 void remove_child(Handle<Entity>);
48 Handle<Entity> get_parent() const { return parent; }
49 Handle<Entity> get_root();
50 const std::vector<Handle<Entity>> &get_children() const { return children; }
56 inline Handle<T> Entity::get_component()
58 for(Handle<Component> c: components)
59 if(Handle<T> tc = dynamic_handle_cast<T>(c))
64 inline Handle<Entity> Entity::get_root()
66 Handle<Entity> e = Handle<Entity>::from_object(this);
67 while(Handle<Entity> p = e->get_parent())
72 } // namespace Msp::Game