Skip to content

Commit 95234cf

Browse files
authored
Add new example to explore time
1 parent 7486c1e commit 95234cf

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,16 @@ description = "Illustrates creating custom system parameters with `SystemParam`"
13821382
category = "ECS (Entity Component System)"
13831383
wasm = false
13841384

1385+
[[example]]
1386+
name = "time"
1387+
path = "examples/ecs/time.rs"
1388+
1389+
[package.metadata.example.time]
1390+
name = "Time handling"
1391+
description = "Explains how Time is handled in ECS"
1392+
category = "ECS (Entity Component System)"
1393+
wasm = false
1394+
13851395
[[example]]
13861396
name = "timers"
13871397
path = "examples/ecs/timers.rs"

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ Example | Description
232232
[System Closure](../examples/ecs/system_closure.rs) | Show how to use closures as systems, and how to configure `Local` variables by capturing external state
233233
[System Parameter](../examples/ecs/system_param.rs) | Illustrates creating custom system parameters with `SystemParam`
234234
[System Piping](../examples/ecs/system_piping.rs) | Pipe the output of one system into a second, allowing you to handle any errors gracefully
235+
[Time handling](../examples/ecs/time.rs) | Explains how Time is handled in ECS
235236
[Timers](../examples/ecs/timers.rs) | Illustrates ticking `Timer` resources inside systems and handling their state
236237

237238
## Games

examples/ecs/time.rs

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

0 commit comments

Comments
 (0)