예제 #1
0
    /// <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);
        }
    }
예제 #2
0
    /// <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);
    }
예제 #3
0
    /// <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));
    }
예제 #4
0
 /// <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>();
 }
예제 #5
0
    /// <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);
    }
예제 #6
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;
         }
     }
 }
예제 #7
0
    /// <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);
    }
예제 #8
0
    /// <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);
        }
    }
예제 #9
0
 /// <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);
 }
예제 #10
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="worldPosition"></param>
    /// <returns></returns>
    public XTerrainChunk    GetTerrainChunk(Vector3 worldPosition)
    {
        XVec2I chunkPosition = GetChunkPosition(worldPosition);

        return(Cache.GetGeneratedChunk(chunkPosition));
    }
예제 #11
0
    /// <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));
    }
예제 #12
0
    /// <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);
    }