Reactive System 響應(yīng)式系統(tǒng)
Reactive System是一個當(dāng)有需要我們處理Entity才會被調(diào)用的system。Reactive System在內(nèi)部使用了一個Collector的實(shí)例來實(shí)現(xiàn)(更多的信息可以查看Collector這個章節(jié))。你需要繼承ReactiveSystem這個抽象類來使用它,下面是一個MatchOne里面的ReactiveSystem例子:
using System.Collections.Generic;
using Entitas;
public sealed class DestroySystem : ReactiveSystem<GameEntity> {
public DestroySystem(Contexts contexts) : base(contexts.game) {
}
protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context) {
return context.CreateCollector(GameMatcher.Destroyed);
}
protected override bool Filter(GameEntity entity) {
return entity.isDestroyed;
}
protected override void Execute(List<GameEntity> entities) {
foreach (var e in entities) {
e.Destroy();
}
}
}
這個系統(tǒng)是用于銷毀那些用DestroyedComponent標(biāo)記的Entitiy。你能看到我們在GetTrigger方法中返回了一個監(jiān)測了DestroyedEntity的Collector。在context.CreateCollector(GameMatcher.Destroyed) 中,我們不需要指定當(dāng)一個Entity何時應(yīng)當(dāng)被收集的事件,因為默認(rèn)就是會收集在Added情況下被通知到的Entity。所以當(dāng)我們增加一個Destroyed組件到一個Entity上時,這個Entity會添加到Destroyed的group里面,并因此被對應(yīng)的collector收集到對應(yīng)的reactive system里面。
響應(yīng)式的系統(tǒng)就像執(zhí)行式系統(tǒng)一樣,會每隔一段時間或是在每一個Update中被觸發(fā),然而Execute(List<GameEntity> entities) 方法只會在收集器距離上一次Execute收集到新的Entity才會被執(zhí)行。
你可能會在猜想Filter方法是用于干什么的。就像之前在收集器章節(jié)提到的那樣,如果一個Entity被收集器收集了的話,即時有些事件像是第一次事件的復(fù)原,這個Entity依然會保持被收集。在我們上面說到的例子中,我們收集所有被銷毀的entity。所即使Destroyed組件別被移除了來復(fù)原被銷毀這個狀態(tài),這個Entity已經(jīng)會被收集著而且會傳入Execute方法中,除非我們把它過濾掉。在Filter方法中,我們可以決定這個被收集的Entity是否可以被傳入Execute方法。如果你你沒有特殊的規(guī)范,你可以直接返回true,不然就像上面的例子,你應(yīng)該檢查收集到的entity是否依然擁有Destroyed組件。
Careful with AnyOf based collector 小心使用AnyOf的收集器
當(dāng)你創(chuàng)建了一個觀察基于AnyOf匹配器的組合的收集器時,你可能會得到一些意料之外的結(jié)果,就像你擁有組件A和B,并且你有一個AnyOf(A,B)的組合。一個Entity只有當(dāng)其中一個組件被添加的時候才會進(jìn)入這個組合。當(dāng)我們添加第二個組件時,這個entity因為已經(jīng)在組合中了,所以不會觸發(fā)Added事件而被收集到。你可能不是你想要的情況。一般來說人們會想看見不管哪個組件被添加這個Entity都要能被收集到。在這種情況下,你應(yīng)該設(shè)置一個分別針對這兩種組件的組合而不是使用AnyOf。這是在MatchOne中的例子:
using System.Collections.Generic;
using DG.Tweening;
using Entitas;
using Entitas.Unity;
using UnityEngine;
public sealed class RemoveViewSystem : ReactiveSystem<GameEntity> {
public RemoveViewSystem(Contexts contexts) : base(contexts.game) {
}
protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context) {
return context.CreateCollector(
GameMatcher.Asset.Removed(),
GameMatcher.Destroyed.Added()
);
}
protected override bool Filter(GameEntity entity) {
return entity.hasView;
}
protected override void Execute(List<GameEntity> entities) {
foreach (var e in entities) {
destroyView(e.view);
e.RemoveView();
}
}
void destroyView(ViewComponent viewComponent) {
var gameObject = viewComponent.gameObject;
var spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
var color = spriteRenderer.color;
color.a = 0f;
spriteRenderer.material.DOColor(color, 0.2f);
gameObject.transform
.DOScale(Vector3.one * 1.5f, 0.2f)
.OnComplete(() => {
gameObject.Unlink();
Object.Destroy(gameObject);
});
}
}
在這個響應(yīng)式系統(tǒng)中,當(dāng)我們移除Asset組件或者添加Destroyed組件到Entity上時,我們會移除掉這個Entity的視圖,
然而這種解決方案也有要注意的地方。雖然收集器可以設(shè)置多個組合,但是這些組合必須基于同一個上下文中的組件。所以當(dāng)你需要設(shè)置一個響應(yīng)式系統(tǒng)收集來自不同上下文的Entity時,你需要擴(kuò)展MultiReactiveSystem類來達(dá)到這個目的。在這個類中GetTrigger方法會返回一組收集器。