Files
esengine/src/ECS/Utils/EntityProcessorList.ts

109 lines
2.3 KiB
TypeScript
Raw Normal View History

import { EntitySystem } from '../Systems/EntitySystem';
/**
*
*
*/
export class EntityProcessorList {
private _processors: EntitySystem[] = [];
private _isDirty = false;
/**
*
*/
public setDirty(): void {
this._isDirty = true;
}
/**
*
* @param processor
*/
public add(processor: EntitySystem): void {
this._processors.push(processor);
this.setDirty();
}
/**
*
* @param processor
*/
public remove(processor: EntitySystem): void {
const index = this._processors.indexOf(processor);
if (index !== -1) {
this._processors.splice(index, 1);
}
}
/**
*
* @param type
*/
public getProcessor<T extends EntitySystem>(type: new (...args: any[]) => T): T | null {
for (const processor of this._processors) {
if (processor instanceof type) {
return processor as T;
2020-07-23 09:10:27 +08:00
}
}
return null;
}
/**
*
*/
public begin(): void {
this.sortProcessors();
for (const processor of this._processors) {
processor.initialize();
}
}
2021-08-28 21:26:44 +08:00
/**
*
*/
public end(): void {
// 清理处理器
}
2021-08-28 21:26:44 +08:00
/**
*
*/
public update(): void {
this.sortProcessors();
for (const processor of this._processors) {
processor.update();
2021-08-28 21:26:44 +08:00
}
}
/**
*
*/
public lateUpdate(): void {
for (const processor of this._processors) {
processor.lateUpdate();
2020-07-23 09:10:27 +08:00
}
}
2020-07-28 16:25:20 +08:00
/**
*
*/
private sortProcessors(): void {
if (this._isDirty) {
this._processors.sort((a, b) => a.updateOrder - b.updateOrder);
this._isDirty = false;
}
}
/** 获取处理器列表 */
public get processors() {
return this._processors;
}
/** 获取处理器数量 */
public get count() {
return this._processors.length;
}
2020-07-28 16:25:20 +08:00
2020-07-23 09:10:27 +08:00
}