任务模块框架
#region Class Information /*----------------------------------------- // File: ICmd.cs // author: wugang // description: 命令策略 // Date: 2015-9-12 // ----------------------------------------*/ # endregion using UnityEngine; using System.Collections; /// <summary> /// 命令接口 所有策略类基础此接口 /// </summary> public interface ICmd { //回调 event Callback callback; //初始化、设置参数 void SetParams(params object[] objs); void Update(); void DoOnce(); void Stop(); }
/// <summary> /// 与npc对话策略实现 继承命令接口 /// </summary> public class ChatNpcCmd : ICmd { public event Callback callback; int m_mapId; int m_npcId; Vector3 npcPosition; public void SetParams(params object[] objs) { m_mapId = (int)objs[0]; m_npcId = (int)objs[1]; } public void Update() { //执行具体逻辑 //DOTO: //执行stop if (true) { Stop(); } } public void DoOnce() { //执行具体逻辑 } public void Stop() { //执行回调 if (callback != null) callback(); } }
#region Class Information /*----------------------------------------- // File: CmdContext.cs // author: wugang // description: 命令中间层 // Date: 2015-9-12 // ----------------------------------------*/ #endregion using UnityEngine; using System.Collections; public class CmdContext { ICmd m_Cmd; public CmdContext(ICmd cmd) { this.m_Cmd = cmd; } public void DoOnce() { m_Cmd.DoOnce(); } public void Update() { m_Cmd.Update(); } }
#region Class Information /*----------------------------------------- // File: CmdManager.cs // author: wugang // description: 命令管理器 // Date: 2015-9-12 // ----------------------------------------*/ # endregion using UnityEngine; using System.Collections; public class CmdManager : MonoBehaviour { bool m_Running = false; CmdContext m_CmdContext; public void StartCmd(TaskDataType type, params object[] objs) { switch (type) { case TaskDataType.ChatNpc: m_CmdContext = new CmdContext(new ChatNpcCmd()); break; case TaskDataType.KillMonster: break; case TaskDataType.GuildPortal: break; } m_Running = true; } public void Stop() { m_Running = false; m_CmdContext = null; } void Update() { if (!m_Running) return; if (m_CmdContext == null) { #if UNITY_EDITOR Debug.LogError("m_CmdContext is null!!!"); #endif return; } m_CmdContext.Update(); } }
------------------2017-2-17
最近接触IOC比较多,有时间的话可以通过IOC来重构一下该代码。