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());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user