@@ -2,14 +2,13 @@ use bevy::{
22 app:: AppExit ,
33 prelude:: {
44 debug, App , AssetServer , Camera2dBundle , Color , Commands , Component , DefaultPlugins ,
5- Entity , EventReader , EventWriter , HorizontalAlign , ParallelSystemDescriptorCoercion ,
6- ParamSet , Query , Res , ResMut , SpriteBundle , Text as BevyText , Text2dBundle , TextAlignment ,
5+ Entity , EventReader , EventWriter , HorizontalAlign , ImagePlugin , ParamSet , PluginGroup ,
6+ Query , Res , ResMut , Resource , SpriteBundle , Text as BevyText , Text2dBundle , TextAlignment ,
77 TextStyle , Transform , Vec2 , VerticalAlign , Windows ,
88 } ,
9- render:: texture:: ImageSettings ,
109 time:: Time ,
1110 utils:: HashMap ,
12- window:: close_on_esc,
11+ window:: { close_on_esc, WindowPlugin } ,
1312} ;
1413use bevy_prototype_lyon:: prelude:: * ;
1514use std:: {
@@ -47,7 +46,7 @@ pub use bevy::window::{WindowDescriptor, WindowMode, WindowResizeConstraints};
4746/// INFO fields are provided as fresh, readable information to you each frame. Since information in
4847/// these fields are overwritten every frame, any changes to them are ignored. Thus, you can feel
4948/// free to, e.g. consume all the events out of the `collision_events` vector.
50- #[ derive( Default , Debug ) ]
49+ #[ derive( Default , Debug , Resource ) ]
5150pub struct Engine {
5251 /// SYNCED - The state of all sprites this frame. To add a sprite, use the
5352 /// [`add_sprite`](Engine::add_sprite) method. Modify & remove sprites as you like.
@@ -173,7 +172,7 @@ fn add_collider_lines(commands: &mut Commands, sprite: &mut Sprite) {
173172 let line = path_builder. build ( ) ;
174173 let transform = sprite. bevy_transform ( ) ;
175174 commands
176- . spawn_bundle ( GeometryBuilder :: build_as (
175+ . spawn ( GeometryBuilder :: build_as (
177176 & line,
178177 DrawMode :: Stroke ( StrokeMode :: new ( Color :: WHITE , 1.0 / transform. scale . x ) ) ,
179178 transform,
@@ -192,11 +191,14 @@ pub fn add_sprites(commands: &mut Commands, asset_server: &Res<AssetServer>, eng
192191 // Create the sprite
193192 let transform = sprite. bevy_transform ( ) ;
194193 let texture_path = sprite. filepath . clone ( ) ;
195- commands. spawn ( ) . insert ( sprite) . insert_bundle ( SpriteBundle {
196- texture : asset_server. load ( texture_path) ,
197- transform,
198- ..Default :: default ( )
199- } ) ;
194+ commands. spawn ( (
195+ sprite,
196+ SpriteBundle {
197+ texture : asset_server. load ( texture_path) ,
198+ transform,
199+ ..Default :: default ( )
200+ } ,
201+ ) ) ;
200202 }
201203}
202204
@@ -209,22 +211,25 @@ pub fn add_texts(commands: &mut Commands, asset_server: &Res<AssetServer>, engin
209211 let font_size = text. font_size ;
210212 let text_string = text. value . clone ( ) ;
211213 let font_path = text. font . clone ( ) ;
212- commands. spawn ( ) . insert ( text) . insert_bundle ( Text2dBundle {
213- text : BevyText :: from_section (
214- text_string,
215- TextStyle {
216- font : asset_server. load ( font_path. as_str ( ) ) ,
217- font_size,
218- color : Color :: WHITE ,
219- } ,
220- )
221- . with_alignment ( TextAlignment {
222- vertical : VerticalAlign :: Center ,
223- horizontal : HorizontalAlign :: Center ,
224- } ) ,
225- transform,
226- ..Default :: default ( )
227- } ) ;
214+ commands. spawn ( (
215+ text,
216+ Text2dBundle {
217+ text : BevyText :: from_section (
218+ text_string,
219+ TextStyle {
220+ font : asset_server. load ( font_path. as_str ( ) ) ,
221+ font_size,
222+ color : Color :: WHITE ,
223+ } ,
224+ )
225+ . with_alignment ( TextAlignment {
226+ vertical : VerticalAlign :: Center ,
227+ horizontal : HorizontalAlign :: Center ,
228+ } ) ,
229+ transform,
230+ ..Default :: default ( )
231+ } ,
232+ ) ) ;
228233 }
229234}
230235
@@ -296,13 +301,17 @@ impl<S: Send + Sync + 'static> Game<S> {
296301
297302 /// Start the game.
298303 pub fn run ( & mut self , initial_game_state : S ) {
299- self . app
300- . insert_resource :: < WindowDescriptor > ( self . window_descriptor . clone ( ) )
301- . insert_resource ( ImageSettings :: default_nearest ( ) )
302- . insert_resource :: < S > ( initial_game_state) ;
304+ self . app . insert_resource :: < S > ( initial_game_state) ;
303305 self . app
304306 // Built-ins
305- . add_plugins ( DefaultPlugins )
307+ . add_plugins (
308+ DefaultPlugins
309+ . set ( WindowPlugin {
310+ window : self . window_descriptor . clone ( ) ,
311+ ..Default :: default ( )
312+ } )
313+ . set ( ImagePlugin :: default_nearest ( ) ) ,
314+ )
306315 . add_system ( close_on_esc)
307316 // External Plugins
308317 . add_plugin ( ShapePlugin ) // bevy_prototype_lyon, for displaying sprite colliders
@@ -312,17 +321,10 @@ impl<S: Send + Sync + 'static> Game<S> {
312321 . add_plugin ( MousePlugin )
313322 . add_plugin ( PhysicsPlugin )
314323 //.insert_resource(ReportExecutionOrderAmbiguities) // for debugging
315- . add_system (
316- update_window_dimensions
317- . label ( "update_window_dimensions" )
318- . before ( "game_logic_sync" ) ,
319- )
320- . add_system ( game_logic_sync :: < S > . label ( "game_logic_sync" ) )
324+ . add_system ( update_window_dimensions)
325+ . add_system ( game_logic_sync) // TODO: ensure after update_window_dimensions
321326 . add_startup_system ( setup) ;
322- self . app
323- . world
324- . spawn ( )
325- . insert_bundle ( Camera2dBundle :: default ( ) ) ;
327+ self . app . world . spawn ( Camera2dBundle :: default ( ) ) ;
326328 let engine = std:: mem:: take ( & mut self . engine ) ;
327329 self . app . insert_resource ( engine) ;
328330 let logic_functions = std:: mem:: take ( & mut self . logic_functions ) ;
@@ -350,7 +352,7 @@ fn game_logic_sync<S: Send + Sync + 'static>(
350352 logic_functions : Res < Vec < fn ( & mut Engine , & mut S ) > > ,
351353 keyboard_state : Res < KeyboardState > ,
352354 mouse_state : Res < MouseState > ,
353- time : Res < Time > ,
355+ time : Time ,
354356 mut app_exit_events : EventWriter < AppExit > ,
355357 mut collision_events : EventReader < CollisionEvent > ,
356358 mut query_set : ParamSet < (
@@ -362,8 +364,8 @@ fn game_logic_sync<S: Send + Sync + 'static>(
362364 // Update this frame's timing info
363365 engine. delta = time. delta ( ) ;
364366 engine. delta_f32 = time. delta_seconds ( ) ;
365- engine. time_since_startup = time. time_since_startup ( ) ;
366- engine. time_since_startup_f64 = time. seconds_since_startup ( ) ;
367+ engine. time_since_startup = time. elapsed ( ) ;
368+ engine. time_since_startup_f64 = time. elapsed_seconds_f64 ( ) ;
367369
368370 // Copy keyboard state over to engine to give to users
369371 engine. keyboard_state = keyboard_state. clone ( ) ;
0 commit comments