#define MSP_GAME_ENTITY_H_
#include "handle.h"
+#include "owned.h"
namespace Msp::Game {
class Component;
class Stage;
+class Transform;
+struct TransformValues;
class hierarchy_error: public std::logic_error
{
class Entity
{
+public:
+ enum TransformTag { NO_TRANSFORM };
+
private:
Handle<Entity> parent;
std::vector<Handle<Component>> components;
std::vector<Handle<Entity>> children;
+ Owned<Transform> transform;
public:
- Entity(Handle<Entity>);
+ Entity(Handle<Entity>, TransformTag);
+ Entity(Handle<Entity>, const TransformValues &);
virtual ~Entity();
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>);
Handle<Entity> get_parent() const { return parent; }
Handle<Entity> get_root();
+ const std::vector<Handle<Entity>> &get_children() const { return children; }
Stage &get_stage();
};
+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);