 ab407aa697
			
		
	
	
		ab407aa697
		
	
	
	
	
		
			
			This pull request is following the discussion on the issue #1127. Additionally, it integrates the change proposed by #1112. The list of change of this pull request: * ✨ Add `Timer::times_finished` method that counts the number of wraps for repeating timers. * ♻️ Refactored `Timer` * 🐛 Fix a bug where 2 successive calls to `Timer::tick` which makes a repeating timer to finish makes `Timer::just_finished` to return `false` where it should return `true`. Minimal failing example: ```rust use bevy::prelude::*; let mut timer: Timer<()> = Timer::from_seconds(1.0, true); timer.tick(1.5); assert!(timer.finished()); assert!(timer.just_finished()); timer.tick(1.5); assert!(timer.finished()); assert!(timer.just_finished()); // <- This fails where it should not ``` * 📚 Add extensive documentation for Timer with doc examples. * ✨ Add a `Stopwatch` struct similar to `Timer` with extensive doc and tests. Even if the type specialization is not retained for bevy, the doc, bugfix and added method are worth salvaging 😅. This is my first PR for bevy, please be kind to me ❤️ . Co-authored-by: Carter Anderson <mcanders1@gmail.com>
		
			
				
	
	
		
			46 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			46 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use bevy::{prelude::*, utils::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).
 | |
| /// This example illustrates how to create a simple plugin that prints out a message.
 | |
| fn main() {
 | |
|     App::build()
 | |
|         .add_plugins(DefaultPlugins)
 | |
|         // 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();
 | |
| }
 | |
| 
 | |
| // This "print message plugin" prints a `message` every `wait_duration`
 | |
| pub struct PrintMessagePlugin {
 | |
|     // Put your plugin configuration here
 | |
|     wait_duration: Duration,
 | |
|     message: String,
 | |
| }
 | |
| 
 | |
| impl Plugin for PrintMessagePlugin {
 | |
|     // this is where we set up our plugin
 | |
|     fn build(&self, app: &mut AppBuilder) {
 | |
|         let state = PrintMessageState {
 | |
|             message: self.message.clone(),
 | |
|             timer: Timer::new(self.wait_duration, true),
 | |
|         };
 | |
|         app.insert_resource(state)
 | |
|             .add_system(print_message_system.system());
 | |
|     }
 | |
| }
 | |
| 
 | |
| struct PrintMessageState {
 | |
|     message: String,
 | |
|     timer: Timer,
 | |
| }
 | |
| 
 | |
| fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
 | |
|     if state.timer.tick(time.delta()).finished() {
 | |
|         println!("{}", state.message);
 | |
|     }
 | |
| }
 |