From d7eefa488135c477f39ea0953b032c7dcdad8bc7 Mon Sep 17 00:00:00 2001 From: Sophia Pearson Date: Sat, 26 Nov 2022 10:08:44 +0100 Subject: scripts: perform code cleanup actions --- Scenes/Menus/MainMenu.tscn | 28 +-------- Scripts/Credits.cs | 22 +++----- Scripts/Game/Command.cs | 31 +++++----- Scripts/Game/CommandInputArea.cs | 45 ++++++++------- Scripts/Game/CommandParser.cs | 25 ++++---- Scripts/Game/Commands/LookCommand.cs | 91 +++++++++++++++--------------- Scripts/Game/Game.cs | 33 ++++++----- Scripts/GodotExtensions/ArrayExtensions.cs | 11 ++-- Scripts/GodotExtensions/NodeExtensions.cs | 11 ++-- Scripts/Location.cs | 9 ++- Scripts/Menus/MainMenu.cs | 39 +++++++------ Scripts/Menus/MainMenuButtons.cs | 55 +++++++++--------- Scripts/Terminal/ButtonBlock.cs | 35 ++++++------ Scripts/Terminal/InputArea.cs | 18 +++--- Scripts/Terminal/MenuScreen.cs | 35 ++++++------ Scripts/Terminal/OutputArea.cs | 60 ++++++++++---------- Scripts/Terminal/OutputBlock.cs | 37 ++++++------ Scripts/Terminal/StatusArea.cs | 37 ++++++------ Texty.csproj | 2 +- addons/ClassExporter/Plugin.cs | 12 ++-- 20 files changed, 295 insertions(+), 341 deletions(-) diff --git a/Scenes/Menus/MainMenu.tscn b/Scenes/Menus/MainMenu.tscn index 8a19ecd..268cf6a 100644 --- a/Scenes/Menus/MainMenu.tscn +++ b/Scenes/Menus/MainMenu.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=4 format=3 uid="uid://bf4ll56nce4ps"] -[ext_resource type="PackedScene" path="res://Scenes/Terminal/MenuScreen.tscn" id="1"] +[ext_resource type="PackedScene" uid="uid://bhu0kaqbhdr74" path="res://Scenes/Terminal/MenuScreen.tscn" id="1"] [ext_resource type="PackedScene" uid="uid://j5l33sidadvf" path="res://Scenes/Menus/MainMenuButtons.tscn" id="2"] [ext_resource type="Script" path="res://Scripts/Menus/MainMenu.cs" id="3"] @@ -8,35 +8,18 @@ script = ExtResource("3") [node name="MenuScreen" parent="." instance=ExtResource("1")] -layout_mode = 3 -anchors_preset = 15 -grow_horizontal = 2 -grow_vertical = 2 Title = "TITLE_MAIN_MENU" -[node name="ScreenContainer" parent="MenuScreen" index="0"] -layout_mode = 1 -anchors_preset = 15 -grow_horizontal = 2 -grow_vertical = 2 - [node name="TopBar" parent="MenuScreen/ScreenContainer" index="0"] -layout_mode = 2 -offset_right = 1152.0 offset_bottom = 25.0 -theme_type_variation = &"InvertedPanelContainer" [node name="TitleArea" parent="MenuScreen/ScreenContainer/TopBar" index="0"] -layout_mode = 2 -offset_right = 1144.0 offset_bottom = 25.0 +grow_horizontal = 1 Title = "[center]TITLE_MAIN_MENU[/center]" [node name="Content" parent="MenuScreen/ScreenContainer" index="1"] -layout_mode = 2 offset_top = 25.0 -offset_right = 1152.0 -offset_bottom = 648.0 [node name="Buttons" parent="MenuScreen/ScreenContainer/Content" index="0" instance=ExtResource("2")] layout_mode = 2 @@ -51,13 +34,6 @@ offset_bottom = 623.0 grow_horizontal = 1 grow_vertical = 1 -[node name="BottomBar" parent="MenuScreen/ScreenContainer" index="2"] -layout_mode = 2 -offset_top = 648.0 -offset_right = 1152.0 -offset_bottom = 648.0 -theme_type_variation = &"InvertedPanelContainer" - [connection signal="CreditsButtonPressed" from="MenuScreen/ScreenContainer/Content/Buttons" to="." method="OnCreditsButtonPressed"] [connection signal="QuitButtonPressed" from="MenuScreen/ScreenContainer/Content/Buttons" to="." method="OnQuitButtonPressed"] [connection signal="StartButtonPressed" from="MenuScreen/ScreenContainer/Content/Buttons" to="." method="OnStartButtonPressed"] diff --git a/Scripts/Credits.cs b/Scripts/Credits.cs index 5432d02..11b7e08 100644 --- a/Scripts/Credits.cs +++ b/Scripts/Credits.cs @@ -1,16 +1,12 @@ using Godot; -namespace Texty.Scripts -{ - public partial class Credits : Node - { - public override void _UnhandledKeyInput(InputEvent @event) - { - if (@event is InputEventKey { Keycode: Key.Escape }) - { - GetTree().ChangeSceneToFile("res://Scenes/Menus/MainMenu.tscn"); - } - } - } -} +namespace Texty.Scripts; +public partial class Credits : Node +{ + public override void _UnhandledKeyInput(InputEvent @event) + { + if (@event is InputEventKey { Keycode: Key.Escape }) + GetTree().ChangeSceneToFile("res://Scenes/Menus/MainMenu.tscn"); + } +} \ No newline at end of file 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($"%{nameof(CommandParser)}"); + + public override void _Ready() { - [Signal] public delegate void CommandSubmittedEventHandler(Command command); - [Signal] public delegate void UnknownInputSubmittedEventHandler(string text); - - private CommandParser CommandParser => GetNodeOrNull($"%{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; } - - /// - /// 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) + 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 { - 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($"%{nameof(InputArea)}"); - private OutputArea OutputArea => GetNodeOrNull($"%{nameof(OutputArea)}"); - private StatusArea StatusArea => GetNodeOrNull($"%{nameof(StatusArea)}"); + private InputArea InputArea => GetNodeOrNull($"%{nameof(InputArea)}"); + private OutputArea OutputArea => GetNodeOrNull($"%{nameof(OutputArea)}"); + private StatusArea StatusArea => GetNodeOrNull($"%{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 diff --git a/Scripts/GodotExtensions/ArrayExtensions.cs b/Scripts/GodotExtensions/ArrayExtensions.cs index 2712232..ad72f94 100644 --- a/Scripts/GodotExtensions/ArrayExtensions.cs +++ b/Scripts/GodotExtensions/ArrayExtensions.cs @@ -2,13 +2,12 @@ using System; using Godot; using Godot.Collections; -namespace Texty.Scripts.GodotExtensions +namespace Texty.Scripts.GodotExtensions; + +public static class ArrayExtensions { - public static class ArrayExtensions + public static void ForEach<[MustBeVariant] T>(this Array array, Action action) { - public static void ForEach<[MustBeVariant] T>(this Array array, Action action) - { - foreach (var element in array) action.Invoke(element); - } + foreach (var element in array) action.Invoke(element); } } \ No newline at end of file diff --git a/Scripts/GodotExtensions/NodeExtensions.cs b/Scripts/GodotExtensions/NodeExtensions.cs index c361496..0acc012 100644 --- a/Scripts/GodotExtensions/NodeExtensions.cs +++ b/Scripts/GodotExtensions/NodeExtensions.cs @@ -2,13 +2,12 @@ using System.Linq; using Godot; using Godot.Collections; -namespace Texty.Scripts.GodotExtensions +namespace Texty.Scripts.GodotExtensions; + +public static class NodeExtensions { - public static class NodeExtensions + public static Array GetChildren<[MustBeVariant] T>(this Node node) { - public static Array GetChildren<[MustBeVariant] T>(this Node node) - { - return new Array(node.GetChildren().OfType()); - } + return new Array(node.GetChildren().OfType()); } } \ No newline at end of file diff --git a/Scripts/Location.cs b/Scripts/Location.cs index aaca980..8cebcf1 100644 --- a/Scripts/Location.cs +++ b/Scripts/Location.cs @@ -1,9 +1,8 @@ using Godot; -namespace Texty.Scripts +namespace Texty.Scripts; + +public partial class Location : Resource { - public partial class Location : Resource - { - [Export(PropertyHint.MultilineText)] public string FlavourText = string.Empty; - } + [Export(PropertyHint.MultilineText)] public string FlavourText = string.Empty; } \ No newline at end of file diff --git a/Scripts/Menus/MainMenu.cs b/Scripts/Menus/MainMenu.cs index 2991fec..5a7c3dd 100644 --- a/Scripts/Menus/MainMenu.cs +++ b/Scripts/Menus/MainMenu.cs @@ -1,26 +1,25 @@ using Godot; -namespace Texty.Scripts.Menus +namespace Texty.Scripts.Menus; + +public partial class MainMenu : Node { - public partial class MainMenu : Node - { - public override void _Ready() - { - } + public override void _Ready() + { + } - private void OnStartButtonPressed() - { - GetTree().ChangeSceneToFile("res://Scenes/Game/Game.tscn"); - } + private void OnStartButtonPressed() + { + GetTree().ChangeSceneToFile("res://Scenes/Game/Game.tscn"); + } - private void OnCreditsButtonPressed() - { - GetTree().ChangeSceneToFile("res://Scenes/Credits.tscn"); - } + private void OnCreditsButtonPressed() + { + GetTree().ChangeSceneToFile("res://Scenes/Credits.tscn"); + } - private void OnQuitButtonPressed() - { - GetTree().Quit(); - } - } -} + private void OnQuitButtonPressed() + { + GetTree().Quit(); + } +} \ No newline at end of file diff --git a/Scripts/Menus/MainMenuButtons.cs b/Scripts/Menus/MainMenuButtons.cs index 64e8914..e343135 100644 --- a/Scripts/Menus/MainMenuButtons.cs +++ b/Scripts/Menus/MainMenuButtons.cs @@ -2,38 +2,37 @@ using System; using Godot; using Texty.Scripts.Terminal; -namespace Texty.Scripts.Menus +namespace Texty.Scripts.Menus; + +public partial class MainMenuButtons : ButtonBlock { - public partial class MainMenuButtons : ButtonBlock - { - [Signal] public delegate void CreditsButtonPressedEventHandler(); - [Signal] public delegate void QuitButtonPressedEventHandler(); - [Signal] public delegate void StartButtonPressedEventHandler(); + [Signal] public delegate void CreditsButtonPressedEventHandler(); + [Signal] public delegate void QuitButtonPressedEventHandler(); + [Signal] public delegate void StartButtonPressedEventHandler(); - protected override void OnButtonPressed(int index) + protected override void OnButtonPressed(int index) + { + if (!Enum.IsDefined(typeof(Buttons), 1)) return; + switch ((Buttons)index) { - if (!Enum.IsDefined(typeof(Buttons), 1)) return; - switch ((Buttons)index) - { - case Buttons.Start: - EmitSignal(SignalName.StartButtonPressed); - break; - case Buttons.Credits: - EmitSignal(SignalName.CreditsButtonPressed); - break; - case Buttons.Quit: - EmitSignal(SignalName.QuitButtonPressed); - break; - default: - throw new ArgumentOutOfRangeException(nameof(index), index, "Button not implemented!"); - } + case Buttons.Start: + EmitSignal(SignalName.StartButtonPressed); + break; + case Buttons.Credits: + EmitSignal(SignalName.CreditsButtonPressed); + break; + case Buttons.Quit: + EmitSignal(SignalName.QuitButtonPressed); + break; + default: + throw new ArgumentOutOfRangeException(nameof(index), index, "Button not implemented!"); } + } - private enum Buttons - { - Start = 0, - Credits = 1, - Quit = 2 - } + private enum Buttons + { + Start = 0, + Credits = 1, + Quit = 2 } } \ No newline at end of file diff --git a/Scripts/Terminal/ButtonBlock.cs b/Scripts/Terminal/ButtonBlock.cs index ef25f8c..ff0e83e 100644 --- a/Scripts/Terminal/ButtonBlock.cs +++ b/Scripts/Terminal/ButtonBlock.cs @@ -2,27 +2,26 @@ using System.Linq; using Godot; using Texty.Scripts.GodotExtensions; -namespace Texty.Scripts.Terminal +namespace Texty.Scripts.Terminal; + +public partial class ButtonBlock : MarginContainer { - public partial class ButtonBlock : MarginContainer - { - private VBoxContainer Buttons => GetNodeOrNull($"%{nameof(Buttons)}"); + private VBoxContainer Buttons => GetNodeOrNull($"%{nameof(Buttons)}"); - public override void _Ready() - { - ConnectButtons(); - Buttons.GetChildren