public static MapData LoadOldXml(string fullPath) { if (!System.IO.File.Exists(fullPath)) { Debug.LogError(string.Format("File not exists. {0}", fullPath)); return null; } MapHeightInfos heightInfos = ChpLibXML.Load<MapHeightInfos>(fullPath); int xCount = heightInfos.xCount-1; int zCount = heightInfos.zCount-1; MapData data = new MapData(xCount, zCount); for ( int x = 0; x < xCount; x++){ for ( int z = 0; z < zCount ; z++){ float[] neighbourHeights = new float[4]; neighbourHeights[0] = heightInfos.heights[x][z]; neighbourHeights[1] = heightInfos.heights[x][z+1]; neighbourHeights[2] = heightInfos.heights[x+1][z+1]; neighbourHeights[3] = heightInfos.heights[x+1][z]; float height = (neighbourHeights[0] + neighbourHeights[1] + neighbourHeights[2] + neighbourHeights[3]) / 4f; MapData.MapNode _node = new MapData.MapNode(x,z,1,height, heightInfos.reachables[x][z] , heightInfos.isWayPoints[x][z], x == 0 || x == xCount-1 || z == 0 || z == zCount-1); _node.neighborHeights = neighbourHeights; data.SetNode(x, z, _node); } } return data; }
public static MapData Load(string fullPath) { Debug.Log("load <-- " + fullPath); XmlDocument doc = new XmlDocument(); doc.Load(fullPath); XmlNode root = doc.DocumentElement; Debug.Log(root.Name); MapData mapData = null; foreach (XmlNode map in root.ChildNodes) { XmlElement mapEle = map as XmlElement; if (mapEle == null) continue; string sceneName = mapEle.GetAttribute("sceneName"); int xCount = System.Convert.ToInt32(mapEle.GetAttribute("xCount")); int zCount = System.Convert.ToInt32(mapEle.GetAttribute("zCount")); Debug.Log(string.Format("{0} {1} {2}", sceneName, xCount, zCount)); mapData = new MapData(xCount, zCount); foreach (XmlNode _nd in mapEle.ChildNodes) { XmlElement nodeEle = _nd as XmlElement; if (nodeEle == null) continue; if (nodeEle.Name == "node") { int x = System.Convert.ToInt32(nodeEle.GetAttribute("x")); int z = System.Convert.ToInt32(nodeEle.GetAttribute("z")); float height = System.Convert.ToSingle(nodeEle.GetAttribute("height")); bool isReachable = System.Convert.ToBoolean(nodeEle.GetAttribute("isReachable")); bool isWayPoint = System.Convert.ToBoolean(nodeEle.GetAttribute("isWayPoint")); bool isBoundary = System.Convert.ToBoolean(nodeEle.GetAttribute("isBoundary")); MapData.MapNode _mapNode = new MapData.MapNode(x, z, 1, height, isReachable, isWayPoint, isBoundary); float[] neighbourHeight = new float[4]; neighbourHeight[0] = System.Convert.ToSingle( nodeEle.GetAttribute("neighbour_0_0")); neighbourHeight[1] = System.Convert.ToSingle( nodeEle.GetAttribute("neighbour_0_1")); neighbourHeight[2] = System.Convert.ToSingle( nodeEle.GetAttribute("neighbour_1_1")); neighbourHeight[3] = System.Convert.ToSingle( nodeEle.GetAttribute("neighbour_1_0")); _mapNode.neighborHeights = neighbourHeight; mapData.SetNode(x, z, _mapNode); } } } return mapData; }
public static MapData LoadBinary( string fullPath){ FileStream fileStream = new FileStream( fullPath, FileMode.Open, FileAccess.Read); BinaryReader binaryReader = new BinaryReader(fileStream); int xCount = binaryReader.ReadInt32(); int zCount = binaryReader.ReadInt32(); MapData data = new MapData(xCount, zCount); for ( int i = 0; i <xCount * zCount; i ++){ int x = binaryReader.ReadInt32(); int z = binaryReader.ReadInt32(); float height =binaryReader.ReadSingle(); bool isReachable = binaryReader.ReadBoolean(); bool isWayPoint = binaryReader.ReadBoolean(); bool isBoundary = binaryReader.ReadBoolean(); float[] neighbourHeights = new float[4]; neighbourHeights[0] = binaryReader.ReadSingle(); neighbourHeights[1] = binaryReader.ReadSingle(); neighbourHeights[2] = binaryReader.ReadSingle(); neighbourHeights[3] = binaryReader.ReadSingle(); MapData.MapNode _node = new MapData.MapNode(x, z, 1, height,isReachable, isWayPoint, isBoundary); _node.neighborHeights = neighbourHeights; data.SetNode(x,z,_node); } return data; }