add plugin example
This commit is contained in:
		
							parent
							
								
									ff0db0df14
								
							
						
					
					
						commit
						a4fe37add1
					
				| @ -101,6 +101,10 @@ path = "examples/app/empty.rs" | |||||||
| name = "headless" | name = "headless" | ||||||
| path = "examples/app/headless.rs" | path = "examples/app/headless.rs" | ||||||
| 
 | 
 | ||||||
|  | [[example]] | ||||||
|  | name = "plugin" | ||||||
|  | path = "examples/app/plugin.rs" | ||||||
|  | 
 | ||||||
| [[example]] | [[example]] | ||||||
| name = "event" | name = "event" | ||||||
| path = "examples/ecs/event.rs" | path = "examples/ecs/event.rs" | ||||||
|  | |||||||
| @ -4,8 +4,8 @@ use bevy::prelude::*; | |||||||
| pub struct ExamplePlugin; | pub struct ExamplePlugin; | ||||||
| 
 | 
 | ||||||
| impl AppPlugin for ExamplePlugin { | impl AppPlugin for ExamplePlugin { | ||||||
|     fn build(&self, app_builder: &mut AppBuilder) { |     fn build(&self, app: &mut AppBuilder) { | ||||||
|         app_builder.add_startup_system(setup); |         app.add_startup_system(setup); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										49
									
								
								examples/app/plugin.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								examples/app/plugin.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,49 @@ | |||||||
|  | use bevy::prelude::*; | ||||||
|  | use std::time::Duration; | ||||||
|  | 
 | ||||||
|  | // Plugins are the foundation of Bevy. They are scoped sets of components, resources, and systems
 | ||||||
|  | // that provide a specific piece of functionality (generally the smaller the scope, the better).
 | ||||||
|  | fn main() { | ||||||
|  |     App::build() | ||||||
|  |         .add_default_plugins() | ||||||
|  |         // plugins are registered as part of the "app building" process
 | ||||||
|  |         .add_plugin(PrintMessagePlugin { | ||||||
|  |             wait_duration: Duration::from_secs(1), | ||||||
|  |             message: "This is an example plugin".to_string(), | ||||||
|  |         }) | ||||||
|  |         .run(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[derive(Default)] | ||||||
|  | pub struct PrintMessagePlugin { | ||||||
|  |     // Put your plugin configuration here
 | ||||||
|  |     wait_duration: Duration, | ||||||
|  |     message: String, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl AppPlugin for PrintMessagePlugin { | ||||||
|  |     // this is where we set up our plugin
 | ||||||
|  |     fn build(&self, app: &mut AppBuilder) { | ||||||
|  |         let state = PrintMessageState { | ||||||
|  |             message: self.message.clone(), | ||||||
|  |             elapsed_time: 0.0, | ||||||
|  |             duration: self.wait_duration, | ||||||
|  |         }; | ||||||
|  |         app.add_resource(state) | ||||||
|  |             .add_system(print_message_system.system()); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | struct PrintMessageState { | ||||||
|  |     message: String, | ||||||
|  |     duration: Duration, | ||||||
|  |     elapsed_time: f32, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn print_message_system(time: Resource<Time>, mut state: ResourceMut<PrintMessageState>) { | ||||||
|  |     state.elapsed_time += time.delta_seconds; | ||||||
|  |     if state.elapsed_time > state.duration.as_secs_f32() { | ||||||
|  |         println!("{}", state.message); | ||||||
|  |         state.elapsed_time = 0.0; | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Carter Anderson
						Carter Anderson