204 lines
6.0 KiB
C#
204 lines
6.0 KiB
C#
using Chickensoft.AutoInject;
|
|
using Chickensoft.Collections;
|
|
using Chickensoft.GodotNodeInterfaces;
|
|
using Chickensoft.Introspection;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
using NathanHoad;
|
|
using SimpleInjector.Lifestyles;
|
|
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;
|
|
|
|
public interface IApp : INode, IProvide<IAppRepo>;
|
|
|
|
[Meta(typeof(IAutoNode))]
|
|
public partial class App : Node, IApp
|
|
{
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
public const string GAME_SCENE_PATH = "res://src/game/Game.tscn";
|
|
|
|
public const string ENEMY_VIEWER_PATH = "res://src/data_viewer/DataViewer.tscn";
|
|
|
|
[Node] private MainMenu MainMenu { get; set; } = default!;
|
|
|
|
[Node] private LoadingScreen LoadingScreen { get; set; } = default!;
|
|
|
|
[Node] private OptionsMenu OptionsMenu { get; set; }
|
|
|
|
public IInstantiator Instantiator { get; set; } = default!;
|
|
|
|
IAppRepo IProvide<IAppRepo>.Value() => AppRepo;
|
|
|
|
public IAppRepo AppRepo { get; set; } = default!;
|
|
public IAppLogic AppLogic { get; set; } = default!;
|
|
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 string _controllerSavePath = string.Empty;
|
|
private ISaveFileManager _saveFileManager;
|
|
|
|
public void Initialize()
|
|
{
|
|
_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";
|
|
_controllerSavePath = $"{OS.GetUserDataDir()}/controls.json";
|
|
|
|
Task.Run(() => _saveFileManager.ReadFromFile<OptionsData>(_optionsSavePath).ContinueWith((data) =>
|
|
{
|
|
if (data.IsCompletedSuccessfully)
|
|
OptionsMenu.CallDeferred("Load", data.Result);
|
|
}));
|
|
|
|
Task.Run(() => _saveFileManager.ReadFromFile<string>(_controllerSavePath).ContinueWith((data) =>
|
|
{
|
|
if (data.IsCompletedSuccessfully)
|
|
OptionsMenu.Controller.CallDeferred(nameof(OptionsMenu.Controller.LoadControllerInput), data.Result);
|
|
}));
|
|
|
|
MainMenu.StartGame += OnStartGame;
|
|
MainMenu.EnemyViewer += OnEnemyViewer;
|
|
MainMenu.Options += OnOptions;
|
|
MainMenu.Quit += OnQuit;
|
|
_loadedScene.Changed += OnGameLoaded;
|
|
|
|
OptionsMenu.OptionsMenuExited += OptionsMenu_OptionsMenuExited;
|
|
|
|
AppRepo = _container.GetInstance<IAppRepo>();
|
|
AppLogic = _container.GetInstance<IAppLogic>();
|
|
|
|
AppLogic.Set(AppRepo);
|
|
AppLogic.Set(new AppLogic.Data());
|
|
|
|
Input.MouseMode = Input.MouseModeEnum.Visible;
|
|
_progress = [];
|
|
this.Provide();
|
|
}
|
|
|
|
private async void OptionsMenu_OptionsMenuExited()
|
|
{
|
|
var saveFileManager = _container.GetInstance<ISaveFileManager>();
|
|
await saveFileManager.WriteToFile(OptionsMenu.OptionsData, _optionsSavePath);
|
|
var controllerOutput = InputHelper.SerializeInputsForActions();
|
|
await saveFileManager.WriteToFile(controllerOutput, _controllerSavePath);
|
|
OptionsMenu.Hide();
|
|
MainMenu.OptionsButton.GrabFocus();
|
|
}
|
|
|
|
private void OnGameLoaded(string sceneName)
|
|
{
|
|
LoadingScreen.Hide();
|
|
var scene = (PackedScene)ResourceLoader.LoadThreadedGet(sceneName);
|
|
var node = scene.Instantiate();
|
|
AddChild(node);
|
|
}
|
|
|
|
public void OnReady()
|
|
{
|
|
AppBinding = AppLogic.Bind();
|
|
|
|
AppBinding
|
|
.Handle((in AppLogic.Output.ShowSplashScreen _) =>
|
|
{
|
|
})
|
|
.Handle((in AppLogic.Output.HideSplashScreen _) =>
|
|
{
|
|
})
|
|
.Handle((in AppLogic.Output.SetupGameScene _) =>
|
|
{
|
|
ResourceLoader.LoadThreadedRequest(GAME_SCENE_PATH);
|
|
_loadingGame = true;
|
|
MainMenu.ReleaseFocus();
|
|
MainMenu.Hide();
|
|
})
|
|
.Handle((in AppLogic.Output.ShowMainMenu _) =>
|
|
{
|
|
})
|
|
.Handle((in AppLogic.Output.ShowGame _) =>
|
|
{
|
|
})
|
|
.Handle((in AppLogic.Output.StartLoadingSaveFile _) =>
|
|
{
|
|
})
|
|
.Handle((in AppLogic.Output.EnemyViewerOpened _) =>
|
|
{
|
|
ResourceLoader.LoadThreadedRequest(ENEMY_VIEWER_PATH);
|
|
_loadingEnemyViewer = true;
|
|
MainMenu.Hide();
|
|
})
|
|
.Handle((in AppLogic.Output.ExitGame _) =>
|
|
{
|
|
GetTree().Quit();
|
|
});
|
|
|
|
AppLogic.Start();
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (_loadingGame)
|
|
{
|
|
ResourceLoader.LoadThreadedGetStatus(GAME_SCENE_PATH, _progress);
|
|
LoadingScreen.ProgressBar.Value = (double)_progress.Single();
|
|
if ((double)_progress.Single() == 1)
|
|
_loadedScene.OnNext(GAME_SCENE_PATH);
|
|
}
|
|
|
|
if (_loadingEnemyViewer)
|
|
{
|
|
ResourceLoader.LoadThreadedGetStatus(ENEMY_VIEWER_PATH, _progress);
|
|
LoadingScreen.ProgressBar.Value = (double)_progress.Single();
|
|
if ((double)_progress.Single() == 1)
|
|
_loadedScene.OnNext(ENEMY_VIEWER_PATH);
|
|
}
|
|
}
|
|
|
|
public void OnStartGame() => AppLogic.Input(new AppLogic.Input.NewGame());
|
|
|
|
private void OnEnemyViewer() => AppLogic.Input(new AppLogic.Input.EnemyViewerOpened());
|
|
|
|
private async void OnOptions()
|
|
{
|
|
OptionsMenu.Show();
|
|
OptionsMenu.MasterVolumeSlider.GrabFocus();
|
|
}
|
|
|
|
public void OnQuit() => AppLogic.Input(new AppLogic.Input.QuitGame());
|
|
|
|
public void OnSaveFileLoaded()
|
|
{
|
|
AppLogic.Input(new AppLogic.Input.SaveFileLoaded());
|
|
}
|
|
|
|
public void OnExitTree()
|
|
{
|
|
AppLogic.Stop();
|
|
AppBinding.Dispose();
|
|
AppRepo.Dispose();
|
|
|
|
MainMenu.StartGame -= OnStartGame;
|
|
MainMenu.EnemyViewer -= OnEnemyViewer;
|
|
MainMenu.Quit -= OnQuit;
|
|
_loadedScene.Changed -= OnGameLoaded;
|
|
}
|
|
}
|