简体   繁体   English

2D阵列-错误的“墙”放置?

[英]2D Array - wrong “Wall ” placement?

Good evening and merry christmas ! 晚上好,圣诞节快乐!

I wanted to make my own Dungeon-Generator. 我想做自己的地牢生成器。 So i decided to first spawn in some empty rooms with placing the walls in the same step. 因此,我决定首先在一些空房间中产卵,并将墙壁放置在同一步骤中。

public void generateRoom(byte[][] dungeon){

    if(roomCornerDownLeft.x + roomWidth < dungeon[0].length && roomCornerDownLeft.y + roomHeigth  < dungeon.length ){


        setRoomFloor(dungeon);

        setWalls(dungeon);


    }

}


public void setRoomFloor(byte[][] dungeon){


    origin = new Vector2(roomCornerDownLeft.x + roomWidth / 2, roomCornerDownLeft.y + roomHeigth / 2);  


    roomHeigth += 1;   // +1 because of the WallBorders. So the Interior of the room is totally 5 tiles height

    roomWidth += 1;    // +1 because of the WallBorders. So the Interior of the room is totally 5 tiles width


    roomCornerUpRight = new Vector2(roomCornerDownLeft.x + roomWidth, roomCornerDownLeft.y + roomHeigth);


    for(int yPos = (int) roomCornerDownLeft.y; yPos <= roomCornerUpRight.y; yPos++){

        for(int xPos = (int) roomCornerDownLeft.x; xPos <= roomCornerUpRight.x ; xPos++){

            dungeon[yPos][xPos] = 1;

        }

    }



}


public void setWalls(byte[][] dungeon){

  // Vertical walls
  for (int i = (int) roomCornerDownLeft.x; i <= roomCornerDownLeft.x + roomHeigth; i++) {

        dungeon[i][(int) roomCornerDownLeft.y] = 2; // North wall

        dungeon[i][(int) roomCornerDownLeft.y + (roomWidth )] = 2; // South wall

  }

  // horizontal walls
    for (int y = (int) roomCornerDownLeft.y; y <= roomCornerDownLeft.y + roomWidth; y++) {

        dungeon[(int) roomCornerDownLeft.x][y] = 2; // North wall

        dungeon[(int) (roomCornerDownLeft.x + ( roomHeigth ))][y] = 2; // South wall

    }

}

But i approached some strange problems with it. 但是我遇到了一些奇怪的问题。 Placing the "room floor" works without any problems. 放置“房间地板”可以正常工作。 The position and size is just correct. 位置和大小是正确的。 But when i try to build a wall around it, it only works sometimes. 但是,当我尝试在其周围建造墙时,它有时仅能起作用。 Heres an example : 这是一个例子:

    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000002222222000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002222222000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000

1 = Floor 2 = Wall 1 =地板2 =墙壁

Xposition = 25; Xposition = 25; Yposition = 25; Yposition = 25;

width = 5; 宽度= 5; height = 5; 高度= 5;

It worked fine right ? 工作正常吧? So now lets try this with some other values : 因此,现在让我们尝试使用其他一些值:

        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000222222222222111110000000000000
        00000000000000000000200001111112111110000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000222222222222000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000

1 = Floor 2 = Wall 1 =地板2 =墙壁

Xposition = 20; Xposition = 20; Yposition = 25; Yposition = 25;

width = 10; 宽度= 10; height = 5; 高度= 5;

As you can see, the align ist right anymore. 如您所见,对齐方式不再正确。 The wall is on a completly other position than the room itself.... The strange part is, that this only happens when the values (xpos,ypos,width and height ) Are different from each other. 墙壁与房间本身的位置完全不同。...奇怪的是,这仅在值(xpos,ypos,width和height)彼此不同时发生。

---UPDATE--- -更新-

When i invert the x and y axis i get this : 当我反转x和y轴时,我得到了:

        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000222222222222000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000222222222222000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000

The coords and sizes are the same as the example above. 坐标和大小与上面的示例相同。

Wheres the problem in my code ? 我的代码中的问题出在哪里? Normally the Wall should be around the room... 通常,墙壁应该在房间周围。

In the inner for-loop of your setRoomFloor method, you're inverting the position on the x-axis and the position on the y-axis. setRoomFloor方法的内部for循环中,您正在反转x轴上的位置和y轴上的位置。

dungeon[yPos][xPos] = 1;

should be 应该

dungeon[xPos][yPos] = 1;

I finally got the solution, my Wall Algorythm was broken, i wrote a new one : 我终于找到了解决方案,我的Wall Algorythm坏了,我写了一个新的解决方案:

  // Vertical Walls 
  for(int y = (int) roomCornerDownLeft.y; y <= roomCornerDownLeft.y + roomHeigth; y++){

      dungeon[y][ (int) roomCornerDownLeft.x ] = 2;

      dungeon[y][ (int) roomCornerDownLeft.x + roomWidth ] = 2;

  } 

  // Horizontal Walls
  for(int x = (int) roomCornerDownLeft.x; x <= roomCornerDownLeft.x + roomWidth; x++){

      dungeon[ (int) roomCornerDownLeft.y ][ x ] = 2;

      dungeon[ (int) roomCornerDownLeft.y + roomHeigth][ x ] = 2;

  } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM