Example #1
0
    private IEnumerator SpawnChunksAroundCenter(Int3 center, MoveDirection dir)
    {
        int w = renderDistance;
        int h = renderDistanceVertical;

        Int3 min = new Int3(-w, -h, -w) + center;
        Int3 max = new Int3(+w, +h, +w) + center;

        if (dir == MoveDirection.Right)
        {
            min.x = max.x;
        }
        if (dir == MoveDirection.Up)
        {
            min.y = max.y;
        }
        if (dir == MoveDirection.Front)
        {
            min.z = max.z;
        }

        if (dir == MoveDirection.Left)
        {
            max.x = min.x;
        }
        if (dir == MoveDirection.Down)
        {
            max.y = min.y;
        }
        if (dir == MoveDirection.Back)
        {
            max.z = min.z;
        }

        List <Int3> spawnPoints = new List <Int3>(); // make a list of locations to spawn chunks at

        for (int x = min.x; x <= max.x; x++)
        {
            for (int y = min.y; y <= max.y; y++)
            {
                for (int z = min.z; z <= max.z; z++)
                {
                    Int3 p = new Int3(x, y, z);

                    // calculate this chunks distance from the center:

                    p.tag = Int3.ManhattanDis(p, center);
                    // add the chunks in order by distance

                    // find the index number to insert the new chunk:
                    int i = 0;
                    while (i < spawnPoints.Count)
                    {
                        // if this other location is further away,
                        // we've found where to insert our chunk, so break out
                        if (p.tag < spawnPoints[i].tag)
                        {
                            break;
                        }
                        i++;
                    }
                    spawnPoints.Insert(i, p); // insert position
                }
            }
        }

        // spawn the chunks:
        foreach (Int3 p in spawnPoints)
        {
            SpawnChunkAt(p);
            yield return(null);
        }
    }