refactor(core): 移除全局EventBus,实现场景级事件隔离 (#211)

This commit is contained in:
YHH
2025-11-01 18:19:23 +08:00
committed by GitHub
parent 620f3eecc7
commit 9c1bf8dbed
2 changed files with 4 additions and 22 deletions

View File

@@ -1,6 +1,5 @@
import { Component } from './Component'; import { Component } from './Component';
import { ComponentRegistry, ComponentType } from './Core/ComponentStorage'; import { ComponentRegistry, ComponentType } from './Core/ComponentStorage';
import { EventBus } from './Core/EventBus';
import { BitMask64Utils, BitMask64Data } from './Utils/BigIntCompatibility'; import { BitMask64Utils, BitMask64Data } from './Utils/BigIntCompatibility';
import { createLogger } from '../Utils/Logger'; import { createLogger } from '../Utils/Logger';
import { getComponentInstanceTypeName, getComponentTypeName } from './Decorators'; import { getComponentInstanceTypeName, getComponentTypeName } from './Decorators';
@@ -70,12 +69,6 @@ export class Entity {
*/ */
public static entityComparer: EntityComparer = new EntityComparer(); public static entityComparer: EntityComparer = new EntityComparer();
/**
* 全局事件总线实例
* 用于发射组件相关事件
*/
public static eventBus: EventBus | null = null;
/** /**
* 实体名称 * 实体名称
*/ */
@@ -428,8 +421,8 @@ export class Entity {
} }
component.onAddedToEntity(); component.onAddedToEntity();
if (Entity.eventBus) { if (this.scene && this.scene.eventSystem) {
Entity.eventBus.emitComponentAdded({ this.scene.eventSystem.emitSync('component:added', {
timestamp: Date.now(), timestamp: Date.now(),
source: 'Entity', source: 'Entity',
entityId: this.id, entityId: this.id,
@@ -561,8 +554,8 @@ export class Entity {
component.entityId = null; component.entityId = null;
if (Entity.eventBus) { if (this.scene && this.scene.eventSystem) {
Entity.eventBus.emitComponentRemoved({ this.scene.eventSystem.emitSync('component:removed', {
timestamp: Date.now(), timestamp: Date.now(),
source: 'Entity', source: 'Entity',
entityId: this.id, entityId: this.id,

View File

@@ -5,7 +5,6 @@ import { EntitySystem } from './Systems/EntitySystem';
import { ComponentStorageManager, ComponentRegistry, ComponentType } from './Core/ComponentStorage'; import { ComponentStorageManager, ComponentRegistry, ComponentType } from './Core/ComponentStorage';
import { QuerySystem } from './Core/QuerySystem'; import { QuerySystem } from './Core/QuerySystem';
import { TypeSafeEventSystem } from './Core/EventSystem'; import { TypeSafeEventSystem } from './Core/EventSystem';
import { EventBus } from './Core/EventBus';
import { ReferenceTracker } from './Core/ReferenceTracker'; import { ReferenceTracker } from './Core/ReferenceTracker';
import { IScene, ISceneConfig } from './IScene'; import { IScene, ISceneConfig } from './IScene';
import { getComponentInstanceTypeName, getSystemInstanceTypeName, getSystemMetadata } from './Decorators'; import { getComponentInstanceTypeName, getSystemInstanceTypeName, getSystemMetadata } from './Decorators';
@@ -245,16 +244,6 @@ export class Scene implements IScene {
if (config?.name) { if (config?.name) {
this.name = config.name; this.name = config.name;
} }
if (!Entity.eventBus) {
Entity.eventBus = new EventBus(false);
}
if (Entity.eventBus) {
Entity.eventBus.onComponentAdded((data: unknown) => {
this.eventSystem.emitSync('component:added', data);
});
}
} }
/** /**