From c909405cdce270fd9e3fee218672fc35c5078827 Mon Sep 17 00:00:00 2001 From: Nuutti Kotivuori Date: Fri, 30 Jun 2023 12:05:46 +0000 Subject: [PATCH] Add new example to explore time --- Cargo.toml | 10 ++++ examples/README.md | 1 + examples/ecs/time.rs | 115 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 examples/ecs/time.rs diff --git a/Cargo.toml b/Cargo.toml index 8e8f2f4997a655..e32b06d360b315 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1231,6 +1231,16 @@ description = "Illustrates creating custom system parameters with `SystemParam`" category = "ECS (Entity Component System)" wasm = false +[[example]] +name = "time" +path = "examples/ecs/time.rs" + +[package.metadata.example.time] +name = "Time handling" +description = "Explains how Time is handled in ECS" +category = "ECS (Entity Component System)" +wasm = false + [[example]] name = "timers" path = "examples/ecs/timers.rs" diff --git a/examples/README.md b/examples/README.md index 382f477e15e8aa..16d085482fc960 100644 --- a/examples/README.md +++ b/examples/README.md @@ -227,6 +227,7 @@ Example | Description [System Closure](../examples/ecs/system_closure.rs) | Show how to use closures as systems, and how to configure `Local` variables by capturing external state [System Parameter](../examples/ecs/system_param.rs) | Illustrates creating custom system parameters with `SystemParam` [System Piping](../examples/ecs/system_piping.rs) | Pipe the output of one system into a second, allowing you to handle any errors gracefully +[Time handling](../examples/ecs/time.rs) | Explains how Time is handled in ECS [Timers](../examples/ecs/timers.rs) | Illustrates ticking `Timer` resources inside systems and handling their state ## Games diff --git a/examples/ecs/time.rs b/examples/ecs/time.rs new file mode 100644 index 00000000000000..653a63bb92d9cb --- /dev/null +++ b/examples/ecs/time.rs @@ -0,0 +1,115 @@ +use bevy::prelude::*; + +use std::io::{self, BufRead}; +use std::time::Duration; + +fn banner() { + println!("This example is meant to intuitively demonstrate how Time works in Bevy."); + println!(); + println!("Time will be printed in three different schedules in the app:"); + println!("- PreUpdate: real time is printed"); + println!("- FixedUpdate: fixed time step time is printed, may be run zero or multiple times"); + println!("- Update: virtual game time is printed"); + println!(); + println!("Max delta time is set to 5 seconds. Fixed timestep is set to 1 second."); + println!(); +} + +fn help() { + println!("The app reads commands line-by-line from standard input."); + println!(); + println!("Commands:"); + println!(" empty line: Run app.update() once on the Bevy App"); + println!(" q: Quit the app."); + println!(" f: Set speed to fast, 2x"); + println!(" n: Set speed to normal, 1x"); + println!(" n: Set speed to slow, 0.5x"); + println!(" p: Pause"); + println!(" u: Unpause"); +} + +fn runner(mut app: App) { + banner(); + help(); + let stdin = io::stdin(); + for line in stdin.lock().lines() { + if let Err(err) = line { + println!("read err: {:#}", err); + break; + } + match line.unwrap().as_str() { + "" => { + app.update(); + } + "f" => { + println!("FAST: setting relative speed to 2x"); + app.world + .resource_mut::>() + .set_relative_speed(2.0); + } + "n" => { + println!("NORMAL: setting relative speed to 1x"); + app.world + .resource_mut::>() + .set_relative_speed(1.0); + } + "s" => { + println!("SLOW: setting relative speed to 0.5x"); + app.world + .resource_mut::>() + .set_relative_speed(0.5); + } + "p" => { + println!("PAUSE: pausing virtual clock"); + app.world.resource_mut::>().pause(); + } + "u" => { + println!("UNPAUSE: resuming virtual clock"); + app.world.resource_mut::>().unpause(); + } + "q" => { + println!("QUITTING!"); + break; + } + _ => { + help(); + } + } + } +} + +fn print_real_time(time: Res>) { + println!( + "PreUpdate: this is real time clock, delta is {:?} and elapsed is {:?}", + time.delta(), + time.elapsed() + ); +} + +fn print_fixed_time(time: Res