Files
esengine/packages/core/src/Core.ts

520 lines
14 KiB
TypeScript
Raw Normal View History

import { GlobalManager } from './Utils/GlobalManager';
import { TimerManager } from './Utils/Timers/TimerManager';
import { ITimer } from './Utils/Timers/ITimer';
import { Timer } from './Utils/Timers/Timer';
import { Time } from './Utils/Time';
import { PerformanceMonitor } from './Utils/PerformanceMonitor';
import { PoolManager } from './Utils/Pool';
import { ECSFluentAPI, createECSAPI } from './ECS/Core/FluentAPI';
import { Scene } from './ECS/Scene';
import { DebugManager } from './Utils/Debug';
2025-06-30 20:43:11 +08:00
import { ICoreConfig, IECSDebugConfig } from './Types';
import { BigIntFactory, EnvironmentInfo } from './ECS/Utils/BigIntCompatibility';
/**
*
*
*
2025-06-12 09:42:35 +08:00
*
*
* @example
* ```typescript
* // 创建核心实例
* const core = Core.create(true);
*
* // 设置场景
* Core.scene = new MyScene();
*
2025-06-12 09:42:35 +08:00
* // 在游戏循环中更新Laya引擎示例
* Laya.timer.frameLoop(1, this, () => {
* const deltaTime = Laya.timer.delta / 1000;
* Core.update(deltaTime);
* });
*
* // 调度定时器
* Core.schedule(1.0, false, null, (timer) => {
* console.log("1秒后执行");
* });
* ```
*/
export class Core {
/**
*
*
* true时
*/
public static paused = false;
/**
*
*/
private static _instance: Core;
/**
*
*
* ECS实体系统功能
*/
public static entitySystemsEnabled: boolean;
/**
*
*
*
*/
public readonly debug: boolean;
/**
*
*
*
*/
public _nextScene: Scene | null = null;
/**
*
*
*
*/
public _globalManagers: GlobalManager[] = [];
/**
*
*
*
*/
public _timerManager: TimerManager;
2022-03-12 23:49:14 +08:00
/**
*
*
*
*/
public _performanceMonitor: PerformanceMonitor;
/**
*
*
*
*/
public _poolManager: PoolManager;
/**
* ECS流式API
*
* 便ECS操作接口
*/
public _ecsAPI?: ECSFluentAPI;
/**
*
*/
public _scene?: Scene;
2025-06-17 00:32:16 +08:00
/**
*
2025-06-17 00:32:16 +08:00
*
*
*/
public _debugManager?: DebugManager;
2025-06-17 00:32:16 +08:00
/**
* Core配置
*/
private _config: ICoreConfig;
/**
*
*/
private _environmentInfo: EnvironmentInfo;
/**
*
*
2025-06-17 00:32:16 +08:00
* @param config - Core配置对象
*/
2025-06-17 00:32:16 +08:00
private constructor(config: ICoreConfig = {}) {
Core._instance = this;
2025-06-17 00:32:16 +08:00
// 保存配置
this._config = {
debug: true,
enableEntitySystems: true,
...config
};
// 检测环境兼容性
this._environmentInfo = BigIntFactory.getEnvironmentInfo();
// 初始化管理器
this._timerManager = new TimerManager();
Core.registerGlobalManager(this._timerManager);
// 初始化性能监控器
this._performanceMonitor = PerformanceMonitor.instance;
2025-06-17 00:32:16 +08:00
// 在调试模式下启用性能监控
if (this._config.debug) {
this._performanceMonitor.enable();
}
// 初始化对象池管理器
this._poolManager = PoolManager.getInstance();
Core.entitySystemsEnabled = this._config.enableEntitySystems ?? true;
this.debug = this._config.debug ?? true;
2025-06-17 00:32:16 +08:00
// 初始化调试管理器
2025-06-17 00:32:16 +08:00
if (this._config.debugConfig?.enabled) {
this._debugManager = new DebugManager(this, this._config.debugConfig);
2025-06-17 00:32:16 +08:00
}
// 在调试模式下显示兼容性信息
if (this._config.debug) {
this.logCompatibilityInfo();
}
this.initialize();
}
/**
*
*
* @returns
*/
public static get Instance() {
return this._instance;
}
/**
*
*
* @returns null
*/
public static get scene(): Scene | null {
if (!this._instance)
return null;
return this._instance._scene || null;
}
/**
*
*
*
*
* @param value -
* @throws {Error}
*/
public static set scene(value: Scene | null) {
if (!value) return;
if (!value) {
throw new Error("场景不能为空");
}
if (this._instance._scene == null) {
this._instance._scene = value;
this._instance.onSceneChanged();
this._instance._scene.begin();
} else {
this._instance._nextScene = value;
}
}
/**
* Core实例
*
*
*
2025-06-17 00:32:16 +08:00
* @param config - Core配置boolean表示debug模式
* @returns Core实例
*/
2025-06-17 00:32:16 +08:00
public static create(config: ICoreConfig | boolean = true): Core {
if (this._instance == null) {
2025-06-17 00:32:16 +08:00
// 向后兼容如果传入boolean转换为配置对象
const coreConfig: ICoreConfig = typeof config === 'boolean'
? { debug: config, enableEntitySystems: true }
: config;
this._instance = new Core(coreConfig);
}
return this._instance;
}
2025-06-12 09:42:35 +08:00
/**
*
*
*
*
* @param deltaTime -
*
* @example
* ```typescript
* // Laya引擎
* Laya.timer.frameLoop(1, this, () => {
* const deltaTime = Laya.timer.delta / 1000;
* Core.update(deltaTime);
* });
*
* // Cocos Creator
* update(deltaTime: number) {
* Core.update(deltaTime);
* }
*
2025-06-12 09:47:25 +08:00
2025-06-12 09:42:35 +08:00
* ```
*/
public static update(deltaTime: number): void {
if (!this._instance) {
console.warn("Core实例未创建请先调用Core.create()");
return;
}
this._instance.updateInternal(deltaTime);
}
/**
*
*
*
*
* @param manager -
*/
public static registerGlobalManager(manager: GlobalManager) {
this._instance._globalManagers.push(manager);
manager.enabled = true;
}
/**
*
*
*
*
* @param manager -
*/
public static unregisterGlobalManager(manager: GlobalManager) {
this._instance._globalManagers.splice(this._instance._globalManagers.indexOf(manager), 1);
manager.enabled = false;
}
2022-03-12 23:49:14 +08:00
/**
*
*
* @param type -
* @returns null
*/
public static getGlobalManager<T extends GlobalManager>(type: new (...args: unknown[]) => T): T | null {
for (const manager of this._instance._globalManagers) {
if (manager instanceof type)
return manager as T;
}
return null;
}
/**
*
*
*
*
* @param timeInSeconds -
* @param repeats - false
* @param context - null
* @param onTime -
* @returns
*/
public static schedule<TContext = unknown>(timeInSeconds: number, repeats: boolean = false, context: TContext = null as any, onTime: (timer: ITimer<TContext>) => void): Timer<TContext> {
return this._instance._timerManager.schedule(timeInSeconds, repeats, context, onTime);
}
/**
* ECS流式API
*
* @returns ECS API实例null
*/
public static get ecsAPI(): ECSFluentAPI | null {
return this._instance?._ecsAPI || null;
}
2023-03-14 11:22:09 +08:00
2025-06-17 00:32:16 +08:00
/**
*
*
* @param config
*/
public static enableDebug(config: IECSDebugConfig): void {
if (!this._instance) {
console.warn("Core实例未创建请先调用Core.create()");
return;
}
if (this._instance._debugManager) {
this._instance._debugManager.updateConfig(config);
2025-06-17 00:32:16 +08:00
} else {
this._instance._debugManager = new DebugManager(this._instance, config);
2025-06-17 00:32:16 +08:00
}
// 更新Core配置
this._instance._config.debugConfig = config;
}
/**
*
*/
public static disableDebug(): void {
if (!this._instance) return;
if (this._instance._debugManager) {
this._instance._debugManager.stop();
this._instance._debugManager = undefined;
2025-06-17 00:32:16 +08:00
}
// 更新Core配置
if (this._instance._config.debugConfig) {
this._instance._config.debugConfig.enabled = false;
}
}
/**
*
*
* @returns null
*/
public static getDebugData(): any {
if (!this._instance?._debugManager) {
2025-06-17 00:32:16 +08:00
return null;
}
return this._instance._debugManager.getDebugData();
2025-06-17 00:32:16 +08:00
}
/**
*
*
* @returns
*/
public static get isDebugEnabled(): boolean {
return this._instance?._config.debugConfig?.enabled || false;
}
/**
*
*
* @returns
*/
public static getEnvironmentInfo(): EnvironmentInfo | null {
return this._instance?._environmentInfo || null;
}
/**
* BigInt是否支持
*
* @returns BigInt
*/
public static get supportsBigInt(): boolean {
return this._instance?._environmentInfo.supportsBigInt || false;
}
/**
*
*
*
*/
public onSceneChanged() {
Time.sceneChanged();
// 初始化ECS API如果场景支持
if (this._scene && typeof (this._scene as any).querySystem !== 'undefined') {
const scene = this._scene as any;
this._ecsAPI = createECSAPI(scene, scene.querySystem, scene.eventSystem);
}
2025-06-17 00:32:16 +08:00
// 通知调试管理器场景已变更
if (this._debugManager) {
this._debugManager.onSceneChanged();
2025-06-17 00:32:16 +08:00
}
}
/**
*
*
*
*/
protected initialize() {
// 核心系统初始化
}
/**
*
*
*
*/
private logCompatibilityInfo(): void {
const info = this._environmentInfo;
console.log('ECS Framework 兼容性检测结果:');
console.log(` 环境: ${info.environment}`);
console.log(` JavaScript引擎: ${info.jsEngine}`);
console.log(` BigInt支持: ${info.supportsBigInt ? '支持' : '不支持'}`);
if (!info.supportsBigInt) {
console.warn('BigInt兼容模式已启用');
}
}
/**
2025-06-12 09:42:35 +08:00
*
*
2025-06-12 09:42:35 +08:00
* @param deltaTime -
*/
2025-06-12 09:42:35 +08:00
private updateInternal(deltaTime: number): void {
if (Core.paused) return;
// 开始性能监控
const frameStartTime = this._performanceMonitor.startMonitoring('Core.update');
2025-06-12 09:42:35 +08:00
// 更新时间系统
Time.update(deltaTime);
2021-04-20 15:46:18 +08:00
// 更新FPS监控如果性能监控器支持
if (typeof (this._performanceMonitor as any).updateFPS === 'function') {
(this._performanceMonitor as any).updateFPS(Time.deltaTime);
}
2023-03-14 14:03:41 +08:00
// 更新全局管理器
const managersStartTime = this._performanceMonitor.startMonitoring('GlobalManagers.update');
for (const globalManager of this._globalManagers) {
if (globalManager.enabled)
globalManager.update();
}
this._performanceMonitor.endMonitoring('GlobalManagers.update', managersStartTime, this._globalManagers.length);
// 更新对象池管理器
this._poolManager.update();
// 处理场景切换
if (this._nextScene != null) {
if (this._scene != null)
2023-03-14 14:03:41 +08:00
this._scene.end();
this._scene = this._nextScene;
this._nextScene = null;
this.onSceneChanged();
this._scene.begin();
2022-03-12 23:49:14 +08:00
}
// 更新当前场景
if (this._scene != null && this._scene.update) {
const sceneStartTime = this._performanceMonitor.startMonitoring('Scene.update');
this._scene.update();
const entityCount = (this._scene as any).entities?.count || 0;
this._performanceMonitor.endMonitoring('Scene.update', sceneStartTime, entityCount);
}
// 更新调试管理器基于FPS的数据发送
if (this._debugManager) {
this._debugManager.onFrameUpdate(deltaTime);
}
// 结束性能监控
this._performanceMonitor.endMonitoring('Core.update', frameStartTime);
}
}