* 通知相关系统实体的组件发生了变化 * * 这是事件驱动设计的核心:当组件被添加或移除时,立即通知相关系统检查该实体是否匹配, * 并触发 onAdded/onRemoved 回调。通过组件ID索引优化,只通知关心该组件的系统。 * * This is the core of event-driven design: when a component is added or removed, * immediately notify relevant systems to check if the entity matches an
(entity: Entity, changedComponentType?: ComponentType)
| 656 | * @param changedComponentType 变化的组件类型(可选) | The changed component type (optional) |
| 657 | */ |
| 658 | public notifyEntityComponentChanged(entity: Entity, changedComponentType?: ComponentType): void { |
| 659 | const notifiedSystems = new Set<EntitySystem>(); |
| 660 | |
| 661 | if (changedComponentType && this.componentRegistry.isRegistered(changedComponentType)) { |
| 662 | const componentId = this.componentRegistry.getBitIndex(changedComponentType); |
| 663 | const interestedSystems = this._componentIdToSystems.get(componentId); |
| 664 | |
| 665 | if (interestedSystems) { |
| 666 | for (const system of interestedSystems) { |
| 667 | system.handleEntityComponentChanged(entity); |
| 668 | notifiedSystems.add(system); |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | for (const system of this._globalNotifySystems) { |
| 674 | if (!notifiedSystems.has(system)) { |
| 675 | system.handleEntityComponentChanged(entity); |
| 676 | notifiedSystems.add(system); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | if (!changedComponentType) { |
| 681 | for (const system of this.systems) { |
| 682 | if (!notifiedSystems.has(system)) { |
| 683 | system.handleEntityComponentChanged(entity); |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * 将系统添加到组件索引 |
no test coverage detected