[Unity] Dungeon Gunner (28) - Dungeon Builder Placing Rooms[3]

suhan0304·2024년 2월 27일
0

유니티 - Dungeon Gunner

목록 보기
28/30
post-thumbnail

Dungeon Builder Placing Rooms[2]에 이어서 작성한 코드 내용이다.

DungeonBuilder.cs

/// <summary>
/// Attempt to place the room node in the dungeon - if room can be placed return the room, else return null
/// </summary>
private bool CanPlaceRoomWithNoOverlaps(RoomNodeSO roomNode, Room parentRoom)
{
    // initialise and assume overlap until proven otherwise.
    bool roomOverlaps = true;

    // Do while Room Overlaps - try to place against all available doorways of the parent until
    // the room is successfully placed without overlap.

    while (roomOverlaps)
    {
        // Select random unconnected available doorway for Parent
        List<Doorway> unconnectedAbailableParentDoorways = GetUnconnectedAvailableDoorways(parentRoom.doorWayList).ToList();

        if (unconnectedAbailableParentDoorways.Count == 0)
        {
            // If no more doorways to try then overlap failure.
            return false; // room overlaps
        }

        Doorway doorwayParent = unconnectedAbailableParentDoorways[UnityEngine.Random.Range(0, unconnectedAbailableParentDoorways.Count)];

        // Get a random room template for room node that is consistent with the parent door orientation
        RoomTemplateSO roomtemplate = GetRandomTemplateForRoomConsistentWithParent(roomNode, doorwayParent);

        // Create a room
        Room room = CreateRoomFromRoomTemplate(roomtemplate, roomNode);

        // Plcae the room - returns true if the room doesn't overlap
        if (PlaceTheRoom(parentRoom, doorwayParent, room))
        {

        }

    }
}

/// <summary>
/// Get random room template for room node taking into account the parent doorway orientation
/// </summary>
private RoomTemplateSO GetRandomTemplateForRoomConsistentWithParent(RoomNodeSO roomNode, Doorway doorwayParent)
{
    RoomTemplateSO roomtemplate = null;

    // If room node is a corridor then select random correct Corridor room template based on
    // parent doorway orientation
    if (roomNode.roomNodeType.isCorridor)
    {
        switch (doorwayParent.orientation)
        {
            case Orientation.north:
            case Orientation.south:
                roomtemplate = GetRandomRoomTemplate(roomNodeTypeList.list.Find(x => x.isCorridorNS));
                break;

            case Orientation.east:
            case Orientation.west:
                roomtemplate = GetRandomRoomTemplate(roomNodeTypeList.list.Find(x => x.isCorridorEW));
                break;

            case Orientation.none:
                break;
            
            default: 
                break;
        }
    }
    //Else select random room template
    else
    {
        roomtemplate = GetRandomRoomTemplate(roomNode.roomNodeType);
    }

    return roomtemplate;
}

/// <summary>
/// Place the room - returns true if the room doesn't overlap, false otherwise
/// </summary>
private bool PlaceTheRoom(Room parnetRoom, Doorway doorwayParent, Room room)
{
    // Get current room doorway position
    Doorway doorway = GetOppositeDoorway(doorwayPoint, room.doorWayList);

    // Returns if no doorway in room opposite to parent doorway
    if (doorway == null)
    {
        // Just mark the parnet doorway as unavailable so we don't try and connect it again
        doorwayParent.isUnavailable = true;

        return false;
    }
}
profile
Be Honest, Be Harder, Be Stronger

0개의 댓글

관련 채용 정보