Skip to content

Commit

Permalink
Merge pull request #100 from benpollarduk/optimisations
Browse files Browse the repository at this point in the history
Optimisations
  • Loading branch information
benpollarduk authored Nov 22, 2024
2 parents 18692d7 + cac7dd3 commit 850d88d
Show file tree
Hide file tree
Showing 23 changed files with 394 additions and 74 deletions.
5 changes: 2 additions & 3 deletions NetAF.Examples/Assets/Regions/Everglades/Rooms/InnerCave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public Room Instantiate()
{
Room room = null;

room = new Room(Name, string.Empty, [new Exit(Direction.West), new Exit(Direction.North, true)], interaction: item =>
var description = new ConditionalDescription("With the bats gone there is daylight to the north. To the west is the cave entrance", "As you enter the inner cave the screeching gets louder, and in the gloom you can make out what looks like a million sets of eyes looking back at you. Bats! You can just make out a few rays of light coming from the north, but the bats are blocking your way.", () => !room[Direction.North].IsLocked);
room = new Room(new(Name), description, [new Exit(Direction.West), new Exit(Direction.North, true)], interaction: item =>
{
if (item != null && ConchShell.Name.EqualsExaminable(item))
{
Expand All @@ -39,8 +40,6 @@ public Room Instantiate()
return new(InteractionResult.NoChange, item);
});

room.SpecifyConditionalDescription(new ConditionalDescription("With the bats gone there is daylight to the north. To the west is the cave entrance", "As you enter the inner cave the screeching gets louder, and in the gloom you can make out what looks like a million sets of eyes looking back at you. Bats! You can just make out a few rays of light coming from the north, but the bats are blocking your way.", () => !room[Direction.North].IsLocked));

return room;

}
Expand Down
10 changes: 10 additions & 0 deletions NetAF.Tests/Assets/ConditionalDescription_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ namespace NetAF.Tests.Assets
[TestClass]
public class ConditionalDescription_Tests
{
[TestMethod]
public void GivenGetDescription_WhenNull_ThenReturnTrueDescription()
{
var conditional = new ConditionalDescription("A", "B", null);

var result = conditional.GetDescription();

Assert.AreEqual("A", result);
}

[TestMethod]
public void GivenGetDescription_WhenTrue_ThenReturnTrueDescription()
{
Expand Down
45 changes: 45 additions & 0 deletions NetAF.Tests/Assets/Locations/Matrix_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,50 @@ public void Given4Rooms_WhenToRooms_Then4Rooms()

Assert.AreEqual(4, result.Length);
}

[TestMethod]
public void Given2RoomsOnZ2_WhenFindAllRoomsOnZ_Then2Rooms()
{
List<RoomPosition> roomPositions =
[
new(new(string.Empty, string.Empty), new Point3D(0, 0, 0)),
new(new(string.Empty, string.Empty), new Point3D(0, 1, 0)),
new(new(string.Empty, string.Empty), new Point3D(0, 1, 1)),
new(new(string.Empty, string.Empty), new Point3D(0, 1, 2))
];
var matrix = new Matrix([.. roomPositions]);

var result = matrix.FindAllRoomsOnZ(0);

Assert.AreEqual(2, result.Length);
}

[TestMethod]
public void Given1RoomOnZ1_WhenFindAllRoomsOnZ_Then1Room()
{
List<RoomPosition> roomPositions =
[
new(new(string.Empty, string.Empty), new Point3D(0, 0, 0)),
new(new(string.Empty, string.Empty), new Point3D(0, 1, 0)),
new(new(string.Empty, string.Empty), new Point3D(0, 1, 1)),
new(new(string.Empty, string.Empty), new Point3D(0, 1, 2))
];
var matrix = new Matrix([.. roomPositions]);

var result = matrix.FindAllRoomsOnZ(1);

Assert.AreEqual(1, result.Length);
}

[TestMethod]
public void Given2Point1UnitApart_WhenDistanceBetweenPoints_Then1()
{
Point3D a = new(0, 0, 0);
Point3D b = new(1, 0, 0);

var result = Matrix.DistanceBetweenPoints(a, b);

Assert.AreEqual(1, (int)result);
}
}
}
2 changes: 1 addition & 1 deletion NetAF.Tests/Assets/Locations/Room_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void GivenNotBeenVisited_WhenGetHasBeenVisited_ThenFalse()
public void GivenVisited_WhenGetHasBeenVisited_ThenTrue()
{
var room = new Room(string.Empty, string.Empty);
room.MovedInto(null);
room.MovedInto();

Assert.IsTrue(room.HasBeenVisited);
}
Expand Down
43 changes: 39 additions & 4 deletions NetAF.Tests/Commands/Scene/Move_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class Move_Tests
public void GivenCantMove_WhenInvoke_ThenError()
{
var region = new Region(Identifier.Empty, Description.Empty);
region.AddRoom(new Room(Identifier.Empty, Description.Empty, [new Exit(Direction.North)]), 0, 0, 0);
region.AddRoom(new Room(Identifier.Empty, Description.Empty, [new Exit(Direction.South)]), 0, 1, 0);
region.AddRoom(new Room(new("Origin"), Description.Empty, [new Exit(Direction.North)]), 0, 0, 0);
region.AddRoom(new Room(new("Target"), Description.Empty, [new Exit(Direction.South)]), 0, 1, 0);
var overworld = new Overworld(string.Empty, string.Empty);
overworld.AddRegion(region);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, null), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
Expand All @@ -30,8 +30,8 @@ public void GivenCantMove_WhenInvoke_ThenError()
public void GivenCanMove_WhenInvoke_ThenSilent()
{
var region = new Region(Identifier.Empty, Description.Empty);
region.AddRoom(new Room(Identifier.Empty, Description.Empty, [new Exit(Direction.North)]), 0, 0, 0);
region.AddRoom(new Room(Identifier.Empty, Description.Empty, [new Exit(Direction.South)]), 0, 1, 0);
region.AddRoom(new Room(new("Origin"), Description.Empty, [new Exit(Direction.North)]), 0, 0, 0);
region.AddRoom(new Room(new("Target"), Description.Empty, [new Exit(Direction.South)]), 0, 1, 0);
var overworld = new Overworld(string.Empty, string.Empty);
overworld.AddRegion(region);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, null), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
Expand All @@ -41,5 +41,40 @@ public void GivenCanMove_WhenInvoke_ThenSilent()

Assert.AreEqual(ReactionResult.Silent, result.Result);
}

