using System; using System.Collections.Generic; namespace Core; /// /// 轻量事件总线(解耦系统之间的通信) /// 设计说明: /// 1) 系统层发布 DomainEvent,View/Controller 可订阅。 /// 2) 不依赖 Godot Signal,便于纯逻辑测试。 /// 注意事项: /// - 事件为进程内同步调用,避免在事件里做耗时操作。 /// 未来扩展: /// - 可加入“事件队列”和“延迟派发”以支持回合结算。 /// public sealed class DomainEventBus { private readonly Dictionary> _handlers = new(); public void Subscribe(Action handler) { var type = typeof(T); if (!_handlers.TryGetValue(type, out var list)) { list = new List(); _handlers[type] = list; } list.Add(handler); } public void Unsubscribe(Action handler) { var type = typeof(T); if (_handlers.TryGetValue(type, out var list)) { list.Remove(handler); } } public void Publish(T evt) { var type = typeof(T); if (!_handlers.TryGetValue(type, out var list)) { return; } foreach (var handler in list) { if (handler is Action typedHandler) { typedHandler.Invoke(evt); } } } }