SceneCmptManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 场景组件 机关/道具 管理者
  6. /// </summary>
  7. public class SceneCmptManager : MonoSingleton<SceneCmptManager>
  8. {
  9. /// <summary>
  10. /// 所有宝箱
  11. /// </summary>
  12. private List<GameObject> _allTreasureBox;
  13. /// <summary>
  14. /// 追踪的宝箱
  15. /// </summary>
  16. private List<GameObject> _allTrackBox;
  17. /// <summary>
  18. /// 打开的宝箱
  19. /// </summary>
  20. private List<GameObject> _openTreasureBox;
  21. /// <summary>
  22. /// 可破坏物体
  23. /// </summary>
  24. private List<GameObject> _damageCmpt;
  25. private List<GameObject> _mapSwitchList;
  26. private List<GameObject> _backDoorList;
  27. /// <summary>
  28. /// 机关 装置
  29. /// </summary>
  30. private List<GameObject> _allMechanism;
  31. private List<GameObject> _openMechanism;
  32. private void Awake()
  33. {
  34. _allTreasureBox = new List<GameObject>();
  35. _allTrackBox = new List<GameObject>();
  36. _openTreasureBox = new List<GameObject>();
  37. _allMechanism = new List<GameObject>();
  38. _openMechanism = new List<GameObject>();
  39. _damageCmpt = new List<GameObject>();
  40. _mapSwitchList = new List<GameObject>();
  41. _backDoorList = new List<GameObject>();
  42. }
  43. // Start is called before the first frame update
  44. void Start()
  45. {
  46. }
  47. // Update is called once per frame
  48. void Update()
  49. {
  50. }
  51. public void Reset()
  52. {
  53. _allTreasureBox.Clear();
  54. _allTrackBox.Clear();
  55. _openTreasureBox.Clear();
  56. _allMechanism.Clear();
  57. _openMechanism.Clear();
  58. _damageCmpt.Clear();
  59. _mapSwitchList.Clear();
  60. _backDoorList.Clear();
  61. }
  62. public List<GameObject> GetAllTreasureBox()
  63. {
  64. return _allTreasureBox;
  65. }
  66. /// <summary>
  67. /// 添加宝箱
  68. /// </summary>
  69. /// <param name="obj"></param>
  70. public void AddTreasureBox(GameObject obj)
  71. {
  72. _allTreasureBox.Add(obj);
  73. // 网格优化分配
  74. if(GameObject.Find("GridMap"))
  75. {
  76. MyGridMap gridMap = GameObject.Find("GridMap").GetComponent<MyGridMap>();
  77. gridMap.AddOneObjToGrid(obj);
  78. }
  79. }
  80. /// <summary>
  81. /// 移除宝箱
  82. /// </summary>
  83. /// <param name="obj"></param>
  84. public void RemoveTreasureBox(GameObject obj)
  85. {
  86. _allTreasureBox.Remove(obj);
  87. }
  88. /// <summary>
  89. /// 打开宝箱
  90. /// </summary>
  91. /// <param name="obj"></param>
  92. public void OpenTreasureBox(GameObject obj)
  93. {
  94. _openTreasureBox.Add(obj);
  95. }
  96. public List<GameObject> GetAllTrackBox()
  97. {
  98. return _allTrackBox;
  99. }
  100. /// <summary>
  101. /// 添加宝箱
  102. /// </summary>
  103. /// <param name="obj"></param>
  104. public void AddTrackBox(GameObject obj)
  105. {
  106. _allTrackBox.Add(obj);
  107. }
  108. public void RemoveTrackBox(GameObject obj)
  109. {
  110. _allTrackBox.Remove(obj);
  111. }
  112. /// <summary>
  113. /// 获取打开宝箱数量
  114. /// </summary>
  115. /// <returns></returns>
  116. public int GetOpenBoxNum()
  117. {
  118. return _openTreasureBox.Count;
  119. }
  120. /// <summary>
  121. /// 获取宝箱总数
  122. /// </summary>
  123. /// <returns></returns>
  124. public int GetAllBoxNum()
  125. {
  126. return _allTreasureBox.Count;
  127. }
  128. /// <summary>
  129. /// 所有宝箱是否开启
  130. /// </summary>
  131. /// <returns></returns>
  132. public bool IsAllTreasureBoxFinish()
  133. {
  134. return _allTreasureBox.Count == _openTreasureBox.Count;
  135. }
  136. /// <summary>
  137. /// 添加机关
  138. /// </summary>
  139. /// <param name="obj"></param>
  140. public void AddMechanism(GameObject obj)
  141. {
  142. _allMechanism.Add(obj);
  143. // 网格优化分配
  144. if (GameObject.Find("GridMap") != null)
  145. {
  146. MyGridMap gridMap = GameObject.Find("GridMap").GetComponent<MyGridMap>();
  147. gridMap.AddOneObjToGrid(obj);
  148. }
  149. }
  150. /// <summary>
  151. /// 移除机关
  152. /// </summary>
  153. /// <param name="obj"></param>
  154. public void RemoveMechanism(GameObject obj)
  155. {
  156. _allMechanism.Remove(obj);
  157. }
  158. /// <summary>
  159. /// 触发机关
  160. /// </summary>
  161. /// <param name="obj"></param>
  162. public void OpenMechanism(GameObject obj)
  163. {
  164. _openMechanism.Add(obj);
  165. }
  166. /// <summary>
  167. /// 获取触发机关数量
  168. /// </summary>
  169. /// <returns></returns>
  170. public int GetMechanismNumber()
  171. {
  172. return _openMechanism.Count;
  173. }
  174. /// <summary>
  175. /// 是否避让所有机关?
  176. /// </summary>
  177. /// <returns></returns>
  178. public bool IsAllMechanismFinish()
  179. {
  180. return _openMechanism.Count == 0;
  181. }
  182. /// <summary>
  183. /// 获取所有可破坏组件
  184. /// </summary>
  185. /// <returns></returns>
  186. public List<GameObject> GetAllDamageCmpt()
  187. {
  188. return _damageCmpt;
  189. }
  190. public void AddDamageCmpt(GameObject obj)
  191. {
  192. this._damageCmpt.Add(obj);
  193. // 网格优化分配
  194. GameObject gridMap = GameObject.Find("GridMap");
  195. if(gridMap)
  196. {
  197. gridMap.GetComponent<MyGridMap>().AddOneObjToGrid(obj);
  198. }
  199. }
  200. public void RemoveDamageCmpt(GameObject obj)
  201. {
  202. this._damageCmpt.Remove(obj);
  203. }
  204. public void AddMapSwitchCmpt(GameObject obj)
  205. {
  206. this._mapSwitchList.Add(obj);
  207. //// 网格优化分配
  208. //if(GameObject.Find("GridMap"))
  209. //{
  210. // MyGridMap gridMap = GameObject.Find("GridMap").GetComponent<MyGridMap>();
  211. // gridMap.AddOneObjToGrid(obj);
  212. //}
  213. }
  214. public void RemoveMapSwitchCmpt(GameObject obj)
  215. {
  216. this._mapSwitchList.Remove(obj);
  217. }
  218. public SceneEventMapSwitchLogic GetMapSwitchCmptByIndex(int index)
  219. {
  220. if(_mapSwitchList == null)
  221. {
  222. return null;
  223. }
  224. for(int i = 0; i < _mapSwitchList.Count; ++i)
  225. {
  226. SceneEventMapSwitchLogic mapSwitch = _mapSwitchList[i].GetComponent<SceneEventMapSwitchLogic>();
  227. if(mapSwitch.info.switchIndex == index)
  228. {
  229. return mapSwitch;
  230. }
  231. }
  232. return null;
  233. }
  234. public List<GameObject> GetAllMapSwitchCmpt()
  235. {
  236. return this._mapSwitchList;
  237. }
  238. public void AddBackDoorCmpt(GameObject obj)
  239. {
  240. this._backDoorList.Add(obj);
  241. // 网格优化分配
  242. MyGridMap gridMap = GameObject.Find("GridMap").GetComponent<MyGridMap>();
  243. gridMap.AddOneObjToGrid(obj);
  244. }
  245. public void RemoveBackDoorCmpt(GameObject obj)
  246. {
  247. this._backDoorList.Remove(obj);
  248. }
  249. public SceneEventBackDoorLogic GetBackDoorCmptByIndex(int index)
  250. {
  251. if (_backDoorList == null)
  252. {
  253. return null;
  254. }
  255. for (int i = 0; i < _backDoorList.Count; ++i)
  256. {
  257. SceneEventBackDoorLogic bd = _backDoorList[i].GetComponent<SceneEventBackDoorLogic>();
  258. if (bd.info.Index == index)
  259. {
  260. return bd;
  261. }
  262. }
  263. return null;
  264. }
  265. public List<GameObject> GetAllBackDoorCmpt(GameObject obj)
  266. {
  267. return this._backDoorList;
  268. }
  269. /// <summary>
  270. /// 获取离指定玩家最近 且距离小于10米的 可破坏组件
  271. /// </summary>
  272. /// <param name="player"></param>
  273. /// <returns></returns>
  274. public GameObject GetDistanceMinDamageObj(Role player)
  275. {
  276. float minDis = 10;
  277. GameObject cmptObj = null;
  278. foreach(var item in _damageCmpt)
  279. {
  280. if (item == null)
  281. {
  282. continue;
  283. }
  284. float dis = Vector3.Distance(item.transform.position, player.transform.position);
  285. if(dis < minDis)
  286. {
  287. cmptObj = item;
  288. dis = minDis;
  289. }
  290. }
  291. return cmptObj;
  292. }
  293. }