/// <summary> /// Adds the occupant to this instance. /// </summary> /// <param name="character">The character.</param> /// <exception cref="System.NullReferenceException">Attempted to add a null character to the Room.</exception> public void MoveOccupantToRoom(ICharacter character, DefaultRoom departingRoom) { // We don't allow the user to enter a disabled room. if (!this.IsEnabled) { // TODO: Need to do some kind of communication back to the caller that this can't be traveled to. throw new InvalidOperationException("The room is disabled and can not be traveled to."); } if (character == null) { throw new NullReferenceException("Attempted to add a null character to the Room."); } // Remove the character from their previous room. //Find the doorway that the character traveled through DefaultDoorway doorwayTraveledThrough = departingRoom?.Doorways.FirstOrDefault(door => door.ArrivalRoom == this); // Get the opposite direction of the doorways travel direction. This will be the direction that they entered from // within this instance's available entry points. ITravelDirection enteredDirection = doorwayTraveledThrough?.DepartureDirection.GetOppositeDirection(); // Handle removing the occupant from the previous room. The character might handle this for us // so our RemoveOccupantFromRoom implementation will try to remove safely. departingRoom?.TryRemoveOccupantFromRoom(character, doorwayTraveledThrough.DepartureDirection, this); this.Occupants.Add(character); character.Deleted += this.OnCharacterDeletingStarting; // Notify our event handles that the character has entered the room. this.OnEnteringRoom(character, enteredDirection, departingRoom); character.Move(enteredDirection, this); }
/// <summary> /// Connects two rooms together. /// </summary> /// <param name="departureRoom">The departure room.</param> /// <param name="arrivalRoom">The arrival room.</param> /// <param name="createDoorwayForArrival">If set to <c>true</c>, a new doorway will be created for the arrival room that is linked back to the originating departure room. /// Otherwise, a one-way door will be created.</param> /// <exception cref="System.NullReferenceException"> /// Neither the departure room or arrival room can be null /// or /// Can not connect rooms when the DepartureDirection is null. /// or /// Can not connect rooms when the doorways collection is null. /// </exception> public virtual void ConnectRooms(DefaultRoom departureRoom, DefaultRoom arrivalRoom, bool createDoorwayForArrival = true) { ExceptionFactory .ThrowIf <ArgumentNullException>( departureRoom == null || arrivalRoom == null, "Neither the departure room or arrival room can be null") .Or <NullReferenceException>( this.DepartureDirection == null, "Can not connect rooms when the DepartureDirection is null.") .Or( departureRoom.Doorways == null, "Can not connect rooms when the doorways collection is null."); // Set up the departure room first. this.ArrivalRoom = arrivalRoom; this.DepartureRoom = departureRoom; departureRoom.Doorways.Add(this); // The opposite rooms arrival needs to be the departing room that this doorway is connected to. if (createDoorwayForArrival) { // Create a new doorway for the arrival room, so that you can leave the room once you are in it. ITravelDirection oppositeDirection = this.DepartureDirection.GetOppositeDirection(); // TODO: Create the doorway from a factory call. DefaultDoorway arrivalDoorway = new DefaultDoorway(oppositeDirection); arrivalDoorway.ConnectRooms(arrivalRoom, departureRoom, false); } }
/// <summary> /// Disconnects the two connected rooms. /// </summary> public virtual void DisconnectRoom() { // If this doorway isn't set up, then there is nothing to disconnect. if (this.DepartureDirection == null || this.ArrivalRoom == null) { return; } // This doorway always belongs to the departing room. We can get the departing room // by walking through the arrival rooms doorways and finding the opposite doorway. string oppositeDirection = this.DepartureDirection.GetOppositeDirection().Direction; DefaultDoorway oppositeDoorway = this.ArrivalRoom.Doorways .FirstOrDefault(d => d.DepartureDirection.Direction == oppositeDirection); ExceptionFactory.ThrowIf <InvalidOperationException>( oppositeDoorway == null, "Opposite direction does not have a connected room."); DefaultRoom departureRoom = oppositeDoorway.ArrivalRoom; // Remove the doorway from the opposite room. oppositeDoorway.ArrivalRoom.Doorways.Remove(oppositeDoorway); // Remove this door from the arrival room this.ArrivalRoom.Doorways.Remove(oppositeDoorway); departureRoom.Doorways.Remove(this); }
/// <summary> /// Connects two rooms together. /// </summary> /// <param name="departureRoom">The departure room.</param> /// <param name="arrivalRoom">The arrival room.</param> /// <param name="createDoorwayForArrival">If set to <c>true</c>, a new doorway will be created for the arrival room that is linked back to the originating departure room. /// Otherwise, a one-way door will be created.</param> /// <exception cref="System.NullReferenceException"> /// Neither the departure room or arrival room can be null /// or /// Can not connect rooms when the DepartureDirection is null. /// or /// Can not connect rooms when the doorways collection is null. /// </exception> public virtual void ConnectRooms(DefaultRoom departureRoom, DefaultRoom arrivalRoom, bool createDoorwayForArrival = true) { ExceptionFactory .ThrowIf<ArgumentNullException>( departureRoom == null || arrivalRoom == null, "Neither the departure room or arrival room can be null") .Or<NullReferenceException>( this.DepartureDirection == null, "Can not connect rooms when the DepartureDirection is null.") .Or( departureRoom.Doorways == null, "Can not connect rooms when the doorways collection is null."); // Set up the departure room first. this.ArrivalRoom = arrivalRoom; this.DepartureRoom = departureRoom; departureRoom.Doorways.Add(this); // The opposite rooms arrival needs to be the departing room that this doorway is connected to. if (createDoorwayForArrival) { // Create a new doorway for the arrival room, so that you can leave the room once you are in it. ITravelDirection oppositeDirection = this.DepartureDirection.GetOppositeDirection(); // TODO: Create the doorway from a factory call. DefaultDoorway arrivalDoorway = new DefaultDoorway(oppositeDirection); arrivalDoorway.ConnectRooms(arrivalRoom, departureRoom, false); } }