新增 _graphicsDeviceChangeTimer 用于凝聚GraphicsDeviceReset事件

This commit is contained in:
yhh
2020-11-26 10:39:32 +08:00
parent 918d998f4c
commit 1bb1b8704b
6 changed files with 110 additions and 65 deletions

View File

@@ -39,15 +39,18 @@ declare module es {
declare module es { declare module es {
class Core { class Core {
static emitter: Emitter<CoreEvents>; static emitter: Emitter<CoreEvents>;
static pauseOnFocusLost: boolean;
static debugRenderEndabled: boolean; static debugRenderEndabled: boolean;
static _instance: Core; static _instance: Core;
static entitySystemsEnabled: boolean;
_nextScene: Scene; _nextScene: Scene;
_sceneTransition: SceneTransition; _sceneTransition: SceneTransition;
_graphicsDeviceChangeTimer: ITimer;
_globalManagers: GlobalManager[]; _globalManagers: GlobalManager[];
_timerManager: TimerManager; _timerManager: TimerManager;
width: number; width: number;
height: number; height: number;
constructor(width: number, height: number); constructor(width: number, height: number, enableEntitySystems?: boolean);
static readonly Instance: Core; static readonly Instance: Core;
_frameCounterElapsedTime: number; _frameCounterElapsedTime: number;
_frameCounter: number; _frameCounter: number;

View File

@@ -349,7 +349,8 @@ var es;
var es; var es;
(function (es) { (function (es) {
var Core = (function () { var Core = (function () {
function Core(width, height) { function Core(width, height, enableEntitySystems) {
if (enableEntitySystems === void 0) { enableEntitySystems = true; }
this._globalManagers = []; this._globalManagers = [];
this._timerManager = new es.TimerManager(); this._timerManager = new es.TimerManager();
this._frameCounterElapsedTime = 0; this._frameCounterElapsedTime = 0;
@@ -360,6 +361,7 @@ var es;
Core.emitter = new es.Emitter(); Core.emitter = new es.Emitter();
Core.emitter.addObserver(es.CoreEvents.FrameUpdated, this.update, this); Core.emitter.addObserver(es.CoreEvents.FrameUpdated, this.update, this);
Core.registerGlobalManager(this._timerManager); Core.registerGlobalManager(this._timerManager);
Core.entitySystemsEnabled = enableEntitySystems;
this.initialize(); this.initialize();
} }
Object.defineProperty(Core, "Instance", { Object.defineProperty(Core, "Instance", {
@@ -428,9 +430,10 @@ var es;
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
if (!this._sceneTransition) return [3, 4]; if (!(this._sceneTransition != null)) return [3, 5];
this._sceneTransition.preRender(); this._sceneTransition.preRender();
if (!(this._scene && !this._sceneTransition.hasPreviousSceneRender)) return [3, 2]; if (!(this._sceneTransition != null)) return [3, 4];
if (!(this._scene != null && !this._sceneTransition.hasPreviousSceneRender)) return [3, 2];
this._scene.render(); this._scene.render();
this._scene.postRender(); this._scene.postRender();
return [4, this._sceneTransition.onBeginTransition()]; return [4, this._sceneTransition.onBeginTransition()];
@@ -438,22 +441,22 @@ var es;
_a.sent(); _a.sent();
return [3, 3]; return [3, 3];
case 2: case 2:
if (this._sceneTransition) { if (this._scene != null && this._sceneTransition.isNewSceneLoaded) {
if (this._scene && this._sceneTransition.isNewSceneLoaded) { this._scene.render();
this._scene.render(); this._scene.postRender();
this._scene.postRender();
}
this._sceneTransition.render();
} }
_a.label = 3; _a.label = 3;
case 3: return [3, 5]; case 3:
case 4: this._sceneTransition.render();
_a.label = 4;
case 4: return [3, 6];
case 5:
if (this._scene) { if (this._scene) {
this._scene.render(); this._scene.render();
this._scene.postRender(); this._scene.postRender();
} }
_a.label = 5; _a.label = 6;
case 5: return [2]; case 6: return [2];
} }
}); });
}); });
@@ -463,7 +466,15 @@ var es;
es.Time.sceneChanged(); es.Time.sceneChanged();
}; };
Core.prototype.onGraphicsDeviceReset = function () { Core.prototype.onGraphicsDeviceReset = function () {
Core.emitter.emit(es.CoreEvents.GraphicsDeviceReset); if (this._graphicsDeviceChangeTimer != null) {
this._graphicsDeviceChangeTimer.reset();
}
else {
this._graphicsDeviceChangeTimer = Core.schedule(0.05, false, this, function (t) {
t.context._graphicsDeviceChangeTimer = null;
Core.emitter.emit(es.CoreEvents.GraphicsDeviceReset);
});
}
}; };
Core.prototype.initialize = function () { Core.prototype.initialize = function () {
}; };
@@ -473,32 +484,33 @@ var es;
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: case 0:
if (!this._scene) return [3, 2]; 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[i].enabled)
this._globalManagers[i].update(); this._globalManagers[i].update();
}
if (!this._sceneTransition ||
(this._sceneTransition &&
(!this._sceneTransition.loadsNewScene || this._sceneTransition.isNewSceneLoaded))) {
this._scene.update();
}
if (this._nextScene != null) {
this._scene.end();
this._scene = this._nextScene;
this._nextScene = null;
this.onSceneChanged();
this._scene.begin();
}
} }
if (!this._sceneTransition || return [4, this.draw()];
(this._sceneTransition && (!this._sceneTransition.loadsNewScene || this._sceneTransition.isNewSceneLoaded))) {
this._scene.update();
}
if (!this._nextScene) return [3, 2];
this._scene.end();
this._scene = this._nextScene;
this._nextScene = null;
this.onSceneChanged();
return [4, this._scene.begin()];
case 1: case 1:
_a.sent();
_a.label = 2;
case 2: return [4, this.draw()];
case 3:
_a.sent(); _a.sent();
return [2]; return [2];
} }
}); });
}); });
}; };
Core.pauseOnFocusLost = true;
Core.debugRenderEndabled = false; Core.debugRenderEndabled = false;
return Core; return Core;
}()); }());
@@ -829,7 +841,7 @@ var es;
Scene.prototype.begin = function () { Scene.prototype.begin = function () {
es.Physics.reset(); es.Physics.reset();
this.updateResolutionScaler(); this.updateResolutionScaler();
if (this.entityProcessors) if (this.entityProcessors != null)
this.entityProcessors.begin(); this.entityProcessors.begin();
es.Core.emitter.addObserver(es.CoreEvents.GraphicsDeviceReset, this.updateResolutionScaler, this); es.Core.emitter.addObserver(es.CoreEvents.GraphicsDeviceReset, this.updateResolutionScaler, this);
es.Core.emitter.addObserver(es.CoreEvents.OrientationChanged, this.updateResolutionScaler, this); es.Core.emitter.addObserver(es.CoreEvents.OrientationChanged, this.updateResolutionScaler, this);
@@ -838,16 +850,17 @@ var es;
}; };
Scene.prototype.end = function () { Scene.prototype.end = function () {
this._didSceneBegin = false; this._didSceneBegin = false;
es.Core.emitter.removeObserver(es.CoreEvents.GraphicsDeviceReset, this.updateResolutionScaler);
es.Core.emitter.removeObserver(es.CoreEvents.OrientationChanged, this.updateResolutionScaler);
for (var i = 0; i < this._renderers.length; i++) { for (var i = 0; i < this._renderers.length; i++) {
this._renderers[i].unload(); this._renderers[i].unload();
} }
es.Core.emitter.removeObserver(es.CoreEvents.GraphicsDeviceReset, 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[i].onRemovedFromScene();
} }
this._sceneComponents.length = 0; this._sceneComponents.length = 0;
es.Physics.clear();
if (this.entityProcessors) if (this.entityProcessors)
this.entityProcessors.end(); this.entityProcessors.end();
this.unload(); this.unload();
@@ -860,10 +873,10 @@ var es;
if (this._sceneComponents[i].enabled) if (this._sceneComponents[i].enabled)
this._sceneComponents[i].update(); this._sceneComponents[i].update();
} }
if (this.entityProcessors) if (this.entityProcessors != null)
this.entityProcessors.update(); this.entityProcessors.update();
this.entities.update(); this.entities.update();
if (this.entityProcessors) if (this.entityProcessors != null)
this.entityProcessors.lateUpdate(); this.entityProcessors.lateUpdate();
this.renderableComponents.updateList(); this.renderableComponents.updateList();
}; };
@@ -4708,6 +4721,7 @@ var es;
} }
Physics.reset = function () { Physics.reset = function () {
this._spatialHash = new es.SpatialHash(this.spatialHashCellSize); this._spatialHash = new es.SpatialHash(this.spatialHashCellSize);
this._hitArray[0].reset();
}; };
Physics.clear = function () { Physics.clear = function () {
this._spatialHash.clear(); this._spatialHash.clear();

File diff suppressed because one or more lines are too long

View File

@@ -7,6 +7,10 @@ module es {
* 核心发射器。只发出核心级别的事件 * 核心发射器。只发出核心级别的事件
*/ */
public static emitter: Emitter<CoreEvents>; public static emitter: Emitter<CoreEvents>;
/**
* 启用/禁用焦点丢失时的暂停。如果为真,则不调用更新或渲染方法
*/
public static pauseOnFocusLost = true;
/** /**
* 是否启用调试渲染 * 是否启用调试渲染
*/ */
@@ -15,8 +19,17 @@ module es {
* 简化对内部类的全局内容实例的访问 * 简化对内部类的全局内容实例的访问
*/ */
public static _instance: Core; public static _instance: Core;
/**
* 用于确定是否应该使用EntitySystems
*/
public static entitySystemsEnabled: boolean;
public _nextScene: Scene; public _nextScene: Scene;
public _sceneTransition: SceneTransition; public _sceneTransition: SceneTransition;
/**
* 用于凝聚GraphicsDeviceReset事件
*/
public _graphicsDeviceChangeTimer: ITimer;
/** /**
* 全局访问系统 * 全局访问系统
*/ */
@@ -25,7 +38,7 @@ module es {
public width: number; public width: number;
public height: number; public height: number;
constructor(width: number, height: number) { constructor(width: number, height: number, enableEntitySystems: boolean = true) {
this.width = width; this.width = width;
this.height = height; this.height = height;
@@ -34,6 +47,7 @@ module es {
Core.emitter.addObserver(CoreEvents.FrameUpdated, this.update, this); Core.emitter.addObserver(CoreEvents.FrameUpdated, this.update, this);
Core.registerGlobalManager(this._timerManager); Core.registerGlobalManager(this._timerManager);
Core.entitySystemsEnabled = enableEntitySystems;
this.initialize(); this.initialize();
} }
@@ -138,16 +152,17 @@ module es {
} }
public async draw() { public async draw() {
if (this._sceneTransition) { if (this._sceneTransition != null) {
this._sceneTransition.preRender(); this._sceneTransition.preRender();
// 如果我们有场景转换的特殊处理。我们要么渲染场景过渡,要么渲染场景 // 如果有的话我们会对SceneTransition进行特殊处理。
if (this._scene && !this._sceneTransition.hasPreviousSceneRender) { // 我们要么渲染SceneTransition要么渲染Scene的
this._scene.render(); if (this._sceneTransition != null){
this._scene.postRender(); if (this._scene != null && !this._sceneTransition.hasPreviousSceneRender) {
await this._sceneTransition.onBeginTransition(); this._scene.render();
} else if (this._sceneTransition) { this._scene.postRender();
if (this._scene && this._sceneTransition.isNewSceneLoaded) { await this._sceneTransition.onBeginTransition();
} else if (this._scene != null && this._sceneTransition.isNewSceneLoaded) {
this._scene.render(); this._scene.render();
this._scene.postRender(); this._scene.postRender();
} }
@@ -157,7 +172,7 @@ module es {
} else if (this._scene) { } else if (this._scene) {
this._scene.render(); this._scene.render();
// 如果我们没有一个活跃的场景转换,就像常一样渲染 // 如果我们没有一个活动的SceneTransition,就像常一样渲染
this._scene.postRender(); this._scene.postRender();
} }
} }
@@ -174,36 +189,46 @@ module es {
* 当屏幕大小发生改变时调用 * 当屏幕大小发生改变时调用
*/ */
protected onGraphicsDeviceReset() { protected onGraphicsDeviceReset() {
Core.emitter.emit(CoreEvents.GraphicsDeviceReset); // 我们用这些来避免垃圾事件的发生
if (this._graphicsDeviceChangeTimer != null){
this._graphicsDeviceChangeTimer.reset();
} else {
this._graphicsDeviceChangeTimer = Core.schedule(0.05, false, this, t => {
(t.context as Core)._graphicsDeviceChangeTimer = null;
Core.emitter.emit(CoreEvents.GraphicsDeviceReset);
});
}
} }
protected initialize() { protected initialize() {
} }
protected async update() { protected async update() {
if (this._scene) { 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[i].enabled)
this._globalManagers[i].update(); this._globalManagers[i].update();
} }
// 仔细阅读: // 仔细阅读:
// 当场景转换发生时,我们不更新场景 // 当场景转换发生时,我们不更新场景
// -除非是不改变场景的场景转换(没有理由不更新) // - 除非是不改变场景的SceneTransition(没有理由不更新)
// -或者是一个已经切换到新场景的场景转换(新场景需要做它自己的事情) // - 或者是一个已经切换到新场景的SceneTransition新场景需要做它的事情
if (!this._sceneTransition || if (!this._sceneTransition ||
(this._sceneTransition && (!this._sceneTransition.loadsNewScene || this._sceneTransition.isNewSceneLoaded))) { (this._sceneTransition &&
(!this._sceneTransition.loadsNewScene || this._sceneTransition.isNewSceneLoaded))) {
this._scene.update(); this._scene.update();
} }
if (this._nextScene) { if (this._nextScene != null) {
this._scene.end(); this._scene.end();
this._scene = this._nextScene; this._scene = this._nextScene;
this._nextScene = null; this._nextScene = null;
this.onSceneChanged(); this.onSceneChanged();
await this._scene.begin(); this._scene.begin();
} }
} }

View File

@@ -62,7 +62,7 @@ module es {
Physics.reset(); Physics.reset();
this.updateResolutionScaler(); this.updateResolutionScaler();
if (this.entityProcessors) if (this.entityProcessors != null)
this.entityProcessors.begin(); this.entityProcessors.begin();
Core.emitter.addObserver(CoreEvents.GraphicsDeviceReset,this.updateResolutionScaler, this); Core.emitter.addObserver(CoreEvents.GraphicsDeviceReset,this.updateResolutionScaler, this);
@@ -76,13 +76,13 @@ module es {
public end() { public end() {
this._didSceneBegin = false; this._didSceneBegin = false;
Core.emitter.removeObserver(CoreEvents.GraphicsDeviceReset, this.updateResolutionScaler);
Core.emitter.removeObserver(CoreEvents.OrientationChanged, this.updateResolutionScaler);
for (let i = 0; i < this._renderers.length; i++) { for (let i = 0; i < this._renderers.length; i++) {
this._renderers[i].unload(); this._renderers[i].unload();
} }
Core.emitter.removeObserver(CoreEvents.GraphicsDeviceReset, this.updateResolutionScaler);
Core.emitter.removeObserver(CoreEvents.OrientationChanged, this.updateResolutionScaler);
this.entities.removeAllEntities(); this.entities.removeAllEntities();
for (let i = 0; i < this._sceneComponents.length; i++) { for (let i = 0; i < this._sceneComponents.length; i++) {
@@ -90,6 +90,8 @@ module es {
} }
this._sceneComponents.length = 0; this._sceneComponents.length = 0;
Physics.clear();
if (this.entityProcessors) if (this.entityProcessors)
this.entityProcessors.end(); this.entityProcessors.end();
@@ -110,16 +112,16 @@ module es {
} }
// 更新我们的实体解析器 // 更新我们的实体解析器
if (this.entityProcessors) if (this.entityProcessors != null)
this.entityProcessors.update(); this.entityProcessors.update();
// 更新我们的实体组 // 更新我们的实体组
this.entities.update(); this.entities.update();
if (this.entityProcessors) if (this.entityProcessors != null)
this.entityProcessors.lateUpdate(); this.entityProcessors.lateUpdate();
// 我们在实体之后更新我们的呈现。如果添加了任何新的渲染,请进行更新 // 我们在entity.update之后更新我们的renderables以防止任何新的Renderables被添加
this.renderableComponents.updateList(); this.renderableComponents.updateList();
} }

View File

@@ -23,6 +23,7 @@ module es {
public static reset() { public static reset() {
this._spatialHash = new SpatialHash(this.spatialHashCellSize); this._spatialHash = new SpatialHash(this.spatialHashCellSize);
this._hitArray[0].reset();
} }
/** /**