time/
time.rs

1//! An example that illustrates how Time is handled in ECS.
2
3use bevy::{app::AppExit, prelude::*};
4
5use std::{
6    io::{self, BufRead},
7    time::Duration,
8};
9
10fn banner() {
11    println!("This example is meant to intuitively demonstrate how Time works in Bevy.");
12    println!();
13    println!("Time will be printed in three different schedules in the app:");
14    println!("- PreUpdate: real time is printed");
15    println!("- FixedUpdate: fixed time step time is printed, may be run zero or multiple times");
16    println!("- Update: virtual game time is printed");
17    println!();
18    println!("Max delta time is set to 5 seconds. Fixed timestep is set to 1 second.");
19    println!();
20}
21
22fn help() {
23    println!("The app reads commands line-by-line from standard input.");
24    println!();
25    println!("Commands:");
26    println!("  empty line: Run app.update() once on the Bevy App");
27    println!("  q: Quit the app.");
28    println!("  f: Set speed to fast, 2x");
29    println!("  n: Set speed to normal, 1x");
30    println!("  s: Set speed to slow, 0.5x");
31    println!("  p: Pause");
32    println!("  u: Unpause");
33}
34
35fn runner(mut app: App) -> AppExit {
36    banner();
37    help();
38    let stdin = io::stdin();
39    for line in stdin.lock().lines() {
40        if let Err(err) = line {
41            println!("read err: {err:#}");
42            break;
43        }
44        match line.unwrap().as_str() {
45            "" => {
46                app.update();
47            }
48            "f" => {
49                println!("FAST: setting relative speed to 2x");
50                app.world_mut()
51                    .resource_mut::<Time<Virtual>>()
52                    .set_relative_speed(2.0);
53            }
54            "n" => {
55                println!("NORMAL: setting relative speed to 1x");
56                app.world_mut()
57                    .resource_mut::<Time<Virtual>>()
58                    .set_relative_speed(1.0);
59            }
60            "s" => {
61                println!("SLOW: setting relative speed to 0.5x");
62                app.world_mut()
63                    .resource_mut::<Time<Virtual>>()
64                    .set_relative_speed(0.5);
65            }
66            "p" => {
67                println!("PAUSE: pausing virtual clock");
68                app.world_mut().resource_mut::<Time<Virtual>>().pause();
69            }
70            "u" => {
71                println!("UNPAUSE: resuming virtual clock");
72                app.world_mut().resource_mut::<Time<Virtual>>().unpause();
73            }
74            "q" => {
75                println!("QUITTING!");
76                break;
77            }
78            _ => {
79                help();
80            }
81        }
82    }
83
84    AppExit::Success
85}
86
87fn print_real_time(time: Res<Time<Real>>) {
88    println!(
89        "PreUpdate: this is real time clock, delta is {:?} and elapsed is {:?}",
90        time.delta(),
91        time.elapsed()
92    );
93}
94
95fn print_fixed_time(time: Res<Time>) {
96    println!(
97        "FixedUpdate: this is generic time clock inside fixed, delta is {:?} and elapsed is {:?}",
98        time.delta(),
99        time.elapsed()
100    );
101}
102
103fn print_time(time: Res<Time>) {
104    println!(
105        "Update: this is generic time clock, delta is {:?} and elapsed is {:?}",
106        time.delta(),
107        time.elapsed()
108    );
109}
110
111fn main() {
112    App::new()
113        .add_plugins(MinimalPlugins)
114        .insert_resource(Time::<Virtual>::from_max_delta(Duration::from_secs(5)))
115        .insert_resource(Time::<Fixed>::from_duration(Duration::from_secs(1)))
116        .add_systems(PreUpdate, print_real_time)
117        .add_systems(FixedUpdate, print_fixed_time)
118        .add_systems(Update, print_time)
119        .set_runner(runner)
120        .run();
121}