Map.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 SpatialAStar<AStarNode, object> aStar;
  15. private List<AstarSearchQueue> searchQueue;
  16. private Dictionary<int, BattleObject> battleObjectDict;
  17. private Dictionary<int, CrystalBase> crystalDict;
  18. private List<CrystalBase> crystalList;
  19. private Dictionary<int, Door> doorDict;
  20. private List<Door> doorList;
  21. private Dictionary<int, List<MapItem>> itemTileDict;
  22. private Dictionary<int, MapItem> itemDict;
  23. private List<MapItem> itemList;
  24. private Dictionary<int, MapItemPos> itemPosDict;
  25. private List<MapItemPos> itemPosList;
  26. private Dictionary<TeamUtil.Team, FlagItem> flagDict;
  27. private Dictionary<TeamUtil.Team, FlagBase> flagBaseDict;
  28. private Dictionary<TeamUtil.Team, StartPos> startPosDict;
  29. private Dictionary<int, UAVehicle> uavDict;
  30. private List<AStarNode> teamBlockList;
  31. private List<Block> blockList;
  32. private XmlNode mapNode;
  33. public Map()
  34. {
  35. searchQueue = new List<AstarSearchQueue>();
  36. battleObjectDict = new Dictionary<int, BattleObject>();
  37. crystalDict = new Dictionary<int, CrystalBase>();
  38. crystalList = new List<CrystalBase>();
  39. doorDict = new Dictionary<int, Door>();
  40. doorList = new List<Door>();
  41. itemTileDict = new Dictionary<int, List<MapItem>>();
  42. itemDict = new Dictionary<int, MapItem>();
  43. itemList = new List<MapItem>();
  44. itemPosDict = new Dictionary<int, MapItemPos>();
  45. itemPosList = new List<MapItemPos>();
  46. flagDict = new Dictionary<TeamUtil.Team, FlagItem>();
  47. flagBaseDict = new Dictionary<TeamUtil.Team, FlagBase>();
  48. startPosDict = new Dictionary<TeamUtil.Team, StartPos>();
  49. uavDict = new Dictionary<int, UAVehicle> ();
  50. teamBlockList = new List<AStarNode>();
  51. blockList = new List<Block>();
  52. }
  53. public static int XToColumn(float x)
  54. {
  55. return (int)(x/TILE_WIDTH);
  56. }
  57. public static int ZToRow(float z)
  58. {
  59. return (int)(z/TILE_LENGTH);
  60. }
  61. public static float GetCenterX(int col)
  62. {
  63. return col*TILE_WIDTH+TILE_WIDTH/2f;
  64. }
  65. public static float GetCenterZ(int row)
  66. {
  67. return row*TILE_WIDTH+TILE_LENGTH/2f;
  68. }
  69. public int GetIndexByGrid(int col, int row)
  70. {
  71. if(!IsInRange(col, row))
  72. return -1;
  73. return row * columns + col;
  74. }
  75. public Vector2 GetGridByIndex(int index)
  76. {
  77. int col = index % rows;
  78. int row = index / columns;
  79. return new Vector2(col, row);
  80. }
  81. public bool IsInRange(int col, int row)
  82. {
  83. if(col < 0 || col >= columns || row < 0 || row >= rows)
  84. return false;
  85. return true;
  86. }
  87. public string name
  88. {
  89. get{return _name;}
  90. }
  91. public int columns
  92. {
  93. get{return _columns;}
  94. }
  95. public int rows
  96. {
  97. get{return _rows;}
  98. }
  99. public void CreateEmptyMap(int columns, int rows)
  100. {
  101. this._columns = columns;
  102. this._rows = rows;
  103. AStarNode[,] grid = new AStarNode[columns, rows];
  104. for(int y=0; y<rows; y++)
  105. {
  106. for(int x=0; x<columns; x++)
  107. {
  108. grid[x, y] = new AStarNode()
  109. {
  110. X = x,
  111. Y = y,
  112. };
  113. }
  114. }
  115. aStar = new SpatialAStar<AStarNode, object>(grid);
  116. }
  117. public void CreateMap(XmlDocument xml)
  118. {
  119. mapNode = xml.SelectSingleNode("map");
  120. XmlNode infoNode = mapNode.SelectSingleNode("info");
  121. this._columns = int.Parse(infoNode.Attributes["columns"].Value);
  122. this._rows = int.Parse(infoNode.Attributes["rows"].Value);
  123. this._name = infoNode.Attributes["name"].Value;
  124. AStarNode[,] grid = new AStarNode[_columns, rows];
  125. XmlNode tileNode = mapNode.SelectSingleNode("tile");
  126. XmlNodeList tileRowNodeList = tileNode.SelectNodes("t");
  127. List<AStarNode> blockNodeList = new List<AStarNode>();
  128. for(int y=0; y<_rows; y++)
  129. {
  130. XmlNode tileRowNode = tileRowNodeList[y];
  131. string tileRowStr = tileRowNode.InnerText;
  132. for(int x=0; x<_columns; x++)
  133. {
  134. int tileType = int.Parse(tileRowStr.Substring(x, 1));
  135. AStarNode.Type t = AStarNode.TypeArr[tileType];
  136. AStarNode node = new AStarNode()
  137. {
  138. type = t,
  139. X = x,
  140. Y = y,
  141. };
  142. if(t == AStarNode.Type.BlueWall || t == AStarNode.Type.RedWall || t == AStarNode.Type.YellowWall)
  143. blockNodeList.Add(node);
  144. grid[x, y] = node;
  145. }
  146. }
  147. aStar = new SpatialAStar<AStarNode, object>(grid);
  148. for(int i=0; i<blockNodeList.Count; i++)
  149. {
  150. CreateBlock(blockNodeList[i]);
  151. }
  152. CreateBuilding(mapNode);
  153. CreateFlag(mapNode);
  154. }
  155. private void CreateBlock(AStarNode node)
  156. {
  157. float x = GetCenterX(node.X);
  158. float z = GetCenterZ(node.Y);
  159. GameObject blockObj = GameObject.Instantiate(Resources.Load(Config.BLOCK_PREFAB)) as GameObject;
  160. Vector3 position = new Vector3();
  161. position.x = x;
  162. position.z = z;
  163. blockObj.transform.position = position;
  164. Block block = blockObj.GetComponent<Block>();
  165. block.Init(this, TeamUtil.GetTeamByAstarNodeType(node.type));
  166. this.blockList.Add(block);
  167. }
  168. private void CreateFlag(XmlNode xml)
  169. {
  170. XmlNode flagNode = xml.SelectSingleNode("flag");
  171. if(flagNode != null)
  172. {
  173. XmlNodeList flagList = flagNode.SelectNodes("f");
  174. for(int i=0; i<flagList.Count; i++)
  175. {
  176. XmlNode f = flagList.Item(i);
  177. int team = int.Parse(f.Attributes["team"].Value);
  178. int column = int.Parse(f.Attributes["c"].Value);
  179. int row = int.Parse(f.Attributes["r"].Value);
  180. Vector3 position = new Vector3();
  181. position.x = GetCenterX(column);
  182. position.z = GetCenterZ(row);
  183. }
  184. }
  185. }
  186. private void CreateBuilding(XmlNode xml)
  187. {
  188. XmlNode buildingNode = xml.SelectSingleNode("building");
  189. if(buildingNode != null)
  190. {
  191. XmlNodeList baseList = buildingNode.SelectNodes("crystal");
  192. for(int i=0; i<baseList.Count; i++)
  193. {
  194. XmlElement b = baseList.Item(i) as XmlElement;
  195. float x = StringUtil.ToFloat(b.GetAttribute("x"));
  196. float y = StringUtil.ToFloat(b.GetAttribute("y"));
  197. int isStart = StringUtil.ToInt(b.GetAttribute("s"));
  198. int occupyPriority = StringUtil.ToInt(b.GetAttribute("p"));
  199. int overwhelming = StringUtil.ToInt(b.GetAttribute("o"));
  200. int startPosDir = StringUtil.ToInt(b.GetAttribute("f"));
  201. string model = b.GetAttribute ("md");
  202. GameObject crystalBaseObj = null;
  203. if(StringUtil.Empty(model))
  204. crystalBaseObj = GameObject.Instantiate(Resources.Load(Config.CRYSTAL_BASE_PREFAB)) as GameObject;
  205. else
  206. crystalBaseObj = GameObject.Instantiate(Resources.Load(Config.MAP_OBJECT_FOLDER + model)) as GameObject;
  207. Vector3 position = new Vector3();
  208. position.x = x;
  209. position.z = y;
  210. crystalBaseObj.transform.position = position;
  211. CrystalBase crystalBase = crystalBaseObj.GetComponent<CrystalBase>();
  212. crystalBase.id = i;
  213. crystalBase.isStart = isStart;
  214. crystalBase.occupyPriority = occupyPriority;
  215. crystalBase.overwhelming = overwhelming>0;
  216. crystalBase.startPositionDirect = MapObject.GetStartPositionDirect(startPosDir);
  217. crystalDict.Add(i, crystalBase);
  218. crystalList.Add(crystalBase);
  219. }
  220. XmlNodeList flagbaseList = buildingNode.SelectNodes("flagbase");
  221. for(int i=0; i<flagbaseList.Count; i++)
  222. {
  223. XmlElement d = flagbaseList.Item(i) as XmlElement;
  224. float x = StringUtil.ToFloat(d.GetAttribute("x"));
  225. float y = StringUtil.ToFloat(d.GetAttribute("y"));
  226. int team = StringUtil.ToInt(d.GetAttribute("t"));
  227. int isStart = StringUtil.ToInt(d.GetAttribute("s"));
  228. int startPosDir = StringUtil.ToInt(d.GetAttribute("f"));
  229. string model = d.GetAttribute ("md");
  230. GameObject flagBaseObj = null;
  231. if (StringUtil.Empty (model))
  232. flagBaseObj = GameObject.Instantiate (Resources.Load (Config.FLAG_BASE_PREFAB)) as GameObject;
  233. else
  234. flagBaseObj = GameObject.Instantiate (Resources.Load (Config.MAP_OBJECT_FOLDER + model)) as GameObject;
  235. Vector3 position = new Vector3();
  236. position.x = x;
  237. position.z = y;
  238. flagBaseObj.transform.position = position;
  239. FlagBase flagBase = flagBaseObj.GetComponent<FlagBase>();
  240. flagBase.Init(this);
  241. flagBase.team = TeamUtil.GetTeam(team);
  242. flagBase.isStart = isStart;
  243. flagBase.startPositionDirect = MapObject.GetStartPositionDirect(startPosDir);
  244. this.flagBaseDict.Add(flagBase.team, flagBase);
  245. }
  246. XmlNodeList doorList = buildingNode.SelectNodes("door");
  247. for(int i=0; i<doorList.Count; i++)
  248. {
  249. XmlNode d = doorList.Item(i);
  250. float x = StringUtil.ToFloat(d.Attributes["x"].Value);
  251. float y = StringUtil.ToFloat(d.Attributes["y"].Value);
  252. int occupyPriority = StringUtil.ToInt(d.Attributes["p"].Value);
  253. GameObject doorObj = GameObject.Instantiate(Resources.Load(Config.DOOR_PREFAB)) as GameObject;
  254. Vector3 position = new Vector3();
  255. position.x = x;
  256. position.z = y;
  257. doorObj.transform.position = position;
  258. Door door = doorObj.GetComponent<Door>();
  259. door.index = i;
  260. door.occupyPriority = occupyPriority;
  261. door.typeId = MapObjectUtil.TypeId.Door.GetHashCode();
  262. doorDict.Add(i, door);
  263. this.doorList.Add(door);
  264. }
  265. XmlNodeList itemList = buildingNode.SelectNodes("item");
  266. for(int i=0; i<itemList.Count; i++)
  267. {
  268. XmlNode d = itemList.Item(i);
  269. int id = StringUtil.ToInt(d.Attributes["i"].Value);
  270. float x = StringUtil.ToFloat(d.Attributes["x"].Value);
  271. float y = StringUtil.ToFloat(d.Attributes["y"].Value);
  272. GameObject mapItemPosObj = GameObject.Instantiate(Resources.Load(Config.ITEM_POS_PREFAB)) as GameObject;
  273. Vector3 position = new Vector3();
  274. position.x = x;
  275. position.z = y;
  276. mapItemPosObj.transform.position = position;
  277. MapItemPos mapItemPos = mapItemPosObj.GetComponent<MapItemPos>();
  278. mapItemPos.id = i;
  279. mapItemPos.typeId = MapObjectUtil.TypeId.MapItemPos.GetHashCode();
  280. mapItemPos.Init(this);
  281. itemPosDict.Add(i, mapItemPos);
  282. itemPosList.Add(mapItemPos);
  283. }
  284. XmlNodeList startList = buildingNode.SelectNodes("start");
  285. for(int i=0; i<startList.Count; i++)
  286. {
  287. XmlElement d = startList.Item(i) as XmlElement;
  288. float x = StringUtil.ToFloat(d.GetAttribute("x"));
  289. float y = StringUtil.ToFloat(d.GetAttribute("y"));
  290. int team = StringUtil.ToInt(d.GetAttribute("t"));
  291. int startPosDir = StringUtil.ToInt(d.GetAttribute("f"));
  292. string model = d.GetAttribute ("md");
  293. GameObject startObj = GameObject.Instantiate (Resources.Load (Config.MAP_OBJECT_FOLDER + model)) as GameObject;
  294. Vector3 position = new Vector3();
  295. position.x = x;
  296. position.z = y;
  297. startObj.transform.position = position;
  298. StartPos startPos = startObj.GetComponent<StartPos>();
  299. startPos.Init(this);
  300. startPos.team = TeamUtil.GetTeam(team);
  301. startPos.startPositionDirect = MapObject.GetStartPositionDirect(startPosDir);
  302. this.startPosDict.Add(startPos.team, startPos);
  303. }
  304. }
  305. }
  306. public AStarNode GetAStarNode(int x, int y)
  307. {
  308. if(x < 0 || x >= columns || y < 0 || y >= rows)
  309. {
  310. return null;
  311. }
  312. return aStar.SearchSpace[x, y];
  313. }
  314. public AStarNode GetAStarNodeByPosition(Vector3 position)
  315. {
  316. int x = (int)(position.x/TILE_WIDTH);
  317. int y = (int)(position.z/TILE_LENGTH);
  318. return GetAStarNode(x, y);
  319. }
  320. public AStarNode[,] GetAStarGrid()
  321. {
  322. return aStar.SearchSpace;
  323. }
  324. public Vector3 GetAstarNodePosition(int x, int y)
  325. {
  326. return new Vector3((x+0.5f)*Map.TILE_WIDTH, 0, (y+0.5f)*Map.TILE_LENGTH);
  327. }
  328. public LinkedList<AStarNode> GetPath(AStarPoint start, AStarPoint end, Craft craft)
  329. {
  330. AStarNode node = GetAStarNode(end.x, end.y);
  331. if(!node.IsWalkable(craft))
  332. {
  333. end = AStarHelper.getGoalFromGoal(start, end, aStar.SearchSpace, craft);
  334. }
  335. return aStar.Search(start, end, craft);
  336. }
  337. public void PathSearchEnqueue(AStarPoint start, AStarPoint end, Craft craft)
  338. {
  339. for(int i=0; i<searchQueue.Count; i++)
  340. {
  341. AstarSearchQueue queue = searchQueue[i];
  342. if(craft == queue.craft)
  343. {
  344. queue.start = start;
  345. queue.end = end;
  346. return;
  347. }
  348. }
  349. searchQueue.Add(new AstarSearchQueue(start, end, craft));
  350. }
  351. public void PathSearchDequeue()
  352. {
  353. if(searchQueue.Count > 0)
  354. {
  355. AstarSearchQueue queue = searchQueue[0];
  356. LinkedList<AStarNode> list = GetPath(queue.start, queue.end, queue.craft);
  357. queue.craft.SetPath(list);
  358. searchQueue.RemoveAt(0);
  359. }
  360. }
  361. public AStarNode FindNearestEmptyAstarNode(int col, int row)
  362. {
  363. int step = 1;
  364. while(true)
  365. {
  366. for(int i=-step; i<=step; i++)
  367. {
  368. int c = i;
  369. int r = step - Mathf.Abs(i);
  370. AStarNode node = GetAStarNode(col+c, row+r);
  371. if(node != null && node.type == AStarNode.Type.Empty)
  372. {
  373. return node;
  374. }
  375. if(r != 0)
  376. {
  377. node = GetAStarNode(col+c, row-r);
  378. if(node != null && node.type == AStarNode.Type.Empty)
  379. {
  380. return node;
  381. }
  382. }
  383. }
  384. step++;
  385. if(step > 10)
  386. {
  387. break;
  388. }
  389. }
  390. return null;
  391. }
  392. public Vector3 GetStartPosition(Player player)
  393. {
  394. Vector3 pos = Vector3.zero;
  395. if(startPosDict.ContainsKey(player.team))
  396. {
  397. return startPosDict[player.team].GetStartPosition();
  398. }
  399. float distance = float.MaxValue;
  400. List<MapBase> list = new List<MapBase>();
  401. for(int i=0; i<crystalList.Count; i++)
  402. {
  403. CrystalBase crystalBase = crystalList[i];
  404. if(crystalBase.startPositionDirect == MapObject.StartPostionDirect.None)
  405. continue;
  406. if(crystalBase.isStart >= 0 && crystalBase.GetStation() != null && crystalBase.GetStation().team == player.team)
  407. {
  408. Player.Hero hero = player.GetHero();
  409. if(hero.IsFirstTimeSelectHero())
  410. {
  411. if(crystalBase.isStart > 0)
  412. list.Add(crystalBase);
  413. }
  414. else
  415. {
  416. float d = NumberUtil.distanceVector3(crystalBase.position, hero.deadPostion);
  417. if(d < distance)
  418. {
  419. distance = d;
  420. pos = crystalBase.position;
  421. list.Clear();
  422. list.Add(crystalBase);
  423. }
  424. else if(d == distance+5f)
  425. {
  426. list.Add(crystalBase);
  427. }
  428. }
  429. }
  430. }
  431. // if(player.IsFirstTimeSelectHero() && flagBaseDict.ContainsKey(player.team))
  432. // {
  433. // list.Add(flagBaseDict[player.team]);
  434. // }
  435. if(list.Count > 0)
  436. {
  437. int i = Random.Range(0, list.Count);
  438. return list[i].GetStartPosition();
  439. }
  440. else if(flagBaseDict.ContainsKey(player.team))
  441. {
  442. return flagBaseDict[player.team].GetStartPosition();
  443. }
  444. return Vector3.zero;
  445. }
  446. public CrystalBase GetCrystalBase(int id)
  447. {
  448. if(crystalDict.ContainsKey(id))
  449. return crystalDict [id];
  450. return null;
  451. }
  452. public List<CrystalBase> GetCrystalBaseList()
  453. {
  454. return crystalList;
  455. }
  456. public CrystalBase GetInRangeCrystalBase(Vector3 pos)
  457. {
  458. for(int i=0; i<crystalList.Count; i++)
  459. {
  460. CrystalBase c = crystalList[i];
  461. float d = NumberUtil.distanceVector3(pos, c.position);
  462. if(d < c.range)
  463. {
  464. return c;
  465. }
  466. }
  467. return null;
  468. }
  469. public Station CreateStation(StationData data, BattleController battleController)
  470. {
  471. GameObject stationObj = null;
  472. if(id == MapData.MapID.Challenge)
  473. stationObj = GameObject.Instantiate(Resources.Load(Config.MAP_OBJECT_FOLDER + "CenterStation")) as GameObject;
  474. else
  475. stationObj = GameObject.Instantiate(Resources.Load(Config.STATION_PREFAB)) as GameObject;
  476. CrystalBase crystalBase = GetCrystalBase(data.crystalId);
  477. Station station = stationObj.GetComponent<Station>();
  478. station.Init(this);
  479. station.id = data.id;
  480. station.userId = data.userId;
  481. station.team = data.team;
  482. station.typeId = MapObjectUtil.TypeId.Station.GetHashCode();
  483. station.aiType = AI.AIType.Station;
  484. station.position = crystalBase.position;
  485. crystalBase.SetStation(station);
  486. AddBattleObject (station);
  487. stationObj.AddComponent<StationAI>().init(battleController);
  488. return station;
  489. }
  490. public void ClearBlocks(TeamUtil.Team team)
  491. {
  492. if(blockList.Count > 0)
  493. {
  494. for(int i=blockList.Count-1; i>=0; i--)
  495. {
  496. Block block = blockList[i];
  497. if(block.team == team)
  498. {
  499. block.Remove();
  500. blockList.RemoveAt(i);
  501. }
  502. }
  503. }
  504. }
  505. public Door GetDoorByIndex(int index)
  506. {
  507. if(doorDict.ContainsKey(index))
  508. return doorDict[index];
  509. return null;
  510. }
  511. public List<Door> GetDoorList()
  512. {
  513. return doorList;
  514. }
  515. public void RemoveDoor(Door door)
  516. {
  517. doorDict.Remove(door.id);
  518. doorList.Remove(door);
  519. CheckDoorRemove();
  520. }
  521. public void RemoveNoIdDoor()
  522. {
  523. foreach(KeyValuePair<int, Door> kvp in doorDict)
  524. {
  525. if(kvp.Value.id == 0)
  526. {
  527. doorDict.Remove(kvp.Key);
  528. doorList.Remove(kvp.Value);
  529. GameObject.Destroy(kvp.Value.gameObject);
  530. }
  531. }
  532. CheckDoorRemove();
  533. }
  534. private void CheckDoorRemove()
  535. {
  536. int blueCount = 0;
  537. int redCount = 0;
  538. for(int i=0; i<doorList.Count; i++)
  539. {
  540. Door door = doorList[i];
  541. if(door.team == TeamUtil.Team.Blue)
  542. {
  543. blueCount++;
  544. }
  545. else if(door.team == TeamUtil.Team.Red)
  546. {
  547. redCount++;
  548. }
  549. }
  550. if(blueCount == 0)
  551. ClearBlocks(TeamUtil.Team.Blue);
  552. if(redCount == 0)
  553. ClearBlocks(TeamUtil.Team.Red);
  554. }
  555. public MapItemPos GetMapItemPos(int id)
  556. {
  557. if(itemPosDict.ContainsKey(id))
  558. return itemPosDict[id];
  559. return null;
  560. }
  561. public List<MapItemPos> GetMapItemPosList()
  562. {
  563. return itemPosList;
  564. }
  565. public MapItem CreateMapItem(MapItemData data)
  566. {
  567. if(itemDict.ContainsKey(data.id))
  568. {
  569. return itemDict [data.id];
  570. }
  571. GameObject mapItemObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>(Config.BIGGER_ITEM_PREFAB));
  572. MapItem mapItem = mapItemObj.GetComponent<MapItem>();
  573. mapItem.Init(this);
  574. mapItem.id = data.id;
  575. mapItem.team = data.team;
  576. mapItem.SetPosition(data.postion);
  577. mapItem.TakeTiles();
  578. itemDict.Add(mapItem.id, mapItem);
  579. itemList.Add(mapItem);
  580. return mapItem;
  581. }
  582. public void MapItemPlace(int tileIndex, MapItem item)
  583. {
  584. if(tileIndex < 0)
  585. return;
  586. if(!itemTileDict.ContainsKey(tileIndex))
  587. itemTileDict.Add(tileIndex, new List<MapItem>());
  588. itemTileDict[tileIndex].Add(item);
  589. }
  590. public void MapItemClean(MapItem item)
  591. {
  592. List<int> tileIndexs = item.GetTileIndexs();
  593. for(int i=0; i<tileIndexs.Count; i++)
  594. {
  595. int index = tileIndexs[i];
  596. if(!itemTileDict.ContainsKey(index))
  597. continue;
  598. itemTileDict[index].Remove(item);
  599. }
  600. }
  601. public MapItem GetMapItem(int id)
  602. {
  603. if(itemDict.ContainsKey(id))
  604. return itemDict[id];
  605. return null;
  606. }
  607. public List<MapItem> GetMapItemByRange(Vector3 originPos, float distance)
  608. {
  609. List<MapItem> list = new List<MapItem>();
  610. for(int i=0; i<itemList.Count; i++)
  611. {
  612. MapItem mapItem = itemList[i];
  613. if(NumberUtil.distanceVector3(originPos, mapItem.position, true) <= distance)
  614. list.Add(mapItem);
  615. }
  616. return list;
  617. }
  618. public List<MapItem> GetMapItemList()
  619. {
  620. return itemList;
  621. }
  622. public List<MapItem> GetMapItemByGrid(int col, int row)
  623. {
  624. int tileIndex = GetIndexByGrid(col, row);
  625. if(!itemTileDict.ContainsKey(tileIndex))
  626. return null;
  627. return itemTileDict[tileIndex];
  628. }
  629. public void RemoveMapItem(MapItem mapItem)
  630. {
  631. if(mapItem != null)
  632. {
  633. itemDict.Remove(mapItem.id);
  634. itemList.Remove(mapItem);
  635. if(itemPosDict.ContainsKey(mapItem.id))
  636. itemPosDict[mapItem.id].item = null;
  637. }
  638. }
  639. public void ClearAllMapItem()
  640. {
  641. for(int i=itemList.Count-1; i>=0; i--)
  642. {
  643. itemList[i].Remove();
  644. }
  645. }
  646. public StartPos GetStartPos(TeamUtil.Team team)
  647. {
  648. if(startPosDict.ContainsKey(team))
  649. return startPosDict[team];
  650. return null;
  651. }
  652. public FlagBase GetFlagBase(TeamUtil.Team team)
  653. {
  654. return flagBaseDict[team];
  655. }
  656. public FlagItem CreateFlag(TeamUtil.Team team)
  657. {
  658. GameObject flagObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>(Config.FLAG_PREFAB));
  659. FlagItem flagItem = flagObj.GetComponent<FlagItem>();
  660. flagItem.Init(this);
  661. flagItem.team = team;
  662. RemoveFlag(team);
  663. flagDict.Add(team, flagItem);
  664. return flagItem;
  665. }
  666. public FlagItem GetFlag(TeamUtil.Team team)
  667. {
  668. if(flagDict.ContainsKey(team))
  669. return flagDict[team];
  670. return null;
  671. }
  672. public bool HasFlag()
  673. {
  674. return flagDict.Count > 0;
  675. }
  676. public FlagItem RemoveFlag(TeamUtil.Team team)
  677. {
  678. FlagItem flag = GetFlag(team);
  679. flagDict.Remove(team);
  680. if(flag != null)
  681. {
  682. flag.Remove();
  683. }
  684. return flag;
  685. }
  686. public void CleanFlag()
  687. {
  688. // how to remove the Map objects
  689. RemoveFlag(TeamUtil.Team.Blue);
  690. RemoveFlag(TeamUtil.Team.Red);
  691. }
  692. public Dictionary<int, BattleObject> GetBattleObjectDict()
  693. {
  694. return battleObjectDict;
  695. }
  696. public BattleObject AddBattleObject(BattleObject battleObject)
  697. {
  698. Dictionary<int, BattleObject> dict = battleObjectDict;
  699. dict.Add(battleObject.id, battleObject);
  700. return battleObject;
  701. }
  702. public void RemoveBattleObject(BattleObject battleObject)
  703. {
  704. battleObjectDict.Remove(battleObject.id);
  705. if(battleObject.typeId == MapObjectUtil.TypeId.Door.GetHashCode())
  706. {
  707. Door door = battleObject as Door;
  708. RemoveDoor(door);
  709. }
  710. }
  711. public void ClearAllBattleObject()
  712. {
  713. List<BattleObject> list = new List<BattleObject>();
  714. Dictionary<int, BattleObject> battleObjDict = GetBattleObjectDict();
  715. foreach(KeyValuePair<int, BattleObject> kvp in battleObjDict)
  716. {
  717. list.Add(kvp.Value);
  718. }
  719. for(int i=0; i<list.Count; i++)
  720. {
  721. list[i].Dead();
  722. }
  723. }
  724. public BattleObject GetBattleObject(int id)
  725. {
  726. if(!battleObjectDict.ContainsKey(id))
  727. {
  728. return null;
  729. }
  730. return battleObjectDict[id];
  731. }
  732. public List<BattleObject> GetBattleObjectByRange(Vector3 origin, float range, TeamUtil.Team team, List<BattleObject> excepts = null)
  733. {
  734. List<BattleObject> list = new List<BattleObject> ();
  735. foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
  736. {
  737. int id = item.Key;
  738. BattleObject obj = item.Value;
  739. bool add = false;
  740. if(team != TeamUtil.Team.None)
  741. {
  742. if(team.Equals(item.Value.team))
  743. {
  744. add = true;
  745. }
  746. }
  747. else
  748. {
  749. add = true;
  750. }
  751. if(excepts != null && excepts.Contains(obj))
  752. continue;
  753. if(add && NumberUtil.distanceVector3(origin, obj.position) <= range)
  754. {
  755. list.Add(item.Value);
  756. }
  757. }
  758. return list;
  759. }
  760. public List<BattleObject> GetBattleObjectBySector(Vector3 origin, float direction, float range, TeamUtil.Team team, List<BattleObject> excepts = null)
  761. {
  762. List<BattleObject> list = new List<BattleObject> ();
  763. foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
  764. {
  765. int id = item.Key;
  766. BattleObject obj = item.Value;
  767. bool add = false;
  768. if(team != TeamUtil.Team.None)
  769. {
  770. if(team.Equals(item.Value.team))
  771. {
  772. add = true;
  773. }
  774. }
  775. else
  776. {
  777. add = true;
  778. }
  779. if(excepts != null && excepts.Contains(obj))
  780. continue;
  781. if(add && NumberUtil.distanceVector3(origin, obj.position) <= range)
  782. {
  783. float targetAngle = NumberUtil.radianToAngle(NumberUtil.getRadianByATan(obj.position.x, obj.position.z, origin.x, origin.z));
  784. float originAngle = NumberUtil.radianToAngle(direction);
  785. float deltaAngle = Mathf.Abs(NumberUtil.corverAngleBetween(targetAngle - originAngle, -180f, 180f));
  786. if(deltaAngle <= 30f)
  787. {
  788. list.Add(item.Value);
  789. }
  790. }
  791. }
  792. return list;
  793. }
  794. public List<BattleObject> GetBattleObjectByFrontRect(Vector3 origin, float direction, float range, TeamUtil.Team team, List<BattleObject> excepts = null)
  795. {
  796. List<BattleObject> list = new List<BattleObject> ();
  797. foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
  798. {
  799. int id = item.Key;
  800. BattleObject obj = item.Value;
  801. bool add = false;
  802. if(team != TeamUtil.Team.None)
  803. {
  804. if(team.Equals(item.Value.team))
  805. {
  806. add = true;
  807. }
  808. }
  809. else
  810. {
  811. add = true;
  812. }
  813. if(!add)
  814. continue;
  815. if(excepts != null && excepts.Contains(obj))
  816. continue;
  817. float distance = NumberUtil.distanceVector3(origin, obj.position, true);
  818. if(distance <= range)
  819. {
  820. float targetRadian = NumberUtil.getRadianByATan(obj.position.x, obj.position.z, origin.x, origin.z);
  821. float targetAngle = NumberUtil.radianToAngle(targetRadian);
  822. float originAngle = NumberUtil.radianToAngle(direction);
  823. float deltaAngle = Mathf.Abs(NumberUtil.corverAngleBetween(targetAngle - originAngle, -180f, 180f));
  824. float delatRadian = NumberUtil.angleToRadian(deltaAngle);
  825. float cos = Mathf.Cos(delatRadian) * distance;
  826. float sin = Mathf.Sin(delatRadian) * distance;
  827. Debuger.LogWarning(obj+" angle:"+deltaAngle+" cos:"+cos+" sin:"+sin+" range:"+range);
  828. if(deltaAngle <= 90f && cos < range && sin < 1.5f)
  829. {
  830. list.Add(item.Value);
  831. }
  832. }
  833. }
  834. return list;
  835. }
  836. public Craft GetNearestCraft(Vector3 origin, TeamUtil.Team team)
  837. {
  838. Craft craft = null;
  839. float minDis = float.MaxValue;
  840. foreach(KeyValuePair<int, BattleObject> item in battleObjectDict)
  841. {
  842. if(item.Value is Craft && item.Value.team == team)
  843. {
  844. float dis = NumberUtil.distanceVector3(origin, item.Value.position, true);
  845. if(dis < minDis)
  846. {
  847. minDis = dis;
  848. craft = item.Value as Craft;
  849. }
  850. }
  851. }
  852. return craft;
  853. }
  854. public UAVehicle GetUAVehicle(int userId)
  855. {
  856. UAVehicle uav = null;
  857. uavDict.TryGetValue (userId, out uav);
  858. return uav;
  859. }
  860. public void AddUAVehicle(UAVehicle uav)
  861. {
  862. if (!uavDict.ContainsKey (uav.userId))
  863. uavDict.Add (uav.userId, uav);
  864. else
  865. Debuger.LogError ("Already exist uav "+uav.userId);
  866. }
  867. public void RemoveUAVehicle(UAVehicle uav)
  868. {
  869. uavDict.Remove (uav.userId);
  870. }
  871. }