新增ServiceContainer服务容器, 所有服务统一实现 IService 接口

This commit is contained in:
YHH
2025-10-10 18:13:28 +08:00
parent 1d2a3e283e
commit 41bbe23404
11 changed files with 702 additions and 33 deletions

View File

@@ -13,6 +13,8 @@ import { TypedQueryBuilder } from './Core/Query/TypedQuery';
import { SceneSerializer, SceneSerializationOptions, SceneDeserializationOptions } from './Serialization/SceneSerializer';
import { IncrementalSerializer, IncrementalSnapshot, IncrementalSerializationOptions } from './Serialization/IncrementalSerializer';
import { ComponentPoolManager } from './Core/ComponentPool';
import { PerformanceMonitor } from '../Utils/PerformanceMonitor';
import { Core } from '../Core';
/**
* 游戏场景默认实现类
@@ -423,8 +425,13 @@ export class Scene implements IScene {
if (this.entityProcessors.processors.includes(processor)) {
return processor;
}
processor.scene = this;
// 从Core获取PerformanceMonitor并注入到System
const perfMonitor = Core.services.resolve(PerformanceMonitor);
processor.setPerformanceMonitor(perfMonitor);
this.entityProcessors.add(processor);
processor.initialize();
processor.setUpdateOrder(this.entityProcessors.count - 1);

View File

@@ -3,6 +3,7 @@ import { ECSFluentAPI, createECSAPI } from './Core/FluentAPI';
import { Time } from '../Utils/Time';
import { Core } from '../Core';
import { createLogger } from '../Utils/Logger';
import type { IService } from '../Core/ServiceContainer';
/**
* 单场景管理器
@@ -46,7 +47,7 @@ import { createLogger } from '../Utils/Logger';
* sceneManager.loadScene(new MenuScene());
* ```
*/
export class SceneManager {
export class SceneManager implements IService {
/**
* 当前活跃场景
*/
@@ -235,4 +236,11 @@ export class SceneManager {
public get hasPendingScene(): boolean {
return this._nextScene !== null;
}
/**
* 释放资源IService接口
*/
public dispose(): void {
this.destroy();
}
}

View File

@@ -68,7 +68,7 @@ export abstract class EntitySystem<
> implements ISystemBase {
private _updateOrder: number;
private _enabled: boolean;
private _performanceMonitor: PerformanceMonitor;
private _performanceMonitor: PerformanceMonitor | null;
private _systemName: string;
private _initialized: boolean;
private _matcher: Matcher;
@@ -148,7 +148,7 @@ export abstract class EntitySystem<
constructor(matcher?: Matcher) {
this._updateOrder = 0;
this._enabled = true;
this._performanceMonitor = PerformanceMonitor.instance;
this._performanceMonitor = null;
this._systemName = getSystemInstanceTypeName(this);
this._initialized = false;
this._matcher = matcher || Matcher.empty();
@@ -192,6 +192,23 @@ export abstract class EntitySystem<
this._scene = value;
}
/**
* 设置性能监控器
*/
public setPerformanceMonitor(monitor: PerformanceMonitor): void {
this._performanceMonitor = monitor;
}
/**
* 获取性能监控器
*/
private getPerformanceMonitor(): PerformanceMonitor {
if (!this._performanceMonitor) {
throw new Error(`${this._systemName}: PerformanceMonitor未注入请确保在Core.create()之后再添加System到Scene`);
}
return this._performanceMonitor;
}
/**
* 获取实体匹配器
*/
@@ -533,7 +550,8 @@ export abstract class EntitySystem<
return;
}
const startTime = this._performanceMonitor.startMonitoring(this._systemName);
const monitor = this.getPerformanceMonitor();
const startTime = monitor.startMonitoring(this._systemName);
let entityCount = 0;
try {
@@ -544,7 +562,7 @@ export abstract class EntitySystem<
this.process(this._entityCache.frame);
} finally {
this._performanceMonitor.endMonitoring(this._systemName, startTime, entityCount);
monitor.endMonitoring(this._systemName, startTime, entityCount);
}
}
@@ -556,7 +574,8 @@ export abstract class EntitySystem<
return;
}
const startTime = this._performanceMonitor.startMonitoring(`${this._systemName}_Late`);
const monitor = this.getPerformanceMonitor();
const startTime = monitor.startMonitoring(`${this._systemName}_Late`);
let entityCount = 0;
try {
@@ -566,7 +585,7 @@ export abstract class EntitySystem<
this.lateProcess(entities);
this.onEnd();
} finally {
this._performanceMonitor.endMonitoring(`${this._systemName}_Late`, startTime, entityCount);
monitor.endMonitoring(`${this._systemName}_Late`, startTime, entityCount);
// 清理帧缓存
this._entityCache.clearFrame();
}
@@ -626,27 +645,27 @@ export abstract class EntitySystem<
/**
* 获取系统的性能数据
*
*
* @returns 性能数据或undefined
*/
public getPerformanceData() {
return this._performanceMonitor.getSystemData(this._systemName);
return this.getPerformanceMonitor().getSystemData(this._systemName);
}
/**
* 获取系统的性能统计
*
*
* @returns 性能统计或undefined
*/
public getPerformanceStats() {
return this._performanceMonitor.getSystemStats(this._systemName);
return this.getPerformanceMonitor().getSystemStats(this._systemName);
}
/**
* 重置系统的性能数据
*/
public resetPerformanceData(): void {
this._performanceMonitor.resetSystem(this._systemName);
this.getPerformanceMonitor().resetSystem(this._systemName);
}
/**