public void SaveData(StaticWorld world, string savePath) { Stopwatch s = Stopwatch.StartNew(); Dictionary <Position, Chunk> chunks = world.Chunks; WorldData data = new WorldData(world.worldSize, world.columnHeight); foreach (KeyValuePair <Position, Chunk> keyValuePair in chunks) { Position position = keyValuePair.Key / World.chunkSize; Chunk chunk = keyValuePair.Value; ChData chunkData = data[position.X, position.Y, position.Z] = new ChData(); for (int z = 0; z < World.chunkSize; z++) { for (int y = 0; y < World.chunkSize; y++) { for (int x = 0; x < World.chunkSize; x++) { chunkData[x, y, z] = chunk.ChunkData.chunkData[x, y, z].Type; } } } } Debug.Log("Parse " + s.ElapsedMilliseconds); this.SaveData(data, savePath); Debug.Log("Save " + s.ElapsedMilliseconds); }
public Chunk(Position position, Transform parrent, ChData data) { this.ChunkGameObject = new GameObject(position.ToString()); this.ChunkGameObject.AddComponent <ObjectPosition>().Position = position; this.ChunkGameObject.transform.parent = parrent; this.ChunkGameObject.transform.localPosition = position * StaticWorld.K; this.Position = position; this.MeshFiltersBlock = new List <BlockQuad>(); this.BuildChunk(data); }
private void BuildChunk(ChData data = null) { this.ChunkData = new ChunkData(new Block[World.chunkSize, World.chunkSize, World.chunkSize]); for (int z = 0; z < World.chunkSize; z++) { for (int y = 0; y < World.chunkSize; y++) { for (int x = 0; x < World.chunkSize; x++) { Vector3 pos = new Vector3(x, y, z); // Load Save if (data != null) { this.ChunkData[x, y, z] = new Block(data[x, y, z], pos, this); } // Create new else { int worldX = (int)(x + this.Position.X); int worldY = (int)(y + this.Position.Y); int worldZ = (int)(z + this.Position.Z); int surfaceHeight = Utils.GenerateHeight(worldX, worldZ); //int surfaceHeight = StaticWorld.columnHeight * World.chunkSize; //Transform chunkTransform = this.ChunkGameObject.gameObject.transform; if (worldY == 0) { this.ChunkData[x, y, z] = new Block(BlockType.COBBLESTONE, pos, this); } else if (Utils.fBM3D(worldX, worldY, worldZ, 0.1f, 3) < 0.42f) { this.ChunkData[x, y, z] = new Block(BlockType.AIR, pos, this); } else if (worldY <= Utils.GenerateStoneHeight(worldX, worldZ)) { this.ChunkData[x, y, z] = new Block(BlockType.STONE, pos, this); } else if (StaticWorld.Instance.grassTemp && worldY == surfaceHeight + 1) { float tallGrass = 0.4f; float flower = 0.6f; if (Mathf.PerlinNoise(worldX * tallGrass, worldZ * tallGrass) > 0.6f) { this.ChunkData[x, y, z] = new Block(BlockType.TALLGRASS, pos, this); } else if (Mathf.PerlinNoise(worldX * flower, worldZ * flower) > 0.8f) { this.ChunkData[x, y, z] = new Block(BlockType.FLOVER, pos, this); } else { this.ChunkData[x, y, z] = new Block(BlockType.AIR, pos, this); } } else if (worldY == surfaceHeight) { this.ChunkData[x, y, z] = new Block(BlockType.GRASS, pos, this); } else if (worldY < surfaceHeight) { this.ChunkData[x, y, z] = new Block(BlockType.DIRT, pos, this); } else { this.ChunkData[x, y, z] = new Block(BlockType.AIR, pos, this); } } } } } }