* 在场景中添加一个EntitySystem处理器 * * 支持两种使用方式: * 1. 传入类型(推荐):自动使用DI创建实例,支持@Injectable和@InjectProperty装饰器 * 2. 传入实例:直接使用提供的实例 * * @param systemTypeOrInstance 系统类型或系统实例 * @returns 添加的处理器实例 * * @example * ```typescript * // 方式1:传入类型,自动DI(推荐) * @Injec
(systemTypeOrInstance: ServiceType<T> | T)
| 1079 | * ``` |
| 1080 | */ |
| 1081 | public addEntityProcessor<T extends EntitySystem>(systemTypeOrInstance: ServiceType<T> | T): T { |
| 1082 | let system: T; |
| 1083 | let constructor: ServiceType<T>; |
| 1084 | |
| 1085 | if (typeof systemTypeOrInstance === 'function') { |
| 1086 | constructor = systemTypeOrInstance; |
| 1087 | |
| 1088 | if (this._services.isRegistered(constructor)) { |
| 1089 | const existingSystem = this._services.resolve(constructor) as T; |
| 1090 | this._logger.debug(`System ${constructor.name} already registered, returning existing instance`); |
| 1091 | return existingSystem; |
| 1092 | } |
| 1093 | |
| 1094 | if (isInjectable(constructor)) { |
| 1095 | system = createInstance(constructor, this._services) as T; |
| 1096 | } else { |
| 1097 | system = new constructor() as T; |
| 1098 | } |
| 1099 | } else { |
| 1100 | system = systemTypeOrInstance; |
| 1101 | constructor = system.constructor as ServiceType<T>; |
| 1102 | |
| 1103 | if (this._services.isRegistered(constructor)) { |
| 1104 | const existingSystem = this._services.resolve(constructor); |
| 1105 | if (existingSystem === system) { |
| 1106 | this._logger.debug(`System ${constructor.name} instance already registered, returning it`); |
| 1107 | return system; |
| 1108 | } else { |
| 1109 | this._logger.warn( |
| 1110 | `Attempting to register a different instance of ${constructor.name}, ` + |
| 1111 | 'but type is already registered. Returning existing instance.' |
| 1112 | ); |
| 1113 | return existingSystem as T; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | system.scene = this; |
| 1119 | system.addOrder = this._systemAddCounter++; // for stable sorting |
| 1120 | system.setPerformanceMonitor(this.performanceMonitor); |
| 1121 | |
| 1122 | const metadata = getSystemMetadata(constructor); |
| 1123 | if (metadata?.updateOrder !== undefined) { |
| 1124 | system.setUpdateOrder(metadata.updateOrder); |
| 1125 | } |
| 1126 | if (metadata?.enabled !== undefined) { |
| 1127 | system.enabled = metadata.enabled; |
| 1128 | } |
| 1129 | |
| 1130 | this._services.registerInstance(constructor, system); |
| 1131 | this.markSystemsOrderDirty(); |
| 1132 | this.indexSystemByComponents(system); |
| 1133 | injectProperties(system, this._services); |
| 1134 | |
| 1135 | // Auto-wrap system methods for profiling in debug mode |
| 1136 | if (ProfilerSDK.isEnabled()) { |
| 1137 | AutoProfiler.wrapInstance(system, system.systemName, ProfileCategory.ECS); |
| 1138 | } |