Files
esengine/source/src/Core.ts

247 lines
8.7 KiB
TypeScript
Raw Normal View History

module es {
/**
*
*/
export class Core {
/**
*
*/
public static emitter: Emitter<CoreEvents>;
2021-04-20 15:46:18 +08:00
public static paused = false;
/**
*
*/
public static debugRenderEndabled = false;
/**
* 访
*/
private static _instance: Core;
/**
* 使EntitySystems
*/
public static entitySystemsEnabled: boolean;
/**
* debug模式
* create时进行更改
*/
public readonly debug: boolean;
public _nextScene: Scene;
2022-03-12 23:49:14 +08:00
public _sceneTransition: SceneTransition;
/**
* GraphicsDeviceReset事件
*/
public _graphicsDeviceChangeTimer: ITimer;
/**
* 访
*/
public _globalManagers: GlobalManager[] = [];
public _coroutineManager: CoroutineManager = new CoroutineManager();
public _timerManager: TimerManager = new TimerManager();
2021-06-22 14:41:21 +08:00
private constructor(debug: boolean = true, enableEntitySystems: boolean = true) {
Core._instance = this;
Core.emitter = new Emitter<CoreEvents>();
Core.emitter.addObserver(CoreEvents.frameUpdated, this.update, this);
Core.registerGlobalManager(this._coroutineManager);
2021-07-03 12:27:21 +08:00
Core.registerGlobalManager(new TweenManager());
Core.registerGlobalManager(this._timerManager);
Core.entitySystemsEnabled = enableEntitySystems;
this.debug = debug;
this.initialize();
}
/**
* /访
* @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;
public _scene: Scene;
/**
*
*/
public static get scene() {
if (!this._instance)
return null;
return this._instance._scene;
}
/**
*
* @param value
*/
public static set scene(value: Scene) {
2021-01-18 19:54:41 +08:00
Insist.isNotNull(value, "场景不能为空");
if (this._instance._scene == null) {
this._instance._scene = value;
this._instance.onSceneChanged();
this._instance._scene.begin();
} else {
this._instance._nextScene = value;
}
}
/**
*
*/
public static create(debug: boolean = true): Core {
if (this._instance == null) {
this._instance = new es.Core(debug);
}
return this._instance;
}
/**
*
* @param manager
*/
public static registerGlobalManager(manager: es.GlobalManager) {
this._instance._globalManagers.push(manager);
manager.enabled = true;
}
/**
*
* @param manager
*/
2023-03-14 11:22:09 +08:00
public static unregisterGlobalManager(manager: GlobalManager) {
2021-03-29 15:28:18 +08:00
new es.List(this._instance._globalManagers).remove(manager);
manager.enabled = false;
}
/**
2023-03-14 11:22:09 +08:00
*
* @param type
* @returns null
*/
2023-03-14 11:22:09 +08:00
public static getGlobalManager<T extends GlobalManager>(type: new (...args) => T): T {
for (let i = 0, s = Core._instance._globalManagers.length; i < s; ++i) {
2021-05-24 17:20:27 +08:00
let manager = Core._instance._globalManagers[i];
if (manager instanceof type)
return manager;
}
return null;
}
2022-03-12 23:49:14 +08:00
/**
* SceneTransition
* @param sceneTransition
*/
public static startSceneTransition<T extends SceneTransition>(sceneTransition: T) {
Insist.isNull(this._instance._sceneTransition, "在前一个场景转换完成之前,无法启动新的场景转换");
this._instance._sceneTransition = sceneTransition;
return sceneTransition;
}
/**
2023-03-14 11:22:09 +08:00
* coroutineCoroutine可以将number延时几秒或延时到其他startCoroutine.Yielding
* null将使coroutine在下一帧被执行
2023-03-14 11:22:09 +08:00
* @param enumerator
*/
public static startCoroutine(enumerator): ICoroutine {
return this._instance._coroutineManager.startCoroutine(enumerator);
}
/**
*
* @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);
}
public startDebugDraw() {
// 如果debug标志未开启则直接返回
if (!this.debug) return;
2023-03-14 11:22:09 +08:00
// 计算帧率和内存使用情况
this._frameCounter++; // 帧计数器递增
this._frameCounterElapsedTime += Time.deltaTime; // 帧计数器累加时间
if (this._frameCounterElapsedTime >= 1) { // 如果时间已经超过1秒则计算帧率和内存使用情况
let memoryInfo = window.performance["memory"]; // 获取内存使用情况
if (memoryInfo != null) { // 如果内存使用情况存在
// 计算内存使用情况并保留2位小数
this._totalMemory = Number((memoryInfo.totalJSHeapSize / 1048576).toFixed(2));
}
if (this._titleMemory) { // 如果回调函数存在,则执行回调函数,更新标题栏显示
this._titleMemory(this._totalMemory, this._frameCounter);
}
this._frameCounter = 0; // 重置帧计数器
this._frameCounterElapsedTime -= 1; // 减去1秒时间
}
}
/**
*
*/
public onSceneChanged() {
2022-03-12 23:49:14 +08:00
Core.emitter.emit(CoreEvents.sceneChanged);
Time.sceneChanged();
}
protected initialize() {
}
2022-03-07 22:52:51 +08:00
protected update(currentTime: number = -1) {
2021-04-20 15:46:18 +08:00
if (Core.paused) {
return;
}
Time.update(currentTime, currentTime != -1);
if (this._scene != null) {
for (let i = this._globalManagers.length - 1; i >= 0; i--) {
if (this._globalManagers[i].enabled)
this._globalManagers[i].update();
}
2022-03-12 23:49:14 +08:00
if (this._sceneTransition == null ||
(this._sceneTransition != null &&
(!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();
}
}
this.startDebugDraw();
2022-03-12 23:49:14 +08:00
this.draw();
}
protected draw() {
if (this._sceneTransition != null)
this._sceneTransition.preRender();
if (this._sceneTransition != null && !this._sceneTransition.hasPreviousSceneRender) {
if (this._scene != null) {
Core.startCoroutine(this._sceneTransition.onBeginTransition());
}
this._sceneTransition.render();
}
}
}
}