Map.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. using System.Xml;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class Map
  6. {
  7. public const float TILE_WIDTH = 1f;
  8. public const float TILE_LENGTH = 1f;
  9. public MapData.MapID id;
  10. public bool isEditing;
  11. private int _columns;
  12. private int _rows;
  13. private string _name;
  14. private Dictionary<int, BattleObject> battleObjectDict;
  15. private Dictionary<int, CrystalBase> crystalDict;
  16. private List<CrystalBase> crystalList;
  17. private Dictionary<int, Door> doorDict;
  18. private List<Door> doorList;
  19. private Dictionary<int, MapItem> itemDict;
  20. private List<MapItem> itemList;
  21. private Dictionary<int, MapItemPos> itemPosDict;
  22. private List<MapItemPos> itemPosList;
  23. private Dictionary<TeamUtil.Team, FlagItem> flagDict;
  24. private Dictionary<TeamUtil.Team, FlagBase> flagBaseDict;
  25. private Dictionary<TeamUtil.Team, StartPos> startPosDict;
  26. private Dictionary<int, UAVehicle> uavDict;
  27. private List<AStarNode> teamBlockList;
  28. private List<Block> blockList;
  29. private XmlNode mapNode;
  30. public Map()
  31. {
  32. battleObjectDict = new Dictionary<int, BattleObject>();
  33. crystalDict = new Dictionary<int, CrystalBase>();
  34. crystalList = new List<CrystalBase>();
  35. doorDict = new Dictionary<int, Door>();
  36. doorList = new List<Door>();
  37. itemDict = new Dictionary<int, MapItem>();
  38. itemList = new List<MapItem>();
  39. itemPosDict = new Dictionary<int, MapItemPos>();
  40. itemPosList = new List<MapItemPos>();
  41. flagDict = new Dictionary<TeamUtil.Team, FlagItem>();
  42. flagBaseDict = new Dictionary<TeamUtil.Team, FlagBase>();
  43. startPosDict = new Dictionary<TeamUtil.Team, StartPos>();
  44. uavDict = new Dictionary<int, UAVehicle> ();
  45. teamBlockList = new List<AStarNode>();
  46. blockList = new List<Block>();
  47. }
  48. public static int XToColumn(float x)
  49. {
  50. return (int)(x/TILE_WIDTH);
  51. }
  52. public static int ZToRow(float z)
  53. {
  54. return (int)(z/TILE_LENGTH);
  55. }
  56. public static float GetCenterX(int col)
  57. {
  58. return col*TILE_WIDTH+TILE_WIDTH/2f;
  59. }
  60. public static float GetCenterZ(int row)
  61. {
  62. return row*TILE_WIDTH+TILE_LENGTH/2f;
  63. }
  64. public int GetIndexByGrid(int col, int row)
  65. {
  66. if(!IsInRange(col, row))
  67. return -1;
  68. return row * columns + col;
  69. }
  70. public Vector2 GetGridByIndex(int index)
  71. {
  72. int col = index % rows;
  73. int row = index / columns;
  74. return new Vector2(col, row);
  75. }
  76. public bool IsInRange(int col, int row)
  77. {
  78. if(col < 0 || col >= columns || row < 0 || row >= rows)
  79. return false;
  80. return true;
  81. }
  82. public string name
  83. {
  84. get{return _name;}
  85. }
  86. public int columns
  87. {
  88. get{return _columns;}
  89. }
  90. public int rows
  91. {
  92. get{return _rows;}
  93. }
  94. public void CreateMap()
  95. {
  96. CreateBlock();
  97. CreateBuilding();
  98. }
  99. private void CreateBlock()
  100. {
  101. Block[] blockArr = GameObject.FindObjectsOfType<Block> ();
  102. for(int i=0; i<blockArr.Length; i++)
  103. {
  104. this.blockList.Add(blockArr[i]);
  105. }
  106. }
  107. private void CreateBuilding()
  108. {
  109. CrystalBase[] crystalBaseArr = GameObject.FindObjectsOfType<CrystalBase> ();
  110. for(int i=0; i<crystalBaseArr.Length; i++)
  111. {
  112. CrystalBase crystalBase = crystalBaseArr[i];
  113. crystalDict.Add(crystalBase.id, crystalBase);
  114. crystalList.Add(crystalBase);
  115. }
  116. FlagBase[] flagBaseArr = GameObject.FindObjectsOfType<FlagBase> ();
  117. for(int i=0; i<flagBaseArr.Length; i++)
  118. {
  119. FlagBase flagBase = flagBaseArr [i];
  120. flagBase.Init(this);
  121. this.flagBaseDict.Add(flagBase.team, flagBase);
  122. }
  123. Door[] doorArr = GameObject.FindObjectsOfType<Door> ();
  124. for(int i=0; i<doorArr.Length; i++)
  125. {
  126. Door door = doorArr[i];
  127. door.Init (this);
  128. door.typeId = MapObjectUtil.TypeId.Door.GetHashCode();
  129. this.doorDict.Add(door.id, door);
  130. this.doorList.Add(door);
  131. }
  132. MapItemPos[] mapItemPosArr = GameObject.FindObjectsOfType<MapItemPos> ();
  133. for(int i=0; i<mapItemPosArr.Length; i++)
  134. {
  135. MapItemPos mapItemPos = mapItemPosArr [i];
  136. mapItemPos.typeId = MapObjectUtil.TypeId.MapItemPos.GetHashCode ();
  137. mapItemPos.Init(this);
  138. this.itemPosDict.Add(mapItemPos.id, mapItemPos);
  139. this.itemPosList.Add(mapItemPos);
  140. }
  141. StartPos[] startPosArr = GameObject.FindObjectsOfType<StartPos> ();
  142. for(int i=0; i<startPosArr.Length; i++)
  143. {
  144. StartPos startPos = startPosArr[i];
  145. startPos.Init(this);
  146. this.startPosDict.Add(startPos.team, startPos);
  147. }
  148. }
  149. public Vector3 GetStartPosition(Player player)
  150. {
  151. Vector3 pos = Vector3.zero;
  152. if(startPosDict.ContainsKey(player.team))
  153. {
  154. return startPosDict[player.team].GetStartPosition();
  155. }
  156. float distance = float.MaxValue;
  157. List<MapBase> list = new List<MapBase>();
  158. for(int i=0; i<crystalList.Count; i++)
  159. {
  160. CrystalBase crystalBase = crystalList[i];
  161. if(crystalBase.startPositionDirect == MapObject.StartPostionDirect.None)
  162. continue;
  163. if(crystalBase.isStart >= 0 && crystalBase.GetStation() != null && crystalBase.GetStation().team == player.team)
  164. {
  165. Player.Hero hero = player.GetHero();
  166. if(hero.IsFirstTimeSelectHero())
  167. {
  168. if(crystalBase.isStart > 0)
  169. list.Add(crystalBase);
  170. }
  171. else
  172. {
  173. float d = NumberUtil.distanceVector3(crystalBase.position, hero.deadPostion);
  174. if(d < distance)
  175. {
  176. distance = d;
  177. pos = crystalBase.position;
  178. list.Clear();
  179. list.Add(crystalBase);
  180. }
  181. else if(d == distance+5f)
  182. {
  183. list.Add(crystalBase);
  184. }
  185. }
  186. }
  187. }
  188. // if(player.IsFirstTimeSelectHero() && flagBaseDict.ContainsKey(player.team))
  189. // {
  190. // list.Add(flagBaseDict[player.team]);
  191. // }
  192. if(list.Count > 0)
  193. {
  194. int i = Random.Range(0, list.Count);
  195. return list[i].GetStartPosition();
  196. }
  197. else if(flagBaseDict.ContainsKey(player.team))
  198. {
  199. return flagBaseDict[player.team].GetStartPosition();
  200. }
  201. return Vector3.zero;
  202. }
  203. public CrystalBase GetCrystalBase(int id)
  204. {
  205. if(crystalDict.ContainsKey(id))
  206. return crystalDict [id];
  207. return null;
  208. }
  209. public List<CrystalBase> GetCrystalBaseList()
  210. {
  211. return crystalList;
  212. }
  213. public CrystalBase GetInRangeCrystalBase(Vector3 pos)
  214. {
  215. for(int i=0; i<crystalList.Count; i++)
  216. {
  217. CrystalBase c = crystalList[i];
  218. float d = NumberUtil.distanceVector3(pos, c.position);
  219. if(d < c.range)
  220. {
  221. return c;
  222. }
  223. }
  224. return null;
  225. }
  226. public Station CreateStation(StationData data, BattleController battleController)
  227. {
  228. GameObject stationObj = null;
  229. if(id == MapData.MapID.Challenge)
  230. stationObj = GameObject.Instantiate(Resources.Load(Config.MAP_OBJECT_FOLDER + "CenterStation")) as GameObject;
  231. else
  232. stationObj = GameObject.Instantiate(Resources.Load(Config.STATION_PREFAB)) as GameObject;
  233. CrystalBase crystalBase = GetCrystalBase(data.crystalId);
  234. Station station = stationObj.GetComponent<Station>();
  235. station.Init(this);
  236. station.id = data.id;
  237. station.userId = data.userId;
  238. station.team = data.team;
  239. station.typeId = MapObjectUtil.TypeId.Station.GetHashCode();
  240. station.aiType = AI.AIType.Station;
  241. station.position = crystalBase.position;
  242. crystalBase.SetStation(station);
  243. AddBattleObject (station);
  244. stationObj.AddComponent<StationAI>().init(battleController);
  245. return station;
  246. }
  247. public void ClearBlocks(TeamUtil.Team team)
  248. {
  249. if(blockList.Count > 0)
  250. {
  251. for(int i=blockList.Count-1; i>=0; i--)
  252. {
  253. Block block = blockList[i];
  254. if(true || block.team == team)
  255. {
  256. block.Remove();
  257. blockList.RemoveAt(i);
  258. }
  259. }
  260. }
  261. }
  262. public Door GetDoorByIndex(int index)
  263. {
  264. if(doorDict.ContainsKey(index))
  265. return doorDict[index];
  266. return null;
  267. }
  268. public List<Door> GetDoorList()
  269. {
  270. return doorList;
  271. }
  272. public void RemoveDoor(Door door)
  273. {
  274. doorDict.Remove(door.id);
  275. doorList.Remove(door);
  276. CheckDoorRemove();
  277. }
  278. public void RemoveNoIdDoor()
  279. {
  280. foreach(KeyValuePair<int, Door> kvp in doorDict)
  281. {
  282. if(kvp.Value.id == 0)
  283. {
  284. doorDict.Remove(kvp.Key);
  285. doorList.Remove(kvp.Value);
  286. GameObject.Destroy(kvp.Value.gameObject);
  287. }
  288. }
  289. CheckDoorRemove();
  290. }
  291. private void CheckDoorRemove()
  292. {
  293. int blueCount = 0;
  294. int redCount = 0;
  295. for(int i=0; i<doorList.Count; i++)
  296. {
  297. Door door = doorList[i];
  298. if(door.team == TeamUtil.Team.Blue)
  299. {
  300. blueCount++;
  301. }
  302. else if(door.team == TeamUtil.Team.Red)
  303. {
  304. redCount++;
  305. }
  306. }
  307. // if(blueCount == 0)
  308. // ClearBlocks(TeamUtil.Team.Blue);
  309. // if(redCount == 0)
  310. // ClearBlocks(TeamUtil.Team.Red);
  311. }
  312. public MapItemPos GetMapItemPos(int id)
  313. {
  314. if(itemPosDict.ContainsKey(id))
  315. return itemPosDict[id];
  316. return null;
  317. }
  318. public List<MapItemPos> GetMapItemPosList()
  319. {
  320. return itemPosList;
  321. }
  322. public MapItem CreateMapItem(MapItemData data)
  323. {
  324. if(itemDict.ContainsKey(data.id))
  325. {
  326. return itemDict [data.id];
  327. }
  328. GameObject mapItemObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>(Config.BIGGER_ITEM_PREFAB));
  329. MapItem mapItem = mapItemObj.GetComponent<MapItem>();
  330. mapItem.Init(this);
  331. mapItem.id = data.id;
  332. mapItem.team = data.team;
  333. mapItem.SetPosition(data.postion);
  334. itemDict.Add(mapItem.id, mapItem);
  335. itemList.Add(mapItem);
  336. return mapItem;
  337. }
  338. public MapItem GetMapItem(int id)
  339. {
  340. if(itemDict.ContainsKey(id))
  341. return itemDict[id];
  342. return null;
  343. }
  344. public List<MapItem> GetMapItemByRange(Vector3 originPos, float distance)
  345. {
  346. List<MapItem> list = new List<MapItem>();
  347. for(int i=0; i<itemList.Count; i++)
  348. {
  349. MapItem mapItem = itemList[i];
  350. if(NumberUtil.distanceVector3(originPos, mapItem.position, true) <= distance)
  351. list.Add(mapItem);
  352. }
  353. return list;
  354. }
  355. public List<MapItem> GetMapItemList()
  356. {
  357. return itemList;
  358. }
  359. public void RemoveMapItem(MapItem mapItem)
  360. {
  361. if(mapItem != null)
  362. {
  363. itemDict.Remove(mapItem.id);
  364. itemList.Remove(mapItem);
  365. if(itemPosDict.ContainsKey(mapItem.id))
  366. itemPosDict[mapItem.id].item = null;
  367. }
  368. }
  369. public void ClearAllMapItem()
  370. {
  371. for(int i=itemList.Count-1; i>=0; i--)
  372. {
  373. itemList[i].Remove();
  374. }
  375. }
  376. public StartPos GetStartPos(TeamUtil.Team team)
  377. {
  378. if(startPosDict.ContainsKey(team))
  379. return startPosDict[team];
  380. return null;
  381. }
  382. public FlagBase GetFlagBase(TeamUtil.Team team)
  383. {
  384. return flagBaseDict[team];
  385. }
  386. public FlagItem CreateFlag(TeamUtil.Team team)
  387. {
  388. GameObject flagObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>(Config.FLAG_PREFAB));
  389. FlagItem flagItem = flagObj.GetComponent<FlagItem>();
  390. flagItem.Init(this);
  391. flagItem.team = team;
  392. RemoveFlag(team);
  393. flagDict.Add(team, flagItem);
  394. return flagItem;
  395. }
  396. public FlagItem GetFlag(TeamUtil.Team team)
  397. {
  398. if(flagDict.ContainsKey(team))
  399. return flagDict[team];
  400. return null;
  401. }
  402. public bool HasFlag()
  403. {
  404. return flagDict.Count > 0;
  405. }
  406. public FlagItem RemoveFlag(TeamUtil.Team team)
  407. {
  408. FlagItem flag = GetFlag(team);
  409. flagDict.Remove(team);
  410. if(flag != null)
  411. {
  412. flag.Remove();
  413. }
  414. return flag;
  415. }
  416. public void CleanFlag()
  417. {
  418. // how to remove the Map objects
  419. RemoveFlag(TeamUtil.Team.Blue);
  420. RemoveFlag(TeamUtil.Team.Red);
  421. }
  422. public Dictionary<int, BattleObject> GetBattleObjectDict()
  423. {
  424. return battleObjectDict;
  425. }
  426. public BattleObject AddBattleObject(BattleObject battleObject)
  427. {
  428. Dictionary<int, BattleObject> dict = battleObjectDict;
  429. dict.Add(battleObject.id, battleObject);
  430. return battleObject;
  431. }
  432. public void RemoveBattleObject(BattleObject battleObject)
  433. {
  434. battleObjectDict.Remove(battleObject.id);
  435. if(battleObject.typeId == MapObjectUtil.TypeId.Door.GetHashCode())
  436. {
  437. Door door = battleObject as Door;
  438. RemoveDoor(door);
  439. }
  440. }
  441. public void ClearAllBattleObject()
  442. {
  443. List<BattleObject> list = new List<BattleObject>();
  444. Dictionary<int, BattleObject> battleObjDict = GetBattleObjectDict();
  445. foreach(KeyValuePair<int, BattleObject> kvp in battleObjDict)
  446. {
  447. list.Add(kvp.Value);
  448. }
  449. for(int i=0; i<list.Count; i++)
  450. {
  451. list[i].Dead();
  452. }
  453. }
  454. public BattleObject GetBattleObject(int id)
  455. {
  456. if(!battleObjectDict.ContainsKey(id))
  457. {
  458. return null;
  459. }
  460. return battleObjectDict[id];
  461. }
  462. public List<BattleObject> GetBattleObjectByRange(Vector3 origin, float range, TeamUtil.Team team, List<BattleObject> excepts = null)
  463. {
  464. List<BattleObject> list = new List<BattleObject> ();
  465. foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
  466. {
  467. int id = item.Key;
  468. BattleObject obj = item.Value;
  469. bool add = false;
  470. if(team != TeamUtil.Team.None)
  471. {
  472. if(team.Equals(item.Value.team))
  473. {
  474. add = true;
  475. }
  476. }
  477. else
  478. {
  479. add = true;
  480. }
  481. if(excepts != null && excepts.Contains(obj))
  482. continue;
  483. if(add && NumberUtil.distanceVector3(origin, obj.position) <= range)
  484. {
  485. list.Add(item.Value);
  486. }
  487. }
  488. return list;
  489. }
  490. public List<BattleObject> GetBattleObjectBySector(Vector3 origin, float direction, float range, TeamUtil.Team team, List<BattleObject> excepts = null)
  491. {
  492. List<BattleObject> list = new List<BattleObject> ();
  493. foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
  494. {
  495. int id = item.Key;
  496. BattleObject obj = item.Value;
  497. bool add = false;
  498. if(team != TeamUtil.Team.None)
  499. {
  500. if(team.Equals(item.Value.team))
  501. {
  502. add = true;
  503. }
  504. }
  505. else
  506. {
  507. add = true;
  508. }
  509. if(excepts != null && excepts.Contains(obj))
  510. continue;
  511. if(add && NumberUtil.distanceVector3(origin, obj.position) <= range)
  512. {
  513. float targetAngle = NumberUtil.radianToAngle(NumberUtil.getRadianByATan(obj.position.x, obj.position.z, origin.x, origin.z));
  514. float originAngle = NumberUtil.radianToAngle(direction);
  515. float deltaAngle = Mathf.Abs(NumberUtil.corverAngleBetween(targetAngle - originAngle, -180f, 180f));
  516. if(deltaAngle <= 30f)
  517. {
  518. list.Add(item.Value);
  519. }
  520. }
  521. }
  522. return list;
  523. }
  524. public List<BattleObject> GetBattleObjectByFrontRect(Vector3 origin, float direction, float range, TeamUtil.Team team, List<BattleObject> excepts = null)
  525. {
  526. List<BattleObject> list = new List<BattleObject> ();
  527. foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
  528. {
  529. int id = item.Key;
  530. BattleObject obj = item.Value;
  531. bool add = false;
  532. if(team != TeamUtil.Team.None)
  533. {
  534. if(team.Equals(item.Value.team))
  535. {
  536. add = true;
  537. }
  538. }
  539. else
  540. {
  541. add = true;
  542. }
  543. if(!add)
  544. continue;
  545. if(excepts != null && excepts.Contains(obj))
  546. continue;
  547. float distance = NumberUtil.distanceVector3(origin, obj.position, true);
  548. if(distance <= range)
  549. {
  550. float targetRadian = NumberUtil.getRadianByATan(obj.position.x, obj.position.z, origin.x, origin.z);
  551. float targetAngle = NumberUtil.radianToAngle(targetRadian);
  552. float originAngle = NumberUtil.radianToAngle(direction);
  553. float deltaAngle = Mathf.Abs(NumberUtil.corverAngleBetween(targetAngle - originAngle, -180f, 180f));
  554. float delatRadian = NumberUtil.angleToRadian(deltaAngle);
  555. float cos = Mathf.Cos(delatRadian) * distance;
  556. float sin = Mathf.Sin(delatRadian) * distance;
  557. Debuger.LogWarning(obj+" angle:"+deltaAngle+" cos:"+cos+" sin:"+sin+" range:"+range);
  558. if(deltaAngle <= 90f && cos < range && sin < 1.5f)
  559. {
  560. list.Add(item.Value);
  561. }
  562. }
  563. }
  564. return list;
  565. }
  566. public Craft GetNearestCraft(Vector3 origin, TeamUtil.Team team)
  567. {
  568. Craft craft = null;
  569. float minDis = float.MaxValue;
  570. foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
  571. {
  572. if(item.Value is Craft && item.Value.team == team)
  573. {
  574. float dis = NumberUtil.distanceVector3(origin, item.Value.position, true);
  575. if(dis < minDis)
  576. {
  577. minDis = dis;
  578. craft = item.Value as Craft;
  579. }
  580. }
  581. }
  582. return craft;
  583. }
  584. public UAVehicle GetUAVehicle(int userId)
  585. {
  586. UAVehicle uav = null;
  587. uavDict.TryGetValue (userId, out uav);
  588. return uav;
  589. }
  590. public void AddUAVehicle(UAVehicle uav)
  591. {
  592. if (!uavDict.ContainsKey (uav.userId))
  593. uavDict.Add (uav.userId, uav);
  594. else
  595. Debuger.LogError ("Already exist uav "+uav.userId);
  596. }
  597. public void RemoveUAVehicle(UAVehicle uav)
  598. {
  599. uavDict.Remove (uav.userId);
  600. }
  601. }