 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>
		
			
				
	
	
		
			42 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use bevy::prelude::*;
 | |
| 
 | |
| fn main() {
 | |
|     App::build()
 | |
|         .add_plugins(DefaultPlugins)
 | |
|         .add_startup_system(setup.system())
 | |
|         .add_system(animate_sprite_system.system())
 | |
|         .run();
 | |
| }
 | |
| 
 | |
| fn animate_sprite_system(
 | |
|     time: Res<Time>,
 | |
|     texture_atlases: Res<Assets<TextureAtlas>>,
 | |
|     mut query: Query<(&mut Timer, &mut TextureAtlasSprite, &Handle<TextureAtlas>)>,
 | |
| ) {
 | |
|     for (mut timer, mut sprite, texture_atlas_handle) in query.iter_mut() {
 | |
|         timer.tick(time.delta());
 | |
|         if timer.finished() {
 | |
|             let texture_atlas = texture_atlases.get(texture_atlas_handle).unwrap();
 | |
|             sprite.index = ((sprite.index as usize + 1) % texture_atlas.textures.len()) as u32;
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn setup(
 | |
|     mut commands: Commands,
 | |
|     asset_server: Res<AssetServer>,
 | |
|     mut texture_atlases: ResMut<Assets<TextureAtlas>>,
 | |
| ) {
 | |
|     let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
 | |
|     let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1);
 | |
|     let texture_atlas_handle = texture_atlases.add(texture_atlas);
 | |
|     commands
 | |
|         .spawn(OrthographicCameraBundle::new_2d())
 | |
|         .spawn(SpriteSheetBundle {
 | |
|             texture_atlas: texture_atlas_handle,
 | |
|             transform: Transform::from_scale(Vec3::splat(6.0)),
 | |
|             ..Default::default()
 | |
|         })
 | |
|         .with(Timer::from_seconds(0.1, true));
 | |
| }
 |