/// <summary> /// Enumerators the load world. /// </summary> /// <returns>The load world.</returns> /// <param name="center">Center.</param> /// <param name="radius">Radius.</param> /// <param name="complate">Complate.</param> IEnumerator OnLoadWorld(System.Action <XVec2I> complate) { generator.InitGenerate(); generator.UpdateTerrain(center.position, radius); do { var exists = generator.IsTerrainAvailable(center.position); if (exists) { CanActivateCharacter = true; } yield return(null); } while (!CanActivateCharacter); centerChunkPosition = generator.GetChunkPosition(center.position); center.position = new Vector3(center.position.x, generator.GetTerrainHeight(center.position) + 0.5f, center.position.z); center.gameObject.SetActive(true); if (complate != null) { complate(centerChunkPosition); } }
/// <summary> /// Gets the generated chunk. /// </summary> /// <returns>The generated chunk.</returns> /// <param name="chunkPosition">Chunk position.</param> public XTerrainChunk GetGeneratedChunk(XVec2I chunkPosition) { if (LoadedChunks.ContainsKey(chunkPosition)) { return(LoadedChunks[chunkPosition]); } return(null); }
/// <summary> /// Chunks the can be removed. /// </summary> /// <returns><c>true</c>, if can be removed was chunked, <c>false</c> otherwise.</returns> /// <param name="x">The x coordinate.</param> /// <param name="z">The z coordinate.</param> public bool ChunkCanBeRemoved(int x, int z) { var key = new XVec2I(x, z); return (RequestedChunks.ContainsKey(key) || ChunksBeingGenerated.ContainsKey(key) || LoadedChunks.ContainsKey(key)); }
/// <summary> /// Initializes a new instance of the <see cref="XTerrainChunk"/> class. /// </summary> /// <param name="setting">Setting.</param> /// <param name="noise">Noise.</param> /// <param name="x">The x coordinate.</param> /// <param name="z">The z coordinate.</param> public XTerrainChunk(XTerrainChunkSetting setting, XNoise noise, int x, int z) { HeightmapThread = new object(); Setting = setting; NoiseProvider = noise; Neighborhood = new XChunkNeighborhood(); Position = new XVec2I(x, z); TreePoint = new List <Vector3>(); ChopPoint = new List <Vector3>(); }
/// <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> /// Update this instance. /// </summary> void Update() { if (CanActivateCharacter && center.gameObject.activeSelf) { XVec2I curChunkPosition = generator.GetChunkPosition(center.position); if (!curChunkPosition.Equals(centerChunkPosition)) { generator.UpdateTerrain(center.position, radius); centerChunkPosition = curChunkPosition; } } }
/// <summary> /// Gets the chunk positions in radius. /// </summary> /// <returns>The chunk positions in radius.</returns> /// <param name="chunkPosition">Chunk position.</param> /// <param name="radius">Radius.</param> private List <XVec2I> GetChunkPositionsInRadius(XVec2I chunkPosition, int radius) { var result = new List <XVec2I>(); for (var zCircle = -radius; zCircle < radius; zCircle++) { for (var xCircle = -radius; xCircle < radius; xCircle++) { if (xCircle * xCircle + zCircle * zCircle < radius * radius) { result.Add(new XVec2I(chunkPosition.X + xCircle, chunkPosition.Z + zCircle)); } } } return(result); }
/// <summary> /// Updates the terrain. /// </summary> /// <param name="worldPosition">World position.</param> /// <param name="radius">Radius.</param> public void UpdateTerrain(Vector3 worldPosition, int radius) { var chunkPosition = GetChunkPosition(worldPosition); var newPositions = GetChunkPositionsInRadius(chunkPosition, radius); var loadedChunks = Cache.GetGeneratedChunks(); var chunksRemove = loadedChunks.Except(newPositions).ToList(); var possGenerate = newPositions.Except(chunksRemove).ToList(); for (int i = 0; i < possGenerate.Count; i++) { XVec2I v = possGenerate [i]; GenerateChunk(v.X, v.Z); } for (int i = 0; i < chunksRemove.Count; i++) { XVec2I v = chunksRemove [i]; RemoveChunk(v.X, v.Z); } }
/// <summary> /// Determines whether this instance is chunk generated the specified chunkPosition. /// </summary> /// <returns><c>true</c> if this instance is chunk generated the specified chunkPosition; otherwise, <c>false</c>.</returns> /// <param name="chunkPosition">Chunk position.</param> public bool IsChunkGenerated(XVec2I chunkPosition) { return(GetGeneratedChunk(chunkPosition) != null); }
/// <summary> /// /// </summary> /// <param name="worldPosition"></param> /// <returns></returns> public XTerrainChunk GetTerrainChunk(Vector3 worldPosition) { XVec2I chunkPosition = GetChunkPosition(worldPosition); return(Cache.GetGeneratedChunk(chunkPosition)); }
/// <summary> /// Determines whether this instance is terrain available the specified worldPosition. /// </summary> /// <returns><c>true</c> if this instance is terrain available the specified worldPosition; otherwise, <c>false</c>.</returns> /// <param name="worldPosition">World position.</param> public bool IsTerrainAvailable(Vector3 worldPosition) { XVec2I chunkPosition = GetChunkPosition(worldPosition); return(Cache.IsChunkGenerated(chunkPosition)); }
/// <summary> /// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="Vec2I"/>. /// </summary> /// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="Vec2I"/>.</param> /// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current <see cref="Vec2I"/>; otherwise, <c>false</c>.</returns> public override bool Equals(object obj) { XVec2I other = obj as XVec2I; return(this.X == other.X && this.Z == other.Z); }