public WearingWithConflictingItemException(Thing item1, Thing item2) : base("You can't wear both " + item1.GetSpecificName() + " and " + item2.GetSpecificName() + " at the same time. You'll have to " + "take off " + item1.GetSpecificName() + " in order to put on " + item2.GetSpecificName() + '.') { }
/// <summary> /// Checks if a given <see cref="Actor"/> can see a given /// <see cref="Thing"/> from their current location(s). /// </summary> /// <param name="actor">the <see cref="Actor"/> trying to interact with something</param> /// <param name="item">the <see cref="Thing"/> being interacted with</param> /// <returns>true if the <see cref="Actor"/> can see the <see cref="Thing"/>; false otherwise</returns> public static bool CanSee(Actor actor, Thing item) { // if either the item's location or the actor's location is null, // return false if (item.GetLocation() == null || actor.GetLocation() == null) { return false; } // if there are no opaque barriers between the item and the actor, // return true else if (GetTopMostVisibleContainer(actor.GetLocation()) == GetTopMostVisibleContainer(item.GetLocation())) { return true; } // if the item is held in someone's hands, else if (typeof(Hands) == GetTopMostVisibleContainer(item.GetLocation()).GetType()) { // do an explicit cast for the sake of legibility Hands hands = (Hands)GetTopMostVisibleContainer( item.GetLocation()); // if the hands belongs to the actor, return true if (hands.GetOwner() == actor) { return true; } // if the actor can see the owner of the hands, return true else if (GetTopMostVisibleContainer(actor.GetLocation()) == GetTopMostVisibleContainer( hands.GetOwner().GetLocation())){ return true; } // otherwise, the hands cannot be seen, // so the item cannot be seen either return false; } // end case "item is in someone's hands" // if the item is a piece of clothing currently being worn // or is inside a piece of clothing currently being worn, else if (typeof(Clothes) == GetTopMostVisibleContainer(item.GetLocation()).GetType()) { // if the person wearing the clothing is the actor, return true if (((Clothes)GetTopMostVisibleContainer(item.GetLocation())).GetOwner() == actor) { return true; } // if the item is worn by someone else and it is visible, return true else if (((Clothes)item.GetLocation()).GetOwner().GetVisibleRecursiveContents().Contains(item)){ return true; } // if it's not visible, return false else { return false; } } // end case "item is being worn" // otherwise, return false return false; }
/// <summary> /// Checks if a given <see cref="Actor"/> can see a given /// <see cref="Thing"/> from their current location(s). /// </summary> /// <param name="actor">the <see cref="Actor"/> trying to interact with something</param> /// <param name="item">the <see cref="Thing"/> being interacted with</param> /// <returns>true if the <see cref="Actor"/> can see the <see cref="Thing"/>; false otherwise</returns> public static bool CanSee(Actor actor, Thing item) { // if either the item's location or the actor's location is null, return false if (item.GetLocation() == null || actor.GetLocation() == null) { return false; } // if there are no opaque barriers between the item and the actor, return true else if (GetTopMostVisibleContainer(actor.GetLocation()) == GetTopMostVisibleContainer(item.GetLocation())) { return true; } // if the item is in an inventory, else if (typeof(Inventory) == item.GetLocation().GetType()) { // if the inventory belongs to the actor, return true if (((Inventory)item.GetLocation()).GetOwner() == actor) { return true; } else { // theft is not coded yet throw new Exception("Theft has not been coded for yet."); } } // end case "item is in an inventory" // if the item is a piece of clothing currently being worn // or is inside a piece of clothing currently being worn, else if (typeof(Clothes) == GetTopMostVisibleContainer(item.GetLocation()).GetType()) { // if the person wearing the clothing is the actor, return true if (((Clothes)GetTopMostVisibleContainer(item.GetLocation())).GetOwner() == actor) { return true; } // if the item is worn by someone else and it is visible, return true else if (((Clothes)item.GetLocation()).GetOwner().GetVisibleRecursiveContents().Contains(item)){ return true; } // if it's not visible, return false else { return false; } } // end case "item is being worn" // otherwise, return false return false; }
/// <summary> /// Prints a generic message reporting a simple action with two parameters. /// </summary> /// <param name="actor">the <see cref="Actor"/> performing the action</param> /// <param name="verb">the action being performed</param> /// <param name="item1">the first <see cref="Thing"/> the action is performed on</param> /// <param name="separator">word or phrase separating the two parameters</param> /// <param name="item2">the second <see cref="Thing"/> the action is performed on</param> public static void ReportIfVisible(Actor actor, VerbSet verb, Thing item1, string separator, Thing item2) { ReportIfVisible(actor, StringManipulator.CapitalizeFirstLetter(actor.GetSpecificName()) + ' ' + actor.GetConjugatedVerb(verb) + ' ' + item1.GetSpecificName() + separator + item2.GetSpecificName() + '.'); }
/// <summary> /// This function checks to see if the <see cref="player"/> can see the /// <see cref="Thing"/> trying to print a message. If the /// <see cref="player"/> can see the <see cref="Thing"/>, the message /// is printed. If not, nothing happens. /// </summary> /// <param name="item">the <see cref="Thing"/> that wants to print a message</param> /// <param name="message">the message to be printed</param> public static void ReportIfVisible(Thing item, string message) { if (IsVisible(item)) { writeFormatted(message); } }
/// <summary> /// Moves a <see cref="Thing"/> into a given <see cref="Container"/>, /// regardless of where the <see cref="Thing"/> is before the move. /// Can be used to bring a <see cref="Thing"/> into play. /// Therefore, the <see cref="Thing"/>'s location may be null. /// This function checks for that possibility and handles it. /// </summary> /// <param name="item">the <see cref="Thing"/> to move</param> /// <param name="place">the <see cref="Container"/> to put it in</param> public static void Move(Thing item, Container place) { if (item.GetLocation() != null) { item.GetLocation().RemoveThing(item); } item.SetLocation(place); place.AddThing(item); }
/// <summary> /// Determines whether or not the given <see cref="Thing"/> is visible /// to the <see cref="player"/>. /// </summary> /// <param name="item">the <see cref="Thing"/> to check if visible</param> /// <returns>true if the <see cref="Thing"/> is visible to the <see cref="player"/>; false otherwise</returns> public static bool IsVisible(Thing item) { return GetTopMostVisibleContainer(item.GetLocation()) == GetTopMostVisibleContainer(player.GetLocation()); }
//===================================================================// // Booleans // //===================================================================// /// <summary> /// Checks whether a given <see cref="Thing"/> is the <see cref="player"/>. /// </summary> /// <param name="item">the <see cref="Thing"/> to check</param> /// <returns>true if the <see cref="Thing"/> is the <see cref="player"/>; false otherwise</returns> public static bool IsPlayer(Thing item) { return item == player; }
/// <summary> /// Prints a description of the specified <see cref="Thing"/>. /// </summary> /// <param name="item">the <see cref="Thing"/> to examine</param> public static void Examine(Thing item) { writeFormatted(item.GetDescription()); }
/// <summary> /// Adds a <see cref="Thing"/> to the contents of the room. If the /// specified item is already in the room, throws an exception instead. /// </summary> /// <param name="item">the thing to be added to the room</param> public void AddThing(Thing item) { // if the room already contains that item, throw an exception if (this.contents.Contains(item)) { throw new Exception("Error: Attempted to add something to a room that it was already inside of."); } else { // just add the item normally this.contents.Add(item); } }
/// <summary> /// Removes a <see cref="Thing"/> from the contents of the room. If the /// specified item is not in the room, throws an exception instead. /// </summary> /// <param name="item">the thing to be removed from the room</param> public void RemoveThing(Thing item) { // if the room does not contain the item, throw an exception if (!this.contents.Remove(item)) { throw new Exception("Error: Attempted to remove something from a room that did not contain it."); } }
/// <summary> /// Determines whether the room contains a specified item. /// </summary> /// <param name="item">the item to check for</param> /// <returns>true if the room contains the item; false if it does not</returns> public bool Contains(Thing item) { return this.contents.Contains(item); }
/// <summary> /// Gets a name for the <see cref="Thing"/> that does not match any of /// the names for the things passed as a parameter. /// </summary> /// <param name="itemSet">the othere things to check against</param> /// <returns> /// a unique name for this thing, if one exists, /// or null if it has no unique name /// </returns> public string GetUniqueName(Thing[] itemSet) { HashSet<string> nameSet = new HashSet<string>(); foreach (Thing item in itemSet) { nameSet.UnionWith(item.parsedNames); } foreach (string name in this.parsedNames) { if (!nameSet.Contains(name)) { return name; } } return null; }
/// <summary> /// The idea behind this function is that if there is no way for /// the player to tell the difference between two things, the parser /// should just randomly pick one when deciding how to parse the command. /// </summary> /// <param name="other"></param> /// <returns></returns> public virtual bool CannotBeDistinguishedFrom(Thing other) { if (this.GetType() != other.GetType()) { return false; } if (this.name != other.name) { return false; } return true; }