2020-11-27 19:39:33 +00:00
|
|
|
use bevy::{log::info, prelude::*};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::build()
|
|
|
|
.add_plugins(DefaultPlugins)
|
|
|
|
.add_resource(Countdown::default())
|
2020-12-16 05:57:16 +00:00
|
|
|
.add_startup_system(setup_system.system())
|
|
|
|
.add_system(countdown_system.system())
|
|
|
|
.add_system(timer_system.system())
|
2020-11-27 19:39:33 +00:00
|
|
|
.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(commands: &mut 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() {
|
2020-11-28 21:08:31 +00:00
|
|
|
if timer.tick(time.delta_seconds()).just_finished() {
|
2020-11-27 19:39:33 +00:00
|
|
|
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>) {
|
2020-11-28 21:08:31 +00:00
|
|
|
countdown.main_timer.tick(time.delta_seconds());
|
2020-11-27 19:39:33 +00:00
|
|
|
|
|
|
|
// 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
|
2020-11-28 21:08:31 +00:00
|
|
|
.tick(time.delta_seconds())
|
2020-11-27 19:39:33 +00:00
|
|
|
.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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|