[TestMethod]
public void GivenCanMoveToPreviouslyUnvisitedRoomWithIntroduction_WhenInvoke_ThenInform()
{
var region = new Region(Identifier.Empty, Description.Empty);
region.AddRoom(new Room(new("Origin"), Description.Empty, [new Exit(Direction.North)]), 0, 0, 0);
region.AddRoom(new Room(new("Target"), Description.Empty, new("ABC"), [new Exit(Direction.South)]), 0, 1, 0);
var overworld = new Overworld(string.Empty, string.Empty);
overworld.AddRegion(region);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, null), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
var command = new Move(Direction.North);

var result = command.Invoke(game);

Assert.AreEqual(ReactionResult.Inform, result.Result);
}

[TestMethod]
public void GivenCanMoveToPreviouslyVisitedRoomWithIntroduction_WhenInvoke_ThenSilent()
{
var region = new Region(Identifier.Empty, Description.Empty);
region.AddRoom(new Room(new("Origin"), Description.Empty, [new Exit(Direction.North)]), 0, 0, 0);
region.AddRoom(new Room(new("Target"), Description.Empty, new("ABC"), [new Exit(Direction.South)]), 0, 1, 0);
var overworld = new Overworld(string.Empty, string.Empty);
overworld.AddRegion(region);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworld, null), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
new Move(Direction.North).Invoke(game);
new Move(Direction.South).Invoke(game);

var command = new Move(Direction.North);

var result = command.Invoke(game);

Assert.AreEqual(ReactionResult.Silent, result.Result);
}
}
}
69 changes: 69 additions & 0 deletions NetAF.Tests/Logic/Modes/RegionMapMode_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,74 @@ public void GivenPositionInBoundsButNotVisitedButRegionIsVisibleWithoutDiscovery

Assert.IsTrue(result);
}

[TestMethod]
public void GivenAnOffsetZ_WhenPanToPosition_ThenReturnTrue()
{
RegionMaker regionMaker = new(string.Empty, string.Empty);
Room room = new(string.Empty, string.Empty);
Room room2 = new(string.Empty, string.Empty);
regionMaker[0, 0, 0] = room;
regionMaker[1, 0, 1] = room2;
var region = regionMaker.Make();
region.JumpToRoom(new(1, 0, 1));
region.JumpToRoom(new(0, 0, 0));

var result = RegionMapMode.CanPanToPosition(region, new Point3D(0, 0, 1));

Assert.IsTrue(result);
}

