IEnumerator saveMap() { int walkable = 0; int nonWalkable = 0; Debug.Log("Saving map"); //UnityEngine.Debug.Log("Nodes: " + nodes.Length); mapData mData = new mapData(); nodeData[,] data = new nodeData[(int)(xSize * (1 / length)), (int)(zSize * (1 / length))]; for (int i = 0; i < (xSize * (1 / length)); i++) { for (int j = 0; j < (zSize * (1 / length)); j++) { nodeData n = new nodeData(); if (nodes[i, j].walkable) { walkable++; } else { nonWalkable++; } n.populate(nodes[i, j]); data[i, j] = n; } yield return(null); } mData.mName = mapName; mData.mapNodes = data; mData.xSize = xSize; mData.zSize = zSize; mData.nodeLength = length; //DataSaver.instance.saveMap(mData); if (File.Exists(dataPath + "/mapData/" + mData.mName + ".dat")) { File.Delete(dataPath + "/mapData/" + mData.mName + ".dat"); Debug.Log("Deleted file"); } BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(dataPath + "/mapData/" + mData.mName + ".dat"); bf.Serialize(file, mData); file.Close(); //Debug.Log("Walkable: " + walkable + ", non walkable: " + nonWalkable); mapIsReady = true; //Debug.Log("Finished making and saving map"); }
public void saveMap(Node[,] nodes, string mapName, int length, int xSize, int zSize) { mapData mData = new mapData(); nodeData[,] nData = new nodeData[nodes.GetLength(0), nodes.GetLength(nodes.Rank - 1)]; Debug.Log("Rank: " + (nodes.Rank - 1)); for (int i = 0; i < nodes.GetLength(0); i++) { for (int j = 0; j < nodes.GetLength(nodes.Rank - 1); j++) { nodeData n = new nodeData(); n.populate(nodes[i, j]); nData[i, j] = n; } } mData.mName = mapName; mData.mapNodes = nData; mData.xSize = xSize; mData.zSize = zSize; mData.nodeLength = length; saveMap(mData); }
IEnumerator setUpMap() { if (DataSaver.instance.mapExists(mapName) && !makeNewMap) { CreateMap create = new CreateMap(false, length, xSize, zSize, mapName); while (create.totalNodes == 0) { yield return(null); } //Debug.Log("Total nodes: " + create.totalNodes); //Debug.Log("Created nodes: " + create.createdNodes); while (!create.mapIsReady) { CameraUI.instance.progressText.text = create.createdNodes + "/" + create.totalNodes; CameraUI.instance.progressBar.value = create.createdNodes / create.totalNodes; yield return(null); } CameraUI.instance.progressText.text = create.createdNodes + "/" + create.totalNodes; CameraUI.instance.progressBar.value = create.createdNodes / create.totalNodes; nodes = create.nodes; xSize = create.xSize; zSize = create.zSize; //print("Node count: " + nodes.Length); GameObject g = Instantiate((GameObject)Resources.Load(mapName)); g.transform.position = new Vector3(xSize / 2, 0, zSize / 2); g = Instantiate(nodeMarker); g.transform.position = nodes[0, 0].position; g.transform.localScale = vLength; g = Instantiate(nodeMarker2); g.transform.position = nodes[0, nodes.GetLength(0) - 1].position; g.transform.localScale = vLength; g = Instantiate(nodeMarker3); g.transform.position = nodes[nodes.GetLength(0) - 1, 0].position; g.transform.localScale = vLength; g = Instantiate(nodeMarker2); g.transform.position = nodes[nodes.GetLength(0) - 1, nodes.GetLength(nodes.Rank - 1) - 1].position; g.transform.localScale = vLength; print("Finished loading map"); mapIsReady = true; } else { print("Making new map"); //print("Length: " + length + ", x size: " + xSize + ", Z size: " + zSize); if (xSize == 0 || zSize == 0) { xSize = 2000; zSize = 2000; } CreateMap m = new CreateMap(makeNewMap, length, xSize, zSize, mapName); while (m.totalNodes == 0) { yield return(null); } while (!m.mapIsReady) { CameraUI.instance.progressText.text = m.touchedNodes + "/" + m.totalNodes; CameraUI.instance.progressBar.value = m.touchedNodes / m.totalNodes; yield return(null); } CameraUI.instance.progressText.text = m.touchedNodes + "/" + m.totalNodes; CameraUI.instance.progressBar.value = m.touchedNodes / m.totalNodes; nodes = m.nodes; //print("Node count: " + nodes.Length); GameObject g = Instantiate(nodeMarker); g.transform.position = nodes[0, 0].position; g.transform.localScale = vLength; g = Instantiate(nodeMarker2); g.transform.position = nodes[0, nodes.GetLength(0) - 1].position; g.transform.localScale = vLength; g = Instantiate(nodeMarker3); g.transform.position = nodes[nodes.GetLength(0) - 1, 0].position; g.transform.localScale = vLength; g = Instantiate(nodeMarker2); g.transform.position = nodes[nodes.GetLength(0) - 1, nodes.GetLength(nodes.Rank - 1) - 1].position; g.transform.localScale = vLength; //Save map mapData mData = new mapData(); nodeData[,] data = new nodeData[(int)(xSize * (1 / length)), (int)(zSize * (1 / length))]; for (int i = 0; i < (xSize * (1 / length)); i++) { for (int j = 0; j < (zSize * (1 / length)); j++) { nodeData n = new nodeData(); n.populate(nodes[i, j]); data[i, j] = n; } yield return(null); } mData.mName = mapName; mData.mapNodes = data; mData.xSize = xSize; mData.zSize = zSize; mData.nodeLength = length; //DataSaver.instance.saveMap(mData); mapIsReady = true; print("Finished making and saving map"); } }