新增组件/系统装饰器避免混淆

更改Set兼容web/小游戏
This commit is contained in:
YHH
2025-08-14 18:35:03 +08:00
parent 62f250b43c
commit 0b7e623748
19 changed files with 434 additions and 90 deletions

View File

@@ -4,6 +4,7 @@ import { Matcher } from '../Utils/Matcher';
import type { Scene } from '../Scene';
import type { ISystemBase } from '../../Types';
import type { QuerySystem } from '../Core/QuerySystem';
import { getSystemInstanceTypeName } from '../Decorators';
/**
* 实体系统的基类
@@ -36,7 +37,6 @@ export abstract class EntitySystem implements ISystemBase {
private _initialized: boolean = false;
private _matcher: Matcher;
private _trackedEntities: Set<Entity> = new Set();
private _lastQueryResult: Entity[] = [];
/**
* 获取系统处理的实体列表(动态查询)
@@ -79,7 +79,7 @@ export abstract class EntitySystem implements ISystemBase {
constructor(matcher?: Matcher) {
this._matcher = matcher ? matcher : Matcher.empty();
this._systemName = this.constructor.name;
this._systemName = getSystemInstanceTypeName(this);
}
private _scene: Scene | null = null;
@@ -101,7 +101,7 @@ export abstract class EntitySystem implements ISystemBase {
public get matcher(): Matcher {
return this._matcher;
}
/**
* 设置更新时序
* @param order 更新时序
@@ -123,9 +123,9 @@ export abstract class EntitySystem implements ISystemBase {
if (this._initialized) {
return;
}
this._initialized = true;
// 调用用户可重写的初始化方法
this.onInitialize();
}
@@ -151,7 +151,6 @@ export abstract class EntitySystem implements ISystemBase {
public reset(): void {
this._initialized = false;
this._trackedEntities.clear();
this._lastQueryResult = [];
}
/**
@@ -159,14 +158,14 @@ export abstract class EntitySystem implements ISystemBase {
*/
private queryEntities(): Entity[] {
if (!this.scene?.querySystem || !this._matcher) {
this._lastQueryResult = [];
return [];
}
const condition = this._matcher.getCondition();
const querySystem = this.scene.querySystem;
let currentEntities: Entity[] = [];
// 空条件返回所有实体
if (this._matcher.isEmpty()) {
currentEntities = querySystem.getAllEntities();
@@ -180,8 +179,8 @@ export abstract class EntitySystem implements ISystemBase {
// 检查实体变化并触发回调
this.updateEntityTracking(currentEntities);
this._lastQueryResult = currentEntities;
return currentEntities;
}
@@ -189,14 +188,15 @@ export abstract class EntitySystem implements ISystemBase {
* 检查是否为单一条件查询
*/
private isSingleCondition(condition: any): boolean {
const conditionCount =
const conditionCount =
(condition.all.length > 0 ? 1 : 0) +
(condition.any.length > 0 ? 1 : 0) +
(condition.none.length > 0 ? 1 : 0) +
(condition.tag !== undefined ? 1 : 0) +
(condition.name !== undefined ? 1 : 0) +
(condition.component !== undefined ? 1 : 0);
return conditionCount === 1;
}
@@ -208,30 +208,30 @@ export abstract class EntitySystem implements ISystemBase {
if (condition.tag !== undefined) {
return querySystem.queryByTag(condition.tag).entities;
}
// 按名称查询
if (condition.name !== undefined) {
return querySystem.queryByName(condition.name).entities;
}
// 单组件查询
if (condition.component !== undefined) {
return querySystem.queryByComponent(condition.component).entities;
}
// 基础组件查询
if (condition.all.length > 0 && condition.any.length === 0 && condition.none.length === 0) {
return querySystem.queryAll(...condition.all).entities;
}
if (condition.all.length === 0 && condition.any.length > 0 && condition.none.length === 0) {
return querySystem.queryAny(...condition.any).entities;
}
if (condition.all.length === 0 && condition.any.length === 0 && condition.none.length > 0) {
return querySystem.queryNone(...condition.none).entities;
}
return [];
}
@@ -241,6 +241,7 @@ export abstract class EntitySystem implements ISystemBase {
private executeComplexQuery(condition: any, querySystem: QuerySystem): Entity[] {
let result: Set<Entity> | null = null;
// 1. 应用标签条件作为基础集合
if (condition.tag !== undefined) {
const tagResult = querySystem.queryByTag(condition.tag);
@@ -251,9 +252,18 @@ export abstract class EntitySystem implements ISystemBase {
if (condition.name !== undefined) {
const nameResult = querySystem.queryByName(condition.name);
const nameSet = new Set(nameResult.entities);
if (result) {
result = new Set([...result].filter(e => nameSet.has(e)));
const intersection = [];
for (const entity of result) {
for (const nameEntity of nameSet) {
if (entity === nameEntity || entity.id === nameEntity.id) {
intersection.push(entity);
break;
}
}
}
result = new Set(intersection);
} else {
result = nameSet;
}
@@ -263,9 +273,18 @@ export abstract class EntitySystem implements ISystemBase {
if (condition.component !== undefined) {
const componentResult = querySystem.queryByComponent(condition.component);
const componentSet = new Set(componentResult.entities);
if (result) {
result = new Set([...result].filter(e => componentSet.has(e)));
const intersection = [];
for (const entity of result) {
for (const componentEntity of componentSet) {
if (entity === componentEntity || entity.id === componentEntity.id) {
intersection.push(entity);
break;
}
}
}
result = new Set(intersection);
} else {
result = componentSet;
}
@@ -275,9 +294,19 @@ export abstract class EntitySystem implements ISystemBase {
if (condition.all.length > 0) {
const allResult = querySystem.queryAll(...condition.all);
const allSet = new Set(allResult.entities);
if (result) {
result = new Set([...result].filter(e => allSet.has(e)));
const intersection = [];
for (const entity of result) {
for (const allEntity of allSet) {
if (entity === allEntity || entity.id === allEntity.id) {
intersection.push(entity);
break;
}
}
}
result = new Set(intersection);
} else {
result = allSet;
}
@@ -287,9 +316,22 @@ export abstract class EntitySystem implements ISystemBase {
if (condition.any.length > 0) {
const anyResult = querySystem.queryAny(...condition.any);
const anySet = new Set(anyResult.entities);
if (result) {
result = new Set([...result].filter(e => anySet.has(e)));
const intersection = [];
for (const entity of result) {
// 通过id匹配来确保正确的交集计算
for (const anyEntity of anySet) {
if (entity === anyEntity || entity.id === anyEntity.id) {
intersection.push(entity);
break;
}
}
}
result = new Set(intersection);
} else {
result = anySet;
}
@@ -301,17 +343,34 @@ export abstract class EntitySystem implements ISystemBase {
// 如果没有前置条件,从所有实体开始
result = new Set(querySystem.getAllEntities());
}
const noneResult = querySystem.queryAny(...condition.none);
const noneSet = new Set(noneResult.entities);
result = new Set([...result].filter(e => !noneSet.has(e)));
const filteredEntities = [];
for (const entity of result) {
let shouldExclude = false;
for (const noneEntity of noneSet) {
if (entity === noneEntity || entity.id === noneEntity.id) {
shouldExclude = true;
break;
}
}
if (!shouldExclude) {
filteredEntities.push(entity);
}
}
result = new Set(filteredEntities);
}
return result ? Array.from(result) : [];
const finalResult = result ? Array.from(result) : [];
return finalResult;
}
/**
* 更新系统
*
@@ -322,14 +381,17 @@ export abstract class EntitySystem implements ISystemBase {
return;
}
const startTime = this._performanceMonitor.startMonitoring(this._systemName);
let entityCount = 0;
try {
this.onBegin();
// 动态查询实体并处理
const entities = this.queryEntities();
entityCount = entities.length;
this.process(entities);
} finally {
this._performanceMonitor.endMonitoring(this._systemName, startTime, entityCount);
@@ -348,7 +410,7 @@ export abstract class EntitySystem implements ISystemBase {
const startTime = this._performanceMonitor.startMonitoring(`${this._systemName}_Late`);
let entityCount = 0;
try {
// 动态查询实体并处理
const entities = this.queryEntities();
@@ -376,7 +438,7 @@ export abstract class EntitySystem implements ISystemBase {
*
* @param entities 要处理的实体列表
*/
protected process(entities: Entity[]): void {
protected process(_entities: Entity[]): void {
// 子类必须实现此方法
}
@@ -387,7 +449,7 @@ export abstract class EntitySystem implements ISystemBase {
*
* @param entities 要处理的实体列表
*/
protected lateProcess(entities: Entity[]): void {
protected lateProcess(_entities: Entity[]): void {
// 子类可以重写此方法
}
@@ -447,7 +509,7 @@ export abstract class EntitySystem implements ISystemBase {
const entityCount = this.entities.length;
const perfData = this.getPerformanceData();
const perfInfo = perfData ? ` (${perfData.executionTime.toFixed(2)}ms)` : '';
return `${this._systemName}[${entityCount} entities]${perfInfo}`;
}
@@ -456,7 +518,7 @@ export abstract class EntitySystem implements ISystemBase {
*/
private updateEntityTracking(currentEntities: Entity[]): void {
const currentSet = new Set(currentEntities);
// 检查新增的实体
for (const entity of currentEntities) {
if (!this._trackedEntities.has(entity)) {
@@ -464,7 +526,7 @@ export abstract class EntitySystem implements ISystemBase {
this.onAdded(entity);
}
}
// 检查移除的实体
for (const entity of this._trackedEntities) {
if (!currentSet.has(entity)) {