MapEditorController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. using UnityEngine;
  2. using System;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. public class MapEditorController : MonoBehaviour
  8. {
  9. public Map map;
  10. private GameObject ground;
  11. private AStarNode.Type astarNodeType;
  12. public static int brushSize = 1;
  13. private Dictionary<string, Tile> tileDict = new Dictionary<string, Tile>();
  14. void Start()
  15. {
  16. ground = GameObject.Find("Plane");
  17. }
  18. public void CreateAStarNodeObj(Map map)
  19. {
  20. this.map = map;
  21. GameObject tileContainer = new GameObject("TileContainer");
  22. GameObject tilePrefab = Resources.Load("Prefabs/Tile/Tile") as GameObject;
  23. tileDict.Clear();
  24. for(int i=0; i<map.rows; i++)
  25. {
  26. for(int j=0; j<map.columns; j++)
  27. {
  28. GameObject tileObj = Instantiate(tilePrefab) as GameObject;
  29. tileObj.transform.SetParent(tileContainer.transform);
  30. tileObj.name = "Tile_"+j+"_"+i;
  31. Tile tile = tileObj.GetComponent(typeof(Tile)) as Tile;
  32. tile.Init(map);
  33. tile.setGridPosition(j, i);
  34. tile.setType(map.GetAStarNode(j, i).type.GetHashCode());
  35. tileDict.Add(tileObj.name, tile);
  36. }
  37. }
  38. Block[] blockArr = GameObject.FindObjectsOfType<Block>();
  39. for(int i=0; i<blockArr.Length; i++)
  40. {
  41. Block block = blockArr[i];
  42. AStarNode astarNode = map.GetAStarNode(block.col, block.row);
  43. astarNode.type = TeamUtil.GetAStarNodeType(block.team);
  44. string tileName = "Tile_"+block.col+"_"+block.row;
  45. Tile tile = tileDict[tileName];
  46. tile.setType(astarNode.type.GetHashCode());
  47. Destroy(block.gameObject);
  48. }
  49. MapItemPos[] itemPosArr = GameObject.FindObjectsOfType<MapItemPos>();
  50. for(int i=0; i<itemPosArr.Length; i++)
  51. {
  52. itemPosArr[i].EnabledRenderer(true);
  53. }
  54. }
  55. public void save()
  56. {
  57. string mapName = map.id.ToString();
  58. XmlDocument xml = new XmlDocument();
  59. xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));
  60. XmlElement el = xml.CreateElement("map");
  61. xml.AppendChild(el);
  62. writeMapInfo(el, xml);
  63. writeMapBase(el, xml);
  64. writeMapFlagBase(el, xml);
  65. writeMapStartPos(el, xml);
  66. // writeMapFlag(el, xml);
  67. writeMapDoor(el, xml);
  68. writeItemPos(el, xml);
  69. writeMapTile(el, xml);
  70. //add base
  71. try
  72. {
  73. string mapFolder = Config.MAP_SAVE_FOLDER;
  74. DirectoryInfo directoryInfo = new DirectoryInfo(mapFolder);
  75. if(!directoryInfo.Exists)
  76. {
  77. Directory.CreateDirectory(mapFolder);
  78. }
  79. string url = Config.MAP_SAVE_FOLDER+mapName+".txt";
  80. xml.Save(url);
  81. Debug.Log("Map("+mapName+") saved");
  82. }
  83. catch(Exception error)
  84. {
  85. Debug.LogException(error);
  86. }
  87. }
  88. private void writeMapInfo(XmlElement el, XmlDocument xml)
  89. {
  90. //add info
  91. XmlElement info = xml.CreateElement("info");
  92. el.AppendChild(info);
  93. XmlAttribute attr = xml.CreateAttribute("name");
  94. attr.Value = map.id.ToString();
  95. info.Attributes.Append(attr);
  96. attr = xml.CreateAttribute("columns");
  97. attr.Value = map.columns.ToString();
  98. info.Attributes.Append(attr);
  99. attr = xml.CreateAttribute("rows");
  100. attr.Value = map.rows.ToString();
  101. info.Attributes.Append(attr);
  102. attr = xml.CreateAttribute("ts");
  103. attr.Value = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  104. info.Attributes.Append(attr);
  105. }
  106. private void writeMapFlag(XmlElement el, XmlDocument xml)
  107. {
  108. GameObject[] flags = GameObject.FindGameObjectsWithTag("Flag");
  109. if(flags.Length > 0)
  110. {
  111. XmlElement flagXML = xml.CreateElement("flag");
  112. for(int i=0; i<flags.Length; i++)
  113. {
  114. GameObject flag = flags[i];
  115. XmlElement f = xml.CreateElement("f");
  116. XmlAttribute attr = xml.CreateAttribute("team");
  117. attr.Value = flag.GetComponent<BattleObject>().team.GetHashCode().ToString();
  118. f.Attributes.Append(attr);
  119. attr = xml.CreateAttribute("c");
  120. attr.Value = Map.XToColumn(flag.transform.position.x).ToString();
  121. f.Attributes.Append(attr);
  122. attr = xml.CreateAttribute("r");
  123. attr.Value = Map.ZToRow(flag.transform.position.z).ToString();
  124. f.Attributes.Append(attr);
  125. flagXML.AppendChild(f);
  126. }
  127. el.AppendChild(flagXML);
  128. }
  129. }
  130. private void writeMapDoor(XmlElement el, XmlDocument xml)
  131. {
  132. XmlElement buildingXML = (XmlElement)el.SelectSingleNode("building");
  133. Door[] doors = GameObject.FindObjectsOfType<Door>();
  134. if(doors.Length > 0)
  135. {
  136. for(int i=0; i<doors.Length; i++)
  137. {
  138. for(int j=0; j<doors.Length - i -1; j++)
  139. {
  140. Door d1 = doors[j];
  141. Door d2 = doors[j + 1];
  142. if(d1.index > d2.index)
  143. {
  144. doors[j] = d2;
  145. doors[j + 1] = d1;
  146. }
  147. }
  148. }
  149. for(int i=0; i<doors.Length; i++)
  150. {
  151. Door door = doors[i];
  152. XmlElement dx = xml.CreateElement("door");
  153. XmlAttribute attr = xml.CreateAttribute("i");
  154. attr.Value = door.index.ToString();
  155. dx.Attributes.Append(attr);
  156. attr = xml.CreateAttribute("x");
  157. attr.Value = door.transform.position.x.ToString();
  158. dx.Attributes.Append(attr);
  159. attr = xml.CreateAttribute("y");
  160. attr.Value = door.transform.position.z.ToString();
  161. dx.Attributes.Append(attr);
  162. attr = xml.CreateAttribute("p");
  163. attr.Value = door.occupyPriority.ToString();
  164. dx.Attributes.Append(attr);
  165. buildingXML.AppendChild(dx);
  166. }
  167. el.AppendChild(buildingXML);
  168. }
  169. }
  170. private void writeMapBase(XmlElement el, XmlDocument xml)
  171. {
  172. XmlElement buildingXML = xml.CreateElement("building");
  173. CrystalBase[] crystalBaseArr = GameObject.FindObjectsOfType<CrystalBase>();
  174. for(int i=0; i<crystalBaseArr.Length; i++)
  175. {
  176. for(int j=0; j<crystalBaseArr.Length - i -1; j++)
  177. {
  178. CrystalBase c1 = crystalBaseArr[j];
  179. CrystalBase c2 = crystalBaseArr[j + 1];
  180. if(c1.id > c2.id)
  181. {
  182. crystalBaseArr[j] = c2;
  183. crystalBaseArr[j + 1] = c1;
  184. }
  185. }
  186. }
  187. for(int i=0; i<crystalBaseArr.Length; i++)
  188. {
  189. CrystalBase crystalBase = crystalBaseArr[i];
  190. XmlElement cx = xml.CreateElement("crystal");
  191. XmlAttribute attr = xml.CreateAttribute("i");
  192. attr.Value = crystalBase.id.ToString();
  193. cx.Attributes.Append(attr);
  194. attr = xml.CreateAttribute("x");
  195. attr.Value = crystalBase.transform.position.x.ToString();
  196. cx.Attributes.Append(attr);
  197. attr = xml.CreateAttribute("y");
  198. attr.Value = crystalBase.transform.position.z.ToString();
  199. cx.Attributes.Append(attr);
  200. attr = xml.CreateAttribute("s");
  201. attr.Value = crystalBase.isStart.ToString();
  202. cx.Attributes.Append(attr);
  203. attr = xml.CreateAttribute("p");
  204. attr.Value = crystalBase.occupyPriority.ToString();
  205. cx.Attributes.Append(attr);
  206. attr = xml.CreateAttribute("o");
  207. attr.Value = crystalBase.overwhelming?"1":"0";
  208. cx.Attributes.Append(attr);
  209. attr = xml.CreateAttribute("f");
  210. attr.Value = crystalBase.startPositionDirect.GetHashCode().ToString();
  211. cx.Attributes.Append(attr);
  212. attr = xml.CreateAttribute ("md");
  213. attr.Value = GetPrefabName(crystalBase.gameObject);
  214. cx.Attributes.Append(attr);
  215. buildingXML.AppendChild(cx);
  216. }
  217. el.AppendChild(buildingXML);
  218. }
  219. private void writeMapFlagBase(XmlElement el, XmlDocument xml)
  220. {
  221. XmlElement buildingXML = (XmlElement)el.SelectSingleNode("building");
  222. FlagBase[] flagBaseArr = GameObject.FindObjectsOfType<FlagBase>();
  223. for(int i=0; i<flagBaseArr.Length; i++)
  224. {
  225. FlagBase flagBase = flagBaseArr[i];
  226. XmlElement cx = xml.CreateElement("flagbase");
  227. XmlAttribute attr = xml.CreateAttribute("t");
  228. attr.Value = flagBase.team.GetHashCode().ToString();
  229. cx.Attributes.Append(attr);
  230. attr = xml.CreateAttribute("x");
  231. attr.Value = flagBase.transform.position.x.ToString();
  232. cx.Attributes.Append(attr);
  233. attr = xml.CreateAttribute("y");
  234. attr.Value = flagBase.transform.position.z.ToString();
  235. cx.Attributes.Append(attr);
  236. attr = xml.CreateAttribute("s");
  237. attr.Value = flagBase.isStart.ToString();
  238. cx.Attributes.Append(attr);
  239. attr = xml.CreateAttribute("f");
  240. attr.Value = flagBase.startPositionDirect.GetHashCode().ToString();
  241. cx.Attributes.Append(attr);
  242. attr = xml.CreateAttribute("md");
  243. attr.Value = GetPrefabName(flagBase.gameObject);
  244. cx.Attributes.Append(attr);
  245. buildingXML.AppendChild(cx);
  246. }
  247. el.AppendChild(buildingXML);
  248. }
  249. private void writeMapStartPos(XmlElement el, XmlDocument xml)
  250. {
  251. XmlElement buildingXML = (XmlElement)el.SelectSingleNode("building");
  252. StartPos[] startPosArr = GameObject.FindObjectsOfType<StartPos>();
  253. for(int i=0; i<startPosArr.Length; i++)
  254. {
  255. StartPos startPos = startPosArr[i];
  256. XmlElement cx = xml.CreateElement("start");
  257. XmlAttribute attr = xml.CreateAttribute("t");
  258. attr.Value = startPos.team.GetHashCode().ToString();
  259. cx.Attributes.Append(attr);
  260. attr = xml.CreateAttribute("x");
  261. attr.Value = startPos.transform.position.x.ToString();
  262. cx.Attributes.Append(attr);
  263. attr = xml.CreateAttribute("y");
  264. attr.Value = startPos.transform.position.z.ToString();
  265. cx.Attributes.Append(attr);
  266. attr = xml.CreateAttribute("f");
  267. attr.Value = startPos.startPositionDirect.GetHashCode().ToString();
  268. cx.Attributes.Append(attr);
  269. attr = xml.CreateAttribute("md");
  270. attr.Value = GetPrefabName(startPos.gameObject);
  271. cx.Attributes.Append(attr);
  272. buildingXML.AppendChild(cx);
  273. }
  274. el.AppendChild(buildingXML);
  275. }
  276. private void writeMapTile(XmlElement el, XmlDocument xml)
  277. {
  278. //add tile
  279. XmlElement tileXML = xml.CreateElement("tile");
  280. el.AppendChild(tileXML);
  281. for(int i=0; i<map.rows; i++)
  282. {
  283. XmlElement e = xml.CreateElement("t");
  284. String tileStr = "";
  285. for(int j=0; j<map.columns; j++)
  286. {
  287. AStarNode astarNode = map.GetAStarNode(j, i);
  288. tileStr += astarNode.type.GetHashCode().ToString();
  289. }
  290. e.InnerText = tileStr;
  291. tileXML.AppendChild(e);
  292. }
  293. }
  294. private void writeItemPos(XmlElement el, XmlDocument xml)
  295. {
  296. XmlElement buildingXML = (XmlElement)el.SelectSingleNode("building");
  297. MapItemPos[] itemPosArr = GameObject.FindObjectsOfType<MapItemPos>();
  298. for(int i=0; i<itemPosArr.Length; i++)
  299. {
  300. MapItemPos itemPos = itemPosArr[i];
  301. XmlElement cx = xml.CreateElement("item");
  302. XmlAttribute attr = xml.CreateAttribute("i");
  303. attr.Value = itemPos.id.ToString();
  304. cx.Attributes.Append(attr);
  305. attr = xml.CreateAttribute("x");
  306. attr.Value = itemPos.position.x.ToString();
  307. cx.Attributes.Append(attr);
  308. attr = xml.CreateAttribute("y");
  309. attr.Value = itemPos.position.z.ToString();
  310. cx.Attributes.Append(attr);
  311. buildingXML.AppendChild(cx);
  312. }
  313. el.AppendChild(buildingXML);
  314. }
  315. private Vector3 lastMousePos;
  316. void Update ()
  317. {
  318. if(Input.GetMouseButtonDown(0))
  319. lastMousePos = Input.mousePosition;
  320. if(Input.GetMouseButton(0))
  321. {
  322. Vector3 currentMousePos = Input.mousePosition;
  323. Vector3 pos = Camera.main.transform.position;
  324. pos.x -= (currentMousePos.x - lastMousePos.x)/20f;
  325. pos.z -= (currentMousePos.y - lastMousePos.y)/20f;
  326. lastMousePos = currentMousePos;
  327. Camera.main.transform.position = pos;
  328. }
  329. if(Input.GetMouseButton(1))
  330. {
  331. AStarNode originNode = getAStarNodeAtMousePosition();
  332. int size = brushSize - 1;
  333. for(int i=-size; i<=size; i++)
  334. {
  335. for(int j=-size; j<=size; j++)
  336. {
  337. AStarNode astarNode = map.GetAStarNode(originNode.X + j, originNode.Y + i);
  338. if(astarNode != null && astarNode.type != astarNodeType)
  339. {
  340. astarNode.type = astarNodeType;
  341. GameObject tileObj = GameObject.Find("Tile_"+astarNode.X+"_"+astarNode.Y);
  342. tileObj.GetComponent<Tile>().setType(astarNode.type.GetHashCode());
  343. }
  344. }
  345. }
  346. }
  347. if(Input.GetKeyDown(KeyCode.B))
  348. {
  349. astarNodeType = AStarNode.Type.BlueWall;
  350. Debug.Log("AstartNodeType change to : "+astarNodeType);
  351. }
  352. else if(Input.GetKeyDown(KeyCode.V))
  353. {
  354. astarNodeType = AStarNode.Type.RedWall;
  355. Debug.Log("AstartNodeType change to : "+astarNodeType);
  356. }
  357. else if(Input.GetKeyDown(KeyCode.C))
  358. {
  359. astarNodeType = AStarNode.Type.YellowWall;
  360. Debug.Log("AstartNodeType change to : "+astarNodeType);
  361. }
  362. else if(Input.GetKeyDown(KeyCode.N))
  363. {
  364. astarNodeType = AStarNode.Type.Wall;
  365. Debug.Log("AstartNodeType change to : "+astarNodeType);
  366. }
  367. else if(Input.GetKeyDown(KeyCode.M))
  368. {
  369. astarNodeType = AStarNode.Type.Empty;
  370. Debug.Log("AstartNodeType change to : "+astarNodeType);
  371. }
  372. }
  373. private AStarNode getAStarNodeAtMousePosition()
  374. {
  375. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  376. RaycastHit hitInfo;
  377. if(Physics.Raycast(ray, out hitInfo))
  378. {
  379. if(hitInfo.collider.gameObject.Equals(ground))
  380. {
  381. int hitCol = (int)(hitInfo.point.x/Map.TILE_WIDTH);
  382. int hitRow = (int)(hitInfo.point.z/Map.TILE_LENGTH);
  383. hitCol = NumberUtil.forceBetween(hitCol, 0, map.columns-1);
  384. hitRow = NumberUtil.forceBetween(hitRow, 0, map.rows-1);
  385. return map.GetAStarNode(hitCol, hitRow);
  386. }
  387. }
  388. return null;
  389. }
  390. public static string GetPrefabName(GameObject gameObj)
  391. {
  392. string name = gameObj.name;
  393. name = name.Replace ("(Clone)", "");
  394. name = name.Replace (" ", "");
  395. return name;
  396. }
  397. }