[TestMethod]
public void GivenAnOffsetZPositionButNotVisited_WhenPanToPosition_ThenReturnFalse()
{
RegionMaker regionMaker = new(string.Empty, string.Empty);
Room room = new(string.Empty, string.Empty);
Room room2 = new(string.Empty, string.Empty);
regionMaker[0, 0, 0] = room;
regionMaker[1, 0, 1] = room2;
var region = regionMaker.Make();

var result = RegionMapMode.CanPanToPosition(region, new Point3D(0, 0, 1));

Assert.IsFalse(result);
}

[TestMethod]
public void GivenAnOffsetZPositionButNotVisitedButRegionIsVisibleWithoutDiscoveryIsTrue_WhenPanToPosition_ThenReturnTrue()
{
RegionMaker regionMaker = new(string.Empty, string.Empty);
Room room = new(string.Empty, string.Empty);
Room room2 = new(string.Empty, string.Empty);
regionMaker[0, 0, 0] = room;
regionMaker[1, 0, 1] = room2;
var region = regionMaker.Make();
region.IsVisibleWithoutDiscovery = true;

var result = RegionMapMode.CanPanToPosition(region, new Point3D(0, 0, 1));

Assert.IsTrue(result);
}

[TestMethod]
public void GivenAnOffsetZPosition_WhenRender_ThenNoException()
{
Assertions.NoExceptionThrown(() =>
{
RegionMaker regionMaker = new(string.Empty, string.Empty);
Room room = new(string.Empty, string.Empty);
Room room2 = new(string.Empty, string.Empty);
regionMaker[0, 0, 0] = room;
regionMaker[1, 0, 1] = room2;
OverworldMaker overworldMaker = new(string.Empty, string.Empty, regionMaker);
var game = Game.Create(new(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworldMaker.Make(), new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
var mode = new RegionMapMode(new Point3D(0, 0, 1));

game.Overworld.CurrentRegion.JumpToRoom(new(1, 0, 1));
game.Overworld.CurrentRegion.JumpToRoom(new(0, 0, 0));

mode.Render(game);
});
}
}
}
2 changes: 1 addition & 1 deletion NetAF.Tests/Persistence/RestorePoint_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void GivenSimpleGameWithCustomCommand_WhenFromJsonFromNewlyCreatedJson_Th
{
var command = new CustomCommand(new CommandHelp("TEST COMMAND", string.Empty), true, true, null);
var regionMaker = new RegionMaker(string.Empty, string.Empty);
var room = new Room(string.Empty, string.Empty, null, commands: [command]);
var room = new Room(string.Empty, string.Empty, exits: null, commands: [command]);
regionMaker[0, 0, 0] = room;
var overworldMaker = new OverworldMaker(string.Empty, string.Empty, regionMaker);
var game = Game.Create(new GameInfo(string.Empty, string.Empty, string.Empty), string.Empty, AssetGenerator.Retained(overworldMaker.Make(), new PlayableCharacter(string.Empty, string.Empty)), GameEndConditions.NoEnd, TestGameConfiguration.Default).Invoke();
Expand Down
4 changes: 2 additions & 2 deletions NetAF/Assets/Characters/NonPlayableCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class NonPlayableCharacter : Character, IConverser, IRestoreFromOb
/// <param name="commands">This objects commands.</param>
/// <param name="interaction">The interaction.</param>
/// <param name="examination">The examination.</param>
public NonPlayableCharacter(Identifier identifier, Description description, Conversation conversation = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null)
public NonPlayableCharacter(Identifier identifier, IDescription description, Conversation conversation = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null)
{
Identifier = identifier;
Description = description;
Expand All @@ -56,7 +56,7 @@ public NonPlayableCharacter(Identifier identifier, Description description, Conv
/// <param name="commands">This objects commands.</param>
/// <param name="interaction">The interaction.</param>
/// <param name="examination">The examination.</param>
public NonPlayableCharacter(Identifier identifier, Description description, bool isAlive, Conversation conversation = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null) : this(identifier, description, conversation, commands, interaction, examination)
public NonPlayableCharacter(Identifier identifier, IDescription description, bool isAlive, Conversation conversation = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null) : this(identifier, description, conversation, commands, interaction, examination)
{
IsAlive = isAlive;
Interaction = interaction;
Expand Down
4 changes: 2 additions & 2 deletions NetAF/Assets/Characters/PlayableCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public sealed class PlayableCharacter : Character
/// <param name="commands">This objects commands.</param>
/// <param name="interaction">The interaction.</param>
/// <param name="examination">The examination.</param>
public PlayableCharacter(Identifier identifier, Description description, Item[] items = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null) : this(identifier, description, true, items, commands, interaction, examination)
public PlayableCharacter(Identifier identifier, IDescription description, Item[] items = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null) : this(identifier, description, true, items, commands, interaction, examination)
{
}

Expand Down Expand Up @@ -68,7 +68,7 @@ public PlayableCharacter(Identifier identifier, Description description, Item[]
/// <param name="commands">This objects commands.</param>
/// <param name="interaction">The interaction.</param>
/// <param name="examination">The examination.</param>
public PlayableCharacter(Identifier identifier, Description description, bool canConverse, Item[] items = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null)
public PlayableCharacter(Identifier identifier, IDescription description, bool canConverse, Item[] items = null, CustomCommand[] commands = null, InteractionCallback interaction = null, ExaminationCallback examination = null)
{
Identifier = identifier;
Description = description;
Expand Down
30 changes: 8 additions & 22 deletions NetAF/Assets/ConditionalDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,23 @@
/// <summary>
/// Represents a conditional description of an object.
/// </summary>
/// <param name="trueDescription">The true description.</param>
/// <param name="falseDescription">The false description.</param>
/// <param name="trueDescription">The description of this object when the condition returns true.</param>
/// <param name="falseDescription">The description of this object when the condition returns false.</param>
/// <param name="condition">The condition.</param>
public sealed class ConditionalDescription(string trueDescription, string falseDescription, Condition condition) : Description(trueDescription)
public sealed class ConditionalDescription(string trueDescription, string falseDescription, Condition condition) : IDescription
{
#region Properties

/// <summary>
/// Get or set the description for when this condition is false
/// </summary>
private readonly string falseDescription = falseDescription;

/// <summary>
/// Get or set the condition
/// </summary>
public Condition Condition { get; set; } = condition;

#endregion

#region Overrides of Description
#region Implementation of IDescription

/// <summary>
/// Get the description.
/// </summary>
/// <returns>The description.</returns>
public override string GetDescription()
public string GetDescription()
{
if (Condition != null)
return Condition.Invoke() ? DefaultDescription : falseDescription;
if (condition != null)
return condition.Invoke() ? trueDescription : falseDescription;

return DefaultDescription;
return trueDescription;
}

#endregion
Expand Down
19 changes: 5 additions & 14 deletions NetAF/Assets/Description.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/// <summary>
/// Represents a description of an object.
/// </summary>
/// <param name="description">The description</param>
public class Description(string description)
/// <param name="description">The description of this object.</param>
public sealed class Description(string description) : IDescription
{
#region StaticProperties

Expand All @@ -15,24 +15,15 @@ public class Description(string description)

#endregion

#region Properties

/// <summary>
/// Get or set the description.
/// </summary>
protected string DefaultDescription { get; set; } = description;

#endregion

#region Methods
#region Implementation of IDescription

/// <summary>
/// Get the description.
/// </summary>
/// <returns>The description.</returns>
public virtual string GetDescription()
public string GetDescription()
{
return DefaultDescription;
return description;
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Assets/ExaminableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class ExaminableObject : IExaminable
/// <summary>
/// Get a description of this object.
/// </summary>
public Description Description { get; protected set; }
public IDescription Description { get; protected set; }

/// <summary>
/// Get this objects commands.
Expand Down
14 changes: 14 additions & 0 deletions NetAF/Assets/IDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace NetAF.Assets
{
/// <summary>
/// Provides a description of an object.
/// </summary>
public interface IDescription
{
/// <summary>
/// Get the description.
/// </summary>
/// <returns>The description.</returns>
string GetDescription();
}
}
2 changes: 1 addition & 1 deletion NetAF/Assets/IExaminable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IExaminable : IPlayerVisible, IRestoreFromObjectSerialization<E
/// <summary>
/// Get a description of this object.
/// </summary>
Description Description { get; }
IDescription Description { get; }
/// <summary>
/// Get this objects commands.
/// </summary>
Expand Down
Loading

0 comments on commit 850d88d

Please sign in to comment.