新增系统时序控制

This commit is contained in:
yhh
2021-08-28 21:26:44 +08:00
parent a4b971bba0
commit cd94326aad
6 changed files with 147 additions and 22 deletions
+23 -6
View File
@@ -1573,6 +1573,16 @@ declare module es {
*/ */
abstract class EntitySystem { abstract class EntitySystem {
private _entities; private _entities;
private _updateOrder;
private _startTime;
private _endTime;
private _useTime;
/** 获取系统在当前帧所消耗的时间 仅在debug模式下生效 */
readonly useTime: number;
/**
* 获取系统的更新时序
*/
updateOrder: number;
constructor(matcher?: Matcher); constructor(matcher?: Matcher);
private _scene; private _scene;
/** /**
@@ -1581,11 +1591,11 @@ declare module es {
scene: Scene; scene: Scene;
private _matcher; private _matcher;
readonly matcher: Matcher; readonly matcher: Matcher;
private _startTime; /**
private _endTime; * 设置更新时序
private _useTime; * @param order
/** 获取系统在当前帧所消耗的时间 仅在debug模式下生效 */ */
readonly useTime: number; setUpdateOrder(order: number): void;
initialize(): void; initialize(): void;
onChanged(entity: Entity): void; onChanged(entity: Entity): void;
add(entity: Entity): void; add(entity: Entity): void;
@@ -2006,7 +2016,12 @@ declare module es {
} }
declare module es { declare module es {
class EntityProcessorList { class EntityProcessorList {
_processors: EntitySystem[]; private _processors;
private _orderDirty;
/** 获取系统列表 */
readonly processors: EntitySystem[];
/** 系统数量 */
readonly count: number;
add(processor: EntitySystem): void; add(processor: EntitySystem): void;
remove(processor: EntitySystem): void; remove(processor: EntitySystem): void;
onComponentAdded(entity: Entity): void; onComponentAdded(entity: Entity): void;
@@ -2017,6 +2032,8 @@ declare module es {
update(): void; update(): void;
lateUpdate(): void; lateUpdate(): void;
end(): void; end(): void;
setDirty(): void;
clearDirty(): void;
getProcessor<T extends EntitySystem>(type: new (...args: any[]) => T): T; getProcessor<T extends EntitySystem>(type: new (...args: any[]) => T): T;
protected notifyEntityChanged(entity: Entity): void; protected notifyEntityChanged(entity: Entity): void;
protected removeFromProcessors(entity: Entity): void; protected removeFromProcessors(entity: Entity): void;
+64 -8
View File
@@ -1760,6 +1760,8 @@ var es;
Scene.prototype.addEntityProcessor = function (processor) { Scene.prototype.addEntityProcessor = function (processor) {
processor.scene = this; processor.scene = this;
this.entityProcessors.add(processor); this.entityProcessors.add(processor);
processor.setUpdateOrder(this.entityProcessors.count - 1);
this.entityProcessors.clearDirty();
return processor; return processor;
}; };
/** /**
@@ -3967,12 +3969,34 @@ var es;
var EntitySystem = /** @class */ (function () { var EntitySystem = /** @class */ (function () {
function EntitySystem(matcher) { function EntitySystem(matcher) {
this._entities = []; this._entities = [];
this._updateOrder = 0;
this._startTime = 0; this._startTime = 0;
this._endTime = 0; this._endTime = 0;
this._useTime = 0; this._useTime = 0;
this._matcher = matcher ? matcher : es.Matcher.empty(); this._matcher = matcher ? matcher : es.Matcher.empty();
this.initialize(); this.initialize();
} }
Object.defineProperty(EntitySystem.prototype, "useTime", {
/** 获取系统在当前帧所消耗的时间 仅在debug模式下生效 */
get: function () {
return this._useTime;
},
enumerable: true,
configurable: true
});
Object.defineProperty(EntitySystem.prototype, "updateOrder", {
/**
* 获取系统的更新时序
*/
get: function () {
return this._updateOrder;
},
set: function (value) {
this.setUpdateOrder(value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(EntitySystem.prototype, "scene", { Object.defineProperty(EntitySystem.prototype, "scene", {
/** /**
* 这个系统所属的场景 * 这个系统所属的场景
@@ -3994,14 +4018,14 @@ var es;
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(EntitySystem.prototype, "useTime", { /**
/** 获取系统在当前帧所消耗的时间 仅在debug模式下生效 */ * 设置更新时序
get: function () { * @param order
return this._useTime; */
}, EntitySystem.prototype.setUpdateOrder = function (order) {
enumerable: true, this._updateOrder = order;
configurable: true this.scene.entityProcessors.setDirty();
}); };
EntitySystem.prototype.initialize = function () { EntitySystem.prototype.initialize = function () {
}; };
EntitySystem.prototype.onChanged = function (entity) { EntitySystem.prototype.onChanged = function (entity) {
@@ -5135,7 +5159,24 @@ var es;
var EntityProcessorList = /** @class */ (function () { var EntityProcessorList = /** @class */ (function () {
function EntityProcessorList() { function EntityProcessorList() {
this._processors = []; this._processors = [];
this._orderDirty = false;
} }
Object.defineProperty(EntityProcessorList.prototype, "processors", {
/** 获取系统列表 */
get: function () {
return this._processors;
},
enumerable: true,
configurable: true
});
Object.defineProperty(EntityProcessorList.prototype, "count", {
/** 系统数量 */
get: function () {
return this._processors.length;
},
enumerable: true,
configurable: true
});
EntityProcessorList.prototype.add = function (processor) { EntityProcessorList.prototype.add = function (processor) {
this._processors.push(processor); this._processors.push(processor);
}; };
@@ -5159,6 +5200,15 @@ var es;
EntityProcessorList.prototype.update = function () { EntityProcessorList.prototype.update = function () {
if (this._processors.length == 0) if (this._processors.length == 0)
return; return;
if (this._orderDirty) {
// 进行排序
this._processors = this._processors.sort(function (a, b) { return a.updateOrder - b.updateOrder; });
for (var i = 0, s = this._processors.length; i < s; ++i) {
var processor = this._processors[i];
processor.setUpdateOrder(i);
}
this.clearDirty();
}
for (var i = 0, s = this._processors.length; i < s; ++i) { for (var i = 0, s = this._processors.length; i < s; ++i) {
this._processors[i].update(); this._processors[i].update();
} }
@@ -5172,6 +5222,12 @@ var es;
}; };
EntityProcessorList.prototype.end = function () { EntityProcessorList.prototype.end = function () {
}; };
EntityProcessorList.prototype.setDirty = function () {
this._orderDirty = true;
};
EntityProcessorList.prototype.clearDirty = function () {
this._orderDirty = false;
};
EntityProcessorList.prototype.getProcessor = function (type) { EntityProcessorList.prototype.getProcessor = function (type) {
if (this._processors.length == 0) if (this._processors.length == 0)
return null; return null;
+1 -1
View File
File diff suppressed because one or more lines are too long
+3
View File
@@ -276,6 +276,9 @@ module es {
public addEntityProcessor(processor: EntitySystem) { public addEntityProcessor(processor: EntitySystem) {
processor.scene = this; processor.scene = this;
this.entityProcessors.add(processor); this.entityProcessors.add(processor);
processor.setUpdateOrder(this.entityProcessors.count - 1);
this.entityProcessors.clearDirty();
return processor; return processor;
} }
+27 -6
View File
@@ -5,6 +5,26 @@ module es {
*/ */
export abstract class EntitySystem { export abstract class EntitySystem {
private _entities: Entity[] = []; private _entities: Entity[] = [];
private _updateOrder: number = 0;
private _startTime = 0;
private _endTime = 0;
private _useTime = 0;
/** 获取系统在当前帧所消耗的时间 仅在debug模式下生效 */
public get useTime() {
return this._useTime;
}
/**
*
*/
public get updateOrder() {
return this._updateOrder;
}
public set updateOrder(value: number) {
this.setUpdateOrder(value);
}
constructor(matcher?: Matcher) { constructor(matcher?: Matcher) {
this._matcher = matcher ? matcher : Matcher.empty(); this._matcher = matcher ? matcher : Matcher.empty();
@@ -31,12 +51,13 @@ module es {
return this._matcher; return this._matcher;
} }
private _startTime = 0; /**
private _endTime = 0; *
private _useTime = 0; * @param order
/** 获取系统在当前帧所消耗的时间 仅在debug模式下生效 */ */
public get useTime() { public setUpdateOrder(order: number) {
return this._useTime; this._updateOrder = order;
this.scene.entityProcessors.setDirty();
} }
public initialize() { public initialize() {
+29 -1
View File
@@ -1,6 +1,16 @@
module es { module es {
export class EntityProcessorList { export class EntityProcessorList {
public _processors: EntitySystem[] = []; private _processors: EntitySystem[] = [];
private _orderDirty: boolean = false;
/** 获取系统列表 */
public get processors() {
return this._processors;
}
/** 系统数量 */
public get count() {
return this._processors.length;
}
public add(processor: EntitySystem) { public add(processor: EntitySystem) {
this._processors.push(processor); this._processors.push(processor);
@@ -34,6 +44,16 @@ module es {
if (this._processors.length == 0) if (this._processors.length == 0)
return; return;
if (this._orderDirty) {
// 进行排序
this._processors = this._processors.sort((a, b) => a.updateOrder - b.updateOrder);
for (let i = 0, s = this._processors.length; i < s; ++ i) {
const processor = this._processors[i];
processor.setUpdateOrder(i);
}
this.clearDirty();
}
for (let i = 0, s = this._processors.length; i < s; ++ i) { for (let i = 0, s = this._processors.length; i < s; ++ i) {
this._processors[i].update(); this._processors[i].update();
} }
@@ -52,6 +72,14 @@ module es {
} }
public setDirty() {
this._orderDirty = true;
}
public clearDirty() {
this._orderDirty = false;
}
public getProcessor<T extends EntitySystem>(type: new (...args: any[]) => T): T { public getProcessor<T extends EntitySystem>(type: new (...args: any[]) => T): T {
if (this._processors.length == 0) if (this._processors.length == 0)
return null; return null;