mirror of
https://github.com/bevyengine/bevy
synced 2024-11-14 00:47:32 +00:00
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>
69 lines
2.2 KiB
Rust
69 lines
2.2 KiB
Rust
use bevy::{log::info, prelude::*};
|
|
|
|
fn main() {
|
|
App::build()
|
|
.add_plugins(DefaultPlugins)
|
|
.insert_resource(Countdown::default())
|
|
.add_startup_system(setup_system.system())
|
|
.add_system(countdown_system.system())
|
|
.add_system(timer_system.system())
|
|
.run();
|
|
}
|
|
|
|
pub struct Countdown {
|
|
pub percent_trigger: Timer,
|
|
pub main_timer: Timer,
|
|
}
|
|
|
|
impl Countdown {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
percent_trigger: Timer::from_seconds(4.0, true),
|
|
main_timer: Timer::from_seconds(20.0, false),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for Countdown {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
fn setup_system(mut commands: Commands) {
|
|
// Add an entity to the world with a timer
|
|
commands.spawn((Timer::from_seconds(5.0, false),));
|
|
}
|
|
|
|
/// This system ticks all the `Timer` components on entities within the scene
|
|
/// using bevy's `Time` resource to get the delta between each update.
|
|
fn timer_system(time: Res<Time>, mut query: Query<&mut Timer>) {
|
|
for mut timer in query.iter_mut() {
|
|
if timer.tick(time.delta()).just_finished() {
|
|
info!("Entity timer just finished")
|
|
}
|
|
}
|
|
}
|
|
|
|
/// This system controls ticking the timer within the countdown resource and
|
|
/// handling its state.
|
|
fn countdown_system(time: Res<Time>, mut countdown: ResMut<Countdown>) {
|
|
countdown.main_timer.tick(time.delta());
|
|
|
|
// The API encourages this kind of timer state checking (if you're only checking for one value)
|
|
// Additionally, `finished()` would accomplish the same thing as `just_finished` due to the timer
|
|
// being repeating, however this makes more sense visually.
|
|
if countdown.percent_trigger.tick(time.delta()).just_finished() {
|
|
if !countdown.main_timer.finished() {
|
|
// Print the percent complete the main timer is.
|
|
info!(
|
|
"Timer is {:0.0}% complete!",
|
|
countdown.main_timer.percent() * 100.0
|
|
);
|
|
} else {
|
|
// The timer has finished so we pause the percent output timer
|
|
countdown.percent_trigger.pause();
|
|
info!("Paused percent trigger timer")
|
|
}
|
|
}
|
|
}
|