diff options
| author | Sophia Pearson <codergal89@gmail.com> | 2022-09-04 15:43:12 +0200 |
|---|---|---|
| committer | Sophia Pearson <codergal89@gmail.com> | 2022-09-04 15:44:04 +0200 |
| commit | 2f3abbb6f1141f15ef77ac27e431bc66bb0c7899 (patch) | |
| tree | 17f7ee50161e1de1c11c6dd14a54a11614e7de03 | |
| parent | 0967a4654c9fa67b5cfc19edf3cfc075bf6bde92 (diff) | |
| download | texty-2f3abbb6f1141f15ef77ac27e431bc66bb0c7899.tar.xz texty-2f3abbb6f1141f15ef77ac27e431bc66bb0c7899.zip | |
game: adapt scripts to new design
| -rw-r--r-- | Scenes/Game/Game.tscn | 16 | ||||
| -rw-r--r-- | Scenes/Game/Input.tscn | 16 | ||||
| -rw-r--r-- | Scenes/Game/Output.tscn | 7 | ||||
| -rw-r--r-- | Scenes/Game/OutputBlock.tscn | 7 | ||||
| -rw-r--r-- | Scripts/Game/Command.cs (renamed from Scripts/Command.cs) | 2 | ||||
| -rw-r--r-- | Scripts/Game/CommandParser.cs (renamed from Scripts/CommandParser.cs) | 2 | ||||
| -rw-r--r-- | Scripts/Game/Commands/LookCommand.cs (renamed from Scripts/Commands/LookCommand.cs) | 1 | ||||
| -rw-r--r-- | Scripts/Game/Game.cs | 25 | ||||
| -rw-r--r-- | Scripts/Game/Input.cs | 29 | ||||
| -rw-r--r-- | Scripts/Game/Output.cs | 43 | ||||
| -rw-r--r-- | Scripts/Game/OutputBlock.cs | 28 | ||||
| -rw-r--r-- | Scripts/Game/StatusLine.cs | 45 | ||||
| -rw-r--r-- | Scripts/InputContainer.cs | 50 | ||||
| -rw-r--r-- | Scripts/OutputContainer.cs | 52 | ||||
| -rw-r--r-- | Scripts/OutputRow.cs | 69 | ||||
| -rw-r--r-- | Scripts/StartMenu.cs | 46 |
16 files changed, 187 insertions, 251 deletions
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/Game/Command.cs index e784576..07d7c84 100644 --- a/Scripts/Command.cs +++ b/Scripts/Game/Command.cs @@ -1,6 +1,6 @@ using Godot; -namespace Texty.Scripts +namespace Texty.Scripts.Game { public enum CommandType { diff --git a/Scripts/CommandParser.cs b/Scripts/Game/CommandParser.cs index 97d8025..5f8249a 100644 --- a/Scripts/CommandParser.cs +++ b/Scripts/Game/CommandParser.cs @@ -2,7 +2,7 @@ using System.Linq; using Godot; using Texty.Scripts.Commands; -namespace Texty.Scripts +namespace Texty.Scripts.Game { public class CommandParser : Node { diff --git a/Scripts/Commands/LookCommand.cs b/Scripts/Game/Commands/LookCommand.cs index 08783a9..b7910cc 100644 --- a/Scripts/Commands/LookCommand.cs +++ b/Scripts/Game/Commands/LookCommand.cs @@ -1,4 +1,5 @@ using System.Linq; +using Texty.Scripts.Game; namespace Texty.Scripts.Commands { 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 diff --git a/Scripts/InputContainer.cs b/Scripts/InputContainer.cs deleted file mode 100644 index 300d568..0000000 --- a/Scripts/InputContainer.cs +++ /dev/null @@ -1,50 +0,0 @@ -using Godot; - -namespace Texty.Scripts -{ - public abstract class InputContainer : HBoxContainer - { - [Signal] public delegate void InputSubmitted(string text); - - public LineEdit InputField; - public Button SubmitButton; - - public override void _Ready() - { - InputField = GetNode<LineEdit>(nameof(InputField)); - SubmitButton = GetNode<Button>(nameof(SubmitButton)); - - SubmitButton.Disabled = InputField.Text.Empty(); - } - - public void OnInputChanged(string newText) - { - SubmitButton.Disabled = newText.Empty(); - } - - public void OnInputSubmitted(string newText) - { - SubmitText(newText); - } - - public void OnInputSubmitted() - { - SubmitText(InputField.Text); - } - - public void OnVisibilityChanged() - { - if (Visible) - InputField.GrabFocus(); - else - InputField.ReleaseFocus(); - } - - private void SubmitText(string newText) - { - if (newText.Empty()) return; - EmitSignal(nameof(InputSubmitted), newText); - InputField.Clear(); - } - } -}
\ No newline at end of file diff --git a/Scripts/OutputContainer.cs b/Scripts/OutputContainer.cs deleted file mode 100644 index 5db1fe6..0000000 --- a/Scripts/OutputContainer.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System.Linq; -using Godot; - -namespace Texty.Scripts -{ - public abstract class OutputContainer : PanelContainer - { - private double _lastScrollLimit; - - private VScrollBar _scrollBar; - - [Export(PropertyHint.Range, "10,40,1")] public int MaximumRows = 20; - - public VBoxContainer OutputRows; - public ScrollContainer ScrollContainer; - - public override void _Ready() - { - ScrollContainer = GetNode<ScrollContainer>(nameof(ScrollContainer)); - _scrollBar = ScrollContainer.GetVScrollbar(); - OutputRows = ScrollContainer.GetNode<VBoxContainer>(nameof(OutputRows)); - - _lastScrollLimit = _scrollBar.MaxValue; - _scrollBar.Connect("changed", this, nameof(OnScrollLimitChanged)); - } - - public void Add(OutputRow row) - { - OutputRows.AddChild(row); - ExpungeOldRows(); - } - - public void OnScrollLimitChanged() - { - if (!(_lastScrollLimit < _scrollBar.MaxValue)) return; - _lastScrollLimit = _scrollBar.MaxValue; - ScrollContainer.ScrollVertical = (int)_scrollBar.MaxValue; - } - - private void ExpungeOldRows() - { - GD.Range(OutputRows.GetChildCount() - MaximumRows) - .Select(idx => OutputRows.GetChild(idx)) - .ToList() - .ForEach(row => - { - OutputRows.RemoveChild(row); - row.QueueFree(); - }); - } - } -}
\ No newline at end of file diff --git a/Scripts/OutputRow.cs b/Scripts/OutputRow.cs deleted file mode 100644 index cfbb2e7..0000000 --- a/Scripts/OutputRow.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using Godot; - -namespace Texty.Scripts -{ - public abstract class OutputRow : VBoxContainer - { - [Signal] public delegate void InputTextChanged(string newText); - [Signal] public delegate void OutputTextChanged(string newText); - - private string _inputText = ""; - private string _outputText = ""; - - public Label Input; - public Label Output; - - [Export] - public string InputText - { - get => _inputText; - set => UpdateText(Field.Input, value ?? ""); - } - - [Export] - public string OutputText - { - get => _outputText; - set => UpdateText(Field.Output, value ?? ""); - } - - public override void _Ready() - { - Input = GetNode<Label>(nameof(Input)); - UpdateLabel(Input, _inputText); - Output = GetNode<Label>(nameof(Output)); - UpdateLabel(Output, _outputText); - } - - private void UpdateText(Field field, string value) - { - switch (field) - { - case Field.Input: - _inputText = value; - EmitSignal(nameof(InputTextChanged), _inputText); - UpdateLabel(Input, value); - break; - case Field.Output: - _outputText = value; - EmitSignal(nameof(OutputTextChanged), value); - UpdateLabel(Output, value); - break; - default: - throw new ArgumentOutOfRangeException(nameof(field), field, "No such field!"); - } - } - - private static void UpdateLabel(Label label, string value) - { - if (label != null) label.Text = value; - } - - private enum Field - { - Input, - Output - } - } -}
\ No newline at end of file diff --git a/Scripts/StartMenu.cs b/Scripts/StartMenu.cs deleted file mode 100644 index a1f64cc..0000000 --- a/Scripts/StartMenu.cs +++ /dev/null @@ -1,46 +0,0 @@ -using Godot; - -namespace Texty.Scripts -{ - public class StartMenu : MarginContainer - { - [Signal] public delegate void QuitGame(); - [Signal] public delegate void ShowCredits(); - [Signal] public delegate void StartGame(); - - public Button CreditsButton; - public Button QuitButton; - public Button StartButton; - - public override void _Ready() - { - var buttons = FindNode("Buttons"); - CreditsButton = buttons.GetNode<Button>(nameof(CreditsButton)); - QuitButton = buttons.GetNode<Button>(nameof(QuitButton)); - StartButton = buttons.GetNode<Button>(nameof(StartButton)); - } - - public void OnCreditsButtonPressed() - { - EmitSignal(nameof(ShowCredits)); - } - - public void OnQuitButtonPressed() - { - EmitSignal(nameof(QuitGame)); - } - - public void OnStartButtonPressed() - { - EmitSignal(nameof(StartGame)); - } - - public void OnVisibilityChanged() - { - if(Visible) - StartButton.GrabFocus(); - else - StartButton.ReleaseFocus(); - } - } -}
\ No newline at end of file |
