From 5a58300c90f7be74bbc8ff1ec0f7f368eda54c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lena=20Miliz=C3=A9?= Date: Thu, 13 Oct 2022 12:50:03 +0200 Subject: [PATCH 1/6] add TimerMode, replace repeating: bool MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As mentioned in #2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or reading the documentation. Signed-off-by: Lena Milizé --- .../src/log_diagnostics_plugin.rs | 4 +- crates/bevy_time/src/lib.rs | 2 +- crates/bevy_time/src/timer.rs | 96 ++++++++++++------- examples/2d/sprite_sheet.rs | 2 +- examples/app/plugin.rs | 2 +- examples/ecs/event.rs | 2 +- examples/ecs/generic_system.rs | 4 +- examples/ecs/timers.rs | 9 +- examples/games/alien_cake_addict.rs | 7 +- examples/games/contributors.rs | 2 +- examples/games/game_menu.rs | 4 +- .../stress_tests/many_animated_sprites.rs | 4 +- examples/stress_tests/many_cubes.rs | 2 +- examples/stress_tests/many_lights.rs | 2 +- examples/stress_tests/many_sprites.rs | 2 +- examples/ui/font_atlas_debug.rs | 2 +- examples/ui/scaling.rs | 2 +- 17 files changed, 90 insertions(+), 58 deletions(-) diff --git a/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs index 947b780725d3f..9966157cb0cf5 100644 --- a/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/log_diagnostics_plugin.rs @@ -2,7 +2,7 @@ use super::{Diagnostic, DiagnosticId, Diagnostics}; use bevy_app::prelude::*; use bevy_ecs::system::{Res, ResMut, Resource}; use bevy_log::{debug, info}; -use bevy_time::{Time, Timer}; +use bevy_time::{Time, Timer, TimerMode}; use bevy_utils::Duration; /// An App Plugin that logs diagnostics to the console @@ -32,7 +32,7 @@ impl Default for LogDiagnosticsPlugin { impl Plugin for LogDiagnosticsPlugin { fn build(&self, app: &mut App) { app.insert_resource(LogDiagnosticsState { - timer: Timer::new(self.wait_duration, true), + timer: Timer::new(self.wait_duration, TimerMode::Repeating), filter: self.filter.clone(), }); diff --git a/crates/bevy_time/src/lib.rs b/crates/bevy_time/src/lib.rs index 6e2ea76f953f4..c6285b9cf0bcd 100644 --- a/crates/bevy_time/src/lib.rs +++ b/crates/bevy_time/src/lib.rs @@ -16,7 +16,7 @@ use crossbeam_channel::{Receiver, Sender}; pub mod prelude { //! The Bevy Time Prelude. #[doc(hidden)] - pub use crate::{Time, Timer}; + pub use crate::{Time, Timer, TimerMode}; } use bevy_app::prelude::*; diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 4661bbeae9ea7..eb78fd17f4c1e 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -14,7 +14,7 @@ use bevy_utils::Duration; pub struct Timer { stopwatch: Stopwatch, duration: Duration, - repeating: bool, + mode: TimerMode, finished: bool, times_finished_this_tick: u32, } @@ -23,10 +23,10 @@ impl Timer { /// Creates a new timer with a given duration. /// /// See also [`Timer::from_seconds`](Timer::from_seconds). - pub fn new(duration: Duration, repeating: bool) -> Self { + pub fn new(duration: Duration, mode: TimerMode) -> Self { Self { duration, - repeating, + mode, ..Default::default() } } @@ -36,12 +36,12 @@ impl Timer { /// # Example /// ``` /// # use bevy_time::*; - /// let mut timer = Timer::from_seconds(1.0, false); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); /// ``` - pub fn from_seconds(duration: f32, repeating: bool) -> Self { + pub fn from_seconds(duration: f32, mode: TimerMode) -> Self { Self { duration: Duration::from_secs_f32(duration), - repeating, + mode, ..Default::default() } } @@ -52,7 +52,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(1.0, false); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); /// timer.tick(Duration::from_secs_f32(1.5)); /// assert!(timer.finished()); /// timer.tick(Duration::from_secs_f32(0.5)); @@ -69,7 +69,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(1.0, false); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); /// timer.tick(Duration::from_secs_f32(1.5)); /// assert!(timer.just_finished()); /// timer.tick(Duration::from_secs_f32(0.5)); @@ -89,7 +89,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(1.0, false); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); /// timer.tick(Duration::from_secs_f32(0.5)); /// assert_eq!(timer.elapsed(), Duration::from_secs_f32(0.5)); /// ``` @@ -113,7 +113,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(1.0, false); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); /// timer.set_elapsed(Duration::from_secs(2)); /// assert_eq!(timer.elapsed(), Duration::from_secs(2)); /// // the timer is not finished even if the elapsed time is greater than the duration. @@ -130,7 +130,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let timer = Timer::new(Duration::from_secs(1), false); + /// let timer = Timer::new(Duration::from_secs(1), TimerMode::Once); /// assert_eq!(timer.duration(), Duration::from_secs(1)); /// ``` #[inline] @@ -144,7 +144,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(1.5, false); + /// let mut timer = Timer::from_seconds(1.5, TimerMode::Once); /// timer.set_duration(Duration::from_secs(1)); /// assert_eq!(timer.duration(), Duration::from_secs(1)); /// ``` @@ -158,12 +158,12 @@ impl Timer { /// # Examples /// ``` /// # use bevy_time::*; - /// let mut timer = Timer::from_seconds(1.0, true); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); /// assert!(timer.repeating()); /// ``` #[inline] pub fn repeating(&self) -> bool { - self.repeating + self.mode.repeating() } /// Sets whether the timer is repeating or not. @@ -171,17 +171,21 @@ impl Timer { /// # Examples /// ``` /// # use bevy_time::*; - /// let mut timer = Timer::from_seconds(1.0, true); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); /// timer.set_repeating(false); /// assert!(!timer.repeating()); /// ``` #[inline] pub fn set_repeating(&mut self, repeating: bool) { - if !self.repeating && repeating && self.finished { + if !self.mode.repeating() && repeating && self.finished { self.stopwatch.reset(); self.finished = self.just_finished(); } - self.repeating = repeating; + self.mode = if repeating { + TimerMode::Repeating + } else { + TimerMode::Once + }; } /// Advance the timer by `delta` seconds. @@ -194,8 +198,8 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(1.0, false); - /// let mut repeating = Timer::from_seconds(1.0, true); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); + /// let mut repeating = Timer::from_seconds(1.0, TimerMode::Repeating); /// timer.tick(Duration::from_secs_f32(1.5)); /// repeating.tick(Duration::from_secs_f32(1.5)); /// assert_eq!(timer.elapsed_secs(), 1.0); @@ -243,7 +247,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(1.0, false); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); /// timer.pause(); /// timer.tick(Duration::from_secs_f32(0.5)); /// assert_eq!(timer.elapsed_secs(), 0.0); @@ -261,7 +265,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(1.0, false); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); /// timer.pause(); /// timer.tick(Duration::from_secs_f32(0.5)); /// timer.unpause(); @@ -280,7 +284,7 @@ impl Timer { /// # Examples /// ``` /// # use bevy_time::*; - /// let mut timer = Timer::from_seconds(1.0, false); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); /// assert!(!timer.paused()); /// timer.pause(); /// assert!(timer.paused()); @@ -300,7 +304,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(1.0, false); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); /// timer.tick(Duration::from_secs_f32(1.5)); /// timer.reset(); /// assert!(!timer.finished()); @@ -319,7 +323,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(2.0, false); + /// let mut timer = Timer::from_seconds(2.0, TimerMode::Once); /// timer.tick(Duration::from_secs_f32(0.5)); /// assert_eq!(timer.percent(), 0.25); /// ``` @@ -334,7 +338,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(2.0, false); + /// let mut timer = Timer::from_seconds(2.0, TimerMode::Once); /// timer.tick(Duration::from_secs_f32(0.5)); /// assert_eq!(timer.percent_left(), 0.75); /// ``` @@ -350,7 +354,7 @@ impl Timer { /// # use bevy_time::*; /// use std::cmp::Ordering; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(2.0, false); + /// let mut timer = Timer::from_seconds(2.0, TimerMode::Once); /// timer.tick(Duration::from_secs_f32(0.5)); /// let result = timer.remaining_secs().total_cmp(&1.5); /// assert_eq!(Ordering::Equal, result); @@ -366,7 +370,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(2.0, false); + /// let mut timer = Timer::from_seconds(2.0, TimerMode::Once); /// timer.tick(Duration::from_secs_f32(0.5)); /// assert_eq!(timer.remaining(), Duration::from_secs_f32(1.5)); /// ``` @@ -385,7 +389,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// use std::time::Duration; - /// let mut timer = Timer::from_seconds(1.0, true); + /// let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); /// timer.tick(Duration::from_secs_f32(6.0)); /// assert_eq!(timer.times_finished_this_tick(), 6); /// timer.tick(Duration::from_secs_f32(2.0)); @@ -399,6 +403,28 @@ impl Timer { } } +/// Specifies [`Timer`] behavior. +#[derive(Debug, Clone, Copy, Reflect)] +#[reflect(Default)] +pub enum TimerMode { + /// Run once and stop. + Once, + /// Reset when finished. + Repeating, +} + +impl TimerMode { + pub fn repeating(self) -> bool { + matches!(self, Self::Repeating) + } +} + +impl Default for TimerMode { + fn default() -> Self { + Self::Once + } +} + #[cfg(test)] #[allow(clippy::float_cmp)] mod tests { @@ -406,7 +432,7 @@ mod tests { #[test] fn non_repeating_timer() { - let mut t = Timer::from_seconds(10.0, false); + let mut t = Timer::from_seconds(10.0, TimerMode::Once); // Tick once, check all attributes t.tick(Duration::from_secs_f32(0.25)); assert_eq!(t.elapsed_secs(), 0.25); @@ -449,7 +475,7 @@ mod tests { #[test] fn repeating_timer() { - let mut t = Timer::from_seconds(2.0, true); + let mut t = Timer::from_seconds(2.0, TimerMode::Repeating); // Tick once, check all attributes t.tick(Duration::from_secs_f32(0.75)); assert_eq!(t.elapsed_secs(), 0.75); @@ -480,7 +506,7 @@ mod tests { #[test] fn times_finished_repeating() { - let mut t = Timer::from_seconds(1.0, true); + let mut t = Timer::from_seconds(1.0, TimerMode::Repeating); assert_eq!(t.times_finished_this_tick(), 0); t.tick(Duration::from_secs_f32(3.5)); assert_eq!(t.times_finished_this_tick(), 3); @@ -493,7 +519,7 @@ mod tests { #[test] fn times_finished_this_tick() { - let mut t = Timer::from_seconds(1.0, false); + let mut t = Timer::from_seconds(1.0, TimerMode::Once); assert_eq!(t.times_finished_this_tick(), 0); t.tick(Duration::from_secs_f32(1.5)); assert_eq!(t.times_finished_this_tick(), 1); @@ -503,7 +529,7 @@ mod tests { #[test] fn times_finished_this_tick_precise() { - let mut t = Timer::from_seconds(0.01, true); + let mut t = Timer::from_seconds(0.01, TimerMode::Repeating); let duration = Duration::from_secs_f64(0.333); // total duration: 0.333 => 33 times finished @@ -522,7 +548,7 @@ mod tests { #[test] fn paused() { - let mut t = Timer::from_seconds(10.0, false); + let mut t = Timer::from_seconds(10.0, TimerMode::Once); t.tick(Duration::from_secs_f32(10.0)); assert!(t.just_finished()); @@ -536,7 +562,7 @@ mod tests { #[test] fn paused_repeating() { - let mut t = Timer::from_seconds(10.0, true); + let mut t = Timer::from_seconds(10.0, TimerMode::Repeating); t.tick(Duration::from_secs_f32(10.0)); assert!(t.just_finished()); diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index c5366f55116dd..b2abdc7be0939 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -49,6 +49,6 @@ fn setup( transform: Transform::from_scale(Vec3::splat(6.0)), ..default() }, - AnimationTimer(Timer::from_seconds(0.1, true)), + AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)), )); } diff --git a/examples/app/plugin.rs b/examples/app/plugin.rs index eeff7c72d998f..e1a5b35aada6c 100644 --- a/examples/app/plugin.rs +++ b/examples/app/plugin.rs @@ -29,7 +29,7 @@ impl Plugin for PrintMessagePlugin { fn build(&self, app: &mut App) { let state = PrintMessageState { message: self.message.clone(), - timer: Timer::new(self.wait_duration, true), + timer: Timer::new(self.wait_duration, TimerMode::Repeating), }; app.insert_resource(state).add_system(print_message_system); } diff --git a/examples/ecs/event.rs b/examples/ecs/event.rs index cc9fa8376d0ab..cc4c2a7762de1 100644 --- a/examples/ecs/event.rs +++ b/examples/ecs/event.rs @@ -30,7 +30,7 @@ struct EventTriggerState { impl Default for EventTriggerState { fn default() -> Self { EventTriggerState { - event_timer: Timer::from_seconds(1.0, true), + event_timer: Timer::from_seconds(1.0, TimerMode::Repeating), } } } diff --git a/examples/ecs/generic_system.rs b/examples/ecs/generic_system.rs index fc6175d2b03f0..d69bdf628ef05 100644 --- a/examples/ecs/generic_system.rs +++ b/examples/ecs/generic_system.rs @@ -51,13 +51,13 @@ fn main() { fn setup_system(mut commands: Commands) { commands.spawn(( - PrinterTick(Timer::from_seconds(1.0, true)), + PrinterTick(Timer::from_seconds(1.0, TimerMode::Repeating)), TextToPrint("I will print until you press space.".to_string()), MenuClose, )); commands.spawn(( - PrinterTick(Timer::from_seconds(1.0, true)), + PrinterTick(Timer::from_seconds(1.0, TimerMode::Repeating)), TextToPrint("I will always print".to_string()), LevelUnload, )); diff --git a/examples/ecs/timers.rs b/examples/ecs/timers.rs index 9bddae5181ebb..b2a3374ba965e 100644 --- a/examples/ecs/timers.rs +++ b/examples/ecs/timers.rs @@ -24,8 +24,8 @@ pub struct Countdown { impl Countdown { pub fn new() -> Self { Self { - percent_trigger: Timer::from_seconds(4.0, true), - main_timer: Timer::from_seconds(20.0, false), + percent_trigger: Timer::from_seconds(4.0, TimerMode::Repeating), + main_timer: Timer::from_seconds(20.0, TimerMode::Once), } } } @@ -38,7 +38,10 @@ impl Default for Countdown { fn setup(mut commands: Commands) { // Add an entity to the world with a timer - commands.spawn(PrintOnCompletionTimer(Timer::from_seconds(5.0, false))); + commands.spawn(PrintOnCompletionTimer(Timer::from_seconds( + 5.0, + TimerMode::Once, + ))); } /// This system ticks all the `Timer` components on entities within the scene diff --git a/examples/games/alien_cake_addict.rs b/examples/games/alien_cake_addict.rs index 27e3f1b359f0f..98c5af8694569 100644 --- a/examples/games/alien_cake_addict.rs +++ b/examples/games/alien_cake_addict.rs @@ -17,7 +17,10 @@ struct BonusSpawnTimer(Timer); fn main() { App::new() .init_resource::() - .insert_resource(BonusSpawnTimer(Timer::from_seconds(5.0, true))) + .insert_resource(BonusSpawnTimer(Timer::from_seconds( + 5.0, + TimerMode::Repeating, + ))) .add_plugins(DefaultPlugins) .add_state(GameState::Playing) .add_startup_system(setup_cameras) @@ -98,7 +101,7 @@ fn setup(mut commands: Commands, asset_server: Res, mut game: ResMu game.score = 0; game.player.i = BOARD_SIZE_I / 2; game.player.j = BOARD_SIZE_J / 2; - game.player.move_cooldown = Timer::from_seconds(0.3, false); + game.player.move_cooldown = Timer::from_seconds(0.3, TimerMode::Once); commands.spawn(PointLightBundle { transform: Transform::from_xyz(4.0, 10.0, 4.0), diff --git a/examples/games/contributors.rs b/examples/games/contributors.rs index 56f4990e34d00..77f5e3bf97640 100644 --- a/examples/games/contributors.rs +++ b/examples/games/contributors.rs @@ -39,7 +39,7 @@ struct SelectionState { impl Default for SelectionState { fn default() -> Self { Self { - timer: Timer::from_seconds(SHOWCASE_TIMER_SECS, true), + timer: Timer::from_seconds(SHOWCASE_TIMER_SECS, TimerMode::Repeating), has_triggered: false, } } diff --git a/examples/games/game_menu.rs b/examples/games/game_menu.rs index 11e0c704fb927..9f2b19ab2ffc1 100644 --- a/examples/games/game_menu.rs +++ b/examples/games/game_menu.rs @@ -96,7 +96,7 @@ mod splash { OnSplashScreen, )); // Insert the timer as a resource - commands.insert_resource(SplashTimer(Timer::from_seconds(1.0, false))); + commands.insert_resource(SplashTimer(Timer::from_seconds(1.0, TimerMode::Once))); } // Tick the timer, and change state when finished @@ -215,7 +215,7 @@ mod game { ); }); // Spawn a 5 seconds timer to trigger going back to the menu - commands.insert_resource(GameTimer(Timer::from_seconds(5.0, false))); + commands.insert_resource(GameTimer(Timer::from_seconds(5.0, TimerMode::Once))); } // Tick the timer, and change state when finished diff --git a/examples/stress_tests/many_animated_sprites.rs b/examples/stress_tests/many_animated_sprites.rs index 91586b8dace7d..9291948624759 100644 --- a/examples/stress_tests/many_animated_sprites.rs +++ b/examples/stress_tests/many_animated_sprites.rs @@ -61,7 +61,7 @@ fn setup( let translation = (position * tile_size).extend(rng.gen::()); let rotation = Quat::from_rotation_z(rng.gen::()); let scale = Vec3::splat(rng.gen::() * 2.0); - let mut timer = Timer::from_seconds(0.1, true); + let mut timer = Timer::from_seconds(0.1, TimerMode::Repeating); timer.set_elapsed(Duration::from_secs_f32(rng.gen::())); commands.spawn(( @@ -118,7 +118,7 @@ struct PrintingTimer(Timer); impl Default for PrintingTimer { fn default() -> Self { - Self(Timer::from_seconds(1.0, true)) + Self(Timer::from_seconds(1.0, TimerMode::Repeating)) } } diff --git a/examples/stress_tests/many_cubes.rs b/examples/stress_tests/many_cubes.rs index 912f23091fdd6..b1afdadd64a29 100644 --- a/examples/stress_tests/many_cubes.rs +++ b/examples/stress_tests/many_cubes.rs @@ -185,6 +185,6 @@ struct PrintingTimer(Timer); impl Default for PrintingTimer { fn default() -> Self { - Self(Timer::from_seconds(1.0, true)) + Self(Timer::from_seconds(1.0, TimerMode::Repeating)) } } diff --git a/examples/stress_tests/many_lights.rs b/examples/stress_tests/many_lights.rs index ac5a078b7482a..fa63701feeb46 100644 --- a/examples/stress_tests/many_lights.rs +++ b/examples/stress_tests/many_lights.rs @@ -187,6 +187,6 @@ struct PrintingTimer(Timer); impl Default for PrintingTimer { fn default() -> Self { - Self(Timer::from_seconds(1.0, true)) + Self(Timer::from_seconds(1.0, TimerMode::Repeating)) } } diff --git a/examples/stress_tests/many_sprites.rs b/examples/stress_tests/many_sprites.rs index 34e7d64d30af1..1a232c37ae30e 100644 --- a/examples/stress_tests/many_sprites.rs +++ b/examples/stress_tests/many_sprites.rs @@ -103,7 +103,7 @@ struct PrintingTimer(Timer); impl Default for PrintingTimer { fn default() -> Self { - Self(Timer::from_seconds(1.0, true)) + Self(Timer::from_seconds(1.0, TimerMode::Repeating)) } } diff --git a/examples/ui/font_atlas_debug.rs b/examples/ui/font_atlas_debug.rs index c8d8b034b4f9c..e2ea358d66512 100644 --- a/examples/ui/font_atlas_debug.rs +++ b/examples/ui/font_atlas_debug.rs @@ -26,7 +26,7 @@ impl Default for State { Self { atlas_count: 0, handle: Handle::default(), - timer: Timer::from_seconds(0.05, true), + timer: Timer::from_seconds(0.05, TimerMode::Repeating), } } } diff --git a/examples/ui/scaling.rs b/examples/ui/scaling.rs index e6306f0eb1484..e2b6d1468a463 100644 --- a/examples/ui/scaling.rs +++ b/examples/ui/scaling.rs @@ -13,7 +13,7 @@ fn main() { .insert_resource(TargetScale { start_scale: 1.0, target_scale: 1.0, - target_time: Timer::new(Duration::from_millis(SCALE_TIME), false), + target_time: Timer::new(Duration::from_millis(SCALE_TIME), TimerMode::Once), }) .add_startup_system(setup) .add_system(apply_scaling.label(ApplyScaling)) From 4881f87ca4b2c5267e5869edf250757030fad87b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lena=20Miliz=C3=A9?= Date: Thu, 13 Oct 2022 15:26:17 +0200 Subject: [PATCH 2/6] replace set_repeating with set_mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces Timer::set_repeating and Timer::repeating with Timer::set_mode and Timer::mode, respectively. Adds PartialEq, Eq, Hash to TimerMode. Replaces impl Default with derive macro. Signed-off-by: Lena Milizé --- crates/bevy_time/src/timer.rs | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index eb78fd17f4c1e..6c9887343fedd 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -162,8 +162,8 @@ impl Timer { /// assert!(timer.repeating()); /// ``` #[inline] - pub fn repeating(&self) -> bool { - self.mode.repeating() + pub fn mode(&self) -> TimerMode { + self.mode } /// Sets whether the timer is repeating or not. @@ -176,16 +176,12 @@ impl Timer { /// assert!(!timer.repeating()); /// ``` #[inline] - pub fn set_repeating(&mut self, repeating: bool) { - if !self.mode.repeating() && repeating && self.finished { + pub fn set_mode(&mut self, mode: TimerMode) { + if !self.mode.repeating() && mode.repeating() && self.finished { self.stopwatch.reset(); self.finished = self.just_finished(); } - self.mode = if repeating { - TimerMode::Repeating - } else { - TimerMode::Once - }; + self.mode = mode; } /// Advance the timer by `delta` seconds. @@ -208,13 +204,13 @@ impl Timer { pub fn tick(&mut self, delta: Duration) -> &Self { if self.paused() { self.times_finished_this_tick = 0; - if self.repeating() { + if self.mode.repeating() { self.finished = false; } return self; } - if !self.repeating() && self.finished() { + if !self.mode.repeating() && self.finished() { self.times_finished_this_tick = 0; return self; } @@ -223,7 +219,7 @@ impl Timer { self.finished = self.elapsed() >= self.duration(); if self.finished() { - if self.repeating() { + if self.mode.repeating() { self.times_finished_this_tick = (self.elapsed().as_nanos() / self.duration().as_nanos()) as u32; // Duration does not have a modulo @@ -404,10 +400,11 @@ impl Timer { } /// Specifies [`Timer`] behavior. -#[derive(Debug, Clone, Copy, Reflect)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Default, Reflect)] #[reflect(Default)] pub enum TimerMode { /// Run once and stop. + #[default] Once, /// Reset when finished. Repeating, @@ -415,13 +412,7 @@ pub enum TimerMode { impl TimerMode { pub fn repeating(self) -> bool { - matches!(self, Self::Repeating) - } -} - -impl Default for TimerMode { - fn default() -> Self { - Self::Once + self == Self::Repeating } } From 3e6cbd16ef5c41b13496d6854bfeff8482f9574c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lena=20Miliz=C3=A9?= Date: Thu, 13 Oct 2022 15:32:24 +0200 Subject: [PATCH 3/6] fix doc tests and tests after 4881f87 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lena Milizé --- crates/bevy_time/src/timer.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 6c9887343fedd..50b6b7ca0b05e 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -159,7 +159,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); - /// assert!(timer.repeating()); + /// assert!(timer.mode() == TimerMode::Repeating); /// ``` #[inline] pub fn mode(&self) -> TimerMode { @@ -172,8 +172,8 @@ impl Timer { /// ``` /// # use bevy_time::*; /// let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); - /// timer.set_repeating(false); - /// assert!(!timer.repeating()); + /// timer.set_mode(TimerMode::Once); + /// assert!(timer.mode() != TimerMode::Repeating); /// ``` #[inline] pub fn set_mode(&mut self, mode: TimerMode) { @@ -431,7 +431,7 @@ mod tests { assert!(!t.finished()); assert!(!t.just_finished()); assert_eq!(t.times_finished_this_tick(), 0); - assert!(!t.repeating()); + assert!(t.mode() != TimerMode::Repeating); assert_eq!(t.percent(), 0.025); assert_eq!(t.percent_left(), 0.975); // Ticking while paused changes nothing @@ -442,7 +442,7 @@ mod tests { assert!(!t.finished()); assert!(!t.just_finished()); assert_eq!(t.times_finished_this_tick(), 0); - assert!(!t.repeating()); + assert!(t.mode() != TimerMode::Repeating); assert_eq!(t.percent(), 0.025); assert_eq!(t.percent_left(), 0.975); // Tick past the end and make sure elapsed doesn't go past 0.0 and other things update @@ -474,7 +474,7 @@ mod tests { assert!(!t.finished()); assert!(!t.just_finished()); assert_eq!(t.times_finished_this_tick(), 0); - assert!(t.repeating()); + assert!(t.mode() == TimerMode::Repeating); assert_eq!(t.percent(), 0.375); assert_eq!(t.percent_left(), 0.625); // Tick past the end and make sure elapsed wraps From d5f752e5d169f6742b692ff80b956c4b1d8e00f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lena=20Miliz=C3=A9?= Date: Thu, 13 Oct 2022 16:45:19 +0200 Subject: [PATCH 4/6] replace assert!(timer.mode() == Repeating) with assert_eq! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lena Milizé --- crates/bevy_time/src/timer.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 50b6b7ca0b05e..6b9be4cc60d4b 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -159,7 +159,7 @@ impl Timer { /// ``` /// # use bevy_time::*; /// let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); - /// assert!(timer.mode() == TimerMode::Repeating); + /// assert_eq!(timer.mode(), TimerMode::Repeating); /// ``` #[inline] pub fn mode(&self) -> TimerMode { @@ -173,7 +173,7 @@ impl Timer { /// # use bevy_time::*; /// let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); /// timer.set_mode(TimerMode::Once); - /// assert!(timer.mode() != TimerMode::Repeating); + /// assert_ne!(timer.mode(), TimerMode::Repeating); /// ``` #[inline] pub fn set_mode(&mut self, mode: TimerMode) { @@ -431,7 +431,7 @@ mod tests { assert!(!t.finished()); assert!(!t.just_finished()); assert_eq!(t.times_finished_this_tick(), 0); - assert!(t.mode() != TimerMode::Repeating); + assert_ne!(t.mode(), TimerMode::Repeating); assert_eq!(t.percent(), 0.025); assert_eq!(t.percent_left(), 0.975); // Ticking while paused changes nothing @@ -442,7 +442,7 @@ mod tests { assert!(!t.finished()); assert!(!t.just_finished()); assert_eq!(t.times_finished_this_tick(), 0); - assert!(t.mode() != TimerMode::Repeating); + assert_ne!(t.mode(), TimerMode::Repeating); assert_eq!(t.percent(), 0.025); assert_eq!(t.percent_left(), 0.975); // Tick past the end and make sure elapsed doesn't go past 0.0 and other things update @@ -474,7 +474,7 @@ mod tests { assert!(!t.finished()); assert!(!t.just_finished()); assert_eq!(t.times_finished_this_tick(), 0); - assert!(t.mode() == TimerMode::Repeating); + assert_eq!(t.mode(), TimerMode::Repeating); assert_eq!(t.percent(), 0.375); assert_eq!(t.percent_left(), 0.625); // Tick past the end and make sure elapsed wraps From ea23581eb1b91c5f9be78571152e4fc60e398c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lena=20Miliz=C3=A9?= Date: Thu, 13 Oct 2022 19:31:01 +0200 Subject: [PATCH 5/6] remove TimerMode::repeating MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit tguichaoua: > The problem with utility methods is that if you want to be consistant > you have to add one for each variant for each enum. That is complex to > maintain. > For fieldless enums using equality is more straightforward and seems > to be the most used way in the rust community. Signed-off-by: Lena Milizé --- crates/bevy_time/src/timer.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 6b9be4cc60d4b..22d4675002bdc 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -177,7 +177,7 @@ impl Timer { /// ``` #[inline] pub fn set_mode(&mut self, mode: TimerMode) { - if !self.mode.repeating() && mode.repeating() && self.finished { + if self.mode != TimerMode::Repeating && mode == TimerMode::Repeating && self.finished { self.stopwatch.reset(); self.finished = self.just_finished(); } @@ -204,13 +204,13 @@ impl Timer { pub fn tick(&mut self, delta: Duration) -> &Self { if self.paused() { self.times_finished_this_tick = 0; - if self.mode.repeating() { + if self.mode == TimerMode::Repeating { self.finished = false; } return self; } - if !self.mode.repeating() && self.finished() { + if self.mode != TimerMode::Repeating && self.finished() { self.times_finished_this_tick = 0; return self; } @@ -219,7 +219,7 @@ impl Timer { self.finished = self.elapsed() >= self.duration(); if self.finished() { - if self.mode.repeating() { + if self.mode == TimerMode::Repeating { self.times_finished_this_tick = (self.elapsed().as_nanos() / self.duration().as_nanos()) as u32; // Duration does not have a modulo @@ -410,12 +410,6 @@ pub enum TimerMode { Repeating, } -impl TimerMode { - pub fn repeating(self) -> bool { - self == Self::Repeating - } -} - #[cfg(test)] #[allow(clippy::float_cmp)] mod tests { From 88ddcc51b9b5938c4a8dcd2fd1583ca676e75284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lena=20Miliz=C3=A9?= Date: Thu, 13 Oct 2022 23:44:52 +0200 Subject: [PATCH 6/6] replace assert_ne with assert_eq MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to check that the timer is what we set it to, and before the condition was backwards. Signed-off-by: Lena Milizé --- crates/bevy_time/src/timer.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_time/src/timer.rs b/crates/bevy_time/src/timer.rs index 22d4675002bdc..6fbdfdc41840d 100644 --- a/crates/bevy_time/src/timer.rs +++ b/crates/bevy_time/src/timer.rs @@ -173,7 +173,7 @@ impl Timer { /// # use bevy_time::*; /// let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating); /// timer.set_mode(TimerMode::Once); - /// assert_ne!(timer.mode(), TimerMode::Repeating); + /// assert_eq!(timer.mode(), TimerMode::Once); /// ``` #[inline] pub fn set_mode(&mut self, mode: TimerMode) { @@ -425,7 +425,7 @@ mod tests { assert!(!t.finished()); assert!(!t.just_finished()); assert_eq!(t.times_finished_this_tick(), 0); - assert_ne!(t.mode(), TimerMode::Repeating); + assert_eq!(t.mode(), TimerMode::Once); assert_eq!(t.percent(), 0.025); assert_eq!(t.percent_left(), 0.975); // Ticking while paused changes nothing @@ -436,7 +436,7 @@ mod tests { assert!(!t.finished()); assert!(!t.just_finished()); assert_eq!(t.times_finished_this_tick(), 0); - assert_ne!(t.mode(), TimerMode::Repeating); + assert_eq!(t.mode(), TimerMode::Once); assert_eq!(t.percent(), 0.025); assert_eq!(t.percent_left(), 0.975); // Tick past the end and make sure elapsed doesn't go past 0.0 and other things update