# core/timemanager (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/timemanager/

Time manager. (the original core/timemanager.js)
- The engine feeds the elapsed seconds every frame, and it tracks the accumulated time, frame interval, frames per second and time scale.
- In a scene, read it with engine.get_time_manager() and change the scale and limit with engine.get_time_manager_mut().

## TimeManager (struct) (Default)

Time manager.
- The original receives the request_animation_frame timestamp (milliseconds). Here it receives the seconds elapsed since the engine started.
- The limit of one frame interval defaults to 0.25 seconds as in the original and is changed with set_maximum_time_delta.

Example: Feed elapsed seconds and read the frame interval and scale

```rust
use vanilla::TimeManager;

let mut time_manager = TimeManager::new();
time_manager.update(0.0);
time_manager.update(0.1);
assert!((time_manager.get_unscale_delta_time() - 0.1).abs() < 1e-6);
time_manager.set_time_scale(0.5);
assert!((time_manager.get_time_delta() - 0.05).abs() < 1e-6);
time_manager.update(2.0);
assert_eq!(time_manager.get_unscale_delta_time(), 0.25);
assert!((time_manager.get_time() - 0.35).abs() < 1e-9);
```

Example: Run slowly in a scene and read the frame interval

```rust
use vanilla::{Engine, EngineConfiguration, Scene, SceneBase};

struct MainScene {
	scene_base: SceneBase,
}

impl Scene for MainScene {
	fn get_scene_base(&self) -> &SceneBase {
		&self.scene_base
	}

	fn get_scene_base_mut(&mut self) -> &mut SceneBase {
		&mut self.scene_base
	}

	fn load(&mut self, engine: &Engine) {
		let mut time_manager = engine.get_time_manager_mut();
		time_manager.set_time_scale(0.5);
		time_manager.set_maximum_time_delta(0.1);
	}

	fn tick(&mut self, time_delta: f64) {
		let engine = Engine::get_engine().unwrap();
		self.scene_base.tick(time_delta);
		let time_manager = engine.get_time_manager();
		let unscaled_time_delta = time_manager.get_unscale_delta_time();
		let frame_per_second = time_manager.get_frame_per_second();
		println!("{} {} {}", time_delta, unscaled_time_delta, frame_per_second);
	}
}

let engine_configuration = EngineConfiguration::new();
let engine = Engine::new(engine_configuration);
let main_scene = MainScene {
	scene_base: SceneBase::new(),
};
engine.run(Box::new(main_scene)).unwrap();
```

- `pub fn new() -> TimeManager` (constructor, since 0.3.2): Constructor.
  - returns TimeManager: A new time manager. (time 0, scale 1, limit 0.25 seconds, waiting for the first update)
- `pub fn update(&mut self, seconds_since_startup: f64)` (method, since 0.3.2): Update. (seconds_since_startup: seconds elapsed since the engine started)
  - The first call takes that time as the start, with an interval of 0. After that, the difference from the last call is clamped to 0 ~ limit and added to the accumulated time.
  - When 1 second has passed since the last frames per second was settled, the frames counted meanwhile are settled as the frames per second.
  - `seconds_since_startup` f64: Time elapsed since the engine started. (seconds, a value that increases every frame)

  Example: The first call has an interval of 0, and large intervals are clamped at the limit

  ```rust
  use vanilla::TimeManager;
  
  let mut time_manager = TimeManager::new();
  time_manager.update(10.0);
  assert_eq!(time_manager.get_unscale_delta_time(), 0.0);
  time_manager.update(15.0);
  assert_eq!(time_manager.get_unscale_delta_time(), 0.25);
  assert_eq!(time_manager.get_realtime_since_startup(), 15.0);
  ```

- `pub fn get_realtime_since_startup(&self) -> f64` (method, since 0.3.2): Time elapsed since the engine started. (seconds)
  - returns f64: Elapsed seconds fed to the last update. (0 before update)
- `pub fn get_time(&self) -> f64` (method, since 0.3.2): Accumulated game time. (seconds, sum of values after the frame interval limit)
  - returns f64: Accumulated time. (seconds, not multiplied by the scale)
- `pub fn get_unscale_delta_time(&self) -> f64` (method, since 0.3.2): Frame interval not multiplied by the scale. (seconds)
  - returns f64: This frame's interval. (seconds, 0 ~ limit)
- `pub fn get_time_delta(&self) -> f64` (method, since 0.3.2): Frame interval multiplied by the time scale. (seconds)
  - returns f64: This frame's interval times the time scale. (seconds, negative if the scale is negative)
- `pub fn get_frame_per_second(&self) -> u32` (method, since 0.3.2): Number of frames in the last 1 second.
  - returns u32: Frames per second. (0 before the first 1 second passes)
- `pub fn set_time_scale(&mut self, time_scale: f64)` (method, since 0.3.2): Sets the time scale. (the value as given, as in the original, get_time_delta is negative too if it is negative)
  - `time_scale` f64: Time scale. (1 is normal, 0.5 is half speed, 0 stops, default 1)
- `pub fn get_time_scale(&self) -> f64` (method, since 0.3.2): Returns the time scale.
  - returns f64: Current time scale. (default 1)
- `pub fn set_maximum_time_delta(&mut self, seconds: f64)` (method, since 0.4.0): Sets the limit of one frame interval. (seconds, default 0.25. Set to 0.25 if less than 0 or not finite)
  - The first frame interval after the window is hidden and returns or after a long stall is clamped to this value. Games that must not advance a lot at once lower it, like 0.1 or 0.05.
  - `seconds` f64: Limit of one frame interval. (seconds, a finite value of 0 or more)

  Example: Lower the limit, and invalid values fall back to the default

  ```rust
  use vanilla::TimeManager;
  
  let mut time_manager = TimeManager::new();
  time_manager.set_maximum_time_delta(0.05);
  assert_eq!(time_manager.get_maximum_time_delta(), 0.05);
  time_manager.set_maximum_time_delta(-1.0);
  assert_eq!(time_manager.get_maximum_time_delta(), 0.25);
  ```

- `pub fn get_maximum_time_delta(&self) -> f64` (method, since 0.4.0): Returns the upper limit of one frame interval. (seconds)
  - returns f64: Current limit. (seconds, default 0.25)
