2020-07-23 11:00:46 +08:00
|
|
|
|
module es {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 追踪实体的子集,但不实现任何排序或迭代。
|
|
|
|
|
|
*/
|
|
|
|
|
|
export abstract class EntitySystem {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
private _entities: Entity[] = [];
|
2020-06-08 18:26:05 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
constructor(matcher?: Matcher) {
|
|
|
|
|
|
this._matcher = matcher ? matcher : Matcher.empty();
|
2021-03-31 16:59:21 +08:00
|
|
|
|
this.initialize();
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 18:26:05 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
private _scene: Scene;
|
|
|
|
|
|
|
2021-01-27 14:58:51 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 这个系统所属的场景
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get scene() {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
return this._scene;
|
|
|
|
|
|
}
|
2020-06-08 18:26:05 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public set scene(value: Scene) {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
this._scene = value;
|
|
|
|
|
|
this._entities = [];
|
|
|
|
|
|
}
|
2020-06-08 18:26:05 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
private _matcher: Matcher;
|
|
|
|
|
|
|
|
|
|
|
|
public get matcher() {
|
|
|
|
|
|
return this._matcher;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 18:26:05 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public initialize() {
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public onChanged(entity: Entity) {
|
2021-03-29 15:28:18 +08:00
|
|
|
|
let contains = new es.List(this._entities).contains(entity);
|
2020-11-24 18:21:26 +08:00
|
|
|
|
let interest = this._matcher.isInterestedEntity(entity);
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-23 11:00:46 +08:00
|
|
|
|
if (interest && !contains)
|
|
|
|
|
|
this.add(entity);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
else if (!interest && contains)
|
2020-07-23 11:00:46 +08:00
|
|
|
|
this.remove(entity);
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public add(entity: Entity) {
|
2020-07-23 11:00:46 +08:00
|
|
|
|
this._entities.push(entity);
|
|
|
|
|
|
this.onAdded(entity);
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2021-01-27 14:58:51 +08:00
|
|
|
|
public onAdded(entity: Entity) { }
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public remove(entity: Entity) {
|
2021-03-29 15:28:18 +08:00
|
|
|
|
new es.List(this._entities).remove(entity);
|
2020-07-23 11:00:46 +08:00
|
|
|
|
this.onRemoved(entity);
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2021-01-27 14:58:51 +08:00
|
|
|
|
public onRemoved(entity: Entity) { }
|
2020-06-08 18:26:05 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public update() {
|
2021-01-27 14:58:51 +08:00
|
|
|
|
if (this.checkProcessing()) {
|
|
|
|
|
|
this.begin();
|
|
|
|
|
|
this.process(this._entities);
|
|
|
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 18:26:05 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public lateUpdate() {
|
2021-01-27 14:58:51 +08:00
|
|
|
|
if (this.checkProcessing()) {
|
|
|
|
|
|
this.lateProcess(this._entities);
|
|
|
|
|
|
this.end();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 在系统处理开始前调用
|
|
|
|
|
|
* 在下一个系统开始处理或新的处理回合开始之前(以先到者为准),使用此方法创建的任何实体都不会激活
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected begin() { }
|
|
|
|
|
|
|
|
|
|
|
|
protected process(entities: Entity[]) { }
|
|
|
|
|
|
|
|
|
|
|
|
protected lateProcess(entities: Entity[]) { }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 系统处理完毕后调用
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected end() { }
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 系统是否需要处理
|
|
|
|
|
|
*
|
|
|
|
|
|
* 在启用系统时有用,但仅偶尔需要处理
|
|
|
|
|
|
* 这只影响处理,不影响事件或订阅列表
|
|
|
|
|
|
* @returns 如果系统应该处理,则为true,如果不处理则为false。
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected checkProcessing() {
|
|
|
|
|
|
return true;
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|
2020-06-08 18:26:05 +08:00
|
|
|
|
}
|
2020-07-23 11:00:46 +08:00
|
|
|
|
}
|