Skip to content

The Basics

Simon Schmid edited this page Apr 25, 2016 · 9 revisions

Group

Groups are always up-to-date and contain all entities matching the specified matcher.

var movables = pool.GetGroup(Matcher.Movable);
var count = movables.count; // count is 0, the group is empty

var entity1 = pool.CreateEntity().IsMovable(true);
var entity2 = pool.CreateEntity().IsMovable(true);

count = movables.count; // count is 2, the group contains the entity1 and entity2

var movableEntities = movables.GetEntities();
foreach (var e in movableEntities) {
    // Do sth
}

pool.DestroyEntity(entity1);
pool.DestroyEntity(entity2);

count = movables.count; // count is 0, the group is empty

Matcher

Matchers are generated by the code generator and can be combined. Matchers are usually used to get groups of entities from the pool.

var matcher = Matcher.Movable;

Matcher.AllOf(Matcher.Movable, Matcher.Position);

Matcher.AnyOf(Matcher.Move, Matcher.Position);

Matcher
    .AllOf(Matcher.Position)
    .AnyOf(Matcher.Health, Matcher.Interactive)
    .NoneOf(Matcher.Animating);

Systems

There are 3 different types of Systems:

  • IInitializeSystem: Executes once (system.Initialize())
  • IExecuteSystem: Executes every frame (system.Execute())
  • IReactiveSystem: Executes when the observed group changed (system.Execute(Entity[]))
public class MoveSystem : IExecuteSystem {
    public void Execute() {
        // Do sth
    }
}

public class CreateLevelSystem : IInitializeSystem {
    public void Initialize() {
        // Do sth
    }
}

public class RenderPositionSystem : IReactiveSystem {
    public TriggerOnEvent trigger {
        get { return Matcher.AllOf(Matcher.Position, Matcher.View).OnEntityAdded(); }
    }

    public void Execute(List<Entity> entities) {
        // Do sth
    }
}

You can also mix interfaces

public class UpdateBoardSystem : IInitializeSystem, IReactiveSystem, ISetPool {

    Pool _pool;
    Group _myGroup;

    public void SetPool(Pool pool) {
        _pool = pool;
        _myGroup = pool.GetGroup(Matcher.Xyz);
    }

    public void Initialize() {
        // Do sth
    }

    public TriggerOnEvent trigger {
        get { return Matcher.AllOf(Matcher.Position, Matcher.View).OnEntityAdded(); }
    }

    public void Execute(List<Entity> entities) {
        // Do sth
    }
}

There a multiple ways how to create systems. The simplest one that works for any kind of system is probably pool.CreateSystem<T>()

new MoveSystem();

// Creates a new MoveSystem instance and
// will call SetPool if ISetPool is implemented
pool.CreateSystem<MoveSystem>();


// Will call SetPool if ISetPool is implemented
pool.CreateSystem(new MoveSystem());


// Recognizes that UpdateBoardSystem is a reactive system.
// It will create a new ReactiveSystem with UpdateBoardSystem as
// the subSystem.
// Will call SetPool if ISetPool is implemented
var reactiveSystem = pool.CreateSystem<UpdateBoardSystem>();
var systems = new Systems()
    .Add(pool.CreateSystem<CreateLevelSystem>())
    .Add(pool.CreateSystem<UpdateBoardSystem>())
    .Add(pool.CreateSystem<MoveSystem>())
    .Add(pool.CreateSystem<RenderPositionSystem>());

// Call once on start
systems.Initialize();

// Call every frame
systems.Execute();

Get inspired by Match-One GameController.cs

Clone this wiki locally