Add options menu persistence
This commit is contained in:
@@ -5,8 +5,12 @@ using Chickensoft.Introspection;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using SimpleInjector.Lifestyles;
|
||||
using System.IO;
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Zennysoft.Game.Abstractions;
|
||||
using Zennysoft.Game.Implementation;
|
||||
using Zennysoft.Ma.Adapter;
|
||||
|
||||
namespace Zennysoft.Game.Ma;
|
||||
@@ -26,6 +30,8 @@ public partial class App : Node, IApp
|
||||
|
||||
[Node] private LoadingScreen LoadingScreen { get; set; } = default!;
|
||||
|
||||
[Node] private OptionsMenu OptionsMenu { get; set; }
|
||||
|
||||
public IInstantiator Instantiator { get; set; } = default!;
|
||||
|
||||
IAppRepo IProvide<IAppRepo>.Value() => AppRepo;
|
||||
@@ -35,25 +41,43 @@ public partial class App : Node, IApp
|
||||
public AppLogic.IBinding AppBinding { get; set; } = default!;
|
||||
|
||||
private Array _progress;
|
||||
private SimpleInjector.Container _container;
|
||||
|
||||
private AutoProp<string> _loadedScene = new(string.Empty);
|
||||
private bool _loadingGame = false;
|
||||
private bool _loadingEnemyViewer = false;
|
||||
private string _optionsSavePath = string.Empty;
|
||||
private ISaveFileManager _saveFileManager;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
var container = new SimpleInjector.Container();
|
||||
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
|
||||
container.RegisterSingleton<IAppRepo, AppRepo>();
|
||||
container.RegisterSingleton<IAppLogic, AppLogic>();
|
||||
MainMenu.Hide();
|
||||
_container = new SimpleInjector.Container();
|
||||
_container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
|
||||
_container.RegisterSingleton<IAppRepo, AppRepo>();
|
||||
_container.RegisterSingleton<IAppLogic, AppLogic>();
|
||||
_container.RegisterSingleton<IFileSystem, FileSystem>();
|
||||
_container.RegisterSingleton<ISaveFileManager, SaveFileManager>();
|
||||
|
||||
_saveFileManager = _container.GetInstance<ISaveFileManager>();
|
||||
_optionsSavePath = $"{OS.GetUserDataDir()}/options.json";
|
||||
|
||||
Task.Run(() => _saveFileManager.ReadFromFile<OptionsData>(_optionsSavePath).ContinueWith((data) =>
|
||||
{
|
||||
if (data.IsCompletedSuccessfully)
|
||||
OptionsMenu.CallDeferred("Load", data.Result);
|
||||
}));
|
||||
|
||||
MainMenu.StartGame += OnStartGame;
|
||||
MainMenu.EnemyViewer += OnEnemyViewer;
|
||||
MainMenu.Options += OnOptions;
|
||||
MainMenu.Quit += OnQuit;
|
||||
_loadedScene.Changed += OnGameLoaded;
|
||||
|
||||
AppRepo = container.GetInstance<IAppRepo>();
|
||||
AppLogic = container.GetInstance<IAppLogic>();
|
||||
OptionsMenu.OptionsMenuExited += OptionsMenu_OptionsMenuExited;
|
||||
|
||||
AppRepo = _container.GetInstance<IAppRepo>();
|
||||
AppLogic = _container.GetInstance<IAppLogic>();
|
||||
|
||||
AppLogic.Set(AppRepo);
|
||||
AppLogic.Set(new AppLogic.Data());
|
||||
@@ -63,6 +87,14 @@ public partial class App : Node, IApp
|
||||
this.Provide();
|
||||
}
|
||||
|
||||
private async void OptionsMenu_OptionsMenuExited()
|
||||
{
|
||||
var saveFileManager = _container.GetInstance<ISaveFileManager>();
|
||||
await saveFileManager.WriteToFile(OptionsMenu.OptionsData, _optionsSavePath);
|
||||
MainMenu.Show();
|
||||
OptionsMenu.Hide();
|
||||
}
|
||||
|
||||
private void OnGameLoaded(string sceneName)
|
||||
{
|
||||
LoadingScreen.Hide();
|
||||
@@ -135,7 +167,11 @@ public partial class App : Node, IApp
|
||||
|
||||
private void OnEnemyViewer() => AppLogic.Input(new AppLogic.Input.EnemyViewerOpened());
|
||||
|
||||
private void OnLoadGame() => AppLogic.Input(new AppLogic.Input.LoadGame());
|
||||
private async void OnOptions()
|
||||
{
|
||||
OptionsMenu.Show();
|
||||
MainMenu.Hide();
|
||||
}
|
||||
|
||||
public void OnQuit() => AppLogic.Input(new AppLogic.Input.QuitGame());
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://cagfc5ridmteu"]
|
||||
[gd_scene load_steps=5 format=3 uid="uid://cagfc5ridmteu"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://d1f8blk5ucqvq" path="res://src/app/App.cs" id="1_rt73h"]
|
||||
[ext_resource type="PackedScene" uid="uid://rfvnddfqufho" path="res://src/menu/MainMenu.tscn" id="2_1uiag"]
|
||||
[ext_resource type="PackedScene" uid="uid://drkl3btdy6uxj" path="res://src/options/OptionsMenu.tscn" id="2_v0mgf"]
|
||||
[ext_resource type="PackedScene" uid="uid://cpjlj7kxdhv16" path="res://src/menu/LoadingScreen.tscn" id="3_3st5l"]
|
||||
|
||||
[node name="App" type="Node"]
|
||||
@@ -11,6 +12,9 @@ script = ExtResource("1_rt73h")
|
||||
[node name="LoadingScreen" parent="." instance=ExtResource("3_3st5l")]
|
||||
unique_name_in_owner = true
|
||||
|
||||
[node name="MainMenu" parent="." instance=ExtResource("2_1uiag")]
|
||||
[node name="OptionsMenu" parent="." instance=ExtResource("2_v0mgf")]
|
||||
unique_name_in_owner = true
|
||||
visible = false
|
||||
|
||||
[node name="MainMenu" parent="." instance=ExtResource("2_1uiag")]
|
||||
unique_name_in_owner = true
|
||||
|
||||
Reference in New Issue
Block a user