bevy/examples/ecs/fixed_timestep.rs
Christopher Durham 644bd5dbc6 Split time functionality into bevy_time (#4187)
# Objective

Reduce the catch-all grab-bag of functionality in bevy_core by minimally splitting off time functionality into bevy_time. Functionality like that provided by #3002 would increase the complexity of bevy_time, so this is a good candidate for pulling into its own unit.

A step in addressing #2931 and splitting bevy_core into more specific locations.

## Solution

Pull the time module of bevy_core into a new crate, bevy_time.

# Migration guide

- Time related types (e.g. `Time`, `Timer`, `Stopwatch`, `FixedTimestep`, etc.) should be imported from `bevy::time::*` rather than `bevy::core::*`.
- If you were adding `CorePlugin` manually, you'll also want to add `TimePlugin` from `bevy::time`.
- The `bevy::core::CorePlugin::Time` system label is replaced with `bevy::time::TimeSystem`.

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2022-05-26 00:27:18 +00:00

52 lines
1.6 KiB
Rust

//! Shows how to create systems that run every fixed timestep, rather than every tick.
use bevy::{
prelude::*,
time::{FixedTimestep, FixedTimesteps},
};
const LABEL: &str = "my_fixed_timestep";
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
struct FixedUpdateStage;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// this system will run once every update (it should match your screen's refresh rate)
.add_system(frame_update)
// add a new stage that runs twice a second
.add_stage_after(
CoreStage::Update,
FixedUpdateStage,
SystemStage::parallel()
.with_run_criteria(
FixedTimestep::step(0.5)
// labels are optional. they provide a way to access the current
// FixedTimestep state from within a system
.with_label(LABEL),
)
.with_system(fixed_update),
)
.run();
}
fn frame_update(mut last_time: Local<f64>, time: Res<Time>) {
info!("update: {}", time.seconds_since_startup() - *last_time);
*last_time = time.seconds_since_startup();
}
fn fixed_update(mut last_time: Local<f64>, time: Res<Time>, fixed_timesteps: Res<FixedTimesteps>) {
info!(
"fixed_update: {}",
time.seconds_since_startup() - *last_time,
);
let fixed_timestep = fixed_timesteps.get(LABEL).unwrap();
info!(
" overstep_percentage: {}",
fixed_timestep.overstep_percentage()
);
*last_time = time.seconds_since_startup();
}