3
3
using System . Collections . ObjectModel ;
4
4
using System . Threading . Tasks ;
5
5
using Godot ;
6
+ using GoDough . Runtime ;
6
7
using Microsoft . Extensions . Logging ;
7
8
8
9
// TODO: OnSceneChanging Event - Scene Change blocker?
@@ -17,6 +18,9 @@ public class SceneManagementService<TSceneEnum>
17
18
private readonly Dictionary < TSceneEnum , string > _registeredScenes = new Dictionary < TSceneEnum , string > ( ) ;
18
19
private readonly ILogger < SceneManagementService < TSceneEnum > > _logger ;
19
20
private readonly IAppHostNodeProvider _appHostNodeProvider ;
21
+ private readonly IGodotApi _godotApi ;
22
+
23
+ private readonly GoDough . Threading . Dispatcher _dispatcher ;
20
24
#endregion
21
25
22
26
#region Properties
@@ -32,7 +36,11 @@ public ReadOnlyDictionary<TSceneEnum, string> RegisteredSceneFiles {
32
36
#region Ctor
33
37
public SceneManagementService (
34
38
ILogger < SceneManagementService < TSceneEnum > > logger ,
35
- IAppHostNodeProvider appHostNodeProvider ) => ( _logger , _appHostNodeProvider ) = ( logger , appHostNodeProvider ) ;
39
+ IGodotApi godotApi ,
40
+ GoDough . Threading . Dispatcher dispatcher ,
41
+ IAppHostNodeProvider appHostNodeProvider ) =>
42
+ ( _dispatcher , _godotApi , _logger , _appHostNodeProvider ) =
43
+ ( dispatcher , godotApi , logger , appHostNodeProvider ) ;
36
44
#endregion
37
45
38
46
#region Events
@@ -58,14 +66,38 @@ public SceneManagementService<TSceneEnum> RegisterSceneFile(TSceneEnum sceneKey,
58
66
return this ;
59
67
}
60
68
61
- public void LoadScene ( TSceneEnum sceneKey ) {
69
+ public async Task LoadScene ( TSceneEnum sceneKey , PackedScene loadingScreen = null ) {
62
70
if ( ! this . _registeredScenes . ContainsKey ( sceneKey ) ) {
63
71
throw new KeyNotFoundException (
64
72
String . Format (
65
73
"Could not find Scene with key '{0}'" ,
66
74
Enum . GetName ( typeof ( TSceneEnum ) , sceneKey ) ) ) ;
67
75
}
68
76
77
+ var appHostNode = this . _appHostNodeProvider . GetNode ( ) ;
78
+ ProgressBar progressBar = null ;
79
+
80
+ Action < double > progress = loadingProgress =>
81
+ this . _dispatcher . Invoke ( ( ) => {
82
+ if ( progressBar != null ) {
83
+ progressBar . Value = loadingProgress ;
84
+ }
85
+ } ) ;
86
+
87
+ if ( loadingScreen != null ) {
88
+ appHostNode
89
+ . GetTree ( )
90
+ . ChangeSceneToPacked ( loadingScreen ) ;
91
+
92
+ double progressValue = 0 ;
93
+
94
+ while ( progressBar == null ) {
95
+ await appHostNode . ToSignal ( appHostNode . GetTree ( ) , "process_frame" ) ;
96
+ progressBar = appHostNode . GetTree ( ) . CurrentScene . GetNode < ProgressBar > ( "%ProgressBar" ) ;
97
+ progressBar . Value = progressValue ;
98
+ }
99
+ }
100
+
69
101
var fileName = this . _registeredScenes [ sceneKey ] ;
70
102
this . _logger . LogInformation ( "Loading Scene '{0}' from '{1}'" ,
71
103
Enum . GetName ( typeof ( TSceneEnum ) , sceneKey ) ,
@@ -75,8 +107,9 @@ public void LoadScene(TSceneEnum sceneKey) {
75
107
this . _logger . LogInformation ( "_appHostNodeProvider null" ) ;
76
108
}
77
109
78
- var appHostNode = this . _appHostNodeProvider . GetNode ( ) ;
79
- appHostNode . GetTree ( ) . ChangeSceneToFile ( fileName ) ;
110
+ var loadingTask = this . _godotApi . LoadSceneAsync ( fileName , progress ) ;
111
+
112
+ appHostNode . GetTree ( ) . ChangeSceneToPacked ( await loadingTask ) ;
80
113
81
114
var task = this . WaitForNextFrame ( appHostNode , ( ) => {
82
115
this . CurrentScene = sceneKey ;
0 commit comments