Files
esengine/source/src/ECS/Systems/EntitySystem.ts

101 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-07-23 11:00:46 +08:00
module es {
/**
*
*/
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();
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;
/**
*
*/
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);
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
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
public onRemoved(entity: Entity) { }
2020-06-08 18:26:05 +08:00
2020-07-28 16:25:20 +08:00
public update() {
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() {
if (this.checkProcessing()) {
this.lateProcess(this._entities);
this.end();
}
}
/**
*
* 使
*/
protected begin() { }
protected process(entities: Entity[]) { }
protected lateProcess(entities: Entity[]) { }
/**
*
*/
protected end() { }
/**
*
*
*
*
* @returns truefalse
*/
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
}