/// <summary> /// 初始化默认地形 /// </summary> public void InitinalMap() { string mapMsg = ""; if (transform.childCount > 1) { for (int i = 0; i < chunks2.Count; i++) { Destroy(chunks2[i].gameObject); } chunks = new Dictionary <Vector3i, Chunk>(); chunks2 = new List <Chunk>(); number = 0; } heightMap = new float[(MapLength + 1) * (MapLength + 1)]; textureMap = new int[MapLength * MapLength]; byte[] byteArray = new byte[(MapLength + 1) * (MapLength + 1) * 2]; StudentCourseInfo info = JsonMapper.ToObject <StudentCourseInfo>(Resources.Load <DefaultSceneInfo>("Config/DefaultSceneInfo").info); mapInfoFromInitinalDemo = info.code; JSONNode obj = JSON.Parse(mapInfoFromInitinalDemo); mapMsg = obj["mapInfo"]["mapData"]; byteArray = System.Convert.FromBase64String(mapMsg); for (int j = 0; j < MapLength + 1; j++) { for (int i = 0; i < MapLength + 1; i++) { byteMsg[0] = byteArray[(i + j * (MapLength + 1)) * 2]; byteMsg[1] = byteArray[(i + j * (MapLength + 1)) * 2 + 1]; TileTerrainMsgTool.ByteToTileMsg(byteMsg, out heightIntNumber, out heightFloatNumber, out TextureNumber); if (i == MapLength || j == MapLength) { heightMap[i + j * (MapLength + 1)] = heightIntNumber - 6f + HeightIntToFloat(heightFloatNumber); } else { heightMap[i + j * (MapLength + 1)] = heightIntNumber - 6f + HeightIntToFloat(heightFloatNumber); textureMap[i + j * MapLength] = TextureNumber; } } } for (int z = 0; z < 0 + ChunkWidth * chunkLineNumber; z += ChunkWidth) { for (int x = 0; x < 0 + ChunkWidth * chunkLineNumber; x += ChunkWidth) { int xx = ChunkWidth * Mathf.FloorToInt(x / ChunkWidth); int zz = ChunkWidth * Mathf.FloorToInt(z / ChunkWidth); if (!ChunkExists(xx, 0, zz)) { CreateChunk(new Vector3i(xx, 0, zz)); } } } LoadMapOver?.Invoke(); }
/// <summary> /// 将数据转换成地形(根据地形数据加载地形) /// </summary> public void MsgToMap(string mapMsg) { if (transform.childCount > 1) { for (int i = 0; i < chunks2.Count; i++) { Destroy(chunks2[i].gameObject); } chunks = new Dictionary <Vector3i, Chunk>(); chunks2 = new List <Chunk>(); number = 0; } heightMap = new float[(MapLength + 1) * (MapLength + 1)]; textureMap = new int[MapLength * MapLength]; byte[] byteArray = new byte[(MapLength + 1) * (MapLength + 1) * 2]; byteArray = System.Convert.FromBase64String(mapMsg); for (int j = 0; j < MapLength + 1; j++) { for (int i = 0; i < MapLength + 1; i++) { byteMsg[0] = byteArray[(i + j * (MapLength + 1)) * 2]; byteMsg[1] = byteArray[(i + j * (MapLength + 1)) * 2 + 1]; TileTerrainMsgTool.ByteToTileMsg(byteMsg, out heightIntNumber, out heightFloatNumber, out TextureNumber); if (i == MapLength || j == MapLength) { heightMap[i + j * (MapLength + 1)] = heightIntNumber - 6f + HeightIntToFloat(heightFloatNumber); } else { heightMap[i + j * (MapLength + 1)] = heightIntNumber - 6f + HeightIntToFloat(heightFloatNumber); textureMap[i + j * MapLength] = TextureNumber; } } } for (int z = 0; z < 0 + ChunkWidth * chunkLineNumber; z += ChunkWidth) { for (int x = 0; x < 0 + ChunkWidth * chunkLineNumber; x += ChunkWidth) { int xx = ChunkWidth * Mathf.FloorToInt(x / ChunkWidth); int zz = ChunkWidth * Mathf.FloorToInt(z / ChunkWidth); if (!ChunkExists(xx, 0, zz)) { CreateChunk(new Vector3i(xx, 0, zz)); } } } LoadMapOver?.Invoke(); }