Skip to content

Commit 4881f87

Browse files
committed
replace set_repeating with set_mode
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é <me@lvmn.org>
1 parent 5a58300 commit 4881f87

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

crates/bevy_time/src/timer.rs

+11-20
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ impl Timer {
162162
/// assert!(timer.repeating());
163163
/// ```
164164
#[inline]
165-
pub fn repeating(&self) -> bool {
166-
self.mode.repeating()
165+
pub fn mode(&self) -> TimerMode {
166+
self.mode
167167
}
168168

169169
/// Sets whether the timer is repeating or not.
@@ -176,16 +176,12 @@ impl Timer {
176176
/// assert!(!timer.repeating());
177177
/// ```
178178
#[inline]
179-
pub fn set_repeating(&mut self, repeating: bool) {
180-
if !self.mode.repeating() && repeating && self.finished {
179+
pub fn set_mode(&mut self, mode: TimerMode) {
180+
if !self.mode.repeating() && mode.repeating() && self.finished {
181181
self.stopwatch.reset();
182182
self.finished = self.just_finished();
183183
}
184-
self.mode = if repeating {
185-
TimerMode::Repeating
186-
} else {
187-
TimerMode::Once
188-
};
184+
self.mode = mode;
189185
}
190186

191187
/// Advance the timer by `delta` seconds.
@@ -208,13 +204,13 @@ impl Timer {
208204
pub fn tick(&mut self, delta: Duration) -> &Self {
209205
if self.paused() {
210206
self.times_finished_this_tick = 0;
211-
if self.repeating() {
207+
if self.mode.repeating() {
212208
self.finished = false;
213209
}
214210
return self;
215211
}
216212

217-
if !self.repeating() && self.finished() {
213+
if !self.mode.repeating() && self.finished() {
218214
self.times_finished_this_tick = 0;
219215
return self;
220216
}
@@ -223,7 +219,7 @@ impl Timer {
223219
self.finished = self.elapsed() >= self.duration();
224220

225221
if self.finished() {
226-
if self.repeating() {
222+
if self.mode.repeating() {
227223
self.times_finished_this_tick =
228224
(self.elapsed().as_nanos() / self.duration().as_nanos()) as u32;
229225
// Duration does not have a modulo
@@ -404,24 +400,19 @@ impl Timer {
404400
}
405401

406402
/// Specifies [`Timer`] behavior.
407-
#[derive(Debug, Clone, Copy, Reflect)]
403+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Default, Reflect)]
408404
#[reflect(Default)]
409405
pub enum TimerMode {
410406
/// Run once and stop.
407+
#[default]
411408
Once,
412409
/// Reset when finished.
413410
Repeating,
414411
}
415412

416413
impl TimerMode {
417414
pub fn repeating(self) -> bool {
418-
matches!(self, Self::Repeating)
419-
}
420-
}
421-
422-
impl Default for TimerMode {
423-
fn default() -> Self {
424-
Self::Once
415+
self == Self::Repeating
425416
}
426417
}
427418

0 commit comments

Comments
 (0)