From 6929d95f7f848294aeb4dba8ff144dbe673f793c Mon Sep 17 00:00:00 2001 From: Cameron <51241057+maniwani@users.noreply.github.com> Date: Fri, 30 Sep 2022 11:37:33 +0000 Subject: [PATCH] Replace fixed timestep in `alien_cake_addict` example with timer (#5760) # Objective Examples should use the correct tools for the job. ## Solution A fixed timestep, by design, can step multiple times consecutively in a single update. That property used to crash the `alien_cake_addict` example (#2525), which was "fixed" in #3411 (by just not panicking). The proper fix is to use a timer instead, since the system is supposed to spawn a cake every 5 seconds. --- A timer guarantees a minimum duration. A fixed timestep guarantees a fixed number of steps per second. Each one works by essentially sacrificing the other's guarantee. You can use them together, but no other systems are timestep-based in this example, so the timer is enough. --- examples/games/alien_cake_addict.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/examples/games/alien_cake_addict.rs b/examples/games/alien_cake_addict.rs index 5ca9ec71b9426..013d71fd6b6f7 100644 --- a/examples/games/alien_cake_addict.rs +++ b/examples/games/alien_cake_addict.rs @@ -2,7 +2,7 @@ use std::f32::consts::PI; -use bevy::{ecs::schedule::SystemSet, prelude::*, time::FixedTimestep}; +use bevy::{ecs::schedule::SystemSet, prelude::*}; use rand::Rng; #[derive(Clone, Eq, PartialEq, Debug, Hash)] @@ -11,9 +11,13 @@ enum GameState { GameOver, } +#[derive(Resource)] +struct BonusSpawnTimer(Timer); + fn main() { App::new() .init_resource::() + .insert_resource(BonusSpawnTimer(Timer::from_seconds(5.0, true))) .add_plugins(DefaultPlugins) .add_state(GameState::Playing) .add_startup_system(setup_cameras) @@ -23,17 +27,13 @@ fn main() { .with_system(move_player) .with_system(focus_camera) .with_system(rotate_bonus) - .with_system(scoreboard_system), + .with_system(scoreboard_system) + .with_system(spawn_bonus), ) .add_system_set(SystemSet::on_exit(GameState::Playing).with_system(teardown)) .add_system_set(SystemSet::on_enter(GameState::GameOver).with_system(display_score)) .add_system_set(SystemSet::on_update(GameState::GameOver).with_system(gameover_keyboard)) .add_system_set(SystemSet::on_exit(GameState::GameOver).with_system(teardown)) - .add_system_set( - SystemSet::new() - .with_run_criteria(FixedTimestep::step(5.0)) - .with_system(spawn_bonus), - ) .add_system(bevy::window::close_on_esc) .run(); } @@ -291,13 +291,17 @@ fn focus_camera( // despawn the bonus if there is one, then spawn a new one at a random location fn spawn_bonus( + time: Res