The Basics - OneYoungMean/Entitas-CSharp-OYM GitHub Wiki

Group

组始终都是在动态更新的,且包含了所有与指定匹配器匹配的实体,每一个上下文都有一个特定的匹配器类型,如果你希望寻找游戏上下文当中的实体,你需要使用匹配器(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

匹配器

匹配器由代码生成器生成,可以组合使用。匹配器通常适用于从上下文中获取你感兴趣的成组的实体。请记住在匹配器前面添加你所感兴趣的上下文名称作为前缀 (e.g. GameMatcher, InputMatcher etc)。我们将使用GameMatcher作为前缀.

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

Entitas与untiy的原生框架不同,它没有传统的生命周期,不过我们可以选择其上面的四个不同的系统:

  • IInitializeSystem: 仅在开始执行一次 (system.Initialize())
  • IExecuteSystem: 每一帧都执行一次 (system.Execute())
  • ICleanupSystem: 每一帧仅在完成了其他系统以后执行一次 (system.Cleanup())
  • ReactiveSystem: 仅当检测到组发生改变(替换,删除)后执行一次 (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;
        }
    }
}

除了ReactiveSystem以外,你可以自由组合这些系统,这个我们会在后面讲到。

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();

有关于系统的更多详细信息,包括一些示例,请参考系统

⚠️ **GitHub.com Fallback** ⚠️