完善sceneComponent中setEnabled。新增Core.Instance._frameCounter与Core.Instance._totalMemory

This commit is contained in:
yhh
2020-11-30 12:57:53 +08:00
parent 3b4a3fad5a
commit 4c329881a1
8 changed files with 98 additions and 40 deletions

View File

@@ -186,7 +186,7 @@ declare module es {
/** /**
* 全局访问系统 * 全局访问系统
*/ */
_globalManagers: GlobalManager[]; _globalManagers: FastList<GlobalManager>;
_timerManager: TimerManager; _timerManager: TimerManager;
width: number; width: number;
height: number; height: number;
@@ -198,6 +198,7 @@ declare module es {
static readonly Instance: Core; static readonly Instance: Core;
_frameCounterElapsedTime: number; _frameCounterElapsedTime: number;
_frameCounter: number; _frameCounter: number;
_totalMemory: number;
_scene: Scene; _scene: Scene;
/** /**
* 当前活动的场景。注意,如果设置了该设置,在更新结束之前场景实际上不会改变 * 当前活动的场景。注意,如果设置了该设置,在更新结束之前场景实际上不会改变
@@ -237,6 +238,7 @@ declare module es {
static schedule(timeInSeconds: number, repeats: boolean, context: any, onTime: (timer: ITimer) => void): Timer; static schedule(timeInSeconds: number, repeats: boolean, context: any, onTime: (timer: ITimer) => void): Timer;
onOrientationChanged(): void; onOrientationChanged(): void;
draw(): Promise<void>; draw(): Promise<void>;
startDebugDraw(): void;
/** /**
* 在一个场景结束后,下一个场景开始之前调用 * 在一个场景结束后,下一个场景开始之前调用
*/ */
@@ -449,7 +451,7 @@ declare module es {
* 管理所有实体处理器 * 管理所有实体处理器
*/ */
readonly entityProcessors: EntityProcessorList; readonly entityProcessors: EntityProcessorList;
readonly _sceneComponents: SceneComponent[]; readonly _sceneComponents: FastList<SceneComponent>;
_renderers: Renderer[]; _renderers: Renderer[];
_didSceneBegin: any; _didSceneBegin: any;
constructor(); constructor();
@@ -796,7 +798,7 @@ declare module es {
} }
} }
declare module es { declare module es {
class SceneComponent { class SceneComponent implements IComparer<SceneComponent> {
/** /**
* 这个场景组件被附加到的场景 * 这个场景组件被附加到的场景
*/ */
@@ -840,7 +842,7 @@ declare module es {
* @param updateOrder * @param updateOrder
*/ */
setUpdateOrder(updateOrder: number): this; setUpdateOrder(updateOrder: number): this;
compareTo(other: SceneComponent): number; compare(other: SceneComponent): number;
} }
} }
declare module es { declare module es {
@@ -3641,7 +3643,15 @@ declare module es {
*/ */
intersectWith(other: Array<T>): void; intersectWith(other: Array<T>): void;
unionWith(other: Array<T>): void; unionWith(other: Array<T>): void;
/**
* 确定当前集合是否为指定集合或数组的子集
* @param other
*/
isSubsetOf(other: Array<T>): boolean; isSubsetOf(other: Array<T>): boolean;
/**
* 确定当前不可变排序集是否为指定集合的超集
* @param other
*/
isSupersetOf(other: Array<T>): boolean; isSupersetOf(other: Array<T>): boolean;
overlaps(other: Array<T>): boolean; overlaps(other: Array<T>): boolean;
setEquals(other: Array<T>): boolean; setEquals(other: Array<T>): boolean;

View File

@@ -399,10 +399,11 @@ var es;
/** /**
* 全局访问系统 * 全局访问系统
*/ */
this._globalManagers = []; this._globalManagers = new es.FastList();
this._timerManager = new es.TimerManager(); this._timerManager = new es.TimerManager();
this._frameCounterElapsedTime = 0; this._frameCounterElapsedTime = 0;
this._frameCounter = 0; this._frameCounter = 0;
this._totalMemory = 0;
this.width = width; this.width = width;
this.height = height; this.height = height;
Core._instance = this; Core._instance = this;
@@ -470,7 +471,7 @@ var es;
* @param manager * @param manager
*/ */
Core.registerGlobalManager = function (manager) { Core.registerGlobalManager = function (manager) {
this._instance._globalManagers.push(manager); this._instance._globalManagers.add(manager);
manager.enabled = true; manager.enabled = true;
}; };
/** /**
@@ -487,8 +488,8 @@ var es;
*/ */
Core.getGlobalManager = function (type) { Core.getGlobalManager = function (type) {
for (var i = 0; i < this._instance._globalManagers.length; i++) { for (var i = 0; i < this._instance._globalManagers.length; i++) {
if (this._instance._globalManagers[i] instanceof type) if (this._instance._globalManagers.buffer[i] instanceof type)
return this._instance._globalManagers[i]; return this._instance._globalManagers.buffer[i];
} }
return null; return null;
}; };
@@ -512,6 +513,7 @@ var es;
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
this.startDebugDraw();
if (!(this._sceneTransition != null)) return [3 /*break*/, 5]; if (!(this._sceneTransition != null)) return [3 /*break*/, 5];
this._sceneTransition.preRender(); this._sceneTransition.preRender();
if (!(this._sceneTransition != null)) return [3 /*break*/, 4]; if (!(this._sceneTransition != null)) return [3 /*break*/, 4];
@@ -544,6 +546,18 @@ var es;
}); });
}); });
}; };
Core.prototype.startDebugDraw = function () {
this._frameCounter++;
this._frameCounterElapsedTime += es.Time.deltaTime;
if (this._frameCounterElapsedTime >= 1) {
var memoryInfo = window.performance["memory"];
if (memoryInfo != null) {
this._totalMemory = Number((memoryInfo.totalJSHeapSize / 1048576).toFixed(2));
}
this._frameCounter = 0;
this._frameCounterElapsedTime -= 1;
}
};
/** /**
* 在一个场景结束后,下一个场景开始之前调用 * 在一个场景结束后,下一个场景开始之前调用
*/ */
@@ -578,15 +592,15 @@ var es;
es.Time.update(currentTime); es.Time.update(currentTime);
if (this._scene != null) { if (this._scene != null) {
for (i = this._globalManagers.length - 1; i >= 0; i--) { for (i = this._globalManagers.length - 1; i >= 0; i--) {
if (this._globalManagers[i].enabled) if (this._globalManagers.buffer[i].enabled)
this._globalManagers[i].update(); this._globalManagers.buffer[i].update();
} }
// 仔细阅读: // 仔细阅读:
// 当场景转换发生时,我们不更新场景 // 当场景转换发生时,我们不更新场景
// - 除非是不改变场景的SceneTransition(没有理由不更新) // - 除非是不改变场景的SceneTransition(没有理由不更新)
// - 或者是一个已经切换到新场景的SceneTransition新场景需要做它的事情 // - 或者是一个已经切换到新场景的SceneTransition新场景需要做它的事情
if (!this._sceneTransition || if (this._sceneTransition == null ||
(this._sceneTransition && (this._sceneTransition != null &&
(!this._sceneTransition.loadsNewScene || this._sceneTransition.isNewSceneLoaded))) { (!this._sceneTransition.loadsNewScene || this._sceneTransition.isNewSceneLoaded))) {
this._scene.update(); this._scene.update();
} }
@@ -1045,7 +1059,7 @@ var es;
/** 场景 */ /** 场景 */
var Scene = /** @class */ (function () { var Scene = /** @class */ (function () {
function Scene() { function Scene() {
this._sceneComponents = []; this._sceneComponents = new es.FastList();
this._renderers = []; this._renderers = [];
this.entities = new es.EntityList(this); this.entities = new es.EntityList(this);
this.renderableComponents = new es.RenderableComponentList(); this.renderableComponents = new es.RenderableComponentList();
@@ -1094,9 +1108,9 @@ var es;
es.Core.emitter.removeObserver(es.CoreEvents.OrientationChanged, this.updateResolutionScaler); es.Core.emitter.removeObserver(es.CoreEvents.OrientationChanged, this.updateResolutionScaler);
this.entities.removeAllEntities(); this.entities.removeAllEntities();
for (var i = 0; i < this._sceneComponents.length; i++) { for (var i = 0; i < this._sceneComponents.length; i++) {
this._sceneComponents[i].onRemovedFromScene(); this._sceneComponents.buffer[i].onRemovedFromScene();
} }
this._sceneComponents.length = 0; this._sceneComponents.clear();
es.Physics.clear(); es.Physics.clear();
if (this.entityProcessors) if (this.entityProcessors)
this.entityProcessors.end(); this.entityProcessors.end();
@@ -1139,8 +1153,8 @@ var es;
Scene.prototype.addSceneComponent = function (component) { Scene.prototype.addSceneComponent = function (component) {
component.scene = this; component.scene = this;
component.onEnabled(); component.onEnabled();
this._sceneComponents.push(component); this._sceneComponents.add(component);
this._sceneComponents.sort(component.compareTo); this._sceneComponents.sort(component);
return component; return component;
}; };
/** /**
@@ -1874,8 +1888,10 @@ var es;
if (this._enabled != isEnabled) { if (this._enabled != isEnabled) {
this._enabled = isEnabled; this._enabled = isEnabled;
if (this._enabled) { if (this._enabled) {
this.onEnabled();
} }
else { else {
this.onDisabled();
} }
} }
return this; return this;
@@ -1887,11 +1903,11 @@ var es;
SceneComponent.prototype.setUpdateOrder = function (updateOrder) { SceneComponent.prototype.setUpdateOrder = function (updateOrder) {
if (this.updateOrder != updateOrder) { if (this.updateOrder != updateOrder) {
this.updateOrder = updateOrder; this.updateOrder = updateOrder;
es.Core.scene._sceneComponents.sort(this.compareTo); es.Core.scene._sceneComponents.sort(this);
} }
return this; return this;
}; };
SceneComponent.prototype.compareTo = function (other) { SceneComponent.prototype.compare = function (other) {
return this.updateOrder - other.updateOrder; return this.updateOrder - other.updateOrder;
}; };
return SceneComponent; return SceneComponent;
@@ -6318,7 +6334,7 @@ var es;
Physics.overlapCircleAll = function (center, randius, results, layerMask) { Physics.overlapCircleAll = function (center, randius, results, layerMask) {
if (layerMask === void 0) { layerMask = -1; } if (layerMask === void 0) { layerMask = -1; }
if (results.length == 0) { if (results.length == 0) {
console.error("An empty results array was passed in. No results will ever be returned."); console.error("传入了一个空的结果数组。不会返回任何结果");
return; return;
} }
return this._spatialHash.overlapCircle(center, randius, results, layerMask); return this._spatialHash.overlapCircle(center, randius, results, layerMask);
@@ -8891,11 +8907,19 @@ var es;
_this.add(value); _this.add(value);
}); });
}; };
/**
* 确定当前集合是否为指定集合或数组的子集
* @param other
*/
Set.prototype.isSubsetOf = function (other) { Set.prototype.isSubsetOf = function (other) {
var _this = this; var _this = this;
var otherBuckets = this.buildInternalBuckets(other); var otherBuckets = this.buildInternalBuckets(other);
return this.toArray().every(function (value) { return _this.bucketsContains(otherBuckets.Buckets, value); }); return this.toArray().every(function (value) { return _this.bucketsContains(otherBuckets.Buckets, value); });
}; };
/**
* 确定当前不可变排序集是否为指定集合的超集
* @param other
*/
Set.prototype.isSupersetOf = function (other) { Set.prototype.isSupersetOf = function (other) {
var _this = this; var _this = this;
return other.every(function (value) { return _this.contains(value); }); return other.every(function (value) { return _this.contains(value); });

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,5 @@
module es { module es {
export class SceneComponent { export class SceneComponent implements IComparer<SceneComponent> {
/** /**
* 这个场景组件被附加到的场景 * 这个场景组件被附加到的场景
*/ */
@@ -60,9 +60,9 @@ module es {
this._enabled = isEnabled; this._enabled = isEnabled;
if (this._enabled){ if (this._enabled){
this.onEnabled();
}else{ }else{
this.onDisabled();
} }
} }
@@ -76,13 +76,13 @@ module es {
public setUpdateOrder(updateOrder: number){ public setUpdateOrder(updateOrder: number){
if (this.updateOrder != updateOrder){ if (this.updateOrder != updateOrder){
this.updateOrder = updateOrder; this.updateOrder = updateOrder;
Core.scene._sceneComponents.sort(this.compareTo); Core.scene._sceneComponents.sort(this);
} }
return this; return this;
} }
public compareTo(other: SceneComponent): number{ public compare(other: SceneComponent): number{
return this.updateOrder - other.updateOrder; return this.updateOrder - other.updateOrder;
} }
} }

View File

@@ -33,7 +33,7 @@ module es {
/** /**
* 全局访问系统 * 全局访问系统
*/ */
public _globalManagers: GlobalManager[] = []; public _globalManagers: FastList<GlobalManager> = new FastList<GlobalManager>();
public _timerManager: TimerManager = new TimerManager(); public _timerManager: TimerManager = new TimerManager();
public width: number; public width: number;
public height: number; public height: number;
@@ -62,6 +62,7 @@ module es {
public _frameCounterElapsedTime: number = 0; public _frameCounterElapsedTime: number = 0;
public _frameCounter: number = 0; public _frameCounter: number = 0;
public _totalMemory: number = 0;
public _scene: Scene; public _scene: Scene;
/** /**
@@ -111,7 +112,7 @@ module es {
* @param manager * @param manager
*/ */
public static registerGlobalManager(manager: es.GlobalManager) { public static registerGlobalManager(manager: es.GlobalManager) {
this._instance._globalManagers.push(manager); this._instance._globalManagers.add(manager);
manager.enabled = true; manager.enabled = true;
} }
@@ -130,8 +131,8 @@ module es {
*/ */
public static getGlobalManager<T extends es.GlobalManager>(type): T { public static getGlobalManager<T extends es.GlobalManager>(type): T {
for (let i = 0; i < this._instance._globalManagers.length; i++) { for (let i = 0; i < this._instance._globalManagers.length; i++) {
if (this._instance._globalManagers[i] instanceof type) if (this._instance._globalManagers.buffer[i] instanceof type)
return this._instance._globalManagers[i] as T; return this._instance._globalManagers.buffer[i] as T;
} }
return null; return null;
} }
@@ -152,6 +153,8 @@ module es {
} }
public async draw() { public async draw() {
this.startDebugDraw();
if (this._sceneTransition != null) { if (this._sceneTransition != null) {
this._sceneTransition.preRender(); this._sceneTransition.preRender();
@@ -177,6 +180,19 @@ module es {
} }
} }
public startDebugDraw() {
this._frameCounter ++;
this._frameCounterElapsedTime += Time.deltaTime;
if (this._frameCounterElapsedTime >= 1) {
let memoryInfo = window.performance["memory"];
if (memoryInfo != null) {
this._totalMemory = Number((memoryInfo.totalJSHeapSize / 1048576).toFixed(2));
}
this._frameCounter = 0;
this._frameCounterElapsedTime -= 1;
}
}
/** /**
* 在一个场景结束后,下一个场景开始之前调用 * 在一个场景结束后,下一个场景开始之前调用
*/ */
@@ -208,16 +224,16 @@ module es {
if (currentTime != null) Time.update(currentTime); if (currentTime != null) Time.update(currentTime);
if (this._scene != null) { if (this._scene != null) {
for (let i = this._globalManagers.length - 1; i >= 0; i--) { for (let i = this._globalManagers.length - 1; i >= 0; i--) {
if (this._globalManagers[i].enabled) if (this._globalManagers.buffer[i].enabled)
this._globalManagers[i].update(); this._globalManagers.buffer[i].update();
} }
// 仔细阅读: // 仔细阅读:
// 当场景转换发生时,我们不更新场景 // 当场景转换发生时,我们不更新场景
// - 除非是不改变场景的SceneTransition(没有理由不更新) // - 除非是不改变场景的SceneTransition(没有理由不更新)
// - 或者是一个已经切换到新场景的SceneTransition新场景需要做它的事情 // - 或者是一个已经切换到新场景的SceneTransition新场景需要做它的事情
if (!this._sceneTransition || if (this._sceneTransition == null ||
(this._sceneTransition && (this._sceneTransition != null &&
(!this._sceneTransition.loadsNewScene || this._sceneTransition.isNewSceneLoaded))) { (!this._sceneTransition.loadsNewScene || this._sceneTransition.isNewSceneLoaded))) {
this._scene.update(); this._scene.update();
} }

View File

@@ -14,7 +14,7 @@ module es {
*/ */
public readonly entityProcessors: EntityProcessorList; public readonly entityProcessors: EntityProcessorList;
public readonly _sceneComponents: SceneComponent[] = []; public readonly _sceneComponents: FastList<SceneComponent> = new FastList<SceneComponent>();
public _renderers: Renderer[] = []; public _renderers: Renderer[] = [];
public _didSceneBegin; public _didSceneBegin;
@@ -76,9 +76,9 @@ module es {
this.entities.removeAllEntities(); this.entities.removeAllEntities();
for (let i = 0; i < this._sceneComponents.length; i++) { for (let i = 0; i < this._sceneComponents.length; i++) {
this._sceneComponents[i].onRemovedFromScene(); this._sceneComponents.buffer[i].onRemovedFromScene();
} }
this._sceneComponents.length = 0; this._sceneComponents.clear();
Physics.clear(); Physics.clear();
@@ -136,8 +136,8 @@ module es {
public addSceneComponent<T extends SceneComponent>(component: T): T { public addSceneComponent<T extends SceneComponent>(component: T): T {
component.scene = this; component.scene = this;
component.onEnabled(); component.onEnabled();
this._sceneComponents.push(component); this._sceneComponents.add(component);
this._sceneComponents.sort(component.compareTo); this._sceneComponents.sort(component);
return component; return component;
} }

View File

@@ -42,7 +42,7 @@ module es {
*/ */
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1) { public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1) {
if (results.length == 0) { if (results.length == 0) {
console.error("An empty results array was passed in. No results will ever be returned."); console.error("传入了一个空的结果数组。不会返回任何结果");
return; return;
} }

View File

@@ -139,12 +139,20 @@ module es {
}); });
} }
/**
* 确定当前集合是否为指定集合或数组的子集
* @param other
*/
isSubsetOf(other: Array<T>) { isSubsetOf(other: Array<T>) {
let otherBuckets = this.buildInternalBuckets(other); let otherBuckets = this.buildInternalBuckets(other);
return this.toArray().every(value => this.bucketsContains(otherBuckets.Buckets, value)); return this.toArray().every(value => this.bucketsContains(otherBuckets.Buckets, value));
} }
/**
* 确定当前不可变排序集是否为指定集合的超集
* @param other
*/
isSupersetOf(other: Array<T>) { isSupersetOf(other: Array<T>) {
return other.every(value => this.contains(value)); return other.every(value => this.contains(value));
} }