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

84 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-07-23 11:00:46 +08:00
module es {
export class EntitySystem {
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;
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) {
2020-07-23 11:00:46 +08:00
let contains = 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
2020-07-28 16:25:20 +08:00
public onAdded(entity: Entity) {
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 remove(entity: Entity) {
2020-07-23 11:00:46 +08:00
this._entities.remove(entity);
this.onRemoved(entity);
}
2020-06-08 20:11:58 +08:00
2020-07-28 16:25:20 +08:00
public onRemoved(entity: Entity) {
2020-06-08 20:11:58 +08:00
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 update() {
2020-07-23 11:00:46 +08:00
this.begin();
this.process(this._entities);
}
2020-06-08 18:26:05 +08:00
2020-07-28 16:25:20 +08:00
public lateUpdate() {
2020-07-23 11:00:46 +08:00
this.lateProcess(this._entities);
this.end();
}
2020-06-08 18:26:05 +08:00
2020-07-28 16:25:20 +08:00
protected begin() {
2020-06-08 18:26:05 +08:00
2020-07-23 11:00:46 +08:00
}
2020-06-08 18:26:05 +08:00
2020-07-28 16:25:20 +08:00
protected process(entities: Entity[]) {
2020-06-08 18:26:05 +08:00
2020-07-23 11:00:46 +08:00
}
2020-06-08 18:26:05 +08:00
2020-07-28 16:25:20 +08:00
protected lateProcess(entities: Entity[]) {
2020-06-08 18:26:05 +08:00
2020-07-23 11:00:46 +08:00
}
2020-06-08 18:26:05 +08:00
2020-07-28 16:25:20 +08:00
protected end() {
2020-06-08 18:26:05 +08:00
2020-07-23 11:00:46 +08:00
}
2020-06-08 18:26:05 +08:00
}
2020-07-23 11:00:46 +08:00
}