querysystem内部框架维护(不需要用户手动调用事件派发)

新增test覆盖测试
This commit is contained in:
YHH
2025-07-28 17:14:10 +08:00
parent ea06a9f07d
commit abec2b3648
11 changed files with 2934 additions and 11 deletions

View File

@@ -151,8 +151,8 @@ export class Core {
// 初始化对象池管理器
this._poolManager = PoolManager.getInstance();
Core.entitySystemsEnabled = this._config.enableEntitySystems || true;
this.debug = this._config.debug || true;
Core.entitySystemsEnabled = this._config.enableEntitySystems ?? true;
this.debug = this._config.debug ?? true;
// 初始化调试管理器
if (this._config.debugConfig?.enabled) {

View File

@@ -346,6 +346,23 @@ export class EntityManager {
// 设置Entity的静态事件总线引用
Entity.eventBus = this._eventBus;
// 监听组件事件来同步更新索引
this._eventBus.on('component:added', (data: any) => {
const entity = this._entities.get(data.entityId);
if (entity) {
this._componentIndexManager.addEntity(entity);
this._archetypeSystem.addEntity(entity);
}
});
this._eventBus.on('component:removed', (data: any) => {
const entity = this._entities.get(data.entityId);
if (entity) {
this._componentIndexManager.removeEntity(entity);
this._archetypeSystem.removeEntity(entity);
}
});
}
/**
@@ -518,7 +535,8 @@ export class EntityManager {
* @returns 具有指定标签的实体数组
*/
public getEntitiesByTag(tag: number): Entity[] {
return [...(this._entitiesByTag.get(tag) || [])];
return Array.from(this._entities.values())
.filter(entity => entity.tag === tag);
}
/**

View File

@@ -6,6 +6,7 @@ import { EntitySystem } from './Systems/EntitySystem';
import { ComponentStorageManager } from './Core/ComponentStorage';
import { QuerySystem } from './Core/QuerySystem';
import { TypeSafeEventSystem, GlobalEventSystem } from './Core/EventSystem';
import { EventBus } from './Core/EventBus';
/**
* 游戏场景类
@@ -99,6 +100,16 @@ export class Scene {
this.querySystem = new QuerySystem();
this.eventSystem = new TypeSafeEventSystem();
if (!Entity.eventBus) {
Entity.eventBus = new EventBus(false);
}
if (Entity.eventBus) {
Entity.eventBus.onComponentAdded((data: any) => {
this.eventSystem.emitSync('component:added', data);
});
}
this.initialize();
}
@@ -189,6 +200,9 @@ export class Scene {
*/
public createEntity(name: string) {
let entity = new Entity(name, this.identifierPool.checkOut());
this.eventSystem.emitSync('entity:created', { entityName: name, entity, scene: this });
return this.addEntity(entity);
}
@@ -266,9 +280,7 @@ export class Scene {
* 从场景中删除所有实体
*/
public destroyAllEntities() {
for (let i = 0; i < this.entities.count; i++) {
this.entities.buffer[i].destroy();
}
this.entities.removeAllEntities();
}
/**
@@ -322,6 +334,10 @@ export class Scene {
* @param processor 处理器
*/
public addEntityProcessor(processor: EntitySystem) {
if (this.entityProcessors.processors.includes(processor)) {
return processor;
}
processor.scene = this;
this.entityProcessors.add(processor);
@@ -343,6 +359,7 @@ export class Scene {
*/
public removeEntityProcessor(processor: EntitySystem) {
this.entityProcessors.remove(processor);
processor.scene = null;
}
/**

View File

@@ -79,16 +79,16 @@ export abstract class EntitySystem implements ISystemBase {
this._systemName = this.constructor.name;
}
private _scene!: Scene;
private _scene: Scene | null = null;
/**
* 这个系统所属的场景
*/
public get scene(): Scene {
public get scene(): Scene | null {
return this._scene;
}
public set scene(value: Scene) {
public set scene(value: Scene | null) {
this._scene = value;
this._entities = [];
}
@@ -108,7 +108,9 @@ export abstract class EntitySystem implements ISystemBase {
*/
public setUpdateOrder(order: number): void {
this._updateOrder = order;
this.scene.entityProcessors.setDirty();
if (this.scene && this.scene.entityProcessors) {
this.scene.entityProcessors.setDirty();
}
}
/**

View File

@@ -71,7 +71,11 @@ export class EntityProcessorList {
public update(): void {
this.sortProcessors();
for (const processor of this._processors) {
processor.update();
try {
processor.update();
} catch (error) {
console.error(`Error in processor ${processor.constructor.name}:`, error);
}
}
}