Restructure loading of game
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using Chickensoft.AutoInject;
|
||||
using Chickensoft.Collections;
|
||||
using Chickensoft.GodotNodeInterfaces;
|
||||
using Chickensoft.Introspection;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using SimpleInjector.Lifestyles;
|
||||
using System.Linq;
|
||||
using Zennysoft.Game.Abstractions;
|
||||
using Zennysoft.Ma.Adapter;
|
||||
|
||||
@@ -17,7 +20,11 @@ public partial class App : Node, IApp
|
||||
|
||||
public const string GAME_SCENE_PATH = "res://src/game/Game.tscn";
|
||||
|
||||
public IGame Game { get; set; } = default!;
|
||||
public const string ENEMY_VIEWER_PATH = "res://src/data_viewer/DataViewer.tscn";
|
||||
|
||||
[Node] private MainMenu MainMenu { get; set; } = default!;
|
||||
|
||||
[Node] private Control Loading { get; set; } = default!;
|
||||
|
||||
public IInstantiator Instantiator { get; set; } = default!;
|
||||
|
||||
@@ -27,6 +34,10 @@ public partial class App : Node, IApp
|
||||
public IAppLogic AppLogic { get; set; } = default!;
|
||||
public AppLogic.IBinding AppBinding { get; set; } = default!;
|
||||
|
||||
private Array _progress;
|
||||
|
||||
private AutoProp<bool> _loaded = new(false);
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
var container = new SimpleInjector.Container();
|
||||
@@ -34,6 +45,11 @@ public partial class App : Node, IApp
|
||||
container.RegisterSingleton<IAppRepo, AppRepo>();
|
||||
container.RegisterSingleton<IAppLogic, AppLogic>();
|
||||
|
||||
MainMenu.NewGame += OnNewGame;
|
||||
MainMenu.LoadGame += OnLoadGame;
|
||||
MainMenu.Quit += OnQuit;
|
||||
_loaded.Sync += OnGameLoaded;
|
||||
|
||||
Instantiator = new Instantiator(GetTree());
|
||||
|
||||
AppRepo = container.GetInstance<IAppRepo>();
|
||||
@@ -43,9 +59,22 @@ public partial class App : Node, IApp
|
||||
AppLogic.Set(new AppLogic.Data());
|
||||
|
||||
Input.MouseMode = Input.MouseModeEnum.Visible;
|
||||
_progress = [];
|
||||
this.Provide();
|
||||
}
|
||||
|
||||
private void OnGameLoaded(bool gameLoaded)
|
||||
{
|
||||
if (gameLoaded)
|
||||
{
|
||||
Loading.Hide();
|
||||
var gameScene = (PackedScene)ResourceLoader.LoadThreadedGet(GAME_SCENE_PATH);
|
||||
var game = gameScene.Instantiate();
|
||||
AddChild(game);
|
||||
Instantiator.SceneTree.Paused = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnReady()
|
||||
{
|
||||
AppBinding = AppLogic.Bind();
|
||||
@@ -59,7 +88,8 @@ public partial class App : Node, IApp
|
||||
})
|
||||
.Handle((in AppLogic.Output.SetupGameScene _) =>
|
||||
{
|
||||
Instantiator.SceneTree.Paused = false;
|
||||
ResourceLoader.LoadThreadedRequest(GAME_SCENE_PATH, useSubThreads: true, cacheMode: ResourceLoader.CacheMode.Ignore);
|
||||
MainMenu.Hide();
|
||||
})
|
||||
.Handle((in AppLogic.Output.ShowMainMenu _) =>
|
||||
{
|
||||
@@ -69,16 +99,28 @@ public partial class App : Node, IApp
|
||||
})
|
||||
.Handle((in AppLogic.Output.StartLoadingSaveFile _) =>
|
||||
{
|
||||
Game.SaveFileLoaded += OnSaveFileLoaded;
|
||||
Game.LoadExistingGame();
|
||||
})
|
||||
.Handle((in AppLogic.Output.EnemyViewerOpened _) =>
|
||||
{
|
||||
Instantiator.LoadAndInstantiate<Node>(ENEMY_VIEWER_PATH);
|
||||
})
|
||||
.Handle((in AppLogic.Output.ExitGame _) =>
|
||||
{
|
||||
GetTree().Quit();
|
||||
});
|
||||
|
||||
|
||||
AppLogic.Start();
|
||||
MainMenu.Show();
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (!_loaded.Value)
|
||||
{
|
||||
ResourceLoader.LoadThreadedGetStatus(GAME_SCENE_PATH, _progress);
|
||||
if ((double)_progress.Single() == 1)
|
||||
_loaded.OnNext(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnNewGame() => AppLogic.Input(new AppLogic.Input.NewGame());
|
||||
@@ -89,7 +131,6 @@ public partial class App : Node, IApp
|
||||
|
||||
public void OnSaveFileLoaded()
|
||||
{
|
||||
Game.SaveFileLoaded -= OnSaveFileLoaded;
|
||||
AppLogic.Input(new AppLogic.Input.SaveFileLoaded());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user