using System.Collections.Generic; using UnityEngine; public enum E_HeroAIType { None, NormalAuto, D2_LockMissionTarget, D3_BackToStartPos, D4_QLZF, D5_Hlyy, D6_RandomPoint, D7_ZMSL, KillUntilDie = 99, } public class HeroAIHandler { private Dictionary m_AIDict = null; public Dictionary aiData = null; private List m_AIList = null; private HeroAI_Base m_Default = null; private HeroAI_Base m_Current = null; public E_HeroAIType currentType; public int PriorityNpcID; public HeroAIHandler() { m_AIList = new List(); m_AIDict = new Dictionary(); aiData = new Dictionary(); m_AIList.Add((byte)E_HeroAIType.KillUntilDie); m_AIDict.Add((byte)E_HeroAIType.KillUntilDie, new HeroAI_KillUntilDie()); aiData.Add((byte)E_HeroAIType.KillUntilDie, new KillUntilDieData()); m_AIList.Add((byte)E_HeroAIType.NormalAuto); m_AIDict.Add((byte)E_HeroAIType.NormalAuto, new HeroAI_NormalAuto()); m_AIList.Add((byte)E_HeroAIType.D2_LockMissionTarget); m_AIDict.Add((byte)E_HeroAIType.D2_LockMissionTarget, new HeroAI_D2_LockMissionTarget()); m_AIList.Add((byte)E_HeroAIType.D3_BackToStartPos); m_AIDict.Add((byte)E_HeroAIType.D3_BackToStartPos, new HeroAI_D3_BackToStartPos()); m_AIList.Add((byte)E_HeroAIType.D4_QLZF); m_AIDict.Add((byte)E_HeroAIType.D4_QLZF, new HeroAI_D4_Qlzf()); m_AIList.Add((byte)E_HeroAIType.D5_Hlyy); m_AIDict.Add((byte)E_HeroAIType.D5_Hlyy, new HeroAI_D5_Hlyy()); m_AIList.Add((byte)E_HeroAIType.D6_RandomPoint); m_AIDict.Add((byte)E_HeroAIType.D6_RandomPoint, new HeroAI_D6_NearestPoint()); m_AIList.Add((byte)E_HeroAIType.D7_ZMSL); m_AIDict.Add((byte)E_HeroAIType.D7_ZMSL, new HeroAI_D7_Hlyy()); currentType = E_HeroAIType.None; } public void Update() { HeroAI_Base _chk = null; byte _aiType; if (m_Current != null) { // 当前状态更新 m_Current.Update(); // 如果当前状态结束 if (!m_Current.IsOver()) { return; } // 已经执行完毕, 退出状态 m_Current.Exit(); m_Current = null; currentType = E_HeroAIType.None; } // 遍历所有状态, 决定此次进入哪个状态 for (int i = 0; i < m_AIList.Count; ++i) { _aiType = m_AIList[i]; _chk = m_AIDict[_aiType]; if (m_Current != _chk) { // 判断每个AI状态是否达到进入条件 if (m_AIDict[_aiType].Condition()) { // 是否存在旧的状态 if (m_Current != null) { // 执行退出 m_Current.Exit(); } // 设置当前状态 m_Current = m_AIDict[_aiType]; // 进入 m_Current.Enter(); break; } } } } public bool IsAuto() { return currentType >= E_HeroAIType.NormalAuto && currentType < E_HeroAIType.KillUntilDie; } public bool IsPause() { if (!IsAuto()) { return false; } HeroAI_Auto _auto = m_Current as HeroAI_Auto; if (_auto == null || !_auto.IsPause()) { return false; } return true; } public void Stop() { m_Current = null; currentType = E_HeroAIType.None; } }