]> git.tdb.fi Git - libs/game.git/blobdiff - source/game/entity.h
Decorate things which constitute the public API of the library
[libs/game.git] / source / game / entity.h
index c28cbe5075b94567709ba562f33698097e959e29..e6d1cf940cd8da44783976a29fd1ce407cf01770 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GAME_ENTITY_H_
 
 #include "handle.h"
+#include "mspgame_api.h"
 #include "owned.h"
 
 namespace Msp::Game {
@@ -11,13 +12,13 @@ class Stage;
 class Transform;
 struct TransformValues;
 
-class hierarchy_error: public std::logic_error
+class MSPGAME_API hierarchy_error: public std::logic_error
 {
 public:
        hierarchy_error(): std::logic_error("hierarchy error") { }
 };
 
-class Entity
+class MSPGAME_API Entity
 {
 public:
        enum TransformTag { NO_TRANSFORM };
@@ -35,6 +36,12 @@ public:
 
        void add_component(Handle<Component>);
        void remove_component(Handle<Component>);
+       const std::vector<Handle<Component>> &get_components() const { return components; }
+
+       template<typename T>
+       Handle<T> get_component();
+
+       Handle<Transform> get_transform() const { return transform; }
 
        void add_child(Handle<Entity>);
        void remove_child(Handle<Entity>);
@@ -43,10 +50,18 @@ public:
        Handle<Entity> get_root();
        const std::vector<Handle<Entity>> &get_children() const { return children; }
        Stage &get_stage();
-       Handle<Transform> get_transform() const { return transform; }
 };
 
 
+template<typename T>
+inline Handle<T> Entity::get_component()
+{
+       for(Handle<Component> c: components)
+               if(Handle<T> tc = dynamic_handle_cast<T>(c))
+                       return tc;
+       return nullptr;
+}
+
 inline Handle<Entity> Entity::get_root()
 {
        Handle<Entity> e = Handle<Entity>::from_object(this);