From 6fe4972aa48a29d0aafee0461ccd6c635ca9ee6c Mon Sep 17 00:00:00 2001 From: Sophia Pearson Date: Mon, 23 May 2022 00:02:58 +0200 Subject: commands: add basic command parser infrastructure --- Scripts/Commands/LookCommand.cs | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Scripts/Commands/LookCommand.cs (limited to 'Scripts/Commands/LookCommand.cs') diff --git a/Scripts/Commands/LookCommand.cs b/Scripts/Commands/LookCommand.cs new file mode 100644 index 0000000..08783a9 --- /dev/null +++ b/Scripts/Commands/LookCommand.cs @@ -0,0 +1,57 @@ +using System.Linq; + +namespace Texty.Scripts.Commands +{ + public enum LookModifier + { + At, + Around + } + + public class LookCommand : Command + { + public LookCommand() + { + } + + 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) + { + 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) + }; + } + } +} \ No newline at end of file -- cgit v1.2.3