summaryrefslogtreecommitdiff
path: root/Scripts
diff options
context:
space:
mode:
Diffstat (limited to 'Scripts')
-rw-r--r--Scripts/Command.cs22
-rw-r--r--Scripts/CommandParser.cs27
-rw-r--r--Scripts/Commands/LookCommand.cs57
-rw-r--r--Scripts/OutputRow.cs8
4 files changed, 99 insertions, 15 deletions
diff --git a/Scripts/Command.cs b/Scripts/Command.cs
index 4a7ce58..e784576 100644
--- a/Scripts/Command.cs
+++ b/Scripts/Command.cs
@@ -1,7 +1,25 @@
+using Godot;
+
namespace Texty.Scripts
{
- public class Command
+ 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/CommandParser.cs b/Scripts/CommandParser.cs
index 1394a5e..97d8025 100644
--- a/Scripts/CommandParser.cs
+++ b/Scripts/CommandParser.cs
@@ -1,14 +1,23 @@
+using System.Linq;
using Godot;
+using Texty.Scripts.Commands;
namespace Texty.Scripts
{
- public class CommandParser : Node
- {
-
- public override void _Ready()
- {
- }
-
- }
-}
+ 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/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; }
+
+ /// <summary>
+ /// Access this <c>LookCommand</c>'s modifier as a string.
+ /// </summary>
+ /// <remarks>
+ /// This function is provided as a means for testing the modifier parsing from GDScript.
+ /// </remarks>
+ private string ModifierAsString => Modifier?.ToString();
+
+ public override string ToString()
+ {
+ return $"look {ModifierAsString?.ToLower()} {string.Join(" ", Target)}";
+ }
+
+ /// <summary>
+ /// Try to parse this <c>LookCommand</c>'s modifier from the given arguments.
+ /// </summary>
+ /// <param name="arguments">The arguments given to this look commands</param>
+ /// <returns>A pair consisting of the parse modifier and the remaining arguments</returns>
+ 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/OutputRow.cs b/Scripts/OutputRow.cs
index 15a6454..13d9293 100644
--- a/Scripts/OutputRow.cs
+++ b/Scripts/OutputRow.cs
@@ -11,12 +11,12 @@ namespace Texty.Scripts
[Signal]
public delegate void OutputTextChanged(string newText);
- public Label Input;
- public Label Output;
-
private string _inputText = "";
private string _outputText = "";
+ public Label Input;
+ public Label Output;
+
[Export]
public string InputText
{
@@ -69,4 +69,4 @@ namespace Texty.Scripts
Output
}
}
-} \ No newline at end of file
+} \ No newline at end of file