完善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

@@ -1,5 +1,5 @@
module es {
export class SceneComponent {
export class SceneComponent implements IComparer<SceneComponent> {
/**
* 这个场景组件被附加到的场景
*/
@@ -60,9 +60,9 @@ module es {
this._enabled = isEnabled;
if (this._enabled){
this.onEnabled();
}else{
this.onDisabled();
}
}
@@ -76,13 +76,13 @@ module es {
public setUpdateOrder(updateOrder: number){
if (this.updateOrder != updateOrder){
this.updateOrder = updateOrder;
Core.scene._sceneComponents.sort(this.compareTo);
Core.scene._sceneComponents.sort(this);
}
return this;
}
public compareTo(other: SceneComponent): number{
public compare(other: SceneComponent): number{
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 width: number;
public height: number;
@@ -62,6 +62,7 @@ module es {
public _frameCounterElapsedTime: number = 0;
public _frameCounter: number = 0;
public _totalMemory: number = 0;
public _scene: Scene;
/**
@@ -111,7 +112,7 @@ module es {
* @param manager
*/
public static registerGlobalManager(manager: es.GlobalManager) {
this._instance._globalManagers.push(manager);
this._instance._globalManagers.add(manager);
manager.enabled = true;
}
@@ -130,8 +131,8 @@ module es {
*/
public static getGlobalManager<T extends es.GlobalManager>(type): T {
for (let i = 0; i < this._instance._globalManagers.length; i++) {
if (this._instance._globalManagers[i] instanceof type)
return this._instance._globalManagers[i] as T;
if (this._instance._globalManagers.buffer[i] instanceof type)
return this._instance._globalManagers.buffer[i] as T;
}
return null;
}
@@ -152,6 +153,8 @@ module es {
}
public async draw() {
this.startDebugDraw();
if (this._sceneTransition != null) {
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 (this._scene != null) {
for (let i = this._globalManagers.length - 1; i >= 0; i--) {
if (this._globalManagers[i].enabled)
this._globalManagers[i].update();
if (this._globalManagers.buffer[i].enabled)
this._globalManagers.buffer[i].update();
}
// 仔细阅读:
// 当场景转换发生时,我们不更新场景
// - 除非是不改变场景的SceneTransition(没有理由不更新)
// - 或者是一个已经切换到新场景的SceneTransition新场景需要做它的事情
if (!this._sceneTransition ||
(this._sceneTransition &&
if (this._sceneTransition == null ||
(this._sceneTransition != null &&
(!this._sceneTransition.loadsNewScene || this._sceneTransition.isNewSceneLoaded))) {
this._scene.update();
}

View File

@@ -14,7 +14,7 @@ module es {
*/
public readonly entityProcessors: EntityProcessorList;
public readonly _sceneComponents: SceneComponent[] = [];
public readonly _sceneComponents: FastList<SceneComponent> = new FastList<SceneComponent>();
public _renderers: Renderer[] = [];
public _didSceneBegin;
@@ -76,9 +76,9 @@ module es {
this.entities.removeAllEntities();
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();
@@ -136,8 +136,8 @@ module es {
public addSceneComponent<T extends SceneComponent>(component: T): T {
component.scene = this;
component.onEnabled();
this._sceneComponents.push(component);
this._sceneComponents.sort(component.compareTo);
this._sceneComponents.add(component);
this._sceneComponents.sort(component);
return component;
}