summaryrefslogtreecommitdiff
path: root/Scripts/Game
diff options
context:
space:
mode:
Diffstat (limited to 'Scripts/Game')
-rw-r--r--Scripts/Game/Command.cs31
-rw-r--r--Scripts/Game/CommandInputArea.cs45
-rw-r--r--Scripts/Game/CommandParser.cs25
-rw-r--r--Scripts/Game/Commands/LookCommand.cs91
-rw-r--r--Scripts/Game/Game.cs33
5 files changed, 110 insertions, 115 deletions
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<CommandParser>($"%{nameof(CommandParser)}");
+
+ public override void _Ready()
{
- [Signal] public delegate void CommandSubmittedEventHandler(Command command);
- [Signal] public delegate void UnknownInputSubmittedEventHandler(string text);
-
- private CommandParser CommandParser => GetNodeOrNull<CommandParser>($"%{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; }
-
- /// <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)
+ 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
{
- 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<InputArea>($"%{nameof(InputArea)}");
- private OutputArea OutputArea => GetNodeOrNull<OutputArea>($"%{nameof(OutputArea)}");
- private StatusArea StatusArea => GetNodeOrNull<StatusArea>($"%{nameof(StatusArea)}");
+ private InputArea InputArea => GetNodeOrNull<InputArea>($"%{nameof(InputArea)}");
+ private OutputArea OutputArea => GetNodeOrNull<OutputArea>($"%{nameof(OutputArea)}");
+ private StatusArea StatusArea => GetNodeOrNull<StatusArea>($"%{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