Beispiel #1
0
    // Adds random doors to random rooms in the list of room coords.
    // Returns false if none possible.
    bool AddRandomDoors(List <Pair> roomCoords, int numDoorsToAdd)
    {
        for (int i = 0; i < numDoorsToAdd; i++)
        {
            if (roomCoords.Count == 0)
            {
                return(false);
            }
            // Get the room to add the random door to.
            Pair room = roomCoords[GameManager.instance.prng.Next(0, roomCoords.Count)];
            // Get the random door from the list of possible doors to add. If none possible, remove it from the
            // list of room coords and try again
            HashSet <Room.Door> possibleNewDoors = GetPossibleNewDoors(room.w, room.h);
            if (possibleNewDoors.Count == 0)
            {
                roomCoords.Remove(room);
                i--;
                continue;
            }
            Room.Door doorToAdd = GameManager.GetRandom <Room.Door>(possibleNewDoors);

            bool ret = AddDoorToRoom(room, doorToAdd);
            if (!ret)   // something went wrong
            {
                Debug.Log("Couldn't add a random door.");
                return(false);
            }
        }
        return(true);
    }
    public void CreateCorridor(Room.Door doorToStart, Room.Door doorToEnd)
    {
        int corridorX = doorToStart.PosX;
        int corridorY = doorToStart.PosY;

        int corridorWidth  = 0;
        int corridorHeight = 0;

        Vector2 direction = new Vector2(doorToEnd.PosX, doorToEnd.PosY) - new Vector2(doorToStart.PosX, doorToStart.PosY).normalized;
    }
    public void DrawTile(Room.Door door)
    {
        Color      color  = new Color(1, 1, 1);
        GameObject parent = new GameObject(door.Room.DebugID.ToString() + " room");

        GameObject tile = Instantiate(Tile, new Vector3(door.PosX, door.PosY, 0), Quaternion.identity);

        tile.transform.parent = parent.transform;
        SpriteRenderer tileColor = tile.GetComponent <SpriteRenderer>();

        tileColor.color = color;
    }
Beispiel #4
0
    // Handles adding a door on both sides (the inputted room, and the connected room)
    bool AddDoorToRoom(Pair roomCoords, Room.Door doorToAdd)
    {
        Room baseRoom        = GetRoomFromCoords(roomCoords);
        Pair otherRoomCoords = null;

        Room.Door otherDoorToAdd = Room.Door.NULL;
        switch (doorToAdd)
        {
        case Room.Door.Bot:
            otherRoomCoords = new Pair(roomCoords.w, roomCoords.h - 1);
            otherDoorToAdd  = Room.Door.Top;
            break;

        case Room.Door.Top:
            otherRoomCoords = new Pair(roomCoords.w, roomCoords.h + 1);
            otherDoorToAdd  = Room.Door.Bot;
            break;

        case Room.Door.Left:
            otherRoomCoords = new Pair(roomCoords.w - 1, roomCoords.h);
            otherDoorToAdd  = Room.Door.Right;
            break;

        case Room.Door.Right:
            otherRoomCoords = new Pair(roomCoords.w + 1, roomCoords.h);
            otherDoorToAdd  = Room.Door.Left;
            break;
        }
        if (otherRoomCoords == null)
        {
            return(false);
        }

        Room otherRoom = GetRoomFromCoords(otherRoomCoords);

        baseRoom.AddDoor(doorToAdd);
        otherRoom.AddDoor(otherDoorToAdd);
        Debug.Log("Added door from " + roomCoords.ToString() + " to " + otherRoomCoords.ToString());
        numDoors++;
        return(true);
    }
    // Returns a list of Room-Door pairs that fit a target door, and get the offset required to transform
    // the position of the prefab clone to designated position
    // Pre  : offsetlist empty
    // Post : offsetlist contains all offsets of room-door pairs
    public List <Vector2Int> FindRoomsThatFitDoor(Room.Door door, List <int> rooms, string orientation, ref List <Vector3Int> offsetlist)
    {
        if (offsetlist.Count > 0)
        {
            offsetlist.Clear();
        }
        List <Vector2Int> room_to_door_pair = new List <Vector2Int>();
        Vector3Int        offset_holder     = new Vector3Int(0, 0, 0);

        if (orientation == "NW")
        {
            foreach (int i in rooms)
            {
                for (int d = 0; d < room_list_[i].nw_doors_.Count; d++)
                {
                    // Get the offset of the room to position room.nw_doors_[d] to door
                    offset_holder = room_list_[i].GetPositionOffset(room_list_[i].nw_doors_[d], door);
                    // Get the temporary bounding box used in calculation with the offset
                    RoomAABB temp_aabb = room_list_[i].GetOffsetBounding(offset_holder);
                    bool     temp_flag = false;
                    // Test if repositioned bounding box intersects with any existing room,
                    // if not, add it to list to be considered a valid room-door combination
                    foreach (Room r in room_clone_list_)
                    {
                        if (r.bounding_.IsOverlap(temp_aabb))
                        {
                            temp_flag = true;
                        }
                    }
                    if (!temp_flag)
                    {
                        room_to_door_pair.Add(new Vector2Int(i, d));
                        offsetlist.Add(offset_holder);
                    }
                }
            }
        }
        else if (orientation == "NE")
        {
            foreach (int i in rooms)
            {
                for (int d = 0; d < room_list_[i].ne_doors_.Count; d++)
                {
                    offset_holder = room_list_[i].GetPositionOffset(room_list_[i].ne_doors_[d], door);
                    RoomAABB temp_aabb = room_list_[i].GetOffsetBounding(offset_holder);
                    bool     temp_flag = false;
                    foreach (Room r in room_clone_list_)
                    {
                        if (r.bounding_.IsOverlap(temp_aabb))
                        {
                            temp_flag = true;
                        }
                    }
                    if (!temp_flag)
                    {
                        room_to_door_pair.Add(new Vector2Int(i, d));
                        offsetlist.Add(offset_holder);
                    }
                }
            }
        }
        else if (orientation == "SW")
        {
            foreach (int i in rooms)
            {
                for (int d = 0; d < room_list_[i].sw_doors_.Count; d++)
                {
                    offset_holder = room_list_[i].GetPositionOffset(room_list_[i].sw_doors_[d], door);
                    RoomAABB temp_aabb = room_list_[i].GetOffsetBounding(offset_holder);
                    bool     temp_flag = false;
                    foreach (Room r in room_clone_list_)
                    {
                        if (r.bounding_.IsOverlap(temp_aabb))
                        {
                            temp_flag = true;
                        }
                    }
                    if (!temp_flag)
                    {
                        room_to_door_pair.Add(new Vector2Int(i, d));
                        offsetlist.Add(offset_holder);
                    }
                }
            }
        }
        else if (orientation == "SE")
        {
            foreach (int i in rooms)
            {
                for (int d = 0; d < room_list_[i].se_doors_.Count; d++)
                {
                    offset_holder = room_list_[i].GetPositionOffset(room_list_[i].se_doors_[d], door);
                    RoomAABB temp_aabb = room_list_[i].GetOffsetBounding(offset_holder);
                    bool     temp_flag = false;
                    foreach (Room r in room_clone_list_)
                    {
                        if (r.bounding_.IsOverlap(temp_aabb))
                        {
                            temp_flag = true;
                        }
                    }
                    if (!temp_flag)
                    {
                        room_to_door_pair.Add(new Vector2Int(i, d));
                        offsetlist.Add(offset_holder);
                    }
                }
            }
        }
        return(room_to_door_pair);
    }