Skip to content

Commit

Permalink
Merge branch 'tidy'
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_pollard committed Jan 10, 2025
2 parents 8926a74 + 89e83d7 commit 18b2ae4
Show file tree
Hide file tree
Showing 25 changed files with 92 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NetAF.Rendering.FrameBuilders;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAF.Rendering.Console.FrameBuilders;

namespace NetAF.Tests.Rendering.FrameBuilders
{
[TestClass]
public class FrameBuilderCollection_Tests
{
[TestMethod]
public void GivenNoMatchingFrameBuilder_WhenGetFrameBuilder_ThenNull()
{
var builders = new FrameBuilderCollection();

var result = builders.GetFrameBuilder<IFrameBuilder>();

Assert.IsNull(result);
}

[TestMethod]
public void GivenMatchingFrameBuilder_WhenGetFrameBuilder_ThenCorrectInstance()
{
var builder = new ConsoleSceneFrameBuilder(null, null);
var builders = new FrameBuilderCollection(builder);

var result = builders.GetFrameBuilder<ISceneFrameBuilder>();

Assert.AreEqual(builder, result);
}
}
}
3 changes: 2 additions & 1 deletion NetAF/Logic/Modes/AboutMode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NetAF.Interpretation;
using NetAF.Rendering.FrameBuilders;

