示例#1
0
    // Generates the chunks near the target, within the chunk distance
    public IEnumerator GenerateNearChunks()
    {
        generatingChunks = true;

        float worldSpaceChunkInterval = (float)(world.chunkSize * world.tileSize) / 100;

        for (int y = -Mathf.FloorToInt(chunkDistance / 2) + 1; y < Mathf.FloorToInt(chunkDistance / 2); y++)
        {
            for (int x = -Mathf.FloorToInt(chunkDistance / 2) + 1; x < Mathf.FloorToInt(chunkDistance / 2); x++)
            {
                Vector2Int chunkPos = new Vector2Int(Mathf.RoundToInt(target.position.x / worldSpaceChunkInterval), Mathf.RoundToInt(target.position.y / worldSpaceChunkInterval)) + new Vector2Int(x, y);

                bool alreadyGenerated = false;
                foreach (ChunkGenerator chunkGenerator in chunkGenerators)
                {
                    if (chunkGenerator.chunkPos == chunkPos)
                    {
                        alreadyGenerated = true;
                    }
                }

                if (!alreadyGenerated)
                {
                    ChunkGenerator cg = ChunkGenerator.Create(world, chunkPos);
                    if (cg != null)
                    {
                        chunkGenerators.Add(cg);

                        while (!cg.hasChunkGenerated())                         // Wait for the chunk to generate before generating another chunk, don't want to use all threads.
                        {
                            yield return(new WaitForFixedUpdate());
                        }
                    }
                }
            }
        }

        UnloadFarChunks();

        generatingChunks = false;
    }