Entity stone = theEntityManager.createEntity();
stone.setComponent(NameComponent("Stone"));
stone.setComponent(PositionComponent(0, 0));
Entity hedgehog = theEntityManager.createEntity();
hedgehog.setComponent(NameComponent("Hedgehog"));
hedgehog.setComponent(PositionComponent(0, 0));
hedgehog.setComponent(VelocityComponent(1, 1));
Entity hare = theEntityManager.createEntity();
hare.setComponent(NameComponent("Hare"));
hare.setComponent(PositionComponent(0, 0));
hare.setComponent(VelocityComponent(2, 2));
MovementSystem moveSys;
RenderSystem rendSys;
std::cout << "The race begins:" << std::endl;
for (int i = 0; i < 3; ++i)
{
// tell the movement system in every step the required time delta
// to simplify the tutorial, a constant time delta of 1 s is given
moveSys.setTimeDelta(1);
moveSys.run();
rendSys.run();
std::cout << std::endl;
}
std::cout << "the hedgehog stops moving" << std::endl;
// any component can be removed (and again be set) during runtime
// you can see in the output, that the hedgehog no longer gets updated
// by the movement system but still gets rendered by the render system
hedgehog.removeComponent(VelocityComponent::getType());
std::cout << "Hedgehogs wife appears and awaits the hare" << std::endl;
// an entity can be created in any scope
int wifeId;
{
Entity hedgehogsWife = theEntityManager.createEntity();
hedgehogsWife.setComponent(NameComponent("Hedegehogs Wife"));
wifeId = hedgehogsWife.getId();
}
// any entity can be accessed by its id
Entity hedgehogsWife = theEntityManager.getEntity(wifeId);
hedgehogsWife.setComponent(PositionComponent(12, 12));
std::cout << "While nobody was watching, someone has stolen the stone";
std::cout << std::endl << std::endl;
// to destroy an entity call Entity::destroy or use the entity manager
theEntityManager.destroyEntity(stone);
// stone.destroy() does the same
for (int i = 0; i < 3; ++i)
{
moveSys.run();
rendSys.run();
std::cout << std::endl;
}
std::cout << "Hedgehogs wife says hello" << std::endl;
std::cout << "The hare gets angry and runs away";
std::cout << std::endl << std::endl;
// call Entity::hasComponent to check if an entity has a component
if (hare.hasComponent(NameComponent::getType()))
{
// any component (iff set) can be accessed and modified this way
hare.getComponent(NameComponent::getType()).name = "Angry Hare";
}
// to modify a component you can also use Entity::setComponent
// an existent component simply gets overwritten (slower than above)
hare.setComponent(VelocityComponent(1, 3));
// to remove all components of an entity, you can call:
theEntityManager.clearEntity(hedgehog);
// or simply
hedgehogsWife.clear();
for (int i = 1; i < 4; ++i)
{
moveSys.setTimeDelta(i);
moveSys.run();
rendSys.run();
}
std::cout << std::endl << "The End" << std::endl;