From 75d93600072ebd3129ba76a0b3b6014641813e5c Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Wed, 3 Jun 2026 21:20:23 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20-=20WIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snake-game/examples/time.rs | 121 ++++++++++++++++++++++++++++++++++++ snake-game/src/domain.rs | 10 +-- snake-game/src/gameplay.rs | 15 ++++- snake-game/src/lib.rs | 2 +- snake-game/src/main.rs | 8 +-- snake-game/src/observers.rs | 10 +-- snake-game/src/startup.rs | 16 +---- 7 files changed, 151 insertions(+), 31 deletions(-) create mode 100644 snake-game/examples/time.rs diff --git a/snake-game/examples/time.rs b/snake-game/examples/time.rs new file mode 100644 index 0000000..c9f21d9 --- /dev/null +++ b/snake-game/examples/time.rs @@ -0,0 +1,121 @@ +//! An example that illustrates how Time is handled in ECS. + +use bevy::{app::AppExit, prelude::*}; + +use std::{ + io::{self, BufRead}, + 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!(" s: Set speed to slow, 0.5x"); + println!(" p: Pause"); + println!(" u: Unpause"); +} + +fn runner(mut app: App) -> AppExit { + 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_mut() + .resource_mut::>() + .set_relative_speed(2.0); + } + "n" => { + println!("NORMAL: setting relative speed to 1x"); + app.world_mut() + .resource_mut::>() + .set_relative_speed(1.0); + } + "s" => { + println!("SLOW: setting relative speed to 0.5x"); + app.world_mut() + .resource_mut::>() + .set_relative_speed(0.5); + } + "p" => { + println!("PAUSE: pausing virtual clock"); + app.world_mut().resource_mut::>().pause(); + } + "u" => { + println!("UNPAUSE: resuming virtual clock"); + app.world_mut().resource_mut::>().unpause(); + } + "q" => { + println!("QUITTING!"); + break; + } + _ => { + help(); + } + } + } + + AppExit::Success +} + +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