| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- using UnityEngine;
- using System;
- using System.IO;
- using System.Xml;
- using System.Collections;
- using System.Collections.Generic;
- public class MapEditorController : MonoBehaviour
- {
- public Map map;
- private GameObject ground;
- private AStarNode.Type astarNodeType;
- public static int brushSize = 1;
- private Dictionary<string, Tile> tileDict = new Dictionary<string, Tile>();
- void Start()
- {
- ground = GameObject.Find("Plane");
- }
- public void CreateAStarNodeObj(Map map)
- {
- this.map = map;
- GameObject tileContainer = new GameObject("TileContainer");
- GameObject tilePrefab = Resources.Load("Prefabs/Tile/Tile") as GameObject;
- tileDict.Clear();
- for(int i=0; i<map.rows; i++)
- {
- for(int j=0; j<map.columns; j++)
- {
- GameObject tileObj = Instantiate(tilePrefab) as GameObject;
- tileObj.transform.SetParent(tileContainer.transform);
- tileObj.name = "Tile_"+j+"_"+i;
- Tile tile = tileObj.GetComponent(typeof(Tile)) as Tile;
- tile.Init(map);
- tile.setGridPosition(j, i);
- tile.setType(map.GetAStarNode(j, i).type.GetHashCode());
- tileDict.Add(tileObj.name, tile);
- }
- }
- Block[] blockArr = GameObject.FindObjectsOfType<Block>();
- for(int i=0; i<blockArr.Length; i++)
- {
- Block block = blockArr[i];
- AStarNode astarNode = map.GetAStarNode(block.col, block.row);
- astarNode.type = TeamUtil.GetAStarNodeType(block.team);
- string tileName = "Tile_"+block.col+"_"+block.row;
- Tile tile = tileDict[tileName];
- tile.setType(astarNode.type.GetHashCode());
- Destroy(block.gameObject);
- }
- MapItemPos[] itemPosArr = GameObject.FindObjectsOfType<MapItemPos>();
- for(int i=0; i<itemPosArr.Length; i++)
- {
- itemPosArr[i].EnabledRenderer(true);
- }
- }
-
- public void save()
- {
- string mapName = map.id.ToString();
- XmlDocument xml = new XmlDocument();
- xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));
-
- XmlElement el = xml.CreateElement("map");
- xml.AppendChild(el);
- writeMapInfo(el, xml);
- writeMapBase(el, xml);
- writeMapFlagBase(el, xml);
- writeMapStartPos(el, xml);
- // writeMapFlag(el, xml);
- writeMapDoor(el, xml);
- writeItemPos(el, xml);
- writeMapTile(el, xml);
- //add base
- try
- {
- string mapFolder = Config.MAP_SAVE_FOLDER;
- DirectoryInfo directoryInfo = new DirectoryInfo(mapFolder);
- if(!directoryInfo.Exists)
- {
- Directory.CreateDirectory(mapFolder);
- }
-
- string url = Config.MAP_SAVE_FOLDER+mapName+".txt";
- xml.Save(url);
- Debug.Log("Map("+mapName+") saved");
- }
- catch(Exception error)
- {
- Debug.LogException(error);
- }
- }
- private void writeMapInfo(XmlElement el, XmlDocument xml)
- {
- //add info
- XmlElement info = xml.CreateElement("info");
- el.AppendChild(info);
-
- XmlAttribute attr = xml.CreateAttribute("name");
- attr.Value = map.id.ToString();
- info.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("columns");
- attr.Value = map.columns.ToString();
- info.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("rows");
- attr.Value = map.rows.ToString();
- info.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("ts");
- attr.Value = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- info.Attributes.Append(attr);
- }
- private void writeMapFlag(XmlElement el, XmlDocument xml)
- {
- GameObject[] flags = GameObject.FindGameObjectsWithTag("Flag");
- if(flags.Length > 0)
- {
- XmlElement flagXML = xml.CreateElement("flag");
- for(int i=0; i<flags.Length; i++)
- {
- GameObject flag = flags[i];
- XmlElement f = xml.CreateElement("f");
- XmlAttribute attr = xml.CreateAttribute("team");
- attr.Value = flag.GetComponent<BattleObject>().team.GetHashCode().ToString();
- f.Attributes.Append(attr);
- attr = xml.CreateAttribute("c");
- attr.Value = Map.XToColumn(flag.transform.position.x).ToString();
- f.Attributes.Append(attr);
- attr = xml.CreateAttribute("r");
- attr.Value = Map.ZToRow(flag.transform.position.z).ToString();
- f.Attributes.Append(attr);
- flagXML.AppendChild(f);
- }
- el.AppendChild(flagXML);
- }
- }
- private void writeMapDoor(XmlElement el, XmlDocument xml)
- {
- XmlElement buildingXML = (XmlElement)el.SelectSingleNode("building");
- Door[] doors = GameObject.FindObjectsOfType<Door>();
- if(doors.Length > 0)
- {
- for(int i=0; i<doors.Length; i++)
- {
- for(int j=0; j<doors.Length - i -1; j++)
- {
- Door d1 = doors[j];
- Door d2 = doors[j + 1];
- if(d1.index > d2.index)
- {
- doors[j] = d2;
- doors[j + 1] = d1;
- }
- }
- }
-
- for(int i=0; i<doors.Length; i++)
- {
- Door door = doors[i];
-
- XmlElement dx = xml.CreateElement("door");
-
- XmlAttribute attr = xml.CreateAttribute("i");
- attr.Value = door.index.ToString();
- dx.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("x");
- attr.Value = door.transform.position.x.ToString();
- dx.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("y");
- attr.Value = door.transform.position.z.ToString();
- dx.Attributes.Append(attr);
- attr = xml.CreateAttribute("p");
- attr.Value = door.occupyPriority.ToString();
- dx.Attributes.Append(attr);
-
- buildingXML.AppendChild(dx);
- }
- el.AppendChild(buildingXML);
- }
- }
- private void writeMapBase(XmlElement el, XmlDocument xml)
- {
- XmlElement buildingXML = xml.CreateElement("building");
- CrystalBase[] crystalBaseArr = GameObject.FindObjectsOfType<CrystalBase>();
- for(int i=0; i<crystalBaseArr.Length; i++)
- {
- for(int j=0; j<crystalBaseArr.Length - i -1; j++)
- {
- CrystalBase c1 = crystalBaseArr[j];
- CrystalBase c2 = crystalBaseArr[j + 1];
- if(c1.id > c2.id)
- {
- crystalBaseArr[j] = c2;
- crystalBaseArr[j + 1] = c1;
- }
- }
- }
-
- for(int i=0; i<crystalBaseArr.Length; i++)
- {
- CrystalBase crystalBase = crystalBaseArr[i];
-
- XmlElement cx = xml.CreateElement("crystal");
- XmlAttribute attr = xml.CreateAttribute("i");
- attr.Value = crystalBase.id.ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("x");
- attr.Value = crystalBase.transform.position.x.ToString();
- cx.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("y");
- attr.Value = crystalBase.transform.position.z.ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("s");
- attr.Value = crystalBase.isStart.ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("p");
- attr.Value = crystalBase.occupyPriority.ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("o");
- attr.Value = crystalBase.overwhelming?"1":"0";
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("f");
- attr.Value = crystalBase.startPositionDirect.GetHashCode().ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute ("md");
- attr.Value = GetPrefabName(crystalBase.gameObject);
- cx.Attributes.Append(attr);
-
- buildingXML.AppendChild(cx);
- }
- el.AppendChild(buildingXML);
- }
- private void writeMapFlagBase(XmlElement el, XmlDocument xml)
- {
- XmlElement buildingXML = (XmlElement)el.SelectSingleNode("building");
-
- FlagBase[] flagBaseArr = GameObject.FindObjectsOfType<FlagBase>();
- for(int i=0; i<flagBaseArr.Length; i++)
- {
- FlagBase flagBase = flagBaseArr[i];
-
- XmlElement cx = xml.CreateElement("flagbase");
-
- XmlAttribute attr = xml.CreateAttribute("t");
- attr.Value = flagBase.team.GetHashCode().ToString();
- cx.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("x");
- attr.Value = flagBase.transform.position.x.ToString();
- cx.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("y");
- attr.Value = flagBase.transform.position.z.ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("s");
- attr.Value = flagBase.isStart.ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("f");
- attr.Value = flagBase.startPositionDirect.GetHashCode().ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("md");
- attr.Value = GetPrefabName(flagBase.gameObject);
- cx.Attributes.Append(attr);
-
- buildingXML.AppendChild(cx);
- }
- el.AppendChild(buildingXML);
- }
- private void writeMapStartPos(XmlElement el, XmlDocument xml)
- {
- XmlElement buildingXML = (XmlElement)el.SelectSingleNode("building");
-
- StartPos[] startPosArr = GameObject.FindObjectsOfType<StartPos>();
-
- for(int i=0; i<startPosArr.Length; i++)
- {
- StartPos startPos = startPosArr[i];
-
- XmlElement cx = xml.CreateElement("start");
-
- XmlAttribute attr = xml.CreateAttribute("t");
- attr.Value = startPos.team.GetHashCode().ToString();
- cx.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("x");
- attr.Value = startPos.transform.position.x.ToString();
- cx.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("y");
- attr.Value = startPos.transform.position.z.ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("f");
- attr.Value = startPos.startPositionDirect.GetHashCode().ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("md");
- attr.Value = GetPrefabName(startPos.gameObject);
- cx.Attributes.Append(attr);
-
- buildingXML.AppendChild(cx);
- }
- el.AppendChild(buildingXML);
- }
- private void writeMapTile(XmlElement el, XmlDocument xml)
- {
- //add tile
- XmlElement tileXML = xml.CreateElement("tile");
- el.AppendChild(tileXML);
-
- for(int i=0; i<map.rows; i++)
- {
- XmlElement e = xml.CreateElement("t");
- String tileStr = "";
-
- for(int j=0; j<map.columns; j++)
- {
- AStarNode astarNode = map.GetAStarNode(j, i);
- tileStr += astarNode.type.GetHashCode().ToString();
- }
-
- e.InnerText = tileStr;
- tileXML.AppendChild(e);
- }
- }
- private void writeItemPos(XmlElement el, XmlDocument xml)
- {
- XmlElement buildingXML = (XmlElement)el.SelectSingleNode("building");
-
- MapItemPos[] itemPosArr = GameObject.FindObjectsOfType<MapItemPos>();
-
- for(int i=0; i<itemPosArr.Length; i++)
- {
- MapItemPos itemPos = itemPosArr[i];
-
- XmlElement cx = xml.CreateElement("item");
-
- XmlAttribute attr = xml.CreateAttribute("i");
- attr.Value = itemPos.id.ToString();
- cx.Attributes.Append(attr);
- attr = xml.CreateAttribute("x");
- attr.Value = itemPos.position.x.ToString();
- cx.Attributes.Append(attr);
-
- attr = xml.CreateAttribute("y");
- attr.Value = itemPos.position.z.ToString();
- cx.Attributes.Append(attr);
- buildingXML.AppendChild(cx);
- }
- el.AppendChild(buildingXML);
- }
- private Vector3 lastMousePos;
- void Update ()
- {
- if(Input.GetMouseButtonDown(0))
- lastMousePos = Input.mousePosition;
- if(Input.GetMouseButton(0))
- {
- Vector3 currentMousePos = Input.mousePosition;
- Vector3 pos = Camera.main.transform.position;
- pos.x -= (currentMousePos.x - lastMousePos.x)/20f;
- pos.z -= (currentMousePos.y - lastMousePos.y)/20f;
- lastMousePos = currentMousePos;
- Camera.main.transform.position = pos;
- }
- if(Input.GetMouseButton(1))
- {
- AStarNode originNode = getAStarNodeAtMousePosition();
- int size = brushSize - 1;
- for(int i=-size; i<=size; i++)
- {
- for(int j=-size; j<=size; j++)
- {
- AStarNode astarNode = map.GetAStarNode(originNode.X + j, originNode.Y + i);
- if(astarNode != null && astarNode.type != astarNodeType)
- {
- astarNode.type = astarNodeType;
- GameObject tileObj = GameObject.Find("Tile_"+astarNode.X+"_"+astarNode.Y);
- tileObj.GetComponent<Tile>().setType(astarNode.type.GetHashCode());
- }
- }
- }
- }
- if(Input.GetKeyDown(KeyCode.B))
- {
- astarNodeType = AStarNode.Type.BlueWall;
- Debug.Log("AstartNodeType change to : "+astarNodeType);
- }
- else if(Input.GetKeyDown(KeyCode.V))
- {
- astarNodeType = AStarNode.Type.RedWall;
- Debug.Log("AstartNodeType change to : "+astarNodeType);
- }
- else if(Input.GetKeyDown(KeyCode.C))
- {
- astarNodeType = AStarNode.Type.YellowWall;
- Debug.Log("AstartNodeType change to : "+astarNodeType);
- }
- else if(Input.GetKeyDown(KeyCode.N))
- {
- astarNodeType = AStarNode.Type.Wall;
- Debug.Log("AstartNodeType change to : "+astarNodeType);
- }
- else if(Input.GetKeyDown(KeyCode.M))
- {
- astarNodeType = AStarNode.Type.Empty;
- Debug.Log("AstartNodeType change to : "+astarNodeType);
- }
- }
- private AStarNode getAStarNodeAtMousePosition()
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hitInfo;
-
- if(Physics.Raycast(ray, out hitInfo))
- {
- if(hitInfo.collider.gameObject.Equals(ground))
- {
- int hitCol = (int)(hitInfo.point.x/Map.TILE_WIDTH);
- int hitRow = (int)(hitInfo.point.z/Map.TILE_LENGTH);
-
- hitCol = NumberUtil.forceBetween(hitCol, 0, map.columns-1);
- hitRow = NumberUtil.forceBetween(hitRow, 0, map.rows-1);
- return map.GetAStarNode(hitCol, hitRow);
- }
- }
- return null;
- }
- public static string GetPrefabName(GameObject gameObj)
- {
- string name = gameObj.name;
- name = name.Replace ("(Clone)", "");
- name = name.Replace (" ", "");
- return name;
- }
- }
|