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 --- Scenes/Game/Game.tscn | 16 ++++++++- Scenes/Game/Input.tscn | 16 ++++++--- Scenes/Game/Output.tscn | 7 ++-- Scenes/Game/OutputBlock.tscn | 7 +++- Scripts/Command.cs | 25 ------------- Scripts/CommandParser.cs | 23 ------------ Scripts/Commands/LookCommand.cs | 57 ----------------------------- 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 +++++++++++------------ Scripts/InputContainer.cs | 50 -------------------------- Scripts/OutputContainer.cs | 52 --------------------------- Scripts/OutputRow.cs | 69 ------------------------------------ Scripts/StartMenu.cs | 46 ------------------------ 19 files changed, 290 insertions(+), 354 deletions(-) delete mode 100644 Scripts/Command.cs delete mode 100644 Scripts/CommandParser.cs delete mode 100644 Scripts/Commands/LookCommand.cs 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 delete mode 100644 Scripts/InputContainer.cs delete mode 100644 Scripts/OutputContainer.cs delete mode 100644 Scripts/OutputRow.cs delete mode 100644 Scripts/StartMenu.cs diff --git a/Scenes/Game/Game.tscn b/Scenes/Game/Game.tscn index 7843f3a..e51a434 100644 --- a/Scenes/Game/Game.tscn +++ b/Scenes/Game/Game.tscn @@ -1,16 +1,30 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://Scenes/Game/StatusLine.tscn" type="PackedScene" id=1] [ext_resource path="res://Scenes/Game/Output.tscn" type="PackedScene" id=2] [ext_resource path="res://Scenes/Game/Input.tscn" type="PackedScene" id=3] +[ext_resource path="res://Scripts/Game/Game.cs" type="Script" id=4] +[ext_resource path="res://Scenes/Game/OutputBlock.tscn" type="PackedScene" id=5] [node name="Game" type="VBoxContainer"] anchor_right = 1.0 anchor_bottom = 1.0 custom_constants/separation = 0 +script = ExtResource( 4 ) [node name="StatusLine" parent="." instance=ExtResource( 1 )] +unique_name_in_owner = true [node name="Output" parent="." instance=ExtResource( 2 )] +unique_name_in_owner = true +OutputBlockScene = ExtResource( 5 ) [node name="Input" parent="." instance=ExtResource( 3 )] +unique_name_in_owner = true +anchor_right = 0.0 +margin_top = 451.0 +margin_right = 640.0 +margin_bottom = 480.0 + +[connection signal="CommandSubmitted" from="Input" to="." method="OnCommandSubmitted"] +[connection signal="UnknownInputSubmitted" from="Input" to="." method="OnUnknownInputSubmitted"] diff --git a/Scenes/Game/Input.tscn b/Scenes/Game/Input.tscn index 7106217..627a0ac 100644 --- a/Scenes/Game/Input.tscn +++ b/Scenes/Game/Input.tscn @@ -1,10 +1,12 @@ -[gd_scene format=2] +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://Scripts/Game/Input.cs" type="Script" id=1] +[ext_resource path="res://Scripts/Game/CommandParser.cs" type="Script" id=2] [node name="Input" type="PanelContainer"] -margin_top = 451.0 -margin_right = 640.0 -margin_bottom = 480.0 +anchor_right = 1.0 theme_type_variation = "InvertedPanelContainer" +script = ExtResource( 1 ) [node name="Container" type="HBoxContainer" parent="."] margin_left = 8.0 @@ -28,3 +30,9 @@ size_flags_horizontal = 3 context_menu_enabled = false selecting_enabled = false caret_blink = true + +[node name="CommandParser" type="Node" parent="."] +unique_name_in_owner = true +script = ExtResource( 2 ) + +[connection signal="text_entered" from="Container/Text" to="." method="OnTextEntered"] diff --git a/Scenes/Game/Output.tscn b/Scenes/Game/Output.tscn index 5a2cd3f..68f36f4 100644 --- a/Scenes/Game/Output.tscn +++ b/Scenes/Game/Output.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://Scenes/Game/OutputBlock.tscn" type="PackedScene" id=1] +[ext_resource path="res://Scripts/Game/Output.cs" type="Script" id=2] [node name="Output" type="PanelContainer"] margin_top = 31.0 @@ -8,6 +9,7 @@ margin_right = 640.0 margin_bottom = 451.0 size_flags_horizontal = 3 size_flags_vertical = 3 +script = ExtResource( 2 ) [node name="ScrollContainer" type="ScrollContainer" parent="."] margin_left = 8.0 @@ -25,6 +27,7 @@ margin_right = 624.0 margin_bottom = 54.0 size_flags_horizontal = 3 -[node name="OutputBlock" type="MarginContainer" parent="ScrollContainer/LineContainer" instance=ExtResource( 1 )] +[node name="OutputBlock" parent="ScrollContainer/LineContainer" instance=ExtResource( 1 )] +anchor_right = 0.0 margin_right = 624.0 margin_bottom = 54.0 diff --git a/Scenes/Game/OutputBlock.tscn b/Scenes/Game/OutputBlock.tscn index e6f0dfd..aa1d60f 100644 --- a/Scenes/Game/OutputBlock.tscn +++ b/Scenes/Game/OutputBlock.tscn @@ -1,9 +1,14 @@ -[gd_scene format=2] +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://Scripts/Game/OutputBlock.cs" type="Script" id=1] [node name="OutputBlock" type="MarginContainer"] anchor_right = 1.0 +script = ExtResource( 1 ) +Content = "This is where the output goes." [node name="ContentLabel" type="RichTextLabel" parent="."] +unique_name_in_owner = true margin_right = 640.0 margin_bottom = 54.0 text = "This is where the output goes and it should wrap correctly if it goes beyond the screen width." diff --git a/Scripts/Command.cs b/Scripts/Command.cs deleted file mode 100644 index e784576..0000000 --- a/Scripts/Command.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Godot; - -namespace Texty.Scripts -{ - 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 deleted file mode 100644 index 97d8025..0000000 --- a/Scripts/CommandParser.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Linq; -using Godot; -using Texty.Scripts.Commands; - -namespace Texty.Scripts -{ - 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 deleted file mode 100644 index 08783a9..0000000 --- a/Scripts/Commands/LookCommand.cs +++ /dev/null @@ -1,57 +0,0 @@ -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 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