【问题标题】:How to spawn in loop like snail but in square如何像蜗牛一样在循环中生成,但在正方形中
【发布时间】:2021-12-28 00:28:17
【问题描述】:

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

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

p>
  • 第一:
  • 二:
  • 它的工作方式类似于:
  • 我希望它像这样工作:

【问题讨论】:

    标签: c# for-loop mesh terrain procedural-generation


    【解决方案1】:

    假设您从原点 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 */);
    

    【讨论】:

    • ok thx 但是现在问题是完成了,地图就像蜗牛但是最后一点太多了
    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 2017-03-27
      • 1970-01-01
      • 2021-12-29
      • 2020-07-28
      • 2021-09-30
      • 2018-02-07
      • 1970-01-01
      相关资源
      最近更新 更多