Files
esengine/source/src/ECS/Core.ts

207 lines
6.8 KiB
TypeScript
Raw Normal View History

module es {
/**
*
*/
export class Core {
/**
*
*/
public static emitter: Emitter<CoreEvents>;
/**
* /
*/
public static pauseOnFocusLost = true;
2020-08-13 17:39:24 +08:00
/**
*
*/
public static debugRenderEndabled = false;
/**
* 访
*/
public static _instance: Core;
/**
* 使EntitySystems
*/
public static entitySystemsEnabled: boolean;
public _nextScene: Scene;
/**
* GraphicsDeviceReset事件
*/
public _graphicsDeviceChangeTimer: ITimer;
/**
* 访
*/
public _globalManagers: FastList<GlobalManager> = new FastList<GlobalManager>();
public _timerManager: TimerManager = new TimerManager();
public width: number;
public height: number;
constructor(width: number, height: number, enableEntitySystems: boolean = true) {
this.width = width;
this.height = height;
2020-07-28 16:25:20 +08:00
Core._instance = this;
Core.emitter = new Emitter<CoreEvents>();
Core.emitter.addObserver(CoreEvents.FrameUpdated, this.update, this);
Core.registerGlobalManager(this._timerManager);
Core.entitySystemsEnabled = enableEntitySystems;
this.initialize();
2020-07-28 16:25:20 +08:00
}
/**
* /访
* @constructor
*/
public static get Instance() {
return this._instance;
}
public _frameCounterElapsedTime: number = 0;
public _frameCounter: number = 0;
public _totalMemory: number = 0;
public _titleMemory: (totalMemory: number, frameCounter: number) => void;
2020-07-28 16:25:20 +08:00
public _scene: Scene;
/**
*
*/
public static get scene() {
2020-07-23 19:28:01 +08:00
if (!this._instance)
return null;
return this._instance._scene;
}
/**
*
* @param value
*/
public static set scene(value: Scene) {
2020-07-28 16:25:20 +08:00
if (!value) {
console.error("场景不能为空");
return;
}
if (this._instance._scene == null) {
this._instance._scene = value;
2020-12-10 16:15:19 +08:00
this._instance.onSceneChanged();
this._instance._scene.begin();
} else {
this._instance._nextScene = value;
}
}
/**
2020-07-28 16:25:20 +08:00
*
* @param manager
*/
2020-07-28 16:25:20 +08:00
public static registerGlobalManager(manager: es.GlobalManager) {
this._instance._globalManagers.add(manager);
2020-07-28 16:25:20 +08:00
manager.enabled = true;
}
2020-07-28 16:25:20 +08:00
/**
*
* @param manager
*/
public static unregisterGlobalManager(manager: es.GlobalManager) {
this._instance._globalManagers.remove(manager);
manager.enabled = false;
}
2020-07-28 16:25:20 +08:00
/**
* T的全局管理器
* @param type
*/
public static getGlobalManager<T extends es.GlobalManager>(type): T {
for (let i = 0; i < this._instance._globalManagers.length; i++) {
if (this._instance._globalManagers.buffer[i] instanceof type)
return this._instance._globalManagers.buffer[i] as T;
}
2020-07-28 16:25:20 +08:00
return null;
}
/**
*
* @param timeInSeconds
* @param repeats
* @param context
* @param onTime
*/
public static schedule(timeInSeconds: number, repeats: boolean = false, context: any = null, onTime: (timer: ITimer)=>void){
return this._instance._timerManager.schedule(timeInSeconds, repeats, context, onTime);
}
2020-07-28 16:25:20 +08:00
public onOrientationChanged() {
Core.emitter.emit(CoreEvents.OrientationChanged);
}
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));
}
if (this._titleMemory) this._titleMemory(this._totalMemory, this._frameCounter);
this._frameCounter = 0;
this._frameCounterElapsedTime -= 1;
}
}
/**
*
*/
2020-07-28 16:25:20 +08:00
public onSceneChanged() {
Core.emitter.emit(CoreEvents.SceneChanged);
Time.sceneChanged();
}
/**
2020-07-28 16:25:20 +08:00
*
*/
2020-07-28 16:25:20 +08:00
protected onGraphicsDeviceReset() {
// 我们用这些来避免垃圾事件的发生
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);
});
}
}
2020-07-28 16:25:20 +08:00
protected initialize() {
}
2020-11-26 17:26:49 +08:00
protected async update(currentTime?: number) {
if (currentTime != null) Time.update(currentTime);
if (this._scene != null) {
2020-07-28 16:25:20 +08:00
for (let i = this._globalManagers.length - 1; i >= 0; i--) {
if (this._globalManagers.buffer[i].enabled)
this._globalManagers.buffer[i].update();
2020-07-28 16:25:20 +08:00
}
2020-11-30 18:23:03 +08:00
this._scene.update();
2020-07-28 16:25:20 +08:00
if (this._nextScene != null) {
2020-07-28 16:25:20 +08:00
this._scene.end();
this._scene = this._nextScene;
this._nextScene = null;
this.onSceneChanged();
this._scene.begin();
2020-07-28 16:25:20 +08:00
}
}
2020-07-28 16:25:20 +08:00
2020-11-30 18:23:03 +08:00
this.startDebugDraw();
2020-07-28 16:25:20 +08:00
}
}
}