/// <summary> /// Sets the chunk neighborhood. /// </summary> /// <param name="chunk">Chunk.</param> private void SetChunkNeighborhood(XTerrainChunk chunk) { XTerrainChunk xUp; XTerrainChunk xDown; XTerrainChunk zUp; XTerrainChunk zDown; LoadedChunks.TryGetValue(new XVec2I(chunk.Position.X + 1, chunk.Position.Z), out xUp); LoadedChunks.TryGetValue(new XVec2I(chunk.Position.X - 1, chunk.Position.Z), out xDown); LoadedChunks.TryGetValue(new XVec2I(chunk.Position.X, chunk.Position.Z + 1), out zUp); LoadedChunks.TryGetValue(new XVec2I(chunk.Position.X, chunk.Position.Z - 1), out zDown); if (xUp != null) { chunk.SetNeighbors(xUp, TerrainNeighbor.XUp); xUp.SetNeighbors(chunk, TerrainNeighbor.XDown); } if (xDown != null) { chunk.SetNeighbors(xDown, TerrainNeighbor.XDown); xDown.SetNeighbors(chunk, TerrainNeighbor.XUp); } if (zUp != null) { chunk.SetNeighbors(zUp, TerrainNeighbor.ZUp); zUp.SetNeighbors(chunk, TerrainNeighbor.ZDown); } if (zDown != null) { chunk.SetNeighbors(zDown, TerrainNeighbor.ZDown); zDown.SetNeighbors(chunk, TerrainNeighbor.ZUp); } }
/// <summary> /// Update this instance. /// </summary> void Update() { if (Input.GetMouseButtonDown(0)) { XTerrainChunk chunk = GetTerrainChunk(Camera.main.ScreenToWorldPoint(Input.mousePosition)); chunk.ReplaceTree(); } Cache.Update(); }
/// <summary> /// Gets the height of the terrain. /// </summary> /// <returns>The terrain height.</returns> /// <param name="worldPosition">World position.</param> public float GetTerrainHeight(Vector3 worldPosition) { XVec2I chunkPosition = GetChunkPosition(worldPosition); XTerrainChunk chunk = Cache.GetGeneratedChunk(chunkPosition); if (chunkPosition != null) { return(chunk.GetTerrainHeight(worldPosition)); } return(0); }
/// <summary> /// Generates the chunk. /// </summary> /// <param name="x">The x coordinate.</param> /// <param name="z">The z coordinate.</param> private void GenerateChunk(int x, int z) { if (Cache.ChunkCanBeAdded(x, z)) { XNoise noise = null; if (NoiseType == XNoiseType.NT_NONE) { noise = NoiseProvider; } else if (NoiseType == XNoiseType.NT_PERLIN) { noise = new XNoisePerlin(); if (x > 0) { int offset = (noise.bottom - noise.top) * x; noise.top = noise.top + offset; noise.bottom = noise.bottom + offset; } else { int offset = (noise.bottom - noise.top) * x; noise.top = noise.top + offset; noise.bottom = noise.bottom + offset; } if (z > 0) { int offset = (noise.right - noise.left) * z; noise.left = noise.left + offset; noise.right = noise.right + offset; } else { int offset = (noise.right - noise.left) * z; noise.left = noise.left + offset; noise.right = noise.right + offset; } } var chunk = new XTerrainChunk(Settings, noise, x, z); Cache.AddNewChunk(chunk); } }
/// <summary> /// Removes from neighborhood. /// </summary> /// <param name="chunk">Chunk.</param> public void RemoveFromNeighborhood(XTerrainChunk chunk) { if (Neighborhood.XDown == chunk) { Neighborhood.XDown = null; } if (Neighborhood.XUp == chunk) { Neighborhood.XUp = null; } if (Neighborhood.ZDown == chunk) { Neighborhood.ZDown = null; } if (Neighborhood.ZUp == chunk) { Neighborhood.ZUp = null; } }
/// <summary> /// Sets the neighbors. /// </summary> /// <param name="chunk">Chunk.</param> /// <param name="direction">Direction.</param> public void SetNeighbors(XTerrainChunk chunk, TerrainNeighbor direction) { if (chunk != null) { switch (direction) { case TerrainNeighbor.XUp: Neighborhood.XUp = chunk; break; case TerrainNeighbor.XDown: Neighborhood.XDown = chunk; break; case TerrainNeighbor.ZUp: Neighborhood.ZUp = chunk; break; case TerrainNeighbor.ZDown: Neighborhood.ZDown = chunk; break; } } }
/// <summary> /// Adds the new chunk. /// </summary> /// <param name="chunk">Chunk.</param> public void AddNewChunk(XTerrainChunk chunk) { RequestedChunks.Add(chunk.Position, chunk); GenerateHeightmapForAvailableChunks(); }