# base/timer (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/base/timer/

Cooldown and repeat timer. (Cooldown and RepeatTimer of the original base/timer.js)
- Time is f64 in seconds. (the same type as the engine's TimeManager::get_time_delta)
- Triggering is activate() in both engines. (the reserved word use is not used as a name)
- The original default arguments (duration = 1, interval = 1, elapsed_event = null) are carried over to Default and the arguments of new.
- The callback of RepeatTimer is `Box<dyn FnMut()>`. (the same form as set_first_touch_callback of a scene) To change game state,
  capture and pass in an `Rc<Cell>` / `Rc<RefCell>`.

## Cooldown (struct) (Clone, Copy, Debug, PartialEq, Default)

Cooldown.
- Standardizes the "timer -= dt; if (timer <= 0) ..." that was written by hand every time.
- After activate() triggers it, is_ready() is false for the given time.

Example: Trigger and become ready again over time

```rust
use vanilla::Cooldown;

let mut cooldown = Cooldown::new(0.5);
assert!(cooldown.activate());
assert!(!cooldown.is_ready());
assert!(!cooldown.activate());
cooldown.tick(0.25);
assert_eq!(cooldown.get_remain_seconds(), 0.25);
assert_eq!(cooldown.get_progress(), 0.5);
cooldown.tick(0.25);
assert!(cooldown.is_ready());
```

- `pub fn new(duration: f64) -> Cooldown` (constructor, since 0.3.2): Constructor. (duration in seconds, 0 if less than 0)
  - `duration` f64: Cooldown time. (seconds, 0 if less than 0)
  - returns Cooldown: A cooldown in the ready state. (remaining time 0)
- `pub fn tick(&mut self, time_delta: f64)` (method, since 0.3.2): Update.
  - `time_delta` f64: Interval since the last frame. (seconds, subtracted from the remaining time, which does not go below 0)
- `pub fn is_ready(&self) -> bool` (method, since 0.3.2): Whether it can be triggered.
  - returns bool: true if the remaining time is 0 or less.
- `pub fn activate(&mut self) -> bool` (method, since 0.3.2): Triggers. (the original activate; if ready, starts the cooldown and returns true)
  - returns bool: true if triggered. false if still cooling down. (if the cooldown time is 0, it is ready again right after triggering)
- `pub fn reset(&mut self)` (method, since 0.3.2): Immediately returns to the ready state.
- `pub fn set_duration(&mut self, duration: f64)` (method, since 0.3.2): Sets the cooldown time. (0 if less than 0)
  - `duration` f64: Cooldown time. (seconds; does not change the current remaining time, used from the next trigger)
- `pub fn get_duration(&self) -> f64` (method, since 0.3.2): Returns the cooldown time.
  - returns f64: Cooldown time. (seconds)
- `pub fn get_remain_seconds(&self) -> f64` (method, since 0.3.2): Returns the remaining time.
  - returns f64: Time remaining until ready again. (seconds, 0 if ready)
- `pub fn get_progress(&self) -> f64` (method, since 0.3.2): Returns the progress ratio. (0 = just triggered, 1 = ready)
  - returns f64: 1 - remaining time / cooldown time. (1 if the cooldown time is 0; can be less than 0 if set_duration after triggering makes it shorter than the remaining time)

## RepeatTimer (struct) (Default)

Repeating timer.
- Calls the callback at every interval. If several intervals passed because a frame ran long, calls it for all the missed ones at once.

Example: Call every 0.25 seconds, catch up on missed calls, pause

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::RepeatTimer;

let spawn_count = Rc::new(Cell::new(0));
let spawn_count_in_event = spawn_count.clone();
let mut repeat_timer = RepeatTimer::new(0.25, Some(Box::new(move || {
	spawn_count_in_event.set(spawn_count_in_event.get() + 1);
})));
repeat_timer.tick(0.125);
assert_eq!(spawn_count.get(), 0);
repeat_timer.tick(0.625);
assert_eq!(spawn_count.get(), 3);
repeat_timer.pause();
repeat_timer.tick(1.0);
assert_eq!(spawn_count.get(), 3);
```

- `pub fn new(interval: f64, elapsed_event: Option<Box<dyn FnMut()>>) -> RepeatTimer` (constructor, since 0.3.2): Constructor. (interval in seconds, elapsed_event is the callback called every interval, runs as soon as it is created)
  - `interval` f64: Interval. (seconds, 0.000001 if less than 0.000001)
  - `elapsed_event` Option<Box<dyn FnMut()>>: Callback called every interval. If None, only time passes.
  - returns RepeatTimer: A repeat timer in the running state. (accumulated time 0)
- `pub fn tick(&mut self, time_delta: f64)` (method, since 0.3.2): Update. (calls the callback once for each interval passed)
  - `time_delta` f64: Interval since the last frame. (seconds, ignored while paused)
- `pub fn start(&mut self)` (method, since 0.3.2): Start.
- `pub fn stop(&mut self)` (method, since 0.3.2): Stops. (also clears the accumulated time)
- `pub fn pause(&mut self)` (method, since 0.3.2): Pauses. (keeps the accumulated time)
- `pub fn set_interval(&mut self, interval: f64)` (method, since 0.3.2): Sets the interval. (lower limit 0.000001 seconds)
  - `interval` f64: Interval. (seconds, the accumulated time is kept as is)
- `pub fn get_interval(&self) -> f64` (method, since 0.3.2): Returns the interval.
  - returns f64: Interval. (seconds)
- `pub fn set_elapsed_event(&mut self, elapsed_event: Option<Box<dyn FnMut()>>)` (method, since 0.3.2): Sets the callback. (if None, only time passes without a callback)
  - `elapsed_event` Option<Box<dyn FnMut()>>: Callback called every interval. (replaces the previous callback)
- `pub fn is_running(&self) -> bool` (method, since 0.3.2): Whether it is running.
  - returns bool: true if running. (true after creation and after start, false after stop and pause)
