Skip to content

Commit a8376e9

Browse files
Rename Timer::finished and Timer::paused to is_finished and is_paused (#19386)
# Objective Renames `Timer::finished` and `Timer::paused` to `Timer::is_finished` and `Timer::is_paused` to align the public APIs for `Time`, `Timer`, and `Stopwatch`. Fixes #19110
1 parent a575502 commit a8376e9

File tree

12 files changed

+92
-32
lines changed

12 files changed

+92
-32
lines changed

crates/bevy_diagnostic/src/log_diagnostics_plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl LogDiagnosticsPlugin {
190190
time: Res<Time<Real>>,
191191
diagnostics: Res<DiagnosticsStore>,
192192
) {
193-
if state.timer.tick(time.delta()).finished() {
193+
if state.timer.tick(time.delta()).is_finished() {
194194
Self::log_diagnostics(&state, &diagnostics);
195195
}
196196
}
@@ -200,7 +200,7 @@ impl LogDiagnosticsPlugin {
200200
time: Res<Time<Real>>,
201201
diagnostics: Res<DiagnosticsStore>,
202202
) {
203-
if state.timer.tick(time.delta()).finished() {
203+
if state.timer.tick(time.delta()).is_finished() {
204204
Self::for_each_diagnostic(&state, &diagnostics, |diagnostic| {
205205
debug!("{:#?}\n", diagnostic);
206206
});

crates/bevy_time/src/common_conditions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn repeating_after_delay(duration: Duration) -> impl FnMut(Res<Time>) -> boo
167167
let mut timer = Timer::new(duration, TimerMode::Once);
168168
move |time: Res<Time>| {
169169
timer.tick(time.delta());
170-
timer.finished()
170+
timer.is_finished()
171171
}
172172
}
173173

@@ -199,7 +199,7 @@ pub fn repeating_after_real_delay(
199199
let mut timer = Timer::new(duration, TimerMode::Once);
200200
move |time: Res<Time<Real>>| {
201201
timer.tick(time.delta());
202-
timer.finished()
202+
timer.is_finished()
203203
}
204204
}
205205

crates/bevy_time/src/timer.rs

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,34 @@ impl Timer {
5454
}
5555
}
5656

57+
/// Returns `true` if the timer has reached its duration.
58+
///
59+
/// For repeating timers, this method behaves identically to [`Timer::just_finished`].
60+
///
61+
/// # Examples
62+
/// ```
63+
/// # use bevy_time::*;
64+
/// use std::time::Duration;
65+
///
66+
/// let mut timer_once = Timer::from_seconds(1.0, TimerMode::Once);
67+
/// timer_once.tick(Duration::from_secs_f32(1.5));
68+
/// assert!(timer_once.is_finished());
69+
/// timer_once.tick(Duration::from_secs_f32(0.5));
70+
/// assert!(timer_once.is_finished());
71+
///
72+
/// let mut timer_repeating = Timer::from_seconds(1.0, TimerMode::Repeating);
73+
/// timer_repeating.tick(Duration::from_secs_f32(1.1));
74+
/// assert!(timer_repeating.is_finished());
75+
/// timer_repeating.tick(Duration::from_secs_f32(0.8));
76+
/// assert!(!timer_repeating.is_finished());
77+
/// timer_repeating.tick(Duration::from_secs_f32(0.6));
78+
/// assert!(timer_repeating.is_finished());
79+
/// ```
80+
#[inline]
81+
pub fn is_finished(&self) -> bool {
82+
self.finished
83+
}
84+
5785
/// Returns `true` if the timer has reached its duration.
5886
///
5987
/// For repeating timers, this method behaves identically to [`Timer::just_finished`].
@@ -77,6 +105,7 @@ impl Timer {
77105
/// timer_repeating.tick(Duration::from_secs_f32(0.6));
78106
/// assert!(timer_repeating.finished());
79107
/// ```
108+
#[deprecated(since = "0.17.0", note = "Use `is_finished` instead")]
80109
#[inline]
81110
pub fn finished(&self) -> bool {
82111
self.finished
@@ -143,7 +172,7 @@ impl Timer {
143172
/// timer.set_elapsed(Duration::from_secs(2));
144173
/// assert_eq!(timer.elapsed(), Duration::from_secs(2));
145174
/// // the timer is not finished even if the elapsed time is greater than the duration.
146-
/// assert!(!timer.finished());
175+
/// assert!(!timer.is_finished());
147176
/// ```
148177
#[inline]
149178
pub fn set_elapsed(&mut self, time: Duration) {
@@ -230,23 +259,23 @@ impl Timer {
230259
/// assert_eq!(repeating.elapsed_secs(), 0.5);
231260
/// ```
232261
pub fn tick(&mut self, delta: Duration) -> &Self {
233-
if self.paused() {
262+
if self.is_paused() {
234263
self.times_finished_this_tick = 0;
235264
if self.mode == TimerMode::Repeating {
236265
self.finished = false;
237266
}
238267
return self;
239268
}
240269

241-
if self.mode != TimerMode::Repeating && self.finished() {
270+
if self.mode != TimerMode::Repeating && self.is_finished() {
242271
self.times_finished_this_tick = 0;
243272
return self;
244273
}
245274

246275
self.stopwatch.tick(delta);
247276
self.finished = self.elapsed() >= self.duration();
248277

249-
if self.finished() {
278+
if self.is_finished() {
250279
if self.mode == TimerMode::Repeating {
251280
self.times_finished_this_tick = self
252281
.elapsed()
@@ -308,6 +337,25 @@ impl Timer {
308337
self.stopwatch.unpause();
309338
}
310339

340+
/// Returns `true` if the timer is paused.
341+
///
342+
/// See also [`Stopwatch::is_paused`](Stopwatch::is_paused).
343+
///
344+
/// # Examples
345+
/// ```
346+
/// # use bevy_time::*;
347+
/// let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
348+
/// assert!(!timer.is_paused());
349+
/// timer.pause();
350+
/// assert!(timer.is_paused());
351+
/// timer.unpause();
352+
/// assert!(!timer.is_paused());
353+
/// ```
354+
#[inline]
355+
pub fn is_paused(&self) -> bool {
356+
self.stopwatch.is_paused()
357+
}
358+
311359
/// Returns `true` if the timer is paused.
312360
///
313361
/// See also [`Stopwatch::is_paused`](Stopwatch::is_paused).
@@ -322,6 +370,7 @@ impl Timer {
322370
/// timer.unpause();
323371
/// assert!(!timer.paused());
324372
/// ```
373+
#[deprecated(since = "0.17.0", note = "Use `is_paused` instead")]
325374
#[inline]
326375
pub fn paused(&self) -> bool {
327376
self.stopwatch.is_paused()
@@ -338,7 +387,7 @@ impl Timer {
338387
/// let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
339388
/// timer.tick(Duration::from_secs_f32(1.5));
340389
/// timer.reset();
341-
/// assert!(!timer.finished());
390+
/// assert!(!timer.is_finished());
342391
/// assert!(!timer.just_finished());
343392
/// assert_eq!(timer.elapsed_secs(), 0.0);
344393
/// ```
@@ -466,7 +515,7 @@ mod tests {
466515
assert_eq!(t.elapsed_secs(), 0.25);
467516
assert_eq!(t.elapsed_secs_f64(), 0.25);
468517
assert_eq!(t.duration(), Duration::from_secs_f32(10.0));
469-
assert!(!t.finished());
518+
assert!(!t.is_finished());
470519
assert!(!t.just_finished());
471520
assert_eq!(t.times_finished_this_tick(), 0);
472521
assert_eq!(t.mode(), TimerMode::Once);
@@ -477,7 +526,7 @@ mod tests {
477526
t.tick(Duration::from_secs_f32(500.0));
478527
assert_eq!(t.elapsed_secs(), 0.25);
479528
assert_eq!(t.duration(), Duration::from_secs_f32(10.0));
480-
assert!(!t.finished());
529+
assert!(!t.is_finished());
481530
assert!(!t.just_finished());
482531
assert_eq!(t.times_finished_this_tick(), 0);
483532
assert_eq!(t.mode(), TimerMode::Once);
@@ -488,7 +537,7 @@ mod tests {
488537
t.tick(Duration::from_secs_f32(500.0));
489538
assert_eq!(t.elapsed_secs(), 10.0);
490539
assert_eq!(t.elapsed_secs_f64(), 10.0);
491-
assert!(t.finished());
540+
assert!(t.is_finished());
492541
assert!(t.just_finished());
493542
assert_eq!(t.times_finished_this_tick(), 1);
494543
assert_eq!(t.fraction(), 1.0);
@@ -497,7 +546,7 @@ mod tests {
497546
t.tick(Duration::from_secs_f32(1.0));
498547
assert_eq!(t.elapsed_secs(), 10.0);
499548
assert_eq!(t.elapsed_secs_f64(), 10.0);
500-
assert!(t.finished());
549+
assert!(t.is_finished());
501550
assert!(!t.just_finished());
502551
assert_eq!(t.times_finished_this_tick(), 0);
503552
assert_eq!(t.fraction(), 1.0);
@@ -512,7 +561,7 @@ mod tests {
512561
assert_eq!(t.elapsed_secs(), 0.75);
513562
assert_eq!(t.elapsed_secs_f64(), 0.75);
514563
assert_eq!(t.duration(), Duration::from_secs_f32(2.0));
515-
assert!(!t.finished());
564+
assert!(!t.is_finished());
516565
assert!(!t.just_finished());
517566
assert_eq!(t.times_finished_this_tick(), 0);
518567
assert_eq!(t.mode(), TimerMode::Repeating);
@@ -522,7 +571,7 @@ mod tests {
522571
t.tick(Duration::from_secs_f32(1.5));
523572
assert_eq!(t.elapsed_secs(), 0.25);
524573
assert_eq!(t.elapsed_secs_f64(), 0.25);
525-
assert!(t.finished());
574+
assert!(t.is_finished());
526575
assert!(t.just_finished());
527576
assert_eq!(t.times_finished_this_tick(), 1);
528577
assert_eq!(t.fraction(), 0.125);
@@ -531,7 +580,7 @@ mod tests {
531580
t.tick(Duration::from_secs_f32(1.0));
532581
assert_eq!(t.elapsed_secs(), 1.25);
533582
assert_eq!(t.elapsed_secs_f64(), 1.25);
534-
assert!(!t.finished());
583+
assert!(!t.is_finished());
535584
assert!(!t.just_finished());
536585
assert_eq!(t.times_finished_this_tick(), 0);
537586
assert_eq!(t.fraction(), 0.625);
@@ -546,7 +595,7 @@ mod tests {
546595
assert_eq!(t.times_finished_this_tick(), 3);
547596
assert_eq!(t.elapsed_secs(), 0.5);
548597
assert_eq!(t.elapsed_secs_f64(), 0.5);
549-
assert!(t.finished());
598+
assert!(t.is_finished());
550599
assert!(t.just_finished());
551600
t.tick(Duration::from_secs_f32(0.2));
552601
assert_eq!(t.times_finished_this_tick(), 0);
@@ -607,12 +656,12 @@ mod tests {
607656

608657
t.tick(Duration::from_secs_f32(10.0));
609658
assert!(t.just_finished());
610-
assert!(t.finished());
659+
assert!(t.is_finished());
611660
// A paused timer should change just_finished to false after a tick
612661
t.pause();
613662
t.tick(Duration::from_secs_f32(5.0));
614663
assert!(!t.just_finished());
615-
assert!(t.finished());
664+
assert!(t.is_finished());
616665
}
617666

618667
#[test]
@@ -621,11 +670,11 @@ mod tests {
621670

622671
t.tick(Duration::from_secs_f32(10.0));
623672
assert!(t.just_finished());
624-
assert!(t.finished());
673+
assert!(t.is_finished());
625674
// A paused repeating timer should change finished and just_finished to false after a tick
626675
t.pause();
627676
t.tick(Duration::from_secs_f32(5.0));
628677
assert!(!t.just_finished());
629-
assert!(!t.finished());
678+
assert!(!t.is_finished());
630679
}
631680
}

examples/app/plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct PrintMessageState {
4646
}
4747

4848
fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
49-
if state.timer.tick(time.delta()).finished() {
49+
if state.timer.tick(time.delta()).is_finished() {
5050
info!("{}", state.message);
5151
}
5252
}

examples/ecs/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn deal_damage_over_time(
4242
mut state: ResMut<DamageTimer>,
4343
mut events: EventWriter<DealDamage>,
4444
) {
45-
if state.tick(time.delta()).finished() {
45+
if state.tick(time.delta()).is_finished() {
4646
// Events can be sent with 'write' and constructed just like any other object.
4747
events.write(DealDamage { amount: 10 });
4848
}

examples/ecs/state_scoped.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn toggle(
116116
state: Res<State<GameState>>,
117117
mut next_state: ResMut<NextState<GameState>>,
118118
) {
119-
if !timer.0.tick(time.delta()).finished() {
119+
if !timer.0.tick(time.delta()).is_finished() {
120120
return;
121121
}
122122
*next_state = match state.get() {

examples/games/alien_cake_addict.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn move_player(
203203
mut transforms: Query<&mut Transform>,
204204
time: Res<Time>,
205205
) {
206-
if game.player.move_cooldown.tick(time.delta()).finished() {
206+
if game.player.move_cooldown.tick(time.delta()).is_finished() {
207207
let mut moved = false;
208208
let mut rotation = 0.0;
209209

@@ -314,7 +314,7 @@ fn spawn_bonus(
314314
mut rng: ResMut<Random>,
315315
) {
316316
// make sure we wait enough time before spawning the next cake
317-
if !timer.0.tick(time.delta()).finished() {
317+
if !timer.0.tick(time.delta()).is_finished() {
318318
return;
319319
}
320320

examples/games/game_menu.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ mod splash {
101101
time: Res<Time>,
102102
mut timer: ResMut<SplashTimer>,
103103
) {
104-
if timer.tick(time.delta()).finished() {
104+
if timer.tick(time.delta()).is_finished() {
105105
game_state.set(GameState::Menu);
106106
}
107107
}
@@ -215,7 +215,7 @@ mod game {
215215
mut game_state: ResMut<NextState<GameState>>,
216216
mut timer: ResMut<GameTimer>,
217217
) {
218-
if timer.tick(time.delta()).finished() {
218+
if timer.tick(time.delta()).is_finished() {
219219
game_state.set(GameState::Menu);
220220
}
221221
}

examples/time/timers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ fn countdown(time: Res<Time>, mut countdown: ResMut<Countdown>) {
5959
countdown.main_timer.tick(time.delta());
6060

6161
// The API encourages this kind of timer state checking (if you're only checking for one value)
62-
// Additionally, `finished()` would accomplish the same thing as `just_finished` due to the
62+
// Additionally, `is_finished()` would accomplish the same thing as `just_finished` due to the
6363
// timer being repeating, however this makes more sense visually.
6464
if countdown.percent_trigger.tick(time.delta()).just_finished() {
65-
if !countdown.main_timer.finished() {
65+
if !countdown.main_timer.is_finished() {
6666
// Print the percent complete the main timer is.
6767
info!(
6868
"Timer is {:0.0}% complete!",

examples/ui/ui_scaling.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl TargetScale {
115115
}
116116

117117
fn already_completed(&self) -> bool {
118-
self.target_time.finished() && !self.target_time.just_finished()
118+
self.target_time.is_finished() && !self.target_time.just_finished()
119119
}
120120
}
121121

0 commit comments

Comments
 (0)