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] 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 } }