Roguelike Random Dungeon | 3. Build a Wall

Roguelike Random Dungeons

Build a wall


Build a wall

Now we have fifteen types of wall resources with combinations of upper, lower, left, and right doors. We name and set up fifteen prefabricated bodies respectively for our subsequent use.
We just need to create an empty GameObject and put our assets into it as a subset.
insert image description here
Next we need to get these prefabs in code.
If we all use Public GameObject to generate, it will appear that our code is messy.
So here I define a new class to store these prefabs.
Here, in order for our Unity to recognize it, we need to add **[System.Serializable]** to the class so that it can be recognized by Unity.

[System.Serializable]
public class WallType
{
    
    
    public GameObject singleLeft, singleRight, singleUp, singleBottom,
                      doubleUL, doubleLR, doubleBL, doubleUR, doubleUB, doubleBR,
                      tripleULR, tripleUBL, tripleUBR, tripleBLR,
                      fourDoors;
}

In this way, when you return to Unity, you can put the prefab into the corresponding box.
insert image description here
The next step is to realize the generation of corresponding prefabs in each room.
In the Room script before, we can judge that a room has up, down, left, and right doors, and how many openings DoorNumber has .
So we can use a simple Switch statement to determine how many doors there are and directly select our prefabs from the corresponding prefabs with several doors . Find the prefab that corresponds to the door that has up, down, left, and right.
Use the four directions we defined to determine the direction of the door.

        newRoom.roomUp = Physics2D.OverlapCircle(roomPosition + new Vector3(0, yOffset, 0), 0.2f, roomLayer);
        newRoom.roomDown = Physics2D.OverlapCircle(roomPosition + new Vector3(0, -yOffset, 0), 0.2f, roomLayer);
        newRoom.roomLeft = Physics2D.OverlapCircle(roomPosition + new Vector3(-xOffset, 0, 0), 0.2f, roomLayer);
        newRoom.roomRight = Physics2D.OverlapCircle(roomPosition + new Vector3(xOffset, 0, 0), 0.2f, roomLayer);

Finally, we can generate the prefab through the Instantiate method.

    public void SetupRoom(Room newRoom,Vector3 roomPosition)
    {
    
    
        newRoom.roomUp = Physics2D.OverlapCircle(roomPosition + new Vector3(0, yOffset, 0), 0.2f, roomLayer);
        newRoom.roomDown = Physics2D.OverlapCircle(roomPosition + new Vector3(0, -yOffset, 0), 0.2f, roomLayer);
        newRoom.roomLeft = Physics2D.OverlapCircle(roomPosition + new Vector3(-xOffset, 0, 0), 0.2f, roomLayer);
        newRoom.roomRight = Physics2D.OverlapCircle(roomPosition + new Vector3(xOffset, 0, 0), 0.2f, roomLayer);

        newRoom.UpdateRoom();

        switch (newRoom.doorNumber)
        {
    
    
            case 1:
                if (newRoom.roomUp)
                    Instantiate(wallType.singleUp, roomPosition, Quaternion.identity);
                if (newRoom.roomDown)
                    Instantiate(wallType.singleBottom, roomPosition, Quaternion.identity);
                if (newRoom.roomLeft)
                    Instantiate(wallType.singleLeft, roomPosition, Quaternion.identity);
                if (newRoom.roomRight)
                    Instantiate(wallType.singleRight, roomPosition, Quaternion.identity);
                break;
            case 2:
                if (newRoom.roomUp && newRoom.roomDown)
                    Instantiate(wallType.doubleUB, roomPosition, Quaternion.identity);
                if (newRoom.roomUp && newRoom.roomLeft)
                    Instantiate(wallType.doubleUL, roomPosition, Quaternion.identity);
                if (newRoom.roomUp && newRoom.roomRight)
                    Instantiate(wallType.doubleUR, roomPosition, Quaternion.identity);
                if (newRoom.roomDown && newRoom.roomLeft)
                    Instantiate(wallType.doubleBL, roomPosition, Quaternion.identity);
                if (newRoom.roomDown && newRoom.roomRight)
                    Instantiate(wallType.doubleBR, roomPosition, Quaternion.identity);
                if (newRoom.roomLeft && newRoom.roomRight)
                    Instantiate(wallType.doubleLR, roomPosition, Quaternion.identity);
                break;
            case 3:
                if (newRoom.roomUp && newRoom.roomDown && newRoom.roomLeft)
                    Instantiate(wallType.tripleUBL, roomPosition, Quaternion.identity);
                if (newRoom.roomUp && newRoom.roomDown && newRoom.roomRight)
                    Instantiate(wallType.tripleUBR, roomPosition, Quaternion.identity);
                if (newRoom.roomDown && newRoom.roomLeft && newRoom.roomRight)
                    Instantiate(wallType.tripleBLR, roomPosition, Quaternion.identity);
                if (newRoom.roomUp && newRoom.roomLeft && newRoom.roomRight)
                    Instantiate(wallType.tripleULR, roomPosition, Quaternion.identity);
                break;
            case 4:
                if (newRoom.roomUp && newRoom.roomLeft && newRoom.roomRight && newRoom.roomDown)
                    Instantiate(wallType.fourDoors, roomPosition, Quaternion.identity);
                break;
        }

The code here may be cumbersome, but it is for our better use in the future. If there is a new material, you can directly replace the prefab.
Finally go back to our Unity and run it.
insert image description here
In this way, the effect we want appears.


Summarize

In this section, we mainly realize the establishment of the Wall. Next time we will use the Tilemap tile function of Unity's component to draw our scene and create our Player.

Guess you like

Origin blog.csdn.net/m0_64058685/article/details/124791188