Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update/FixedUpdate Stepping #8168

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions crates/bevy_app/src/main_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,41 @@ impl Main {
}

world.resource_scope(|world, order: Mut<MainScheduleOrder>| {
let step = world
.get_resource_mut::<Stepping>()
cart marked this conversation as resolved.
Show resolved Hide resolved
.and_then(|mut s| s.try_step());
for label in &order.labels {
if let Some(false) = step {
let label = &**label;
if label.eq(&Update) || label.eq(&RunFixedUpdateLoop) {
continue;
}
}
let _ = world.try_run_schedule_ref(&**label);
}
});
}
}

#[derive(Resource)]
pub enum Stepping {
Disabled,
Enabled { frames: usize },
Copy link
Member

@alice-i-cecile alice-i-cecile Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The frames field is hard to understand at a glance. It either needs a clearer name, or we should embed the "this still needs to step" information out into an enum variant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah im not convinced either. Just a convenience at this point / provides functionality to help test things.

}

impl Stepping {
pub fn try_step(&mut self) -> Option<bool> {
match self {
Stepping::Disabled => None,
Stepping::Enabled { frames } => {
let should_step = *frames > 0;
*frames = frames.saturating_sub(1);
Some(should_step)
}
}
}
}

/// Initializes the [`Main`] schedule, sub schedules, and resources for a given [`App`].
pub struct MainSchedulePlugin;

Expand Down
9 changes: 9 additions & 0 deletions examples/games/breakout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A simplified implementation of the classic game "Breakout".

use bevy::{
app::Stepping,
prelude::*,
sprite::collide_aabb::{collide, Collision},
sprite::MaterialMesh2dBundle,
Expand Down Expand Up @@ -47,14 +48,22 @@ const WALL_COLOR: Color = Color::rgb(0.8, 0.8, 0.8);
const TEXT_COLOR: Color = Color::rgb(0.5, 0.5, 1.0);
const SCORE_COLOR: Color = Color::rgb(1.0, 0.5, 0.5);

fn step_on_press(input: Res<Input<KeyCode>>, mut stepping: ResMut<Stepping>) {
if input.pressed(KeyCode::Space) {
*stepping = Stepping::Enabled { frames: 1 };
}
}

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(Stepping::Enabled { frames: 0 })
.insert_resource(Scoreboard { score: 0 })
.insert_resource(ClearColor(BACKGROUND_COLOR))
.add_event::<CollisionEvent>()
// Configure how frequently our gameplay systems are run
.insert_resource(FixedTime::new_from_secs(1.0 / 60.0))
.add_systems(PreUpdate, step_on_press)
.add_systems(Startup, setup)
// Add our gameplay simulation systems to the fixed timestep schedule
.add_systems(
Expand Down