/// <summary> /// Offset the location (usually to rebuild) /// </summary> /// <param name="offset"></param> public void OffsetLocation(Point offset, SpeedyDictionary chunkCollection) { // Remove old chunks and update location for (int i = 0; i < chunks.Length; ++i) { chunkCollection.Remove(new Vector3(location.X, i * chunkSize, location.Y)); } this.location.X += offset.X; this.location.Y += offset.Y; // Update the chunk locations as well for (int i = 0; i < chunks.Length; ++i) { chunks[i].SetLocation(new Vector3(location.X, i * chunkSize, location.Y)); } // Reset biome location and generate 2D biome map data biome.SetLocation(location); Parallel.For(0, chunkSize + 2, x => { for (int z = 0; z < chunkSize + 2; ++z) { biome.GetBaseHeight(x, z, true); biome.GetLocalHumidity(x, z, 0.05f, true); } }); // Set dirty flag state = ChunkState.Dirty; }
/// <summary> /// Attempt to connect neighboring chunks which are already loaded. /// </summary> public void ConnectNeighbors(SpeedyDictionary chunks, int size) { // Loop through possible neighbors in all directions for (int i = 0; i < 9; i++) { int x = 0; int z = 0; if (i != 4) { if (i % 3 == 0) { x -= 1; } if (i % 3 == 2) { x += 1; } if (i < 3) { z -= 1; } if (i > 5) { z += 1; } Vector3 location = offset + new Vector3(x * size, 0, z * size); Chunk neighborChunk = (Chunk)chunks.Get(location); int j = (i > 4) ? i - 1 : i; if (neighborChunk != null) { if (neighborChunk.state != ChunkState.Dirty) { sideNeighbors[j] = neighborChunk; } } // Finish looking up this chunk } } // Finish checking all neighbors }
/// <summary> /// Load chunks for the first time /// </summary> public ChunkManager(GraphicsDevice graphicsDevice, int seed) { // Set the initial seed this.mapSeed = seed; // Get the bounds of the visible world and set up chunk structures int totalChunks = (int)Math.Pow((visibleRadius * 2 + 1), 2); chunkStacks = new Dictionary <Point, ChunkStack>(totalChunks); orderedChunkStacks = new List <ChunkStack>(totalChunks); // Initialize lists for chunk distance ordering chunks = new SpeedyDictionary();// Dictionary<Vector3, Chunk>(totalChunks * chunksPerStack); orderedChunks = new List <Chunk>(totalChunks * chunksPerStack); // Setup biome and cache voxelCache = new VoxelCache(chunkSize, chunkSize, chunkSize); // Populator to add additional objects to chunks //populator = new Engine.TreePopulator(biome, voxelCache, new Random(seed)); }