|
| 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