Skip to content

Commit

Permalink
Fixed an issue where exits couldn't be located as interaction targets
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_pollard committed Dec 6, 2024
1 parent be8b65d commit 94a70a0
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 80 deletions.
2 changes: 1 addition & 1 deletion NetAF.Examples/Assets/Regions/Flat/Flat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Region Instantiate()

if (Guitar.Name.EqualsIdentifier(item.Identifier))
{
if (spareBedroom.ContainsItem(Lead.Name))
if (spareBedroom.FindItem(Lead.Name, out _))
{
easternHallway[Direction.East].Unlock();

Expand Down
4 changes: 2 additions & 2 deletions NetAF.Examples/Assets/Regions/Flat/Rooms/Lounge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public Room Instantiate()

ConditionalDescription description = new("You're in a large sitting room. Theres a huge map hanging on the eastern wall. On the southern wall there is a canvas. Theres a large coffee table in the center of the room. Beth is sat on a green sofa watching the TV. There is what appears to be a lead of some sort poking out from underneath the sofa. The kitchen is to the north.",
"You're in a large sitting room. Theres a huge map hanging on the eastern wall. On the southern wall there is a canvas. Theres a large coffee table in the center of the room. Beth is sat on a green sofa watching the TV. The kitchen is to the north.",
() => room.ContainsItem(Lead.Name));
() => room.FindItem(Lead.Name, out _));

room = new(new Identifier(Name), description, [new Exit(Direction.North)], interaction: item =>
{
if (item != null)
{
if (CoffeeMug.Name.EqualsIdentifier(item.Identifier))
{
if (room.ContainsCharacter(Beth.Name))
if (room.FindCharacter(Beth.Name, out _))
return new(InteractionResult.ItemExpires, item, "Beth takes the cup of coffee and smiles. Brownie points to you!");

return new(InteractionResult.NoChange, item, "As no one is about you decide to drink the coffee yourself. Your nose wasn't lying, it is bitter but delicious.");
Expand Down
2 changes: 1 addition & 1 deletion NetAF.Examples/Assets/Regions/Flat/Rooms/Roof.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Room Instantiate()
Room room = null;
ConditionalDescription description = new("The roof is small and gravely, and it hurts your shoe-less feet to stand on it. There is a large skylight in the center of the roof, and a coffee mug sits to the side, indicating someone has been here recently. The window behind you south leads back into the bathroom.",
"The roof is small and gravely, and it hurts your shoe-less feet to stand on it. There is a large skylight in the center of the roof. The window behind you south leads back into the bathroom.",
() => room.ContainsItem(CoffeeMug.Name));
() => room.FindItem(CoffeeMug.Name, out _));

room = new(new Identifier(Name), description, [new Exit(Direction.South)]);

Expand Down
2 changes: 1 addition & 1 deletion NetAF.Examples/Assets/Regions/Flat/Rooms/SpareBedroom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Room Instantiate()

ConditionalDescription description = new("You are in a very tidy room. The eastern wall is painted in a dark red colour. Against the south wall is a line of guitar amplifiers, all turned on. A very tidy blue guitar rests against the amps just begging to be played. There is a Gamecube against the northern wall. A doorway to the north leads back to the Western Hallway.",
"You are in a very tidy room. The eastern wall is painted in a dark red colour. Against the south wall is a line of guitar amplifiers, all turned on. There is a Gamecube against the northern wall. A doorway to the north leads back to the Western Hallway.",
() => room.ContainsItem(Guitar.Name));
() => room.FindItem(Guitar.Name, out _));

room = new(new Identifier(Name), description, [new Exit(Direction.North)], interaction: Interaction);

Expand Down
2 changes: 1 addition & 1 deletion NetAF.Examples/Assets/Regions/Zelda/Rooms/Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Room Instantiate()
Room room = null;
ConditionalDescription description = new ("A small stream flows east to west in front of you. The water is clear, and looks good enough to drink. On the bank is a small bush. To the south is the Kokiri forest",
"A small stream flows east to west in front of you. The water is clear, and looks good enough to drink. On the bank is a stump where the bush was. To the south is the Kokiri forest.",
() => room.ContainsItem(Bush.Name));
() => room.FindItem(Bush.Name, out _));

room = new(new Identifier(Name), description, [new Exit(Direction.South)]);

Expand Down
24 changes: 0 additions & 24 deletions NetAF.Tests/Assets/Locations/Room_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,6 @@ public void GivenHasItem_WhenContainsItem_ThenTrue()
Assert.IsTrue(result);
}

[TestMethod]
public void GivenHasItem_WhenContainsItemByName_ThenTrue()
{
var room = new Room(string.Empty, string.Empty);
var item = new Item("A", string.Empty);
room.AddItem(item);

var result = room.ContainsItem("A");

Assert.IsTrue(result);
}

[TestMethod]
public void GivenDoesNotHaveCharacter_WhenContainsCharacter_ThenFalse()
{
Expand All @@ -151,18 +139,6 @@ public void GivenHasCharacter_WhenContainsCharacter_ThenTrue()
Assert.IsTrue(result);
}

[TestMethod]
public void GivenHasCharacter_WhenContainsCharacterByName_ThenTrue()
{
var room = new Room(string.Empty, string.Empty);
var character = new NonPlayableCharacter("A", string.Empty);
room.AddCharacter(character);

var result = room.ContainsCharacter("A");

Assert.IsTrue(result);
}

[TestMethod]
public void GivenValidCharacter_WhenRemoveInteractionTarget_ThenCharacterRemoved()
{
Expand Down
74 changes: 30 additions & 44 deletions NetAF/Assets/Locations/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NetAF.Assets.Characters;
using NetAF.Commands;
using NetAF.Extensions;
using NetAF.Interpretation;
using NetAF.Serialization;
using NetAF.Serialization.Assets;
using NetAF.Utilities;
Expand Down Expand Up @@ -275,35 +276,16 @@ public bool FindExit(Direction direction, bool includeInvisibleExits, out Exit e
return false;
}

/// <summary>
/// Get if this Room contains an item. This will not include items whose ExaminableObject.IsPlayerVisible property is set to false.
/// </summary>
/// <param name="item">The item to check for.</param>
/// <returns>True if the item is in this room, else false.</returns>
public bool ContainsItem(Item item)
{
return Items.Contains(item);
}

/// <summary>
/// Get if this Room contains an item.
/// </summary>
/// <param name="itemName">The item name to check for.</param>
/// <param name="includeInvisibleItems">Specify if invisible items should be included.</param>
/// <param name="item">The item to check for.</param>
/// <param name="includeInvisibleItems">Specify if invisible exits should be included.</param>
/// <returns>True if the item is in this room, else false.</returns>
public bool ContainsItem(string itemName, bool includeInvisibleItems = false)
public bool ContainsItem(Item item, bool includeInvisibleItems = false)
{
return Array.Exists(Items, item => itemName.EqualsExaminable(item) && (includeInvisibleItems || item.IsPlayerVisible));
}

/// <summary>
/// Get if this Room contains an interaction target.
/// </summary>
/// <param name="targetName">The name of the target to check for.</param>
/// <returns>True if the target is in this room, else false.</returns>
public bool ContainsInteractionTarget(string targetName)
{
return Array.Exists(Items, i => targetName.EqualsExaminable(i) || Array.Exists(Characters, targetName.EqualsExaminable));

return Array.Exists(Items, i => i == item && (includeInvisibleItems || i.IsPlayerVisible));
}

/// <summary>
Expand Down Expand Up @@ -347,25 +329,40 @@ public bool FindItem(string itemName, out Item item, bool includeInvisibleItems)
public bool FindInteractionTarget(string targetName, out IInteractWithItem target)
{
var items = Items.Where(targetName.EqualsExaminable).ToArray();
var nPCS = Characters.Where(targetName.EqualsExaminable).ToArray();
var exits = Exits.Where(targetName.EqualsExaminable).ToArray();
List<IInteractWithItem> interactions = [];

if (items.Length > 0)
interactions.AddRange(items);
{
target = items[0];
return true;
}

var nPCS = Characters.Where(targetName.EqualsExaminable).ToArray();

if (nPCS.Length > 0)
interactions.AddRange(nPCS);
{
target = nPCS[0];
return true;
}

if (exits.Length > 0)
interactions.AddRange(exits);
var exits = Exits.Where(targetName.EqualsExaminable).ToArray();

if (interactions.Count > 0)
if (exits.Length > 0)
{
target = interactions[0];
target = exits[0];
return true;
}

if (SceneCommandInterpreter.TryParseToDirection(targetName, out var direction))
{
exits = Exits.Where(x => x.Direction == direction).ToArray();

if (exits.Length > 0)
{
target = exits[0];
return true;
}
}

target = null;
return false;
}
Expand All @@ -381,17 +378,6 @@ public bool ContainsCharacter(NonPlayableCharacter character, bool includeInvisi
return Characters.Contains(character) && (includeInvisibleCharacters || character.IsPlayerVisible);
}

/// <summary>
/// Get if this Room contains a character.
/// </summary>
/// <param name="characterName">The character name to check for.</param>
/// <param name="includeInvisibleCharacters">Specify if invisible characters should be included.</param>
/// <returns>True if the item is in this room, else false.</returns>
public bool ContainsCharacter(string characterName, bool includeInvisibleCharacters = false)
{
return Array.Exists(Characters, character => characterName.EqualsExaminable(character) && (includeInvisibleCharacters || character.IsPlayerVisible));
}

/// <summary>
/// Find a character. This will not include characters whose ExaminableObject.IsPlayerVisible property is set to false.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Commands/Scene/UseOn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static void ItemExpires(Game game, Item item, IInteractWithItem target)
/// <param name="target">The target that expired.</param>
private static void TargetExpires(Game game, IInteractWithItem target)
{
if (target is IExaminable examinable && game.Overworld.CurrentRegion.CurrentRoom.ContainsInteractionTarget(examinable.Identifier.Name))
if (target is IExaminable examinable && game.Overworld.CurrentRegion.CurrentRoom.FindInteractionTarget(examinable.Identifier.Name, out _))
game.Overworld.CurrentRegion.CurrentRoom.RemoveInteractionTarget(target);

if (target is Item item)
Expand Down
3 changes: 1 addition & 2 deletions NetAF/Interpretation/SceneCommandInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private static bool TryParseUseOnCommand(string text, Game game, out ICommand co
/// <param name="text">The string to parse.</param>
/// <param name="direction">The direction.</param>
/// <returns>The result of the parse.</returns>
private static bool TryParseToDirection(string text, out Direction direction)
public static bool TryParseToDirection(string text, out Direction direction)
{
if (Move.NorthCommandHelp.Equals(text))
{
Expand Down Expand Up @@ -399,7 +399,6 @@ private static bool TryParseToDirection(string text, out Direction direction)
return false;
}


#endregion

#region Implementation of IInterpreter
Expand Down
3 changes: 0 additions & 3 deletions NetAF/Logic/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,6 @@ public IInteractWithItem FindInteractionTarget(string name)
if (name.EqualsExaminable(Overworld.CurrentRegion.CurrentRoom))
return Overworld.CurrentRegion.CurrentRoom;

if (!Overworld.CurrentRegion.CurrentRoom.ContainsInteractionTarget(name))
return null;

Overworld.CurrentRegion.CurrentRoom.FindInteractionTarget(name, out var target);
return target;
}
Expand Down

0 comments on commit 94a70a0

Please sign in to comment.