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

148 lines
3.9 KiB
TypeScript
Raw Normal View History

///<reference path="../../Utils/Collections/HashMap.ts"/>
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[] = [];
2021-08-28 21:26:44 +08:00
private _updateOrder: number = 0;
private _startTime = 0;
private _endTime = 0;
private _useTime = 0;
2021-08-28 21:26:44 +08:00
/** 获取系统在当前帧所消耗的时间 仅在debug模式下生效 */
public get useTime() {
return this._useTime;
}
/**
*
*/
public get updateOrder() {
return this._updateOrder;
}
public set updateOrder(value: number) {
this.setUpdateOrder(value);
}
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();
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;
/**
*
*/
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
2021-08-28 21:26:44 +08:00
/**
*
* @param order
2021-08-28 21:26:44 +08:00
*/
public setUpdateOrder(order: number) {
2021-08-28 21:26:44 +08:00
this._updateOrder = order;
this.scene.entityProcessors.setDirty();
2021-04-07 15:24:37 +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-05-07 16:23:15 +08:00
let contains = !!this._entities.find(e => e.id == entity.id);
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) {
2021-05-07 16:23:15 +08:00
if (!this._entities.find(e => e.id == entity.id))
this._entities.push(entity);
2020-07-23 11:00:46 +08:00
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() {
2021-04-07 15:24:37 +08:00
if (!Core.Instance.debug)
return;
this._startTime = Date.now();
}
protected process(entities: Entity[]) {
}
protected lateProcess(entities: Entity[]) {
}
/**
*
*/
protected end() {
2021-04-07 15:24:37 +08:00
if (!Core.Instance.debug)
return;
this._endTime = Date.now();
this._useTime = this._endTime - this._startTime;
}
/**
*
*
*
*
* @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
}