From d7eefa488135c477f39ea0953b032c7dcdad8bc7 Mon Sep 17 00:00:00 2001 From: Sophia Pearson Date: Sat, 26 Nov 2022 10:08:44 +0100 Subject: scripts: perform code cleanup actions --- Scripts/Game/Command.cs | 31 ++++++------ Scripts/Game/CommandInputArea.cs | 45 +++++++++--------- Scripts/Game/CommandParser.cs | 25 +++++----- Scripts/Game/Commands/LookCommand.cs | 91 ++++++++++++++++++------------------ Scripts/Game/Game.cs | 33 +++++++------ 5 files changed, 110 insertions(+), 115 deletions(-) (limited to 'Scripts/Game') diff --git a/Scripts/Game/Command.cs b/Scripts/Game/Command.cs index 20b8437..56d80a7 100644 --- a/Scripts/Game/Command.cs +++ b/Scripts/Game/Command.cs @@ -1,25 +1,24 @@ using Godot; -namespace Texty.Scripts.Game +namespace Texty.Scripts.Game; + +public enum CommandType +{ + Look +} + +public partial class Command : Object { - public enum CommandType + public Command() { - Look } - public partial class Command : Object + public Command(CommandType type, string[] arguments) { - public Command() - { - } - - public Command(CommandType type, string[] arguments) - { - RawArguments = arguments; - Type = type; - } - - public string[] RawArguments { get; } - public CommandType Type { get; } + RawArguments = arguments; + Type = type; } + + public string[] RawArguments { get; } + public CommandType Type { get; } } \ No newline at end of file diff --git a/Scripts/Game/CommandInputArea.cs b/Scripts/Game/CommandInputArea.cs index af54a15..3b88417 100644 --- a/Scripts/Game/CommandInputArea.cs +++ b/Scripts/Game/CommandInputArea.cs @@ -1,32 +1,31 @@ using Godot; using Texty.Scripts.Terminal; -namespace Texty.Scripts.Game +namespace Texty.Scripts.Game; + +[Tool] +public partial class CommandInputArea : InputArea { - [Tool] - public partial class CommandInputArea : InputArea + [Signal] public delegate void CommandSubmittedEventHandler(Command command); + [Signal] public delegate void UnknownInputSubmittedEventHandler(string text); + + private CommandParser CommandParser => GetNodeOrNull($"%{nameof(CommandParser)}"); + + public override void _Ready() { - [Signal] public delegate void CommandSubmittedEventHandler(Command command); - [Signal] public delegate void UnknownInputSubmittedEventHandler(string text); - - private CommandParser CommandParser => GetNodeOrNull($"%{nameof(CommandParser)}"); - - public override void _Ready() - { - TextInput.GrabFocus(); - } + TextInput.GrabFocus(); + } - public override void OnTextEntered(string text) - { - if (text.Length == 0) - return; + public override void OnTextEntered(string text) + { + if (text.Length == 0) + return; - TextInput.Clear(); - var command = CommandParser.TryParse(text); - if (command != null) - EmitSignal(SignalName.CommandSubmitted, command); - else - EmitSignal(SignalName.UnknownInputSubmitted, text); - } + TextInput.Clear(); + var command = CommandParser.TryParse(text); + if (command != null) + EmitSignal(SignalName.CommandSubmitted, command); + else + EmitSignal(SignalName.UnknownInputSubmitted, text); } } \ No newline at end of file diff --git a/Scripts/Game/CommandParser.cs b/Scripts/Game/CommandParser.cs index 9b02ee5..6c29545 100644 --- a/Scripts/Game/CommandParser.cs +++ b/Scripts/Game/CommandParser.cs @@ -2,22 +2,21 @@ using System.Linq; using Godot; using Texty.Scripts.Commands; -namespace Texty.Scripts.Game +namespace Texty.Scripts.Game; + +public partial class CommandParser : Node { - public partial class CommandParser : Node + public override void _Ready() { - public override void _Ready() - { - } + } - public Command TryParse(string text) + public Command TryParse(string text) + { + var components = text.Split(' '); + return components[0].ToLower() switch { - var components = text.Split(' '); - return components[0].ToLower() switch - { - "look" => new LookCommand(components.Skip(1).ToArray()), - _ => null - }; - } + "look" => new LookCommand(components.Skip(1).ToArray()), + _ => null + }; } } \ No newline at end of file diff --git a/Scripts/Game/Commands/LookCommand.cs b/Scripts/Game/Commands/LookCommand.cs index 22ba77e..c66fa43 100644 --- a/Scripts/Game/Commands/LookCommand.cs +++ b/Scripts/Game/Commands/LookCommand.cs @@ -1,58 +1,57 @@ using System.Linq; using Texty.Scripts.Game; -namespace Texty.Scripts.Commands +namespace Texty.Scripts.Commands; + +public enum LookModifier +{ + At, + Around +} + +public partial class LookCommand : Command { - public enum LookModifier + public LookCommand() { - At, - Around } - public partial class LookCommand : Command + public LookCommand(string[] arguments) : base(CommandType.Look, arguments) { - public LookCommand() - { - } + (Modifier, arguments) = TryParseModifier(arguments); + Target = string.Join(" ", arguments); + } - public LookCommand(string[] arguments) : base(CommandType.Look, arguments) - { - (Modifier, arguments) = TryParseModifier(arguments); - Target = string.Join(" ", arguments); - } - - public string Target { get; } - public LookModifier? Modifier { get; } - - /// - /// Access this LookCommand's modifier as a string. - /// - /// - /// This function is provided as a means for testing the modifier parsing from GDScript. - /// - private string ModifierAsString => Modifier?.ToString(); - - public override string ToString() - { - return $"look {ModifierAsString?.ToLower()} {string.Join(" ", Target)}"; - } - - /// - /// Try to parse this LookCommand's modifier from the given arguments. - /// - /// The arguments given to this look commands - /// A pair consisting of the parse modifier and the remaining arguments - private static (LookModifier?, string[]) TryParseModifier(string[] arguments) + public string Target { get; } + public LookModifier? Modifier { get; } + + /// + /// Access this LookCommand's modifier as a string. + /// + /// + /// This function is provided as a means for testing the modifier parsing from GDScript. + /// + private string ModifierAsString => Modifier?.ToString(); + + public override string ToString() + { + return $"look {ModifierAsString?.ToLower()} {string.Join(" ", Target)}"; + } + + /// + /// Try to parse this LookCommand's modifier from the given arguments. + /// + /// The arguments given to this look commands + /// A pair consisting of the parse modifier and the remaining arguments + private static (LookModifier?, string[]) TryParseModifier(string[] arguments) + { + if (arguments.Length == 0) + return (null, arguments); + + return arguments[0].ToLower() switch { - if (arguments.Length == 0) - return (null, arguments); - - return arguments[0].ToLower() switch - { - "at" => (LookModifier.At, arguments.Skip(1).ToArray()), - "around" => (LookModifier.Around, new string[] { }), - _ => (null, arguments) - }; - } + "at" => (LookModifier.At, arguments.Skip(1).ToArray()), + "around" => (LookModifier.Around, new string[] { }), + _ => (null, arguments) + }; } } \ No newline at end of file diff --git a/Scripts/Game/Game.cs b/Scripts/Game/Game.cs index 07d42b2..e6d4c6a 100644 --- a/Scripts/Game/Game.cs +++ b/Scripts/Game/Game.cs @@ -1,26 +1,25 @@ using Godot; using Texty.Scripts.Terminal; -namespace Texty.Scripts.Game +namespace Texty.Scripts.Game; + +public partial class Game : Node { - public partial class Game : Node - { - private InputArea InputArea => GetNodeOrNull($"%{nameof(InputArea)}"); - private OutputArea OutputArea => GetNodeOrNull($"%{nameof(OutputArea)}"); - private StatusArea StatusArea => GetNodeOrNull($"%{nameof(StatusArea)}"); + private InputArea InputArea => GetNodeOrNull($"%{nameof(InputArea)}"); + private OutputArea OutputArea => GetNodeOrNull($"%{nameof(OutputArea)}"); + private StatusArea StatusArea => GetNodeOrNull($"%{nameof(StatusArea)}"); - public override void _Ready() - { - } + public override void _Ready() + { + } - public void OnCommandSubmitted(Command command) - { - OutputArea.Push($"! {command}"); - } + public void OnCommandSubmitted(Command command) + { + OutputArea.Push($"! {command}"); + } - public void OnUnknownInputSubmitted(string text) - { - OutputArea.Push($"? {text}"); - } + public void OnUnknownInputSubmitted(string text) + { + OutputArea.Push($"? {text}"); } } \ No newline at end of file -- cgit v1.2.3