如何像蜗牛一样循环生成但在正方形中

How to spawn in loop like snail but in square

你好,我是法国人,请原谅我的英语不好, 我用带有预制游戏对象的种子创建了一代土地,但它们从一列到另一列生成,所以我想创建它,但像蜗牛一样从中心到外部开始,但呈正方形

我部分成功了,第一张图片显示了 columne 中的生成,但也显示了 snail 中生成的所需最终结果,第二张显示了我在 snail 中设法做到的,但我的土地是方形的,看起来很糟糕

假设您从原点 O 开始,其他 4 个方向是 E、S、W、N

  N
W O E
  S

那么您要移动的步序是

E S
W W N N
E E E S S S
W W W W N N N N
...

你可能会注意到,每转两次,就需要将这个方向的移动次数增加一次。

一些伪代码:

var dirs = new Vector3[] { E, S, W, N } // 4 directions
Vector3 pos = O; // origin

do
{
    var dir = 0
    var moveTimes = 1

    for (i = 0; i != 2; ++i)
    {
        for (j = 0; j != moveTimes; ++j)
        {
            pos += dirs[dir]; // step forward
        }
        dir = (dir + 1) % 4; // turn
    }

    moveTimes++;
}
while(/* condition to stop */);

好的,我找到了解决方案,但我认为这不是最好的解决方案

for(int i = 0; i != Size; i++)
        {
            if(i == Size - 1)
            {
                for (int y = 0; y != moveTimes; y++)
                {
                    pos += dirs[dir];
                    GameObject inst = Instantiate(TileMap, pos, Quaternion.identity);
                    inst.GetComponent<MapGeneratorChunk>().ID = ++SetID;
                    inst.GetComponent<MapGeneratorChunk>().Seed = Seed;
                    print("hello");
                }
                dir = (dir + 1) % 4;
                for (int y = 0; y != moveTimes; y++)
                {
                    pos += dirs[dir];
                    GameObject inst = Instantiate(TileMap, pos, Quaternion.identity);
                    inst.GetComponent<MapGeneratorChunk>().ID = ++SetID;
                    inst.GetComponent<MapGeneratorChunk>().Seed = Seed;
                    print("hello");
                }
                dir = (dir + 1) % 4;
                for (int y = 0; y != moveTimes; y++)
                {
                    pos += dirs[dir];
                    GameObject inst = Instantiate(TileMap, pos, Quaternion.identity);
                    inst.GetComponent<MapGeneratorChunk>().ID = ++SetID;
                    inst.GetComponent<MapGeneratorChunk>().Seed = Seed;
                    print("hello");
                }
                dir = (dir + 1) % 4;
            }
            else
            {
                for (int x = 0; x != 2; x++)
                {
                    for (int y = 0; y != moveTimes; y++)
                    {
                        pos += dirs[dir];
                        GameObject inst = Instantiate(TileMap, pos, Quaternion.identity);
                        inst.GetComponent<MapGeneratorChunk>().ID = ++SetID;
                        inst.GetComponent<MapGeneratorChunk>().Seed = Seed;
                    }
                    dir = (dir + 1) % 4;
                }
                moveTimes++;
            }
        }

The result