Haiku API: ECSΒΆ

Haiku-hpp provides a simple wrapper over the ENTT library to propose a simple Entity-Component-System.

  • Data structures

ECS::Entity entity; /* Entity & Component handler */
ECS::World  world;  /* Entity Manager Class */
ECS::ISystem; /* System Interface */
/* Components are just POD/Standard layout types */
  • Simple Components example

struct PositionComponent
{
    Haiku::Maths::Vec3f pos;
    PositionComponent(float x, float y, float z) :pos(x,y,z) {;}
    PositionComponent(const PositionComponent & pc) :pos(pc.pos) {;}
    operator Haiku::Maths::Vec3f  () const {return pos;}
    operator Haiku::Maths::Vec3f& ()       {return pos;}
};
  • Entity Creation

ECS::Entity entity = m_world->createEntity();
  • Set Component to entity

entity.addComponent<PositionComponent>(pos.x,pos.y,pos.z);
entity.addComponent<ColorComponent>(col.r,col.g,col.b);
entity.addComponent<RotationSpeedComponent>( pick_random_float() * 0.01f );
  • Iterate over all entities that shares a component list

/* this == System* */
auto view = this->getView<PositionComponent>();
for(auto handle : view)
{
    ECS::Entity entity = this->getEntity(handle);
    Vec3f pos = entity.getComponent<PositionComponent>();
    Vec3f col = entity.getComponent<ColorComponent>();
    /* ... Do stuff ... */
}