From 2f3abbb6f1141f15ef77ac27e431bc66bb0c7899 Mon Sep 17 00:00:00 2001 From: Sophia Pearson Date: Sun, 4 Sep 2022 15:43:12 +0200 Subject: game: adapt scripts to new design --- Scripts/Game/Command.cs | 25 ++++++++++++++++ Scripts/Game/CommandParser.cs | 23 ++++++++++++++ Scripts/Game/Commands/LookCommand.cs | 58 ++++++++++++++++++++++++++++++++++++ Scripts/Game/Game.cs | 25 ++++++++++++++++ Scripts/Game/Input.cs | 29 ++++++++++++++++++ Scripts/Game/Output.cs | 43 ++++++++++++++++++++++++++ Scripts/Game/OutputBlock.cs | 28 +++++++++++++++++ Scripts/Game/StatusLine.cs | 45 +++++++++++++--------------- 8 files changed, 252 insertions(+), 24 deletions(-) create mode 100644 Scripts/Game/Command.cs create mode 100644 Scripts/Game/CommandParser.cs create mode 100644 Scripts/Game/Commands/LookCommand.cs create mode 100644 Scripts/Game/Game.cs create mode 100644 Scripts/Game/Input.cs create mode 100644 Scripts/Game/Output.cs create mode 100644 Scripts/Game/OutputBlock.cs (limited to 'Scripts/Game') diff --git a/Scripts/Game/Command.cs b/Scripts/Game/Command.cs new file mode 100644 index 0000000..07d7c84 --- /dev/null +++ b/Scripts/Game/Command.cs @@ -0,0 +1,25 @@ +using Godot; + +namespace Texty.Scripts.Game +{ + public enum CommandType + { + Look + } + + public class Command : Object + { + public Command() + { + } + + public Command(CommandType type, string[] arguments) + { + RawArguments = arguments; + Type = type; + } + + public string[] RawArguments { get; } + public CommandType Type { get; } + } +} \ No newline at end of file diff --git a/Scripts/Game/CommandParser.cs b/Scripts/Game/CommandParser.cs new file mode 100644 index 0000000..5f8249a --- /dev/null +++ b/Scripts/Game/CommandParser.cs @@ -0,0 +1,23 @@ +using System.Linq; +using Godot; +using Texty.Scripts.Commands; + +namespace Texty.Scripts.Game +{ + public class CommandParser : Node + { + public override void _Ready() + { + } + + public Command TryParse(string text) + { + var components = text.Split(' '); + return components[0].ToLower() switch + { + "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 new file mode 100644 index 0000000..b7910cc --- /dev/null +++ b/Scripts/Game/Commands/LookCommand.cs @@ -0,0 +1,58 @@ +using System.Linq; +using Texty.Scripts.Game; + +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 diff --git a/Scripts/Game/Game.cs b/Scripts/Game/Game.cs new file mode 100644 index 0000000..50db736 --- /dev/null +++ b/Scripts/Game/Game.cs @@ -0,0 +1,25 @@ +using Godot; + +namespace Texty.Scripts.Game +{ + public class Game : Node + { + private Input Input => GetNodeOrNull($"%{nameof(Input)}"); + private Output Output => GetNodeOrNull($"%{nameof(Output)}"); + private StatusLine StatusLine => GetNodeOrNull($"%{nameof(StatusLine)}"); + + public override void _Ready() + { + } + + public void OnCommandSubmitted(Command command) + { + Output.Push($"! {command}"); + } + + public void OnUnknownInputSubmitted(string text) + { + Output.Push($"? {text}"); + } + } +} \ No newline at end of file diff --git a/Scripts/Game/Input.cs b/Scripts/Game/Input.cs new file mode 100644 index 0000000..2342ac2 --- /dev/null +++ b/Scripts/Game/Input.cs @@ -0,0 +1,29 @@ +using Godot; + +namespace Texty.Scripts.Game +{ + public class Input : PanelContainer + { + [Signal] public delegate void CommandSubmitted(Command command); + [Signal] public delegate void UnknownInputSubmitted(string text); + + private CommandParser CommandParser => GetNodeOrNull($"%{nameof(CommandParser)}"); + private Label Prompt => GetNodeOrNull