Map.cs 24 KB

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