- 组合式单位与数值基础重构:scripts/Models/UnitModel.cs, scripts/Models/UnitComponents.cs, scripts/Models/StudentModel.cs, scripts/Models/MentorModel.cs, scripts/Models/StaffModel.cs, scripts/Models/PropertyValue.cs, scripts/Models/StatusValue.cs - 任务与运行时状态骨架:scripts/Models/Task.cs, scripts/Models/TaskDefinitions.cs, scripts/Models/GameState.cs - 配置与规则定义骨架:scripts/Models/DefinitionSupport.cs, scripts/Models/DomainEnums.cs, scripts/Models/Modifiers.cs, scripts/Models/DisciplineDefinitions.cs, scripts/Models/SynergyDefinitions.cs, scripts/Models/ItemDefinitions.cs, scripts/Models/PaperDefinitions.cs, scripts/Models/RogueliteDefinitions.cs, scripts/Models/GameContentDatabase.cs, scripts/Models/CoreIds.cs - MVC/会话/系统/i18n/Mod 支撑:scripts/Core/GameSession.cs, scripts/Core/GameSystems.cs, scripts/Core/GameController.cs, scripts/Core/Mvc.cs, scripts/Core/LocalizationService.cs, scripts/Core/ContentRegistry.cs, scripts/Core/ModManifest.cs, scripts/Core/EventBus.cs - 主控流程衔接:scripts/GameManager.cs
59 lines
1.2 KiB
C#
59 lines
1.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
|
||
namespace Core;
|
||
|
||
/// <summary>
|
||
/// 轻量事件总线(解耦系统之间的通信)
|
||
/// 设计说明:
|
||
/// 1) 系统层发布 DomainEvent,View/Controller 可订阅。
|
||
/// 2) 不依赖 Godot Signal,便于纯逻辑测试。
|
||
/// 注意事项:
|
||
/// - 事件为进程内同步调用,避免在事件里做耗时操作。
|
||
/// 未来扩展:
|
||
/// - 可加入“事件队列”和“延迟派发”以支持回合结算。
|
||
/// </summary>
|
||
public sealed class DomainEventBus
|
||
{
|
||
private readonly Dictionary<Type, List<Delegate>> _handlers = new();
|
||
|
||
public void Subscribe<T>(Action<T> handler)
|
||
{
|
||
var type = typeof(T);
|
||
if (!_handlers.TryGetValue(type, out var list))
|
||
{
|
||
list = new List<Delegate>();
|
||
_handlers[type] = list;
|
||
}
|
||
|
||
list.Add(handler);
|
||
}
|
||
|
||
public void Unsubscribe<T>(Action<T> handler)
|
||
{
|
||
var type = typeof(T);
|
||
if (_handlers.TryGetValue(type, out var list))
|
||
{
|
||
list.Remove(handler);
|
||
}
|
||
}
|
||
|
||
public void Publish<T>(T evt)
|
||
{
|
||
var type = typeof(T);
|
||
if (!_handlers.TryGetValue(type, out var list))
|
||
{
|
||
return;
|
||
}
|
||
|
||
foreach (var handler in list)
|
||
{
|
||
if (handler is Action<T> typedHandler)
|
||
{
|
||
typedHandler.Invoke(evt);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|