C#Unity-Roguelike房间生成

时间:2018-06-24 13:21:05

标签: c# unity3d

我在业余时间闲逛Unity。我正在学习通过创建一个roguelike。我是Unity引擎的新手。这是我不太了解的问题:我实例化了预制件,并且效果很好。但是每个的位置都有些“偏离”。看起来像是网格,而不是连续的房间。

This is what it looks like

这是我用来创建此代码的代码:

int roomWidth = rnd.Next(7, 14);
    int roomHeight = rnd.Next(10, 16);

    lastRoomHeight = roomHeight;
    lastRoomWidth = roomWidth;


    for(int y = 0; y < roomHeight; y ++)
    {
        for(int x = 0; x < roomWidth; x++)
        {
            GameObject newWall = null;
            if (x == 0 || x == roomWidth - 1 || y == 0 || y == roomHeight - 1)
            {
                if(x == 0 && y == 0 || x == roomWidth-1 && y == 0 || x == 0 && y == roomHeight-1 || x == roomWidth-1 && y == roomHeight-1)
                {
                    newWall = Instantiate(floorTile, new Vector3(x, 1, y), Quaternion.identity);
                }
                else
                {
                    newWall = Instantiate(wallTile, new Vector3(x, 1, y), Quaternion.identity);
                    newWall.transform.parent = this.transform;

                    if (y == roomHeight - 1)
                        newWall.transform.Rotate(new Vector3(0, 180, 0));
                    if (x == 0)
                        newWall.transform.Rotate(new Vector3(0, -270, 0));
                    if (x == roomWidth - 1)
                        newWall.transform.Rotate(new Vector3(0, 270, 0));
                }

            }
            GameObject newTile =  Instantiate(floorTile, new Vector3(x, 0, y), Quaternion.identity);
            newTile.transform.parent = this.transform;
        }
    }

也许有人可以看到我在做错什么。

0 个答案:

没有答案