using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace YLBattle { /// /// 战场事件处理中心 /// public class FieldEventDispatcher : IEventDispatcher { /// /// 上下文环境信息 /// private BattleParam env = null; /// /// 事件队列 /// private Queue mFieldEventQueue = new Queue(); /// /// 事件处理器映射表 /// private Dictionary> mHandle = new Dictionary>(); /// /// 事件ID生成器 /// private int mEventIDGenerator = 0; /// /// 对象事件 /// private BattleFighterEventHandler warriorEventHandle = new BattleFighterEventHandler(); /// /// 初始化 /// /// 环境信息 public void Initialize(BattleParam env) { this.env = env; this.mFieldEventQueue.Clear(); this.mHandle.Clear(); this.mEventIDGenerator = 0; this.warriorEventHandle.Initialize(env); } /// /// 事件投递 /// /// 事件信息 public void PostEvent(FieldEvent ev) { if (ev == null) { return; } this.mFieldEventQueue.Enqueue(ev); } /// /// 创建事件对象 /// /// 事件类型 /// 事件对象 public FieldEvent MakeEvent(EFieldEventType type) { FieldEvent fe = new FieldEvent(); fe.id = ++this.mEventIDGenerator; fe.type = type; fe.ts = DateTime.Now.ToFileTimeUtc(); return fe; } /// /// 事件处理注册 /// /// 事件类型 /// 事件处理器 public void RegisterEventHandle(EFieldEventType type, Action handle) { if (this.mHandle.ContainsKey(type)) { this.env.DebugHelper().Warning("事件重复注册,编号:" + type.ToString()); return; } //this.env.DebugHelper().Error(type.ToString()); this.mHandle.Add(type, handle); } /// /// 查询事件处理器 /// /// 事件类型 /// 处理器 private Action FindEventHandle(EFieldEventType type) { if (this.mHandle.ContainsKey(type)) { return this.mHandle[type]; } return null; } /// /// 调度战场事件 /// /// 当前逻辑时间 public void OnUpdateFieldEvent(long now) { if (0 == this.mFieldEventQueue.Count) { return; } FieldEvent ev = null; Action handler = null; while (this.mFieldEventQueue.Count > 0) { ev = this.mFieldEventQueue.Dequeue(); handler = this.FindEventHandle(ev.type); if (null == handler) { this.env.DebugHelper().Warning("未知战斗事件:" + ev.type.ToString()); continue; } handler.Invoke(ev); } } } }