Skip to content

The Basics

Jeremy Ries edited this page Dec 20, 2018 · 9 revisions

Group

Groups are always up-to-date and contain all entities matching the specified matcher. There is a specific Matcher Type for each context - if you're looking for entities in your Game context (i.e. GameEntities) you'll need to use a GameMatcher.

var context = contexts.game;

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

var entity1 = context.CreateEntity();
entity1.isMovable = true;
var entity2 = context.CreateEntity();
entity2.IsMovable = true;

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

// GetEntities() always provides an up to date list
var movableEntities = movables.GetEntities();
foreach (var e in movableEntities) {
    // Do sth
}

entity1.Destroy();
entity2.Destroy();

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 context of interest. Remember to prefix the matcher with the context name you are interested (e.g. GameMatcher, InputMatcher etc). We will use GameMatcher for these examples.

var matcher = GameMatcher.Movable;

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

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

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

Systems

There are 4 different types of Systems:

  • IInitializeSystem: Executes once (system.Initialize())
  • IExecuteSystem: Executes every frame (system.Execute())
  • ICleanupSystem: Executes every frame after the other systems are finished (system.Cleanup())
  • ReactiveSystem: 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: ReactiveSystem<GameEntity> {

    public RenderPositionSystem(Contexts contexts) : base(contexts.Game) {
        
    }

    protected override Collector<GameEntity> GetTrigger(IContext<GameEntity> context) {
        return context.CreateCollector(GameMatcher.Position);
    }

    protected override bool Filter(GameEntity entity) {
        // check for required components (here it is position and view)
        return entity.hasPosition && entity.hasView;
    }

    protected override void Execute(List<GameEntity> entities) {
        foreach (var e in entities) {
            // do stuff to the matched entities
            e.view.gameObject.transform.position = e.position.position;
        }
    }
}

You can also mix interfaces

public class UpdateBoardSystem : IInitializeSystem, ReactiveSystem<GameEntity> {

    Context _context;
    Group _myGroup;

    public UpdateBoardSystem (Contexts contexts) : base(context.game) {
        _context= contexts.game;
        _myGroup = _context.GetGroup(GameMatcher.Xyz);
    }

    public void Initialize() {
        // Do initialization stuff
    }

    protected override Collector<GameEntity> GetTrigger(IContext<GameEntity> context) {
        // define which group we are reacting to
    }

    protected override bool Filter(GameEntity entity) {
        // check for required components
    }

    protected override void Execute(List<GameEntity> entities) {
        foreach (var e in entities) {
            // do stuff to the matched entities
        }
    }
}
var systems = new Systems(contexts)
    .Add(new CreateLevelSystem(contexts))
    .Add(new UpdateBoardSystem(contexts))
    .Add(new MoveSystem(contexts))
    .Add(new RenderPositionSystem(contexts));

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

// Call every frame
systems.Execute();

Get inspired by Match-One GameController.cs

For more detail about systems, including some examples, see Systems

Clone this wiki locally