Latest commit b724a0f

use bevy::{
    core::{FixedTimestep, FixedTimesteps},
    prelude::*,
};

const LABEL: &str = "my_fixed_timestep";

#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
struct FixedUpdateStage;

/// 展示了如何创建一个运行于固定时间步长的系统,而不是每一个tick都运行
fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        // 这个系统会在每次update时运行(应该会与屏幕刷新率匹配)
        .add_system(frame_update)
        // 添加一个新的stage,每两秒运行一次
        .add_stage_after(
            CoreStage::Update,
            FixedUpdateStage,
            SystemStage::parallel()
                .with_run_criteria(
                    FixedTimestep::step(0.5)
                        // 标签是可选的。它可以从系统内部访问当前FixedTimestep的状态
                        .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!("步进百分比: {}。", fixed_timestep.overstep_percentage());

    *last_time = time.seconds_since_startup();
}