AI系统(一)
AIManager 管理所有阵营的AI
using System.Collections; using System.Collections.Generic; using UnityEngine; public enum Camp{ Player, Enemy, NPC, Build } public class AIManager : MonoBehaviour { private static AIManager instance; public static AIManager Instance { get { if (instance == null) { GameObject gameObject = new GameObject(); gameObject.name = "AIManager"; DontDestroyOnLoad(gameObject); instance = gameObject.AddComponent<AIManager>(); } return instance; } } //全部AI public Dictionary<Camp, List<GameObject>> enemy = new Dictionary<Camp, List<GameObject>>(); /// <summary> /// 注册怪物 /// </summary> /// <param name="camp"></param> /// <param name="gameObject"></param> public void Register(Camp camp,GameObject gameObject) { if (!enemy.ContainsKey(camp)) { enemy.Add(camp,new List<GameObject>()); } if (enemy[camp] == null) { enemy[camp] = new List<GameObject>(); } enemy[camp].Add(gameObject); } /// <summary> /// 销毁怪物时反注册 /// </summary> /// <param name="camp"></param> /// <param name="gameObject"></param> public void UnRegister(Camp camp,GameObject gameObject) { if (!enemy.ContainsKey(camp)) { return; } if (enemy[camp] == null) { return; } enemy[camp].Remove(gameObject); } /// <summary> /// 获取目标阵营的敌人 /// </summary> /// <param name="camp"></param> /// <returns></returns> public List<GameObject> GetTargetCamp(Camp camp) { if (!enemy.ContainsKey(camp)) { return null; } return enemy[camp]; } }