コード例 #1
0
    /// <summary>
    /// Removes the room from the overall dictionary, sets all door parents and destroys the room object
    /// </summary>
    /// <param name="room">Room to destroy</param>
    /// <param name="roomId">Unique ID of the room</param>
    private void HandleRoomDespawning(BaseRoom room, Guid roomId)
    {
        // Remove the room from overall dictionary
        roomDictionary.Remove(roomId);

        Dictionary <Vector2, RoomDoor>  roomDoors        = room.roomDoors;
        Dictionary <RoomDoor, BaseRoom> neighboringRooms = new Dictionary <RoomDoor, BaseRoom>();

        // Enumerate through the room's doors
        foreach (KeyValuePair <Vector2, RoomDoor> kvp in roomDoors)
        {
            RoomDoor door = kvp.Value;

            Vector2 doorExit = room.GetDoorExit(door);

            // Skip doors that don't have another room connected
            if (!roomLocations.ContainsKey(doorExit))
            {
                continue;
            }

            BaseRoom exitRoom = roomLocations[doorExit];

            neighboringRooms.Add(door, exitRoom);
        }

        // Enumerate through all doors
        foreach (KeyValuePair <RoomDoor, BaseRoom> kvp in neighboringRooms)
        {
            BaseRoom neighboringRoom = kvp.Value;

            if (neighboringRoom == null)
            {
                continue;
            }

            RoomDoor currentDoor = kvp.Key;

            Vector2 setLocation = currentDoor.GetLocation();

            // Change connected door's parent to their other connected room so they don't get destroyed
            currentDoor.transform.SetParent(neighboringRoom.transform);
        }

        // Loop through all the tile locations for the room to be destroyed
        for (int i = 0; i < room.roomLocations.Count; ++i)
        {
            Vector2 location = room.roomLocations[i];
            if (roomLocations.ContainsKey(location))
            {
                // Remove the tile location from the overall used locations
                roomLocations.Remove(location);
            }
        }

        // Finally destroy the room object
        Destroy(room.gameObject);
    }