diff --git a/Autoloads/GameSettings.cs b/Autoloads/GameSettings.cs index 822ed2e..d10432e 100644 --- a/Autoloads/GameSettings.cs +++ b/Autoloads/GameSettings.cs @@ -1,6 +1,63 @@ +using Newtonsoft.Json; + namespace CMSGame { public partial class GameSettings : Node + { + public BattleSettings BattleSettings; + + protected string BattleSettingsSavePath = new GodotPath("user://Settings/BattleSettings.json"); + + public override void _Ready() + { + MakeDirectories(); + BattleSettings = GetSettings(BattleSettingsSavePath); + } + + public override void _ExitTree() + { + SaveSettings(BattleSettingsSavePath); + } + + private void MakeDirectories() + { + DirAccess.MakeDirRecursiveAbsolute("user://Settings/"); + } + + private SettingsType GetSettings(string path) where SettingsType : new() + { + string settings_text = ReadFileAsString(path); + var settings = JsonConvert.DeserializeObject(settings_text) ?? new SettingsType(); + return settings; + } + + private string ReadFileAsString(string path) + { + if (FileAccess.FileExists(path)) + { + return FileAccess.Open(path, FileAccess.ModeFlags.Read).GetAsText(); + } + return "null"; + } + + private void SaveSettings(string path) + { + string settings_text = JsonConvert.SerializeObject(BattleSettings); + var file = FileAccess.Open(path, FileAccess.ModeFlags.Write); + file.StoreString(settings_text); + file.Dispose(); + } + } + + public class SettingsBase + { + public override string ToString() + { + return JsonConvert.SerializeObject(this); + } + } + + public class BattleSettings : SettingsBase { public bool PauseBattleWhenCharacterIsSelected = true; } diff --git a/Components/PauseMenu.cs b/Components/PauseMenu.cs index 6d022af..ee74f71 100644 --- a/Components/PauseMenu.cs +++ b/Components/PauseMenu.cs @@ -1,19 +1,10 @@ namespace CMSGame { - public partial class PauseMenu : Control + public partial class PauseMenu : Popup { - private GameSettings _settings; - - public override void _Ready() + public void On_ButtonExit_Pressed() { - _settings = GetNode("/root/GameSettings"); - GetNode("%CheckBoxPauseBattleWhenCharacterIsSelected").ToggleMode = _settings.PauseBattleWhenCharacterIsSelected; - } - - public void On_CheckBoxPauseBattleWhenCharacterIsSelected_Toggled() - { - _settings.PauseBattleWhenCharacterIsSelected = false; + Hide(); } } - } diff --git a/Components/PauseMenu.tscn b/Components/PauseMenu.tscn index eb94fb2..7099cc3 100644 --- a/Components/PauseMenu.tscn +++ b/Components/PauseMenu.tscn @@ -1,37 +1,33 @@ -[gd_scene load_steps=2 format=3 uid="uid://c1c87rr8eubhg"] +[gd_scene load_steps=3 format=3 uid="uid://c1c87rr8eubhg"] [ext_resource type="Script" path="res://Components/PauseMenu.cs" id="1_p1e8d"] +[ext_resource type="PackedScene" uid="uid://blk2uswpo2a7k" path="res://Components/SettingsMenu.tscn" id="2_smral"] -[node name="PauseMenu" type="Control"] -layout_mode = 3 -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 +[node name="PauseMenu" type="Popup"] +size = Vector2i(342, 289) +visible = true script = ExtResource("1_p1e8d") [node name="VBoxContainer" type="VBoxContainer" parent="."] -layout_mode = 1 anchors_preset = 8 anchor_left = 0.5 anchor_top = 0.5 anchor_right = 0.5 anchor_bottom = 0.5 -offset_left = -20.0 -offset_top = -20.0 -offset_right = 20.0 -offset_bottom = 20.0 +offset_left = -86.0 +offset_top = -33.0 +offset_right = 86.0 +offset_bottom = 33.0 grow_horizontal = 2 grow_vertical = 2 +size_flags_horizontal = 4 +size_flags_vertical = 4 -[node name="CheckBoxPauseBattleWhenCharacterIsSelected" type="CheckBox" parent="VBoxContainer"] -unique_name_in_owner = true +[node name="VBoxContainer" parent="VBoxContainer" instance=ExtResource("2_smral")] layout_mode = 2 -text = "选中角色时暂停战斗" [node name="ButtonExit" type="Button" parent="VBoxContainer"] layout_mode = 2 -text = "退出" +text = "确认" -[connection signal="toggled" from="VBoxContainer/CheckBoxPauseBattleWhenCharacterIsSelected" to="." method="_On_CheckBoxPauseBattleWhenCharacterIsSelected_Toggled"] +[connection signal="pressed" from="VBoxContainer/ButtonExit" to="." method="On_ButtonExit_Pressed"] diff --git a/Components/SettingsMenu.cs b/Components/SettingsMenu.cs new file mode 100644 index 0000000..9e27cbd --- /dev/null +++ b/Components/SettingsMenu.cs @@ -0,0 +1,27 @@ +namespace CMSGame +{ + public partial class SettingsMenu : VBoxContainer + { + private GameSettings _settings; + + private CheckBox _checkBoxPauseBattleWhenCharacterIsSelected; + + public override void _Ready() + { + InitializeComponents(); + + _checkBoxPauseBattleWhenCharacterIsSelected.ButtonPressed = _settings.BattleSettings.PauseBattleWhenCharacterIsSelected; + } + + private void InitializeComponents() + { + _settings = GetNode("/root/GameSettings"); + _checkBoxPauseBattleWhenCharacterIsSelected = GetNode("%CheckBoxPauseBattleWhenCharacterIsSelected"); + } + + public void On_CheckBoxPauseBattleWhenCharacterIsSelected_Toggled(bool pressed) + { + _settings.BattleSettings.PauseBattleWhenCharacterIsSelected = pressed; + } + } +} diff --git a/Components/SettingsMenu.tscn b/Components/SettingsMenu.tscn new file mode 100644 index 0000000..5f26dd1 --- /dev/null +++ b/Components/SettingsMenu.tscn @@ -0,0 +1,13 @@ +[gd_scene load_steps=2 format=3 uid="uid://blk2uswpo2a7k"] + +[ext_resource type="Script" path="res://Components/SettingsMenu.cs" id="1_b12ly"] + +[node name="VBoxContainer" type="VBoxContainer"] +script = ExtResource("1_b12ly") + +[node name="CheckBoxPauseBattleWhenCharacterIsSelected" type="CheckBox" parent="."] +unique_name_in_owner = true +layout_mode = 2 +text = "选中角色时暂停战斗" + +[connection signal="toggled" from="CheckBoxPauseBattleWhenCharacterIsSelected" to="." method="On_CheckBoxPauseBattleWhenCharacterIsSelected_Toggled"] diff --git a/Helpers/FileHelper.cs b/Helpers/FileHelper.cs new file mode 100644 index 0000000..a096b8a --- /dev/null +++ b/Helpers/FileHelper.cs @@ -0,0 +1,10 @@ +namespace CMSGame +{ + public static class FileHelper + { + public static string GodotPathToSystemPath(string godotPath) + { + return ProjectSettings.GlobalizePath(godotPath); + } + } +} diff --git a/Models/GodotPath.cs b/Models/GodotPath.cs new file mode 100644 index 0000000..8716631 --- /dev/null +++ b/Models/GodotPath.cs @@ -0,0 +1,18 @@ +using System.Runtime.CompilerServices; + +namespace CMSGame +{ + public class GodotPath + { + private readonly string _path = string.Empty; + + public GodotPath(string godotPath) + { + _path = ProjectSettings.GlobalizePath(godotPath); + } + + public override string ToString() => _path; + + public static implicit operator string(GodotPath g) => g.ToString(); + } +} diff --git a/Scenes/MainScene.cs b/Scenes/MainScene.cs index 50f4949..c9586bf 100644 --- a/Scenes/MainScene.cs +++ b/Scenes/MainScene.cs @@ -2,19 +2,19 @@ namespace CMSGame { public partial class MainScene : Control { - public static void On_ButtonBattleDemo_Pressed() + public void On_ButtonBattleDemo_Pressed() { - + GetTree().ChangeSceneToFile("res://Scenes/BattleScene.tscn"); } - public static void On_ButtonSettings_Pressed() + public void On_ButtonSettings_Pressed() { - + GetNode("%PauseMenu").PopupCentered(); } - public static void On_ButtonExit_Pressed() + public void On_ButtonExit_Pressed() { - + GetTree().Quit(); } } } diff --git a/Scenes/MainScene.tscn b/Scenes/MainScene.tscn index 9234658..a159672 100644 --- a/Scenes/MainScene.tscn +++ b/Scenes/MainScene.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=2 format=3 uid="uid://dxnmltew81ny0"] +[gd_scene load_steps=3 format=3 uid="uid://dxnmltew81ny0"] [ext_resource type="Script" path="res://Scenes/MainScene.cs" id="1_kso8c"] +[ext_resource type="PackedScene" uid="uid://c1c87rr8eubhg" path="res://Components/PauseMenu.tscn" id="2_txg46"] [node name="MainScene" type="Control"] layout_mode = 3 @@ -11,10 +12,15 @@ grow_horizontal = 2 grow_vertical = 2 script = ExtResource("1_kso8c") -[node name="Label" type="Label" parent="."] -layout_mode = 0 -offset_right = 40.0 -offset_bottom = 23.0 +[node name="LabelGameTitle" type="Label" parent="."] +layout_mode = 1 +anchors_preset = 5 +anchor_left = 0.5 +anchor_right = 0.5 +offset_left = -137.5 +offset_right = 137.5 +offset_bottom = 26.0 +grow_horizontal = 2 text = "The Country of Mountains and Seas" [node name="VBoxContainer" type="VBoxContainer" parent="."] @@ -43,4 +49,10 @@ text = "游戏设置" layout_mode = 2 text = "退出" +[node name="PauseMenu" parent="." instance=ExtResource("2_txg46")] +unique_name_in_owner = true +visible = false + +[connection signal="pressed" from="VBoxContainer/ButtonBattleDemo" to="." method="On_ButtonBattleDemo_Pressed"] [connection signal="pressed" from="VBoxContainer/ButtonSettings" to="." method="On_ButtonSettings_Pressed"] +[connection signal="pressed" from="VBoxContainer/ButtonExit" to="." method="On_ButtonExit_Pressed"]