Restructure loading of game

This commit is contained in:
2025-09-25 02:50:55 -07:00
parent e7f5da5b1a
commit 84f0f8338f
266 changed files with 696 additions and 675 deletions

View File

@@ -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());
}

View File

@@ -1,10 +1,49 @@
[gd_scene load_steps=3 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://33ek675mfb5n" path="res://src/game/Game.tscn" id="2_1uiag"]
[ext_resource type="PackedScene" uid="uid://rfvnddfqufho" path="res://src/menu/MainMenu.tscn" id="2_1uiag"]
[ext_resource type="FontFile" uid="uid://cm8j5vcdop5x0" path="res://src/ui/fonts/Mrs-Eaves-OT-Roman_31443.ttf" id="2_3st5l"]
[sub_resource type="LabelSettings" id="LabelSettings_v0mgf"]
font = ExtResource("2_3st5l")
font_size = 100
font_color = Color(0.737255, 0.705882, 0.690196, 1)
[node name="App" type="Node"]
process_mode = 3
script = ExtResource("1_rt73h")
[node name="Game" parent="." instance=ExtResource("2_1uiag")]
[node name="Loading" type="Control" parent="."]
unique_name_in_owner = true
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="ColorRect" type="ColorRect" parent="Loading"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0.137255, 0.121569, 0.12549, 1)
[node name="CenterContainer" type="CenterContainer" parent="Loading"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="Label" type="Label" parent="Loading/CenterContainer"]
layout_mode = 2
text = "Loading..."
label_settings = SubResource("LabelSettings_v0mgf")
[node name="MainMenu" parent="." instance=ExtResource("2_1uiag")]
unique_name_in_owner = true
visible = false