Skip to content

Commit

Permalink
Minor tweaks to simplify game logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_singer committed Nov 18, 2024
1 parent 0eb835f commit 77074a0
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 54 deletions.
1 change: 0 additions & 1 deletion NetAF.Tests/TestGameConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions NetAF/Logic/Configuration/ConsoleGameConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public sealed class ConsoleGameConfiguration(Size displaySize, ExitMode exitMode
/// </summary>
public FrameBuilderCollection FrameBuilders { get; set; } = FrameBuilderCollections.Default;

/// <summary>
/// Get or set the prefix to use when displaying errors.
/// </summary>
public string ErrorPrefix { get; set; } = "Oops";

/// <summary>
/// Get or set if the command list is displayed in scene frames.
/// </summary>
Expand Down
5 changes: 0 additions & 5 deletions NetAF/Logic/Configuration/IGameConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public interface IGameConfiguration
/// </summary>
public FrameBuilderCollection FrameBuilders { get; set; }

/// <summary>
/// Get or set the prefix to use when displaying errors.
/// </summary>
public string ErrorPrefix { get; set; }

/// <summary>
/// Get or set if the command list is displayed in scene frames.
/// </summary>
Expand Down
55 changes: 21 additions & 34 deletions NetAF/Logic/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -192,6 +183,25 @@ internal void Execute()
state = GameState.Finished;
}

/// <summary>
/// Handle a reaction.
/// </summary>
/// <param name="reaction">The reaction to handle.</param>
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());
}
}

/// <summary>
/// Check to see if the game has ended.
/// </summary>
Expand Down Expand Up @@ -320,29 +330,6 @@ private Reaction ProcessInput(string input)
return new(ReactionResult.Silent, string.Empty);
}

/// <summary>
/// Display a reaction.
/// </summary>
/// <param name="reaction">The reaction to handle.</param>
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();
}
}

/// <summary>
/// End the game.
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions NetAF/Logic/Modes/ReactionMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace NetAF.Logic.Modes
/// </summary>
/// <param name="title">The title.</param>
/// <param name="message">The message.</param>
public sealed class ReactionMode(string title, string message) : IGameMode
/// <param name="isError">If the message is an error..</param>
public sealed class ReactionMode(string title, string message, bool isError) : IGameMode
{
#region Implementation of IGameMode

Expand All @@ -27,7 +28,7 @@ public sealed class ReactionMode(string title, string message) : IGameMode
/// <param name="game">The game.</param>
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public sealed class ConsoleConversationFrameBuilder(GridStringBuilder gridString
/// </summary>
public AnsiColor InputColor { get; set; } = AnsiColor.White;

/// <summary>
/// Get or set the command title.
/// </summary>
public string CommandTitle { get; set; } = "You can:";

#endregion

#region StaticMethods
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public sealed class ConsoleReactionFrameBuilder(GridStringBuilder gridStringBuil
/// </summary>
public AnsiColor MessageColor { get; set; } = AnsiColor.White;

/// <summary>
/// Get or set the error message color.
/// </summary>
public AnsiColor ErrorMessageColor { get; set; } = AnsiColor.White;

#endregion

#region Implementation of IReactionFrameBuilder
Expand All @@ -47,8 +52,9 @@ public sealed class ConsoleReactionFrameBuilder(GridStringBuilder gridStringBuil
/// </summary>
/// <param name="title">The title to display to the user.</param>
/// <param name="message">The message to display to the user.</param>
/// <param name="isError">If the message is an error.</param>
/// <param name="size">The size of the frame.</param>
public IFrame Build(string title, string message, Size size)
public IFrame Build(string title, string message, bool isError, Size size)
{
gridStringBuilder.Resize(size);

Expand All @@ -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 };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public sealed class ConsoleRegionMapFrameBuilder(GridStringBuilder gridStringBui
/// </summary>
public AnsiColor InputColor { get; set; } = AnsiColor.White;

/// <summary>
/// Get or set the command title.
/// </summary>
public string CommandTitle { get; set; } = "You can:";

#endregion

#region Implementation of IRegionMapFrameBuilder
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public sealed class ConsoleSceneFrameBuilder(GridStringBuilder gridStringBuilder
/// </summary>
public bool DisplayMessagesInIsolation { get; set; } = true;

/// <summary>
/// Get or set the command title.
/// </summary>
public string CommandTitle { get; set; } = "You can:";

#endregion

#region Implementation of ISceneFrameBuilder
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion NetAF/Rendering/FrameBuilders/IReactionFrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public interface IReactionFrameBuilder
/// </summary>
/// <param name="title">The title to display to the user.</param>
/// <param name="message">The message to display to the user.</param>
/// <param name="isError">If the message is an error.</param>
/// <param name="size">The size of the frame.</param>
IFrame Build(string title, string message, Size size);
IFrame Build(string title, string message, bool isError, Size size);
}
}

0 comments on commit 77074a0

Please sign in to comment.