From b97f3a8431ecf9bf9c7aaa72a0580d4d0608e9f0 Mon Sep 17 00:00:00 2001 From: YHH <359807859@qq.com> Date: Tue, 14 Oct 2025 12:08:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=BA=86=20EntitySystem.upda?= =?UTF-8?q?te()=20=E4=B8=AD=E7=9A=84=E5=86=97=E4=BD=99=20invalidate()=20?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/ECS/Systems/EntitySystem.ts | 4 +- .../tests/ECS/Core/ReactiveQueryDebug.test.ts | 92 ---------- .../Core/SystemInitializationDebug.test.ts | 164 ------------------ 3 files changed, 1 insertion(+), 259 deletions(-) delete mode 100644 packages/core/tests/ECS/Core/ReactiveQueryDebug.test.ts delete mode 100644 packages/core/tests/ECS/Core/SystemInitializationDebug.test.ts diff --git a/packages/core/src/ECS/Systems/EntitySystem.ts b/packages/core/src/ECS/Systems/EntitySystem.ts index 5572c50c..96dd35cf 100644 --- a/packages/core/src/ECS/Systems/EntitySystem.ts +++ b/packages/core/src/ECS/Systems/EntitySystem.ts @@ -554,10 +554,8 @@ export abstract class EntitySystem< try { this.onBegin(); - // 清除持久缓存,确保每次update都重新查询并检测实体变化 - // 这对于响应式查询正确工作至关重要 - this._entityCache.invalidate(); // 查询实体并存储到帧缓存中 + // 响应式查询会自动维护最新的实体列表,updateEntityTracking会在检测到变化时invalidate this._entityCache.frame = this.queryEntities(); entityCount = this._entityCache.frame.length; diff --git a/packages/core/tests/ECS/Core/ReactiveQueryDebug.test.ts b/packages/core/tests/ECS/Core/ReactiveQueryDebug.test.ts deleted file mode 100644 index e9489223..00000000 --- a/packages/core/tests/ECS/Core/ReactiveQueryDebug.test.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { Scene } from '../../../src/ECS/Scene'; -import { Entity } from '../../../src/ECS/Entity'; -import { Component } from '../../../src/ECS/Component'; -import { ComponentTypeManager } from '../../../src/ECS/Utils/ComponentTypeManager'; - -/** - * 响应式查询调试测试 - * - * 隔离测试响应式查询初始化问题 - */ - -class HealthComponent extends Component { - public health: number; - - constructor(health: number = 100) { - super(); - this.health = health; - } -} - -describe('ReactiveQueryDebug - 响应式查询初始化调试', () => { - let scene: Scene; - - beforeEach(() => { - ComponentTypeManager.instance.reset(); - scene = new Scene(); - scene.name = 'DebugScene'; - }); - - test('场景有实体时QuerySystem.queryAll应该能找到实体', () => { - const entity = scene.createEntity('TestEntity'); - entity.addComponent(new HealthComponent(100)); - - // 直接使用QuerySystem查询 - const result = scene.querySystem!.queryAll(HealthComponent); - - console.log('QuerySystem.queryAll结果:', { - count: result.count, - entities: result.entities.map(e => ({ id: e.id, name: e.name })) - }); - - expect(result.count).toBe(1); - expect(result.entities[0]).toBe(entity); - }); - - test('第一次查询和第二次查询应该返回相同结果', () => { - const entity = scene.createEntity('TestEntity'); - entity.addComponent(new HealthComponent(100)); - - // 第一次查询 - const result1 = scene.querySystem!.queryAll(HealthComponent); - - console.log('第一次查询结果:', { - count: result1.count, - entities: result1.entities.map(e => ({ id: e.id, name: e.name })) - }); - - // 第二次查询 - const result2 = scene.querySystem!.queryAll(HealthComponent); - - console.log('第二次查询结果:', { - count: result2.count, - entities: result2.entities.map(e => ({ id: e.id, name: e.name })) - }); - - expect(result1.count).toBe(1); - expect(result2.count).toBe(1); - expect(result1.entities[0]).toBe(result2.entities[0]); - }); - - test('添加组件后查询应该能找到实体', () => { - const entity = scene.createEntity('TestEntity'); - - // 第一次查询(实体还没有组件) - const result1 = scene.querySystem!.queryAll(HealthComponent); - console.log('添加组件前查询结果:', { count: result1.count }); - expect(result1.count).toBe(0); - - // 添加组件 - entity.addComponent(new HealthComponent(100)); - - // 第二次查询(实体已有组件) - const result2 = scene.querySystem!.queryAll(HealthComponent); - console.log('添加组件后查询结果:', { - count: result2.count, - entities: result2.entities.map(e => ({ id: e.id, name: e.name })) - }); - - expect(result2.count).toBe(1); - expect(result2.entities[0]).toBe(entity); - }); -}); diff --git a/packages/core/tests/ECS/Core/SystemInitializationDebug.test.ts b/packages/core/tests/ECS/Core/SystemInitializationDebug.test.ts deleted file mode 100644 index eb534198..00000000 --- a/packages/core/tests/ECS/Core/SystemInitializationDebug.test.ts +++ /dev/null @@ -1,164 +0,0 @@ -import { Scene } from '../../../src/ECS/Scene'; -import { Entity } from '../../../src/ECS/Entity'; -import { Component } from '../../../src/ECS/Component'; -import { EntitySystem } from '../../../src/ECS/Systems/EntitySystem'; -import { Matcher } from '../../../src/ECS/Utils/Matcher'; -import { ComponentTypeManager } from '../../../src/ECS/Utils/ComponentTypeManager'; - -/** - * System初始化调试测试 - */ - -class HealthComponent extends Component { - public health: number; - - constructor(health: number = 100) { - super(); - this.health = health; - } -} - -class HealthSystem extends EntitySystem { - public initializeCalled = false; - public onAddedEntities: Entity[] = []; - public queryResults: readonly Entity[] = []; - - constructor() { - super(Matcher.empty().all(HealthComponent)); - } - - public override initialize(): void { - console.log('[HealthSystem] initialize() 开始'); - console.log('[HealthSystem] scene:', !!this.scene); - console.log('[HealthSystem] matcher:', this.matcher.toString()); - - this.initializeCalled = true; - super.initialize(); - - // 初始化后立即查询 - if (this.scene?.querySystem) { - const result = this.scene.querySystem.queryAll(HealthComponent); - console.log('[HealthSystem] 初始化后立即查询结果:', { - count: result.count, - entities: result.entities.map(e => ({ id: e.id, name: e.name })) - }); - } - - console.log('[HealthSystem] initialize() 完成, onAddedEntities.length=', this.onAddedEntities.length); - } - - protected override onAdded(entity: Entity): void { - console.log('[HealthSystem] onAdded() 被调用:', { id: entity.id, name: entity.name }); - this.onAddedEntities.push(entity); - } - - protected override process(entities: readonly Entity[]): void { - this.queryResults = entities; - for (const entity of entities) { - const health = entity.getComponent(HealthComponent)!; - if (health.health <= 0) { - entity.enabled = false; - } - } - } -} - -describe('SystemInitializationDebug - 系统初始化调试', () => { - let scene: Scene; - - beforeEach(() => { - ComponentTypeManager.instance.reset(); - scene = new Scene(); - scene.name = 'DebugScene'; - }); - - test('先创建实体再添加系统 - 应该触发onAdded', () => { - console.log('\n=== 测试开始 ==='); - - // 1. 创建实体并添加组件 - const entity = scene.createEntity('TestEntity'); - console.log('[Test] 创建实体:', { id: entity.id, name: entity.name }); - - entity.addComponent(new HealthComponent(100)); - console.log('[Test] 添加HealthComponent'); - - // 2. 验证QuerySystem能查询到实体 - const queryResult = scene.querySystem!.queryAll(HealthComponent); - console.log('[Test] QuerySystem查询结果:', { - count: queryResult.count, - entities: queryResult.entities.map(e => ({ id: e.id, name: e.name })) - }); - - expect(queryResult.count).toBe(1); - expect(queryResult.entities[0]).toBe(entity); - - // 3. 添加系统 - const system = new HealthSystem(); - console.log('[Test] 准备添加系统到场景'); - scene.addEntityProcessor(system); - console.log('[Test] 系统已添加'); - - // 4. 验证系统状态 - console.log('[Test] 系统状态:', { - initializeCalled: system.initializeCalled, - onAddedEntitiesLength: system.onAddedEntities.length, - onAddedEntities: system.onAddedEntities.map(e => ({ id: e.id, name: e.name })) - }); - - expect(system.initializeCalled).toBe(true); - expect(system.onAddedEntities).toHaveLength(1); - expect(system.onAddedEntities[0]).toBe(entity); - - console.log('=== 测试结束 ===\n'); - }); - - test('添加同类型的第二个系统 - 应该返回已注册的实例', () => { - console.log('\n=== 单例模式测试开始 ==='); - - // 1. 创建实体并添加组件 - const entity = scene.createEntity('TestEntity'); - console.log('[Test] 创建实体:', { id: entity.id, name: entity.name }); - - entity.addComponent(new HealthComponent(100)); - console.log('[Test] 添加HealthComponent'); - - // 2. 添加第一个系统 - const system1 = new HealthSystem(); - console.log('[Test] 准备添加第一个系统'); - const returnedSystem1 = scene.addEntityProcessor(system1); - console.log('[Test] 第一个系统已添加, system1===returnedSystem1?', system1 === returnedSystem1); - - console.log('[Test] 第一个系统状态:', { - initializeCalled: system1.initializeCalled, - onAddedEntitiesLength: system1.onAddedEntities.length - }); - - expect(system1.initializeCalled).toBe(true); - expect(system1.onAddedEntities).toHaveLength(1); - expect(system1.onAddedEntities[0]).toBe(entity); - - // 3. 尝试添加第二个同类型系统 - 应该返回已注册的system1 - const system2 = new HealthSystem(); - console.log('[Test] 准备添加第二个系统, system1===system2?', system1 === system2); - console.log('[Test] 系统是否已在ServiceContainer注册:', scene.services.isRegistered(HealthSystem)); - const returnedSystem2 = scene.addEntityProcessor(system2); - console.log('[Test] 第二个系统调用完成'); - console.log('[Test] system2===returnedSystem2?', system2 === returnedSystem2); - console.log('[Test] returnedSystem1===returnedSystem2?', returnedSystem1 === returnedSystem2); - - // 验证单例行为: returnedSystem2应该是system1而不是system2 - expect(returnedSystem1).toBe(returnedSystem2); - expect(returnedSystem2).toBe(system1); - expect(returnedSystem2).not.toBe(system2); - - // system2不应该被初始化(因为被拒绝了) - expect(system2.initializeCalled).toBe(false); - expect(system2.onAddedEntities).toHaveLength(0); - - // 返回的应该是已初始化的system1 - expect(returnedSystem2.initializeCalled).toBe(true); - expect(returnedSystem2.onAddedEntities).toHaveLength(1); - - console.log('=== 单例模式测试结束 ===\n'); - }); -});