summaryrefslogtreecommitdiff
path: root/Scripts/Game
diff options
context:
space:
mode:
authorSophia Pearson <codergal89@gmail.com>2022-09-04 15:43:12 +0200
committerSophia Pearson <codergal89@gmail.com>2022-09-04 15:44:04 +0200
commit2f3abbb6f1141f15ef77ac27e431bc66bb0c7899 (patch)
tree17f7ee50161e1de1c11c6dd14a54a11614e7de03 /Scripts/Game
parent0967a4654c9fa67b5cfc19edf3cfc075bf6bde92 (diff)
downloadtexty-2f3abbb6f1141f15ef77ac27e431bc66bb0c7899.tar.xz
texty-2f3abbb6f1141f15ef77ac27e431bc66bb0c7899.zip
game: adapt scripts to new design
Diffstat (limited to 'Scripts/Game')
-rw-r--r--Scripts/Game/Command.cs25
-rw-r--r--Scripts/Game/CommandParser.cs23
-rw-r--r--Scripts/Game/Commands/LookCommand.cs58
-rw-r--r--Scripts/Game/Game.cs25
-rw-r--r--Scripts/Game/Input.cs29
-rw-r--r--Scripts/Game/Output.cs43
-rw-r--r--Scripts/Game/OutputBlock.cs28
-rw-r--r--Scripts/Game/StatusLine.cs45
8 files changed, 252 insertions, 24 deletions
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; }
+
+ /// <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/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<Input>($"%{nameof(Input)}");
+ private Output Output => GetNodeOrNull<Output>($"%{nameof(Output)}");
+ private StatusLine StatusLine => GetNodeOrNull<StatusLine>($"%{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<CommandParser>($"%{nameof(CommandParser)}");
+ private Label Prompt => GetNodeOrNull<Label>($"%{nameof(Prompt)}");
+ private LineEdit Text => GetNodeOrNull<LineEdit>($"%{nameof(Text)}");
+
+ public override void _Ready()
+ {
+ Text.GrabFocus();
+ }
+
+ public void OnTextEntered(string text)
+ {
+ Text.Clear();
+ var command = CommandParser.TryParse(text);
+ if (command != null)
+ EmitSignal(nameof(CommandSubmitted), command);
+ else
+ EmitSignal(nameof(UnknownInputSubmitted), text);
+ }
+ }
+} \ No newline at end of file
diff --git a/Scripts/Game/Output.cs b/Scripts/Game/Output.cs
new file mode 100644
index 0000000..be943f7
--- /dev/null
+++ b/Scripts/Game/Output.cs
@@ -0,0 +1,43 @@
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using Godot;
+using Godot.Collections;
+
+namespace Texty.Scripts.Game
+{
+ public class Output : PanelContainer
+ {
+ [Export(PropertyHint.File, "*.tscn")] public PackedScene OutputBlockScene;
+ public Array<string> TextBlocks => new Array<string>(BlockNodes.ToList().Select(block => block.Content));
+
+ private VBoxContainer LineContainer => GetNodeOrNull<VBoxContainer>($"%{nameof(LineContainer)}");
+
+ private IEnumerable<OutputBlock> BlockNodes =>
+ GD.Range(LineContainer?.GetChildCount() ?? 0)
+ .Select(index => LineContainer.GetChild(index))
+ .Cast<OutputBlock>();
+
+ public override void _Ready()
+ {
+ Debug.Assert(OutputBlockScene != null, "OutputBlockScene has not been configured!");
+ Debug.Assert(OutputBlockScene.CanInstance(), "OutputBlockScene can not be instanced!");
+ }
+
+ public void Clear()
+ {
+ BlockNodes.ToList().ForEach(block =>
+ {
+ LineContainer.RemoveChild(block);
+ block.QueueFree();
+ });
+ }
+
+ public void Push(string text)
+ {
+ var block = OutputBlockScene.Instance<OutputBlock>();
+ block.Content = text;
+ LineContainer.AddChild(block);
+ }
+ }
+} \ No newline at end of file
diff --git a/Scripts/Game/OutputBlock.cs b/Scripts/Game/OutputBlock.cs
new file mode 100644
index 0000000..491958c
--- /dev/null
+++ b/Scripts/Game/OutputBlock.cs
@@ -0,0 +1,28 @@
+using Godot;
+
+namespace Texty.Scripts.Game
+{
+ public class OutputBlock : MarginContainer
+ {
+ private string _content = "";
+
+ private RichTextLabel ContentLabel => GetNodeOrNull<RichTextLabel>($"%{nameof(ContentLabel)}");
+
+ [Export(PropertyHint.MultilineText)]
+ public string Content
+ {
+ get => ContentLabel?.BbcodeText ?? "";
+ set
+ {
+ _content = value;
+ if (ContentLabel != null)
+ ContentLabel.BbcodeText = GodotSharp.Singleton.Tr(_content);
+ }
+ }
+
+ public override void _Ready()
+ {
+ Content = _content;
+ }
+ }
+} \ No newline at end of file
diff --git a/Scripts/Game/StatusLine.cs b/Scripts/Game/StatusLine.cs
index 4a3fb23..ef98a0a 100644
--- a/Scripts/Game/StatusLine.cs
+++ b/Scripts/Game/StatusLine.cs
@@ -2,29 +2,26 @@ using Godot;
namespace Texty.Scripts.Game
{
- public class StatusLine : PanelContainer
- {
- private string _titleText = "Title Text";
-
- [Export]
- public string Title
- {
- get => _titleText;
- set
- {
- if (TitleLabel != null)
- TitleLabel.Text = value;
- _titleText = value;
- }
- }
+ public class StatusLine : PanelContainer
+ {
+ private string _titleText = "Title Text";
+ private RichTextLabel TitleLabel => GetNodeOrNull<RichTextLabel>($"%{nameof(TitleLabel)}");
- public RichTextLabel TitleLabel;
-
- public override void _Ready()
- {
- TitleLabel = GetNode<RichTextLabel>($"%{nameof(TitleLabel)}");
- TitleLabel.Text = _titleText;
- }
- }
-}
+ [Export]
+ public string Title
+ {
+ get => _titleText;
+ set
+ {
+ _titleText = value;
+ if (TitleLabel != null)
+ TitleLabel.Text = GodotSharp.Singleton.Tr(value);
+ }
+ }
+ public override void _Ready()
+ {
+ Title = _titleText;
+ }
+ }
+} \ No newline at end of file