11use crate :: { save:: SaveFile , GameState , Handles , MainCamera } ;
22use bevy:: { asset:: LoadState , prelude:: * } ;
3+ use bevy_pipelines_ready:: { PipelinesReady , PipelinesReadyPlugin } ;
4+ use bevy_prototype_lyon:: prelude:: * ;
35use bevy_simple_prefs:: PrefsStatus ;
46
57pub struct LoadingPlugin ;
68
9+ #[ cfg( not( target_arch = "wasm32" ) ) ]
10+ const EXPECTED_PIPELINES : usize = 10 ;
11+ #[ cfg( target_arch = "wasm32" ) ]
12+ const EXPECTED_PIPELINES : usize = 6 ;
13+
714pub const NUM_LEVELS : u32 = 12 ;
815
916impl Plugin for LoadingPlugin {
1017 fn build ( & self , app : & mut App ) {
18+ app. add_plugins ( PipelinesReadyPlugin ) ;
1119 app. init_resource :: < Handles > ( ) ;
1220 app. add_systems ( OnEnter ( GameState :: Loading ) , loading_setup) ;
1321 app. add_systems ( Update , loading_update. run_if ( in_state ( GameState :: Loading ) ) ) ;
22+ app. add_systems (
23+ Update ,
24+ print_pipelines. run_if ( resource_changed :: < PipelinesReady > ) ,
25+ ) ;
1426 }
1527}
1628
@@ -26,6 +38,13 @@ fn loading_setup(
2638 MainCamera ,
2739 ) ) ;
2840
41+ commands. spawn ( (
42+ ShapeBuilder :: with ( & shapes:: RegularPolygon :: default ( ) )
43+ . fill ( Color :: BLACK )
44+ . build ( ) ,
45+ StateScoped ( GameState :: Loading ) ,
46+ ) ) ;
47+
2948 for i in 1 ..=NUM_LEVELS {
3049 handles
3150 . levels
@@ -35,14 +54,34 @@ fn loading_setup(
3554 handles
3655 . fonts
3756 . push ( asset_server. load ( "fonts/ChakraPetch-Regular-PixieWrangler.ttf" ) ) ;
57+
58+ commands. spawn ( (
59+ Node {
60+ width : Val :: Percent ( 100.0 ) ,
61+ height : Val :: Percent ( 100.0 ) ,
62+ justify_content : JustifyContent :: Center ,
63+ align_items : AlignItems :: Center ,
64+ ..default ( )
65+ } ,
66+ Children :: spawn ( Spawn ( Text :: new ( "Loading..." ) ) ) ,
67+ StateScoped ( GameState :: Loading ) ,
68+ ) ) ;
69+
70+ handles. music = asset_server. load ( "music/galactic_odyssey_by_alkakrab.ogg" ) ;
3871}
3972
4073fn loading_update (
4174 handles : Res < Handles > ,
4275 asset_server : Res < AssetServer > ,
4376 mut next_state : ResMut < NextState < GameState > > ,
4477 prefs : Res < PrefsStatus < SaveFile > > ,
78+ ready : Res < PipelinesReady > ,
79+ mut frames_since_pipelines_ready : Local < u32 > ,
4580) {
81+ if ready. get ( ) >= EXPECTED_PIPELINES {
82+ * frames_since_pipelines_ready += 1 ;
83+ }
84+
4685 if handles
4786 . fonts
4887 . iter ( )
@@ -59,9 +98,26 @@ fn loading_update(
5998 return ;
6099 }
61100
101+ if !matches ! (
102+ asset_server. get_load_state( & handles. music) ,
103+ Some ( LoadState :: Loaded ) ,
104+ ) {
105+ return ;
106+ }
107+
108+ // Firefox's FPS seems to take a few frames to recover after pipelines are
109+ // compiled, resulting in weird audio artifacts.
110+ if * frames_since_pipelines_ready < 10 {
111+ return ;
112+ }
113+
62114 if !prefs. loaded {
63115 return ;
64116 }
65117
66118 next_state. set ( GameState :: LevelSelect ) ;
67119}
120+
121+ fn print_pipelines ( ready : Res < PipelinesReady > ) {
122+ info ! ( "Pipelines Ready: {}/{}" , ready. get( ) , EXPECTED_PIPELINES ) ;
123+ }
0 commit comments