namespace NetAF.Logic.Modes
{
Expand All @@ -25,7 +26,7 @@ public sealed class AboutMode : IGameMode
/// <param name="game">The game.</param>
public void Render(Game game)
{
var frame = game.Configuration.FrameBuilders.AboutFrameBuilder.Build("About", game, game.Configuration.DisplaySize);
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<IAboutFrameBuilder>().Build("About", game, game.Configuration.DisplaySize);
game.Configuration.Adapter.RenderFrame(frame);
}

Expand Down
3 changes: 2 additions & 1 deletion NetAF/Logic/Modes/CommandListMode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NetAF.Commands;
using NetAF.Interpretation;
using NetAF.Rendering.FrameBuilders;

namespace NetAF.Logic.Modes
{
Expand Down Expand Up @@ -27,7 +28,7 @@ public sealed class CommandListMode(CommandHelp[] commands) : IGameMode
/// <param name="game">The game.</param>
public void Render(Game game)
{
var frame = game.Configuration.FrameBuilders.CommandListFrameBuilder.Build("Commands", string.Empty, commands, game.Configuration.DisplaySize);
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<ICommandListFrameBuilder>().Build("Commands", string.Empty, commands, game.Configuration.DisplaySize);
game.Configuration.Adapter.RenderFrame(frame);
}

Expand Down
3 changes: 2 additions & 1 deletion NetAF/Logic/Modes/CompletionMode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NetAF.Interpretation;
using NetAF.Rendering.FrameBuilders;

namespace NetAF.Logic.Modes
{
Expand Down Expand Up @@ -27,7 +28,7 @@ public sealed class CompletionMode(string title, string message) : IGameMode
/// <param name="game">The game.</param>
public void Render(Game game)
{
var frame = game.Configuration.FrameBuilders.CompletionFrameBuilder.Build(title, message, game.Configuration.DisplaySize);
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<ICompletionFrameBuilder>().Build(title, message, game.Configuration.DisplaySize);
game.Configuration.Adapter.RenderFrame(frame);
}

Expand Down
3 changes: 2 additions & 1 deletion NetAF/Logic/Modes/ConversationMode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NetAF.Assets.Characters;
using NetAF.Interpretation;
using NetAF.Rendering.FrameBuilders;

namespace NetAF.Logic.Modes
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public ConversationMode(IConverser converser)
/// <param name="game">The game.</param>
public void Render(Game game)
{
var frame = game.Configuration.FrameBuilders.ConversationFrameBuilder.Build($"Conversation with {Converser.Identifier.Name}", Converser, Interpreter.GetContextualCommandHelp(game), game.Configuration.DisplaySize);
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<IConversationFrameBuilder>().Build($"Conversation with {Converser.Identifier.Name}", Converser, Interpreter.GetContextualCommandHelp(game), game.Configuration.DisplaySize);
game.Configuration.Adapter.RenderFrame(frame);
}

Expand Down
3 changes: 2 additions & 1 deletion NetAF/Logic/Modes/GameOverMode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NetAF.Interpretation;
using NetAF.Rendering.FrameBuilders;

namespace NetAF.Logic.Modes
{
Expand Down Expand Up @@ -27,7 +28,7 @@ public sealed class GameOverMode(string title, string message) : IGameMode
/// <param name="game">The game.</param>
public void Render(Game game)
{
var frame = game.Configuration.FrameBuilders.GameOverFrameBuilder.Build(title, message, game.Configuration.DisplaySize);
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<IGameOverFrameBuilder>().Build(title, message, game.Configuration.DisplaySize);
game.Configuration.Adapter.RenderFrame(frame);
}

Expand Down
3 changes: 2 additions & 1 deletion NetAF/Logic/Modes/HelpMode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NetAF.Commands;
using NetAF.Interpretation;
using NetAF.Rendering.FrameBuilders;

namespace NetAF.Logic.Modes
{
Expand Down Expand Up @@ -27,7 +28,7 @@ public sealed class HelpMode(CommandHelp command) : IGameMode
/// <param name="game">The game.</param>
public void Render(Game game)
{
var frame = game.Configuration.FrameBuilders.HelpFrameBuilder.Build("Help", command, game.Configuration.DisplaySize);
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<IHelpFrameBuilder>().Build("Help", command, game.Configuration.DisplaySize);
game.Configuration.Adapter.RenderFrame(frame);
}

Expand Down
3 changes: 2 additions & 1 deletion NetAF/Logic/Modes/ReactionMode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NetAF.Commands;
using NetAF.Interpretation;
using NetAF.Rendering.FrameBuilders;

namespace NetAF.Logic.Modes
{
Expand Down Expand Up @@ -28,7 +29,7 @@ public sealed class ReactionMode(string title, Reaction reaction) : IGameMode
/// <param name="game">The game.</param>
public void Render(Game game)
{
var frame = game.Configuration.FrameBuilders.ReactionModeFrameBuilder.Build(title, reaction.Description, reaction.Result == ReactionResult.Error, game.Configuration.DisplaySize);
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<IReactionFrameBuilder>().Build(title, reaction.Description, reaction.Result == ReactionResult.Error, game.Configuration.DisplaySize);
game.Configuration.Adapter.RenderFrame(frame);
}

Expand Down
3 changes: 2 additions & 1 deletion NetAF/Logic/Modes/RegionMapMode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NetAF.Assets;
using NetAF.Assets.Locations;
using NetAF.Interpretation;
using NetAF.Rendering.FrameBuilders;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -56,7 +57,7 @@ public void Render(Game game)

FocusPosition = GetSnappedLocation(region, FocusPosition);

var frame = game.Configuration.FrameBuilders.RegionMapFrameBuilder.Build(region, FocusPosition, Interpreter?.GetContextualCommandHelp(game) ?? [], game.Configuration.DisplaySize);
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<IRegionMapFrameBuilder>().Build(region, FocusPosition, Interpreter?.GetContextualCommandHelp(game) ?? [], game.Configuration.DisplaySize);
game.Configuration.Adapter.RenderFrame(frame);
}

Expand Down
3 changes: 2 additions & 1 deletion NetAF/Logic/Modes/SceneMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NetAF.Commands;
using NetAF.Interpretation;
using NetAF.Rendering;
using NetAF.Rendering.FrameBuilders;
using System.Collections.Generic;

namespace NetAF.Logic.Modes
Expand Down Expand Up @@ -47,7 +48,7 @@ public void Render(Game game)
commands.AddRange(Interpreter.GetContextualCommandHelp(game));
commands.AddRange(game.Configuration.Interpreter.GetContextualCommandHelp(game));

var frame = game.Configuration.FrameBuilders.SceneFrameBuilder.Build(game.Overworld.CurrentRegion.CurrentRoom, ViewPoint.Create(game.Overworld.CurrentRegion), game.Player, DisplayCommandList ? [.. commands] : null, KeyType, game.Configuration.DisplaySize);
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<ISceneFrameBuilder>().Build(game.Overworld.CurrentRegion.CurrentRoom, ViewPoint.Create(game.Overworld.CurrentRegion), game.Player, DisplayCommandList ? [.. commands] : null, KeyType, game.Configuration.DisplaySize);
game.Configuration.Adapter.RenderFrame(frame);
}

Expand Down
3 changes: 2 additions & 1 deletion NetAF/Logic/Modes/TitleMode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NetAF.Interpretation;
using NetAF.Rendering.FrameBuilders;

namespace NetAF.Logic.Modes
{
Expand All @@ -25,7 +26,7 @@ public sealed class TitleMode : IGameMode
/// <param name="game">The game.</param>
public void Render(Game game)
{
var frame = game.Configuration.FrameBuilders.TitleFrameBuilder.Build(game.Info.Name, game.Introduction, game.Configuration.DisplaySize);
var frame = game.Configuration.FrameBuilders.GetFrameBuilder<ITitleFrameBuilder>().Build(game.Info.Name, game.Introduction, game.Configuration.DisplaySize);
game.Configuration.Adapter.RenderFrame(frame);
}

Expand Down
76 changes: 15 additions & 61 deletions NetAF/Rendering/FrameBuilders/FrameBuilderCollection.cs
Original file line number Diff line number Diff line change
@@ -1,71 +1,25 @@
namespace NetAF.Rendering.FrameBuilders
using System;

namespace NetAF.Rendering.FrameBuilders
{
/// <summary>
/// Provides a collection of all of the frame builders required to run a game.
/// </summary>
/// <param name="titleFrameBuilder">The builder to use for building title frames.</param>
/// <param name="sceneFrameBuilder">The builder to use for building scene frames.</param>
/// <param name="regionMapFrameBuilder">The builder to use for building region map frames.</param>
/// <param name="commandListFrameBuilder">The builder to use for building command list frames.</param>
/// <param name="helpFrameBuilder">The builder to use for building help frames.</param>
/// <param name="completionFrameBuilder">The builder to use for building completion frames.</param>
/// <param name="gameOverFrameBuilder">The builder to use for building game over frames.</param>
/// <param name="aboutFrameBuilder">The builder to use for building about frames.</param>
/// <param name="reactionFrameBuilder">The builder to use for building reaction frames.</param>
/// <param name="conversationFrameBuilder">The builder to use for building conversation frames.</param>
public class FrameBuilderCollection(ITitleFrameBuilder titleFrameBuilder, ISceneFrameBuilder sceneFrameBuilder, IRegionMapFrameBuilder regionMapFrameBuilder, ICommandListFrameBuilder commandListFrameBuilder, IHelpFrameBuilder helpFrameBuilder, ICompletionFrameBuilder completionFrameBuilder, IGameOverFrameBuilder gameOverFrameBuilder, IAboutFrameBuilder aboutFrameBuilder, IReactionFrameBuilder reactionFrameBuilder, IConversationFrameBuilder conversationFrameBuilder)
{
#region Properties

/// <summary>
/// Get the builder to use for title frames.
/// </summary>
public ITitleFrameBuilder TitleFrameBuilder { get; } = titleFrameBuilder;

/// <summary>
/// Get the builder to use for scene frames.
/// </summary>
public ISceneFrameBuilder SceneFrameBuilder { get; } = sceneFrameBuilder;

/// <summary>
/// Get the builder to use for region map frames.
/// </summary>
public IRegionMapFrameBuilder RegionMapFrameBuilder { get; } = regionMapFrameBuilder;

/// <summary>
/// Get the builder to use for command list frames.
/// </summary>
public ICommandListFrameBuilder CommandListFrameBuilder { get; } = commandListFrameBuilder;

/// <summary>
/// Get the builder to use for help frames.
/// </summary>
public IHelpFrameBuilder HelpFrameBuilder { get; } = helpFrameBuilder;

/// <summary>
/// Get the builder to use for completion frames.
/// </summary>
public ICompletionFrameBuilder CompletionFrameBuilder { get; } = completionFrameBuilder;

/// <summary>
/// Get the builder to use for game over frames.
/// </summary>
public IGameOverFrameBuilder GameOverFrameBuilder { get; } = gameOverFrameBuilder;

/// <summary>
/// Get the builder to use for about frames.
/// </summary>
public IAboutFrameBuilder AboutFrameBuilder { get; } = aboutFrameBuilder;

/// <summary>
/// Get the builder to use for reaction frames.
/// </summary>
public IReactionFrameBuilder ReactionModeFrameBuilder { get; } = reactionFrameBuilder;
/// <param name="frameBuilders">The frame builders.</param>
public class FrameBuilderCollection(params IFrameBuilder[] frameBuilders)
{
#region Methods

/// <summary>
/// Get the builder to use for conversation frames.
/// Get a frame builder for a specified type.
/// </summary>
public IConversationFrameBuilder ConversationFrameBuilder { get; } = conversationFrameBuilder;
/// <typeparam name="T">The type of frame builder.</typeparam>
/// <returns>The frame builder.</returns>
public T GetFrameBuilder<T>()
{
var match = Array.Find(frameBuilders, x => x is T);
return match != null ? (T)match : default;
}

#endregion
}
Expand Down
8 changes: 6 additions & 2 deletions NetAF/Rendering/FrameBuilders/FrameBuilderCollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public static FrameBuilderCollection Default
{
var gridLayoutBuilder = new GridStringBuilder();

return new(
IFrameBuilder[] frameBuilders =
[
new ConsoleTitleFrameBuilder(gridLayoutBuilder),
new ConsoleSceneFrameBuilder(gridLayoutBuilder, new ConsoleRoomMapBuilder(gridLayoutBuilder)),
new ConsoleRegionMapFrameBuilder(gridLayoutBuilder, new ConsoleRegionMapBuilder(gridLayoutBuilder)),
Expand All @@ -27,7 +28,10 @@ public static FrameBuilderCollection Default
new ConsoleGameOverFrameBuilder(gridLayoutBuilder),
new ConsoleAboutFrameBuilder(gridLayoutBuilder),
new ConsoleReactionFrameBuilder(gridLayoutBuilder),
new ConsoleConversationFrameBuilder(gridLayoutBuilder));
new ConsoleConversationFrameBuilder(gridLayoutBuilder)
];

return new(frameBuilders);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Rendering/FrameBuilders/IAboutFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace NetAF.Rendering.FrameBuilders
/// <summary>
/// Represents any object that can build about frames.
/// </summary>
public interface IAboutFrameBuilder
public interface IAboutFrameBuilder : IFrameBuilder
{
/// <summary>
/// Build a frame.
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Rendering/FrameBuilders/ICommandListFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace NetAF.Rendering.FrameBuilders
/// <summary>
/// Represents any object that can build command list frames.
/// </summary>
public interface ICommandListFrameBuilder
public interface ICommandListFrameBuilder : IFrameBuilder
{
/// <summary>
/// Build a frame.
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Rendering/FrameBuilders/ICompletionFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NetAF.Rendering.FrameBuilders
/// <summary>
/// Represents any object that can build completion frames.
/// </summary>
public interface ICompletionFrameBuilder
public interface ICompletionFrameBuilder : IFrameBuilder
{
/// <summary>
/// Build a frame.
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Rendering/FrameBuilders/IConversationFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NetAF.Rendering.FrameBuilders
/// <summary>
/// Represents any object that can build conversation frames.
/// </summary>
public interface IConversationFrameBuilder
public interface IConversationFrameBuilder : IFrameBuilder
{
/// <summary>
/// Build a frame.
Expand Down
9 changes: 9 additions & 0 deletions NetAF/Rendering/FrameBuilders/IFrameBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NetAF.Rendering.FrameBuilders
{
/// <summary>
/// Represents any object that can build frames.
/// </summary>
public interface IFrameBuilder
{
}
}
2 changes: 1 addition & 1 deletion NetAF/Rendering/FrameBuilders/IGameOverFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NetAF.Rendering.FrameBuilders
/// <summary>
/// Represents any object that can build game over frames.
/// </summary>
public interface IGameOverFrameBuilder
public interface IGameOverFrameBuilder : IFrameBuilder
{
/// <summary>
/// Build a frame.
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Rendering/FrameBuilders/IHelpFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace NetAF.Rendering.FrameBuilders
/// <summary>
/// Represents any object that can build help frames.
/// </summary>
public interface IHelpFrameBuilder
public interface IHelpFrameBuilder : IFrameBuilder
{
/// <summary>
/// Build a frame.
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Rendering/FrameBuilders/IReactionFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NetAF.Rendering.FrameBuilders
/// <summary>
/// Represents any object that can build reaction frames.
/// </summary>
public interface IReactionFrameBuilder
public interface IReactionFrameBuilder : IFrameBuilder
{
/// <summary>
/// Build a frame.
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Rendering/FrameBuilders/IRegionMapFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NetAF.Rendering.FrameBuilders
/// <summary>
/// Represents any object that can build region map frames.
/// </summary>
public interface IRegionMapFrameBuilder
public interface IRegionMapFrameBuilder : IFrameBuilder
{
/// <summary>
/// Build a frame.
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Rendering/FrameBuilders/ISceneFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NetAF.Rendering.FrameBuilders
/// <summary>
/// Represents any object that can build scene frames.
/// </summary>
public interface ISceneFrameBuilder
public interface ISceneFrameBuilder : IFrameBuilder
{
/// <summary>
/// Build a frame.
Expand Down
2 changes: 1 addition & 1 deletion NetAF/Rendering/FrameBuilders/ITitleFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NetAF.Rendering.FrameBuilders
/// <summary>
/// Represents any object that can build title frames.
/// </summary>
public interface ITitleFrameBuilder
public interface ITitleFrameBuilder : IFrameBuilder
{
/// <summary>
/// Build a frame.
Expand Down
Loading

0 comments on commit 18b2ae4

Please sign in to comment.