Entities - simple-entertainment/simplicity GitHub Wiki

Entities are the objects that a scene is comprised of. Being a component-based engine, entities in simplicity are nothing more than containers of components. Here is a good article on component-based architecture. To create an entity do this:

std::unique_ptr<Entity> entity(new Entity);

You probably don't want it at the origin so:

setPosition(entity->getTransform(), Vector3(1.0f, 1.0f, 1.0f)); // Position at (1,1,1)
rotate(entity->getTransform(), MathConstants::PI * 0.5f, Vector3(0.0f, 1.0f, 0.0f)); // Rotate about the Y axis by 90 degrees

That won't do much good though as an entity without components doesn't do anything...

Components

Components could be anything! Usually though, they are designed to work with a particular engine or type of engine.

The standard components included in simplicity are:

  • Bodies (for use with physics engines)
  • Models (for use with rendering engines and useful in other areas as well)
  • Scripts (for use with scripting engines)

To add a component to an entity do this:

std::unique_ptr<Model> model(ModelFactory::getInstance()->createCubeMesh(...));
entity->addUniqueComponent(move(model));

Components can be unique or shared. As the name suggests, shared components can be shared amongst multiple entities. This helps to save resources. If we wanted to use that cube in lots of entities we could do something like this:

std::unique_ptr<Model> model(ModelFactory::getInstance()->createCubeMesh(...));
std::shared_ptr<Model> sharedModel = move(model);
entity->addSharedComponent(sharedModel);

Now that we have an entity that will actually do something (in this case, be drawn) we can add it to the scene!

Simplicity::getScene()->addEntity(move(entity));

To create your own component type just extend Component so that it can be added to an entity. That's it! Well... to be fair you will probably want to create an engine that does something with your component otherwise it will just sit there and do nothing.

Categories

Both entities and components can be categorized for easy identification and searching:

std::unique_ptr<Entity> entity(new Entity(MyCategories::BAD_GUY));

std::unique_ptr<Component> component = // Some component...
component->setCategory(MyCategories::SPECIAL);

When creating your own categories, you should begin with the value Categories::USER_ID_0 to avoid conflicts with built-in categories:

namespace MyCategories
{
    const unsigned short BAD_GUY = Category::USER_ID_0;
    const unsigned short SPECIAL = Category::USER_ID_0 + 1;
}
⚠️ **GitHub.com Fallback** ⚠️