From 77074a014e374fc1ef8bf1faf6f6e10bd6489dbc Mon Sep 17 00:00:00 2001 From: ben_singer Date: Mon, 18 Nov 2024 09:28:15 +0000 Subject: [PATCH] Minor tweaks to simplify game logic --- NetAF.Tests/TestGameConfiguration.cs | 1 - .../Configuration/ConsoleGameConfiguration.cs | 5 -- .../Logic/Configuration/IGameConfiguration.cs | 5 -- NetAF/Logic/Game.cs | 55 +++++++------------ NetAF/Logic/Modes/ReactionMode.cs | 5 +- .../ConsoleConversationFrameBuilder.cs | 7 ++- .../Console/ConsoleReactionFrameBuilder.cs | 12 +++- .../Console/ConsoleRegionMapFrameBuilder.cs | 7 ++- .../Console/ConsoleSceneFrameBuilder.cs | 7 ++- .../FrameBuilders/IReactionFrameBuilder.cs | 3 +- 10 files changed, 53 insertions(+), 54 deletions(-) diff --git a/NetAF.Tests/TestGameConfiguration.cs b/NetAF.Tests/TestGameConfiguration.cs index 150c9cef..1a43bbfe 100644 --- a/NetAF.Tests/TestGameConfiguration.cs +++ b/NetAF.Tests/TestGameConfiguration.cs @@ -20,7 +20,6 @@ internal class TestGameConfiguration(Size displaySize, ExitMode exitMode, IIOAda new CustomCommandInterpreter()); public FrameBuilderCollection FrameBuilders { get; set; } = FrameBuilderCollections.Default; - public string ErrorPrefix { get; set; } = "Oops"; public bool DisplayCommandListInSceneFrames { get; set; } = true; public KeyType SceneMapKeyType { get; set; } = KeyType.Dynamic; public IIOAdapter Adapter { get; private set; } = adapter; diff --git a/NetAF/Logic/Configuration/ConsoleGameConfiguration.cs b/NetAF/Logic/Configuration/ConsoleGameConfiguration.cs index 4ecf7ace..edf34d8c 100644 --- a/NetAF/Logic/Configuration/ConsoleGameConfiguration.cs +++ b/NetAF/Logic/Configuration/ConsoleGameConfiguration.cs @@ -45,11 +45,6 @@ public sealed class ConsoleGameConfiguration(Size displaySize, ExitMode exitMode /// public FrameBuilderCollection FrameBuilders { get; set; } = FrameBuilderCollections.Default; - /// - /// Get or set the prefix to use when displaying errors. - /// - public string ErrorPrefix { get; set; } = "Oops"; - /// /// Get or set if the command list is displayed in scene frames. /// diff --git a/NetAF/Logic/Configuration/IGameConfiguration.cs b/NetAF/Logic/Configuration/IGameConfiguration.cs index 4e5b3d26..3006b043 100644 --- a/NetAF/Logic/Configuration/IGameConfiguration.cs +++ b/NetAF/Logic/Configuration/IGameConfiguration.cs @@ -33,11 +33,6 @@ public interface IGameConfiguration /// public FrameBuilderCollection FrameBuilders { get; set; } - /// - /// Get or set the prefix to use when displaying errors. - /// - public string ErrorPrefix { get; set; } - /// /// Get or set if the command list is displayed in scene frames. /// diff --git a/NetAF/Logic/Game.cs b/NetAF/Logic/Game.cs index cb6786a1..f1a08598 100644 --- a/NetAF/Logic/Game.cs +++ b/NetAF/Logic/Game.cs @@ -153,17 +153,8 @@ internal void Execute() // process the input var reaction = ProcessInput(input); - // if the reaction should be displayed - if (reaction.Result != ReactionResult.Silent) - { - // display the reaction now - DisplayReaction(reaction); - } - else if (reaction.Result != ReactionResult.GameModeChanged && Mode.Type == GameModeType.Information) - { - // revert back to scene mode as the command didn't change the mode and the current mode is information, essentially the mode has expired - ChangeMode(new SceneMode()); - } + // handle the reaction + HandleReaction(reaction); // check if the game has ended if (CheckForGameEnd(EndConditions, out endMode)) @@ -192,6 +183,25 @@ internal void Execute() state = GameState.Finished; } + /// + /// Handle a reaction. + /// + /// The reaction to handle. + private void HandleReaction(Reaction reaction) + { + // check if needed to + if (reaction.Result == ReactionResult.Error || reaction.Result == ReactionResult.Inform) + { + // display the reaction + ChangeMode(new ReactionMode(Overworld.CurrentRegion.CurrentRoom.Identifier.Name, reaction.Description)); + } + else if (reaction.Result != ReactionResult.GameModeChanged && Mode.Type == GameModeType.Information) + { + // revert back to scene mode as the command didn't change the mode and the current mode information, essentially the mode has expired + ChangeMode(new SceneMode()); + } + } + /// /// Check to see if the game has ended. /// @@ -320,29 +330,6 @@ private Reaction ProcessInput(string input) return new(ReactionResult.Silent, string.Empty); } - /// - /// Display a reaction. - /// - /// The reaction to handle. - private void DisplayReaction(Reaction reaction) - { - switch (reaction.Result) - { - case ReactionResult.Error: - var message = Configuration.ErrorPrefix + ": " + reaction.Description; - ChangeMode(new ReactionMode(Overworld.CurrentRegion.CurrentRoom.Identifier.Name, message)); - break; - case ReactionResult.Silent: - case ReactionResult.GameModeChanged: - break; - case ReactionResult.Inform: - ChangeMode(new ReactionMode(Overworld.CurrentRegion.CurrentRoom.Identifier.Name, reaction.Description)); - break; - default: - throw new NotImplementedException(); - } - } - /// /// End the game. /// diff --git a/NetAF/Logic/Modes/ReactionMode.cs b/NetAF/Logic/Modes/ReactionMode.cs index 011e4f3c..db07f5c4 100644 --- a/NetAF/Logic/Modes/ReactionMode.cs +++ b/NetAF/Logic/Modes/ReactionMode.cs @@ -7,7 +7,8 @@ namespace NetAF.Logic.Modes /// /// The title. /// The message. - public sealed class ReactionMode(string title, string message) : IGameMode + /// If the message is an error.. + public sealed class ReactionMode(string title, string message, bool isError) : IGameMode { #region Implementation of IGameMode @@ -27,7 +28,7 @@ public sealed class ReactionMode(string title, string message) : IGameMode /// The game. public void Render(Game game) { - var frame = game.Configuration.FrameBuilders.ReactionModeFrameBuilder.Build(title, message, game.Configuration.DisplaySize); + var frame = game.Configuration.FrameBuilders.ReactionModeFrameBuilder.Build(title, message, isError, game.Configuration.DisplaySize); game.Configuration.Adapter.RenderFrame(frame); } diff --git a/NetAF/Rendering/FrameBuilders/Console/ConsoleConversationFrameBuilder.cs b/NetAF/Rendering/FrameBuilders/Console/ConsoleConversationFrameBuilder.cs index 21f37d8e..61d333f5 100644 --- a/NetAF/Rendering/FrameBuilders/Console/ConsoleConversationFrameBuilder.cs +++ b/NetAF/Rendering/FrameBuilders/Console/ConsoleConversationFrameBuilder.cs @@ -58,6 +58,11 @@ public sealed class ConsoleConversationFrameBuilder(GridStringBuilder gridString /// public AnsiColor InputColor { get; set; } = AnsiColor.White; + /// + /// Get or set the command title. + /// + public string CommandTitle { get; set; } = "You can:"; + #endregion #region StaticMethods @@ -149,7 +154,7 @@ public IFrame Build(string title, IConverser converser, CommandHelp[] contextual if (contextualCommands?.Any() ?? false) { gridStringBuilder.DrawHorizontalDivider(lastY + linePadding, BorderColor); - gridStringBuilder.DrawWrapped("You can:", leftMargin, lastY + 4, availableWidth, ResponseColor, out _, out lastY); + gridStringBuilder.DrawWrapped(CommandTitle, leftMargin, lastY + 4, availableWidth, ResponseColor, out _, out lastY); var maxCommandLength = contextualCommands.Max(x => x.Command.Length); const int padding = 4; diff --git a/NetAF/Rendering/FrameBuilders/Console/ConsoleReactionFrameBuilder.cs b/NetAF/Rendering/FrameBuilders/Console/ConsoleReactionFrameBuilder.cs index d724ec84..4cd9a1b9 100644 --- a/NetAF/Rendering/FrameBuilders/Console/ConsoleReactionFrameBuilder.cs +++ b/NetAF/Rendering/FrameBuilders/Console/ConsoleReactionFrameBuilder.cs @@ -38,6 +38,11 @@ public sealed class ConsoleReactionFrameBuilder(GridStringBuilder gridStringBuil /// public AnsiColor MessageColor { get; set; } = AnsiColor.White; + /// + /// Get or set the error message color. + /// + public AnsiColor ErrorMessageColor { get; set; } = AnsiColor.White; + #endregion #region Implementation of IReactionFrameBuilder @@ -47,8 +52,9 @@ public sealed class ConsoleReactionFrameBuilder(GridStringBuilder gridStringBuil /// /// The title to display to the user. /// The message to display to the user. + /// If the message is an error. /// The size of the frame. - public IFrame Build(string title, string message, Size size) + public IFrame Build(string title, string message, bool isError, Size size) { gridStringBuilder.Resize(size); @@ -67,8 +73,8 @@ public IFrame Build(string title, string message, Size size) lastY += 3; } - gridStringBuilder.DrawWrapped(message.EnsureFinishedSentence(), leftMargin, lastY, availableWidth, MessageColor, out _, out _); - + gridStringBuilder.DrawWrapped(message.EnsureFinishedSentence(), leftMargin, lastY, availableWidth, isError ? ErrorMessageColor : MessageColor, out _, out _); + return new GridTextFrame(gridStringBuilder, 0, 0, BackgroundColor) { ShowCursor = false }; } diff --git a/NetAF/Rendering/FrameBuilders/Console/ConsoleRegionMapFrameBuilder.cs b/NetAF/Rendering/FrameBuilders/Console/ConsoleRegionMapFrameBuilder.cs index 5479be00..1298c38b 100644 --- a/NetAF/Rendering/FrameBuilders/Console/ConsoleRegionMapFrameBuilder.cs +++ b/NetAF/Rendering/FrameBuilders/Console/ConsoleRegionMapFrameBuilder.cs @@ -52,6 +52,11 @@ public sealed class ConsoleRegionMapFrameBuilder(GridStringBuilder gridStringBui /// public AnsiColor InputColor { get; set; } = AnsiColor.White; + /// + /// Get or set the command title. + /// + public string CommandTitle { get; set; } = "You can:"; + #endregion #region Implementation of IRegionMapFrameBuilder @@ -90,7 +95,7 @@ public IFrame Build(Region region, Point3D focusPosition, CommandHelp[] contextu var requiredYToFitAllCommands = size.Height - commandSpace; gridStringBuilder.DrawHorizontalDivider(requiredYToFitAllCommands, BorderColor); - gridStringBuilder.DrawWrapped("You can:", leftMargin, requiredYToFitAllCommands + 2, availableWidth, CommandsColor, out _, out lastY); + gridStringBuilder.DrawWrapped(CommandTitle, leftMargin, requiredYToFitAllCommands + 2, availableWidth, CommandsColor, out _, out lastY); var maxCommandLength = contextualCommands.Max(x => x.Command.Length); const int padding = 4; diff --git a/NetAF/Rendering/FrameBuilders/Console/ConsoleSceneFrameBuilder.cs b/NetAF/Rendering/FrameBuilders/Console/ConsoleSceneFrameBuilder.cs index 6b4da0b4..a4c1018a 100644 --- a/NetAF/Rendering/FrameBuilders/Console/ConsoleSceneFrameBuilder.cs +++ b/NetAF/Rendering/FrameBuilders/Console/ConsoleSceneFrameBuilder.cs @@ -56,6 +56,11 @@ public sealed class ConsoleSceneFrameBuilder(GridStringBuilder gridStringBuilder /// public bool DisplayMessagesInIsolation { get; set; } = true; + /// + /// Get or set the command title. + /// + public string CommandTitle { get; set; } = "You can:"; + #endregion #region Implementation of ISceneFrameBuilder @@ -119,7 +124,7 @@ public IFrame Build(Room room, ViewPoint viewPoint, PlayableCharacter player, Co lastY = yStart; gridStringBuilder.DrawHorizontalDivider(lastY + linePadding, BorderColor); - gridStringBuilder.DrawWrapped("You can:", leftMargin, lastY + 4, availableWidth, CommandsColor, out _, out lastY); + gridStringBuilder.DrawWrapped(CommandTitle, leftMargin, lastY + 4, availableWidth, CommandsColor, out _, out lastY); var maxCommandLength = contextualCommands.Max(x => x.Command.Length); const int padding = 4; diff --git a/NetAF/Rendering/FrameBuilders/IReactionFrameBuilder.cs b/NetAF/Rendering/FrameBuilders/IReactionFrameBuilder.cs index 35c7e153..a0fb9a56 100644 --- a/NetAF/Rendering/FrameBuilders/IReactionFrameBuilder.cs +++ b/NetAF/Rendering/FrameBuilders/IReactionFrameBuilder.cs @@ -13,7 +13,8 @@ public interface IReactionFrameBuilder /// /// The title to display to the user. /// The message to display to the user. + /// If the message is an error. /// The size of the frame. - IFrame Build(string title, string message, Size size); + IFrame Build(string title, string message, bool isError, Size size); } }