2025-06-09 14:51:26 +08:00
|
|
|
|
import { Entity } from '../Entity';
|
|
|
|
|
|
import { PerformanceMonitor } from '../../Utils/PerformanceMonitor';
|
2025-09-28 15:23:59 +08:00
|
|
|
|
import { Matcher, type QueryCondition } from '../Utils/Matcher';
|
2025-06-09 14:51:26 +08:00
|
|
|
|
import type { Scene } from '../Scene';
|
2025-06-30 20:43:11 +08:00
|
|
|
|
import type { ISystemBase } from '../../Types';
|
2025-07-31 11:56:04 +08:00
|
|
|
|
import type { QuerySystem } from '../Core/QuerySystem';
|
2025-08-14 18:35:03 +08:00
|
|
|
|
import { getSystemInstanceTypeName } from '../Decorators';
|
2025-09-28 23:35:25 +08:00
|
|
|
|
import { createLogger } from '../../Utils/Logger';
|
2025-09-24 11:03:37 +08:00
|
|
|
|
import type { EventListenerConfig, TypeSafeEventSystem, EventHandler } from '../Core/EventSystem';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 事件监听器记录
|
|
|
|
|
|
*/
|
|
|
|
|
|
interface EventListenerRecord {
|
|
|
|
|
|
eventSystem: TypeSafeEventSystem;
|
|
|
|
|
|
eventType: string;
|
|
|
|
|
|
handler: EventHandler;
|
|
|
|
|
|
listenerRef: string;
|
|
|
|
|
|
}
|
2025-06-09 14:51:26 +08:00
|
|
|
|
|
2025-09-28 23:58:43 +08:00
|
|
|
|
|
2025-06-09 14:51:26 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 实体系统的基类
|
|
|
|
|
|
*
|
|
|
|
|
|
* 用于处理一组符合特定条件的实体。系统是ECS架构中的逻辑处理单元,
|
|
|
|
|
|
* 负责对拥有特定组件组合的实体执行业务逻辑。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @example
|
|
|
|
|
|
* ```typescript
|
|
|
|
|
|
* class MovementSystem extends EntitySystem {
|
|
|
|
|
|
* constructor() {
|
2025-07-31 11:56:04 +08:00
|
|
|
|
* super(Transform, Velocity);
|
2025-06-09 14:51:26 +08:00
|
|
|
|
* }
|
|
|
|
|
|
*
|
2025-09-28 09:40:36 +08:00
|
|
|
|
* protected process(entities: readonly Entity[]): void {
|
2025-06-09 14:51:26 +08:00
|
|
|
|
* for (const entity of entities) {
|
|
|
|
|
|
* const transform = entity.getComponent(Transform);
|
|
|
|
|
|
* const velocity = entity.getComponent(Velocity);
|
|
|
|
|
|
* transform.position.add(velocity.value);
|
|
|
|
|
|
* }
|
|
|
|
|
|
* }
|
|
|
|
|
|
* }
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*/
|
|
|
|
|
|
export abstract class EntitySystem implements ISystemBase {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
private _updateOrder: number;
|
|
|
|
|
|
private _enabled: boolean;
|
|
|
|
|
|
private _performanceMonitor: PerformanceMonitor;
|
2025-06-09 14:51:26 +08:00
|
|
|
|
private _systemName: string;
|
2025-09-26 18:30:49 +08:00
|
|
|
|
private _initialized: boolean;
|
2025-07-31 11:56:04 +08:00
|
|
|
|
private _matcher: Matcher;
|
2025-09-26 18:30:49 +08:00
|
|
|
|
private _eventListeners: EventListenerRecord[];
|
|
|
|
|
|
private _scene: Scene | null;
|
2025-09-29 09:53:13 +08:00
|
|
|
|
protected logger: ReturnType<typeof createLogger>;
|
2025-09-26 18:30:49 +08:00
|
|
|
|
|
2025-09-28 23:58:43 +08:00
|
|
|
|
|
2025-09-26 18:30:49 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 实体ID映射缓存
|
|
|
|
|
|
*/
|
|
|
|
|
|
private _entityIdMap: Map<number, Entity> | null;
|
|
|
|
|
|
private _entityIdMapVersion: number;
|
|
|
|
|
|
private _entityIdMapSize: number;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 统一的实体缓存管理器
|
|
|
|
|
|
*/
|
|
|
|
|
|
private _entityCache: {
|
2025-09-28 09:40:36 +08:00
|
|
|
|
frame: readonly Entity[] | null;
|
|
|
|
|
|
persistent: readonly Entity[] | null;
|
2025-09-26 18:30:49 +08:00
|
|
|
|
tracked: Set<Entity>;
|
|
|
|
|
|
invalidate(): void;
|
|
|
|
|
|
clearFrame(): void;
|
|
|
|
|
|
clearAll(): void;
|
|
|
|
|
|
};
|
2025-06-09 14:51:26 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-26 09:45:22 +08:00
|
|
|
|
* 获取系统处理的实体列表
|
2025-06-09 14:51:26 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public get entities(): readonly Entity[] {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
// 如果在update周期内,优先使用帧缓存
|
|
|
|
|
|
if (this._entityCache.frame !== null) {
|
|
|
|
|
|
return this._entityCache.frame;
|
2025-09-26 10:50:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 否则使用持久缓存
|
2025-09-26 18:30:49 +08:00
|
|
|
|
if (this._entityCache.persistent === null) {
|
|
|
|
|
|
this._entityCache.persistent = this.queryEntities();
|
2025-09-26 10:50:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-26 18:30:49 +08:00
|
|
|
|
return this._entityCache.persistent;
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取系统的更新时序
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get updateOrder(): number {
|
|
|
|
|
|
return this._updateOrder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public set updateOrder(value: number) {
|
|
|
|
|
|
this.setUpdateOrder(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取系统的启用状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get enabled(): boolean {
|
|
|
|
|
|
return this._enabled;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置系统的启用状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
public set enabled(value: boolean) {
|
|
|
|
|
|
this._enabled = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取系统名称
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get systemName(): string {
|
|
|
|
|
|
return this._systemName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
constructor(matcher?: Matcher) {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this._updateOrder = 0;
|
|
|
|
|
|
this._enabled = true;
|
|
|
|
|
|
this._performanceMonitor = PerformanceMonitor.instance;
|
2025-08-14 18:35:03 +08:00
|
|
|
|
this._systemName = getSystemInstanceTypeName(this);
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this._initialized = false;
|
|
|
|
|
|
this._matcher = matcher || Matcher.empty();
|
|
|
|
|
|
this._eventListeners = [];
|
|
|
|
|
|
this._scene = null;
|
|
|
|
|
|
|
|
|
|
|
|
this._entityIdMap = null;
|
|
|
|
|
|
this._entityIdMapVersion = -1;
|
|
|
|
|
|
this._entityIdMapSize = 0;
|
|
|
|
|
|
|
2025-09-29 09:53:13 +08:00
|
|
|
|
// 初始化logger
|
|
|
|
|
|
this.logger = createLogger(this.getLoggerName());
|
|
|
|
|
|
|
2025-09-28 23:58:43 +08:00
|
|
|
|
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this._entityCache = {
|
|
|
|
|
|
frame: null,
|
|
|
|
|
|
persistent: null,
|
|
|
|
|
|
tracked: new Set<Entity>(),
|
|
|
|
|
|
invalidate() {
|
|
|
|
|
|
this.persistent = null;
|
|
|
|
|
|
},
|
|
|
|
|
|
clearFrame() {
|
|
|
|
|
|
this.frame = null;
|
|
|
|
|
|
},
|
|
|
|
|
|
clearAll() {
|
|
|
|
|
|
this.frame = null;
|
|
|
|
|
|
this.persistent = null;
|
|
|
|
|
|
this.tracked.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 这个系统所属的场景
|
|
|
|
|
|
*/
|
2025-07-28 17:14:10 +08:00
|
|
|
|
public get scene(): Scene | null {
|
2025-06-09 14:51:26 +08:00
|
|
|
|
return this._scene;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-28 17:14:10 +08:00
|
|
|
|
public set scene(value: Scene | null) {
|
2025-06-09 14:51:26 +08:00
|
|
|
|
this._scene = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取实体匹配器
|
|
|
|
|
|
*/
|
|
|
|
|
|
public get matcher(): Matcher {
|
|
|
|
|
|
return this._matcher;
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-06-09 14:51:26 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 设置更新时序
|
|
|
|
|
|
* @param order 更新时序
|
|
|
|
|
|
*/
|
|
|
|
|
|
public setUpdateOrder(order: number): void {
|
|
|
|
|
|
this._updateOrder = order;
|
2025-07-28 17:14:10 +08:00
|
|
|
|
if (this.scene && this.scene.entityProcessors) {
|
|
|
|
|
|
this.scene.entityProcessors.setDirty();
|
|
|
|
|
|
}
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-31 11:56:04 +08:00
|
|
|
|
* 系统初始化(框架调用)
|
2025-09-26 10:50:31 +08:00
|
|
|
|
*
|
2025-07-31 11:56:04 +08:00
|
|
|
|
* 在系统创建时调用。框架内部使用,用户不应直接调用。
|
2025-06-09 14:51:26 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public initialize(): void {
|
2025-07-30 15:42:19 +08:00
|
|
|
|
// 防止重复初始化
|
|
|
|
|
|
if (this._initialized) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-07-30 15:42:19 +08:00
|
|
|
|
this._initialized = true;
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-08-15 12:58:55 +08:00
|
|
|
|
// 框架内部初始化:触发一次实体查询,以便正确跟踪现有实体
|
|
|
|
|
|
if (this.scene) {
|
2025-09-26 10:50:31 +08:00
|
|
|
|
// 清理缓存确保初始化时重新查询
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this._entityCache.invalidate();
|
2025-08-15 12:58:55 +08:00
|
|
|
|
this.queryEntities();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
// 调用用户可重写的初始化方法
|
|
|
|
|
|
this.onInitialize();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 系统初始化回调
|
|
|
|
|
|
*
|
|
|
|
|
|
* 子类可以重写此方法进行初始化操作。
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected onInitialize(): void {
|
|
|
|
|
|
// 子类可以重写此方法进行初始化
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-26 10:50:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 清除实体缓存(内部使用)
|
|
|
|
|
|
* 当Scene中的实体发生变化时调用
|
|
|
|
|
|
*/
|
|
|
|
|
|
public clearEntityCache(): void {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this._entityCache.invalidate();
|
2025-09-26 10:50:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-09 14:51:26 +08:00
|
|
|
|
/**
|
2025-07-30 15:42:19 +08:00
|
|
|
|
* 重置系统状态
|
2025-09-24 11:03:37 +08:00
|
|
|
|
*
|
2025-07-30 15:42:19 +08:00
|
|
|
|
* 当系统从场景中移除时调用,重置初始化状态以便重新添加时能正确初始化。
|
|
|
|
|
|
*/
|
|
|
|
|
|
public reset(): void {
|
2025-09-24 18:14:22 +08:00
|
|
|
|
this.scene = null;
|
2025-07-30 15:42:19 +08:00
|
|
|
|
this._initialized = false;
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this._entityCache.clearAll();
|
|
|
|
|
|
|
|
|
|
|
|
// 清理实体ID映射缓存
|
|
|
|
|
|
this._entityIdMap = null;
|
|
|
|
|
|
this._entityIdMapVersion = -1;
|
|
|
|
|
|
this._entityIdMapSize = 0;
|
2025-09-24 11:03:37 +08:00
|
|
|
|
|
|
|
|
|
|
// 清理所有事件监听器
|
2025-09-28 23:58:43 +08:00
|
|
|
|
// 调用框架销毁方法
|
|
|
|
|
|
this.destroy();
|
2025-07-30 15:42:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-31 11:56:04 +08:00
|
|
|
|
* 查询匹配的实体
|
2025-06-09 14:51:26 +08:00
|
|
|
|
*/
|
2025-09-28 09:40:36 +08:00
|
|
|
|
private queryEntities(): readonly Entity[] {
|
2025-07-31 11:56:04 +08:00
|
|
|
|
if (!this.scene?.querySystem || !this._matcher) {
|
|
|
|
|
|
return [];
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
const condition = this._matcher.getCondition();
|
|
|
|
|
|
const querySystem = this.scene.querySystem;
|
2025-09-28 09:40:36 +08:00
|
|
|
|
let currentEntities: readonly Entity[] = [];
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
// 空条件返回所有实体
|
|
|
|
|
|
if (this._matcher.isEmpty()) {
|
2025-08-06 09:39:08 +08:00
|
|
|
|
currentEntities = querySystem.getAllEntities();
|
|
|
|
|
|
} else if (this.isSingleCondition(condition)) {
|
|
|
|
|
|
// 单一条件优化查询
|
|
|
|
|
|
currentEntities = this.executeSingleConditionQuery(condition, querySystem);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 复合查询
|
|
|
|
|
|
currentEntities = this.executeComplexQuery(condition, querySystem);
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
2025-07-31 11:56:04 +08:00
|
|
|
|
|
2025-08-06 09:39:08 +08:00
|
|
|
|
// 检查实体变化并触发回调
|
|
|
|
|
|
this.updateEntityTracking(currentEntities);
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-08-06 09:39:08 +08:00
|
|
|
|
return currentEntities;
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-31 11:56:04 +08:00
|
|
|
|
* 检查是否为单一条件查询
|
2025-06-09 14:51:26 +08:00
|
|
|
|
*/
|
2025-09-28 15:23:59 +08:00
|
|
|
|
private isSingleCondition(condition: QueryCondition): boolean {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
const flags =
|
|
|
|
|
|
((condition.all.length > 0) ? 1 : 0) |
|
|
|
|
|
|
((condition.any.length > 0) ? 2 : 0) |
|
|
|
|
|
|
((condition.none.length > 0) ? 4 : 0) |
|
|
|
|
|
|
((condition.tag !== undefined) ? 8 : 0) |
|
|
|
|
|
|
((condition.name !== undefined) ? 16 : 0) |
|
|
|
|
|
|
((condition.component !== undefined) ? 32 : 0);
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-09-26 18:30:49 +08:00
|
|
|
|
return flags !== 0 && (flags & (flags - 1)) === 0;
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-31 11:56:04 +08:00
|
|
|
|
* 执行单一条件查询
|
2025-06-09 14:51:26 +08:00
|
|
|
|
*/
|
2025-09-28 15:23:59 +08:00
|
|
|
|
private executeSingleConditionQuery(condition: QueryCondition, querySystem: QuerySystem): readonly Entity[] {
|
2025-07-31 11:56:04 +08:00
|
|
|
|
// 按标签查询
|
|
|
|
|
|
if (condition.tag !== undefined) {
|
|
|
|
|
|
return querySystem.queryByTag(condition.tag).entities;
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
// 按名称查询
|
|
|
|
|
|
if (condition.name !== undefined) {
|
|
|
|
|
|
return querySystem.queryByName(condition.name).entities;
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
// 单组件查询
|
|
|
|
|
|
if (condition.component !== undefined) {
|
|
|
|
|
|
return querySystem.queryByComponent(condition.component).entities;
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
// 基础组件查询
|
|
|
|
|
|
if (condition.all.length > 0 && condition.any.length === 0 && condition.none.length === 0) {
|
|
|
|
|
|
return querySystem.queryAll(...condition.all).entities;
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
if (condition.all.length === 0 && condition.any.length > 0 && condition.none.length === 0) {
|
|
|
|
|
|
return querySystem.queryAny(...condition.any).entities;
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
if (condition.all.length === 0 && condition.any.length === 0 && condition.none.length > 0) {
|
|
|
|
|
|
return querySystem.queryNone(...condition.none).entities;
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
return [];
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-31 11:56:04 +08:00
|
|
|
|
* 执行复合查询
|
2025-06-09 14:51:26 +08:00
|
|
|
|
*/
|
2025-09-28 15:23:59 +08:00
|
|
|
|
private executeComplexQueryWithIdSets(condition: QueryCondition, querySystem: QuerySystem): readonly Entity[] {
|
2025-09-02 17:17:07 +08:00
|
|
|
|
let resultIds: Set<number> | null = null;
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
// 1. 应用标签条件作为基础集合
|
|
|
|
|
|
if (condition.tag !== undefined) {
|
|
|
|
|
|
const tagResult = querySystem.queryByTag(condition.tag);
|
2025-09-02 17:17:07 +08:00
|
|
|
|
resultIds = this.extractEntityIds(tagResult.entities);
|
2025-07-31 11:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
// 2. 应用名称条件 (交集)
|
2025-07-31 11:56:04 +08:00
|
|
|
|
if (condition.name !== undefined) {
|
2025-09-02 17:17:07 +08:00
|
|
|
|
const nameIds = this.extractEntityIds(querySystem.queryByName(condition.name).entities);
|
|
|
|
|
|
resultIds = resultIds ? this.intersectIdSets(resultIds, nameIds) : nameIds;
|
2025-07-31 11:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
// 3. 应用单组件条件 (交集)
|
2025-07-31 11:56:04 +08:00
|
|
|
|
if (condition.component !== undefined) {
|
2025-09-02 17:17:07 +08:00
|
|
|
|
const componentIds = this.extractEntityIds(querySystem.queryByComponent(condition.component).entities);
|
|
|
|
|
|
resultIds = resultIds ? this.intersectIdSets(resultIds, componentIds) : componentIds;
|
2025-07-31 11:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
// 4. 应用all条件 (交集)
|
2025-07-31 11:56:04 +08:00
|
|
|
|
if (condition.all.length > 0) {
|
2025-09-02 17:17:07 +08:00
|
|
|
|
const allIds = this.extractEntityIds(querySystem.queryAll(...condition.all).entities);
|
|
|
|
|
|
resultIds = resultIds ? this.intersectIdSets(resultIds, allIds) : allIds;
|
2025-07-31 11:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
// 5. 应用any条件 (交集)
|
2025-07-31 11:56:04 +08:00
|
|
|
|
if (condition.any.length > 0) {
|
2025-09-02 17:17:07 +08:00
|
|
|
|
const anyIds = this.extractEntityIds(querySystem.queryAny(...condition.any).entities);
|
|
|
|
|
|
resultIds = resultIds ? this.intersectIdSets(resultIds, anyIds) : anyIds;
|
2025-07-31 11:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
// 6. 应用none条件 (差集)
|
2025-07-31 11:56:04 +08:00
|
|
|
|
if (condition.none.length > 0) {
|
2025-09-02 17:17:07 +08:00
|
|
|
|
if (!resultIds) {
|
|
|
|
|
|
resultIds = this.extractEntityIds(querySystem.getAllEntities());
|
2025-07-31 11:56:04 +08:00
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-07-31 11:56:04 +08:00
|
|
|
|
const noneResult = querySystem.queryAny(...condition.none);
|
2025-09-02 17:17:07 +08:00
|
|
|
|
const noneIds = this.extractEntityIds(noneResult.entities);
|
|
|
|
|
|
resultIds = this.differenceIdSets(resultIds, noneIds);
|
2025-07-31 11:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
return resultIds ? this.idSetToEntityArray(resultIds, querySystem.getAllEntities()) : [];
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 提取实体ID集合
|
|
|
|
|
|
*/
|
2025-09-28 09:40:36 +08:00
|
|
|
|
private extractEntityIds(entities: readonly Entity[]): Set<number> {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
const len = entities.length;
|
2025-09-02 17:17:07 +08:00
|
|
|
|
const idSet = new Set<number>();
|
2025-09-26 18:30:49 +08:00
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < len; i = (i + 1) | 0) {
|
|
|
|
|
|
idSet.add(entities[i].id | 0);
|
2025-09-02 17:17:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
return idSet;
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* ID集合交集运算
|
|
|
|
|
|
*/
|
|
|
|
|
|
private intersectIdSets(setA: Set<number>, setB: Set<number>): Set<number> {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
let smaller: Set<number>, larger: Set<number>;
|
|
|
|
|
|
|
|
|
|
|
|
if (setA.size <= setB.size) {
|
|
|
|
|
|
smaller = setA;
|
|
|
|
|
|
larger = setB;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
smaller = setB;
|
|
|
|
|
|
larger = setA;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
const result = new Set<number>();
|
2025-09-26 18:30:49 +08:00
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
for (const id of smaller) {
|
|
|
|
|
|
if (larger.has(id)) {
|
|
|
|
|
|
result.add(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-26 18:30:49 +08:00
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
return result;
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* ID集合差集运算
|
|
|
|
|
|
*/
|
|
|
|
|
|
private differenceIdSets(setA: Set<number>, setB: Set<number>): Set<number> {
|
|
|
|
|
|
const result = new Set<number>();
|
2025-09-26 18:30:49 +08:00
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
for (const id of setA) {
|
|
|
|
|
|
if (!setB.has(id)) {
|
|
|
|
|
|
result.add(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-26 18:30:49 +08:00
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-26 18:30:49 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取或构建实体ID映射
|
|
|
|
|
|
*/
|
2025-09-28 09:40:36 +08:00
|
|
|
|
private getEntityIdMap(allEntities: readonly Entity[]): Map<number, Entity> {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
const currentVersion = this.scene?.querySystem?.version ?? 0;
|
|
|
|
|
|
if (this._entityIdMap !== null &&
|
|
|
|
|
|
this._entityIdMapVersion === currentVersion) {
|
|
|
|
|
|
return this._entityIdMap;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this.rebuildEntityIdMap(allEntities, currentVersion);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 重建实体ID映射
|
|
|
|
|
|
*/
|
2025-09-28 09:40:36 +08:00
|
|
|
|
private rebuildEntityIdMap(allEntities: readonly Entity[], version: number): Map<number, Entity> {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
let entityMap = this._entityIdMap;
|
|
|
|
|
|
|
|
|
|
|
|
if (!entityMap) {
|
|
|
|
|
|
entityMap = new Map<number, Entity>();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
entityMap.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const len = allEntities.length;
|
|
|
|
|
|
for (let i = 0; i < len; i = (i + 1) | 0) {
|
|
|
|
|
|
const entity = allEntities[i];
|
|
|
|
|
|
entityMap.set(entity.id | 0, entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this._entityIdMap = entityMap;
|
|
|
|
|
|
this._entityIdMapVersion = version;
|
|
|
|
|
|
this._entityIdMapSize = len;
|
|
|
|
|
|
|
|
|
|
|
|
return entityMap;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 从ID集合构建Entity数组
|
|
|
|
|
|
*/
|
2025-09-28 09:40:36 +08:00
|
|
|
|
private idSetToEntityArray(idSet: Set<number>, allEntities: readonly Entity[]): readonly Entity[] {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
const entityMap = this.getEntityIdMap(allEntities);
|
|
|
|
|
|
|
|
|
|
|
|
const size = idSet.size;
|
|
|
|
|
|
const result = new Array(size);
|
|
|
|
|
|
let index = 0;
|
2025-09-02 17:17:07 +08:00
|
|
|
|
|
|
|
|
|
|
for (const id of idSet) {
|
|
|
|
|
|
const entity = entityMap.get(id);
|
2025-09-26 18:30:49 +08:00
|
|
|
|
if (entity !== undefined) {
|
|
|
|
|
|
result[index] = entity;
|
|
|
|
|
|
index = (index + 1) | 0;
|
2025-09-02 17:17:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-31 11:56:04 +08:00
|
|
|
|
|
2025-09-26 18:30:49 +08:00
|
|
|
|
if (index < size) {
|
|
|
|
|
|
result.length = index;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:17:07 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 执行复合查询
|
|
|
|
|
|
*
|
|
|
|
|
|
* 使用基于ID集合的单次扫描算法进行复杂查询
|
|
|
|
|
|
*/
|
2025-09-28 15:23:59 +08:00
|
|
|
|
private executeComplexQuery(condition: QueryCondition, querySystem: QuerySystem): readonly Entity[] {
|
2025-09-02 17:17:07 +08:00
|
|
|
|
return this.executeComplexQueryWithIdSets(condition, querySystem);
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-06-09 14:51:26 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 更新系统
|
|
|
|
|
|
*/
|
|
|
|
|
|
public update(): void {
|
2025-07-31 11:56:04 +08:00
|
|
|
|
if (!this._enabled || !this.onCheckProcessing()) {
|
2025-06-09 14:51:26 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const startTime = this._performanceMonitor.startMonitoring(this._systemName);
|
2025-07-31 11:56:04 +08:00
|
|
|
|
let entityCount = 0;
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-06-09 14:51:26 +08:00
|
|
|
|
try {
|
2025-07-31 11:56:04 +08:00
|
|
|
|
this.onBegin();
|
2025-09-26 09:45:22 +08:00
|
|
|
|
// 查询实体并存储到帧缓存中
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this._entityCache.frame = this.queryEntities();
|
|
|
|
|
|
entityCount = this._entityCache.frame.length;
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this.process(this._entityCache.frame);
|
2025-06-09 14:51:26 +08:00
|
|
|
|
} finally {
|
2025-07-31 11:56:04 +08:00
|
|
|
|
this._performanceMonitor.endMonitoring(this._systemName, startTime, entityCount);
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 后期更新系统
|
|
|
|
|
|
*/
|
|
|
|
|
|
public lateUpdate(): void {
|
2025-07-31 11:56:04 +08:00
|
|
|
|
if (!this._enabled || !this.onCheckProcessing()) {
|
2025-06-09 14:51:26 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const startTime = this._performanceMonitor.startMonitoring(`${this._systemName}_Late`);
|
2025-07-31 11:56:04 +08:00
|
|
|
|
let entityCount = 0;
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-06-09 14:51:26 +08:00
|
|
|
|
try {
|
2025-09-26 09:45:22 +08:00
|
|
|
|
// 使用缓存的实体列表,避免重复查询
|
2025-09-26 18:30:49 +08:00
|
|
|
|
const entities = this._entityCache.frame || [];
|
2025-07-31 11:56:04 +08:00
|
|
|
|
entityCount = entities.length;
|
|
|
|
|
|
this.lateProcess(entities);
|
|
|
|
|
|
this.onEnd();
|
2025-06-09 14:51:26 +08:00
|
|
|
|
} finally {
|
2025-07-31 11:56:04 +08:00
|
|
|
|
this._performanceMonitor.endMonitoring(`${this._systemName}_Late`, startTime, entityCount);
|
2025-09-26 09:45:22 +08:00
|
|
|
|
// 清理帧缓存
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this._entityCache.clearFrame();
|
2025-06-09 14:51:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 在系统处理开始前调用
|
|
|
|
|
|
*
|
|
|
|
|
|
* 子类可以重写此方法进行预处理操作。
|
|
|
|
|
|
*/
|
2025-07-31 11:56:04 +08:00
|
|
|
|
protected onBegin(): void {
|
2025-06-09 14:51:26 +08:00
|
|
|
|
// 子类可以重写此方法
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 处理实体列表
|
|
|
|
|
|
*
|
|
|
|
|
|
* 系统的核心逻辑,子类必须实现此方法来定义具体的处理逻辑。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param entities 要处理的实体列表
|
|
|
|
|
|
*/
|
2025-09-28 09:40:36 +08:00
|
|
|
|
protected process(entities: readonly Entity[]): void {
|
2025-06-09 14:51:26 +08:00
|
|
|
|
// 子类必须实现此方法
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 后期处理实体列表
|
|
|
|
|
|
*
|
|
|
|
|
|
* 在主要处理逻辑之后执行,子类可以重写此方法。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param entities 要处理的实体列表
|
|
|
|
|
|
*/
|
2025-09-28 09:40:36 +08:00
|
|
|
|
protected lateProcess(_entities: readonly Entity[]): void {
|
2025-06-09 14:51:26 +08:00
|
|
|
|
// 子类可以重写此方法
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 系统处理完毕后调用
|
|
|
|
|
|
*
|
|
|
|
|
|
* 子类可以重写此方法进行后处理操作。
|
|
|
|
|
|
*/
|
2025-07-31 11:56:04 +08:00
|
|
|
|
protected onEnd(): void {
|
2025-06-09 14:51:26 +08:00
|
|
|
|
// 子类可以重写此方法
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 检查系统是否需要处理
|
|
|
|
|
|
*
|
|
|
|
|
|
* 在启用系统时有用,但仅偶尔需要处理。
|
|
|
|
|
|
* 这只影响处理,不影响事件或订阅列表。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @returns 如果系统应该处理,则为true,如果不处理则为false
|
|
|
|
|
|
*/
|
2025-07-31 11:56:04 +08:00
|
|
|
|
protected onCheckProcessing(): boolean {
|
2025-06-09 14:51:26 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取系统的性能数据
|
|
|
|
|
|
*
|
|
|
|
|
|
* @returns 性能数据或undefined
|
|
|
|
|
|
*/
|
|
|
|
|
|
public getPerformanceData() {
|
|
|
|
|
|
return this._performanceMonitor.getSystemData(this._systemName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取系统的性能统计
|
|
|
|
|
|
*
|
|
|
|
|
|
* @returns 性能统计或undefined
|
|
|
|
|
|
*/
|
|
|
|
|
|
public getPerformanceStats() {
|
|
|
|
|
|
return this._performanceMonitor.getSystemStats(this._systemName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 重置系统的性能数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
public resetPerformanceData(): void {
|
|
|
|
|
|
this._performanceMonitor.resetSystem(this._systemName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取系统信息的字符串表示
|
|
|
|
|
|
*
|
|
|
|
|
|
* @returns 系统信息字符串
|
|
|
|
|
|
*/
|
|
|
|
|
|
public toString(): string {
|
2025-07-31 11:56:04 +08:00
|
|
|
|
const entityCount = this.entities.length;
|
2025-06-09 14:51:26 +08:00
|
|
|
|
const perfData = this.getPerformanceData();
|
|
|
|
|
|
const perfInfo = perfData ? ` (${perfData.executionTime.toFixed(2)}ms)` : '';
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-06-09 14:51:26 +08:00
|
|
|
|
return `${this._systemName}[${entityCount} entities]${perfInfo}`;
|
|
|
|
|
|
}
|
2025-08-06 09:39:08 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新实体跟踪,检查新增和移除的实体
|
|
|
|
|
|
*/
|
2025-09-28 09:40:36 +08:00
|
|
|
|
private updateEntityTracking(currentEntities: readonly Entity[]): void {
|
2025-08-06 09:39:08 +08:00
|
|
|
|
const currentSet = new Set(currentEntities);
|
2025-09-26 10:50:31 +08:00
|
|
|
|
let hasChanged = false;
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-08-06 09:39:08 +08:00
|
|
|
|
// 检查新增的实体
|
|
|
|
|
|
for (const entity of currentEntities) {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
if (!this._entityCache.tracked.has(entity)) {
|
|
|
|
|
|
this._entityCache.tracked.add(entity);
|
2025-08-06 09:39:08 +08:00
|
|
|
|
this.onAdded(entity);
|
2025-09-26 10:50:31 +08:00
|
|
|
|
hasChanged = true;
|
2025-08-06 09:39:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-14 18:35:03 +08:00
|
|
|
|
|
2025-08-06 09:39:08 +08:00
|
|
|
|
// 检查移除的实体
|
2025-09-26 18:30:49 +08:00
|
|
|
|
for (const entity of this._entityCache.tracked) {
|
2025-08-06 09:39:08 +08:00
|
|
|
|
if (!currentSet.has(entity)) {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this._entityCache.tracked.delete(entity);
|
2025-08-06 09:39:08 +08:00
|
|
|
|
this.onRemoved(entity);
|
2025-09-26 10:50:31 +08:00
|
|
|
|
hasChanged = true;
|
2025-08-06 09:39:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-26 10:50:31 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果实体发生了变化,使缓存失效
|
|
|
|
|
|
if (hasChanged) {
|
2025-09-26 18:30:49 +08:00
|
|
|
|
this._entityCache.invalidate();
|
2025-09-26 10:50:31 +08:00
|
|
|
|
}
|
2025-08-06 09:39:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 当实体被添加到系统时调用
|
|
|
|
|
|
*
|
|
|
|
|
|
* 子类可以重写此方法来处理实体添加事件。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param entity 被添加的实体
|
|
|
|
|
|
*/
|
2025-09-24 10:45:33 +08:00
|
|
|
|
protected onAdded(entity: Entity): void {
|
2025-08-06 09:39:08 +08:00
|
|
|
|
// 子类可以重写此方法
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 当实体从系统中移除时调用
|
|
|
|
|
|
*
|
|
|
|
|
|
* 子类可以重写此方法来处理实体移除事件。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param entity 被移除的实体
|
|
|
|
|
|
*/
|
2025-09-24 10:45:33 +08:00
|
|
|
|
protected onRemoved(entity: Entity): void {
|
2025-08-06 09:39:08 +08:00
|
|
|
|
// 子类可以重写此方法
|
|
|
|
|
|
}
|
2025-09-24 11:03:37 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加事件监听器
|
|
|
|
|
|
*
|
|
|
|
|
|
* 推荐使用此方法而不是直接调用eventSystem.on(),
|
|
|
|
|
|
* 这样可以确保系统移除时自动清理监听器,避免内存泄漏。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param eventType 事件类型
|
|
|
|
|
|
* @param handler 事件处理函数
|
|
|
|
|
|
* @param config 监听器配置
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected addEventListener<T = any>(
|
|
|
|
|
|
eventType: string,
|
|
|
|
|
|
handler: EventHandler<T>,
|
|
|
|
|
|
config?: EventListenerConfig
|
|
|
|
|
|
): void {
|
|
|
|
|
|
if (!this.scene?.eventSystem) {
|
2025-09-28 23:35:25 +08:00
|
|
|
|
this.logger.warn(`${this.systemName}: 无法添加事件监听器,scene.eventSystem 不可用`);
|
2025-09-24 11:03:37 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const listenerRef = this.scene.eventSystem.on(eventType, handler, config);
|
|
|
|
|
|
|
|
|
|
|
|
// 跟踪监听器以便后续清理
|
|
|
|
|
|
if (listenerRef) {
|
|
|
|
|
|
this._eventListeners.push({
|
|
|
|
|
|
eventSystem: this.scene.eventSystem,
|
|
|
|
|
|
eventType,
|
|
|
|
|
|
handler,
|
|
|
|
|
|
listenerRef
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 移除特定的事件监听器
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param eventType 事件类型
|
|
|
|
|
|
* @param handler 事件处理函数
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected removeEventListener<T = any>(
|
|
|
|
|
|
eventType: string,
|
|
|
|
|
|
handler: EventHandler<T>
|
|
|
|
|
|
): void {
|
|
|
|
|
|
const listenerIndex = this._eventListeners.findIndex(
|
|
|
|
|
|
listener => listener.eventType === eventType && listener.handler === handler
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (listenerIndex >= 0) {
|
|
|
|
|
|
const listener = this._eventListeners[listenerIndex];
|
|
|
|
|
|
|
|
|
|
|
|
// 从事件系统中移除
|
|
|
|
|
|
listener.eventSystem.off(eventType, listener.listenerRef);
|
|
|
|
|
|
|
|
|
|
|
|
// 从跟踪列表中移除
|
|
|
|
|
|
this._eventListeners.splice(listenerIndex, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 23:58:43 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 清理手动添加的事件监听器
|
|
|
|
|
|
*/
|
|
|
|
|
|
private cleanupManualEventListeners(): void {
|
2025-09-24 11:03:37 +08:00
|
|
|
|
for (const listener of this._eventListeners) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
listener.eventSystem.off(listener.eventType, listener.listenerRef);
|
|
|
|
|
|
} catch (error) {
|
2025-09-28 23:35:25 +08:00
|
|
|
|
this.logger.warn(`${this.systemName}: 移除事件监听器失败 "${listener.eventType}"`, error);
|
2025-09-24 11:03:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空跟踪列表
|
|
|
|
|
|
this._eventListeners.length = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-28 23:58:43 +08:00
|
|
|
|
* 框架内部销毁方法
|
|
|
|
|
|
* 由框架调用,处理系统的完整销毁流程
|
|
|
|
|
|
*/
|
|
|
|
|
|
public destroy(): void {
|
|
|
|
|
|
this.cleanupManualEventListeners();
|
|
|
|
|
|
|
|
|
|
|
|
this.onDestroy();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 09:53:13 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取Logger名称
|
|
|
|
|
|
* 子类可以重写此方法来自定义logger名称
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected getLoggerName(): string {
|
|
|
|
|
|
return 'EntitySystem';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 23:58:43 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 用户销毁回调
|
2025-09-24 11:03:37 +08:00
|
|
|
|
*
|
|
|
|
|
|
* 当系统从场景中移除时调用,子类可以重写此方法进行清理操作。
|
|
|
|
|
|
* 注意:事件监听器会被框架自动清理,无需手动处理。
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected onDestroy(): void {
|
|
|
|
|
|
// 子类可以重写此方法进行清理操作
|
|
|
|
|
|
}
|
2025-07-31 11:56:04 +08:00
|
|
|
|
}
|