系统添加缓存实体机制避免频繁开销

This commit is contained in:
YHH
2025-09-26 10:50:31 +08:00
parent 5e052a7e7d
commit 64ea53eba1
4 changed files with 58 additions and 5 deletions

View File

@@ -207,6 +207,16 @@ export class Scene implements IScene {
return this.addEntity(entity);
}
/**
* 清除所有EntitySystem的实体缓存
* 当实体或组件发生变化时调用
*/
public clearSystemEntityCaches(): void {
for (const system of this.entityProcessors.processors) {
system.clearEntityCache();
}
}
/**
* 在场景的实体列表中添加一个实体
* @param entity 要添加的实体
@@ -215,13 +225,18 @@ export class Scene implements IScene {
public addEntity(entity: Entity, deferCacheClear: boolean = false) {
this.entities.add(entity);
entity.scene = this;
// 将实体添加到查询系统(可延迟缓存清理)
this.querySystem.addEntity(entity, deferCacheClear);
// 清除系统缓存以确保系统能及时发现新实体
if (!deferCacheClear) {
this.clearSystemEntityCaches();
}
// 触发实体添加事件
this.eventSystem.emitSync('entity:added', { entity, scene: this });
return entity;
}