Entity Systems

Game entity

Game entity

Game entity

OOP entities

OOP entities - advantages

OOP entities - the Blob

OOP entities - heavy leaves

OOP entities - example

OOP entities - example

OOP entities - example

OOP entities - real example

OOP entities - real example

Component entities

Component entities cont.

Component entities - component examples

Component entities - entity example

visual: visual/harpooner.yaml
engine:
  maxSpeed: 250
  acceleration: 2500
volume:
  aabbox:
    min: [-25, 0]
    max: [25, 30]
weapon:
  0: weapons/harpoonLauncher.yaml
collidable:
health: 55
warhead:
  damage: 45
  killsEntity: false
dumbScript: dumbscripts/arrows.yaml
score:
  exp: 120

Component entities - organized blob

Component entities - organized blob

class Entity
{
    // These may be null
    PhysicsComponent* physics;
    GraphicsComponent* graphics;
    AIComponent* ai;
    CollisionVolumeComponent* volume;

    ...
    Lots of pointers (~10 - ~300)
    ...
};

Component entities - container of components

Component entities - entity-component system (ECS)

Execution in a naive ECS (OOP components)

foreach(componentType)
{
    foreach(entity)
    {
        getComponent(componentType, entity).update();
    }
}

OOP components - issues

RDBMS style ECS

RDBMS style ECS

RDBMS style ECS: System

RDBMS style ECS: System - example

class PhysicsSystem : System
{
    private:
        const GameTime gameTime_;

    public:
        void process(ref PositionComponent pos, ref VelocityComponent vel)
        {
            pos.position += gameTime_.timeStep * vel.velocity;
        }
}

RDBMS style ECS: System - execution

foreach(system; systems)
{
    foreach(entity; entities)
    {
        if(entity.hasComponents(system.processedComponents()))
        {
            system.process(entity.getComponents(system.processedComponents()));
        }
    }
}

RDBMS style ECS: advantages

RDBMS style ECS: implementation example

RDBMS style ECS: implementation example

RDBMS style ECS: implementation example

uint[] entityIDs;

ulong[] componentFlags;

RDBMS style ECS: implementation example

// One array for every component type (use templated code or pointers/casting
// to support any number of component types)

Position[] positionComponents;
Graphics[] graphicsComponents;
Volume[]   volumeComponents;

RDBMS style ECS: implementation example

foreach(system; systems)
{
    ulong flags = system.componentFlags();
    uint[flags.length] componentIndices;
    componentIndices[] = 0;

    for(uint entityIndex = 0; entityIndex < entityIDs.length; entityIndex++)
    {
        incrementComponentIndices(componentFlags[entityIndex], componentIndices);
        if(componentFlags[entityIndex] && flags)
        {
            system.process(getComponents(componentIndices));
        }
        // increment component indices corresponding to flags
    }
}

RDBMS style ECS: implementation example

The machine matters

ECS: projects

Sources

Thank you