# misc/fixedstepper (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/fixedstepper/

Fixed stepper.
- A port of the original misc/fixedstepper.js. Even when frame intervals are uneven, rules (physics, the board's flow) always run in the same step (step_seconds).
- advance(time_delta, step_function) accumulates the given time and calls step_function(step_seconds) each time a full step is filled.
  The return value is the remaining fraction (0 ~ 1). Blending between the previous and current step by this fraction when drawing keeps the steps from jittering against the screen refresh rate.
- Runs at most maximum_steps_per_frame steps per frame. If time remains after that many, the remaining time is discarded.
  (so a slow device that stalls does not get even slower by running all the backlog at once)
- Difference from the original: the original step function stops only when it returns false and may return nothing. The Rust version always returns bool.
  (true continues, false stops this frame's steps there and keeps the remaining time accumulated)

## FixedStepper (struct) (Clone, Debug, Default)

Fixed stepper.
- Passing the frame time to advance every frame runs fixed steps for the accumulated time and returns the fraction to blend.

Example: Advance 0.3125 seconds in 1/8 second steps

```rust
use vanilla::FixedStepper;

let mut fixed_stepper = FixedStepper::new(0.125, 3);
let mut simulated_seconds = 0.0;
let alpha = fixed_stepper.advance(0.3125, |step_seconds| {
	simulated_seconds += step_seconds;
	true
});
assert_eq!(fixed_stepper.get_last_step_count(), 2);
assert_eq!(simulated_seconds, 0.25);
assert!((alpha - 0.5).abs() < 0.000001);
```

- `pub fn new(step_seconds: f64, maximum_steps_per_frame: u32) -> FixedStepper` (constructor, since 0.4.0): Constructor. (time of one step in seconds, upper limit of steps run in one frame. 1 / 60 seconds if the time is invalid, 1 step if the limit is 0)
  - `step_seconds` f64: Time of one step. (seconds) 1 / 60 seconds if 0 or less or not finite.
  - `maximum_steps_per_frame` u32: Upper limit of steps per frame. Raised to 1 if 0.
  - returns FixedStepper: A new fixed stepper with 0 accumulated time.
- `pub fn set_step_seconds(&mut self, step_seconds: f64)` (method, since 0.4.0): Sets the time of one step. (seconds, must be greater than 0 and finite. Otherwise unchanged)
  - `step_seconds` f64: New step time. (seconds)
- `pub fn get_step_seconds(&self) -> f64` (method, since 0.4.0): Returns the duration of one step. (Seconds)
  - returns f64: Current step time. (seconds)
- `pub fn set_maximum_steps_per_frame(&mut self, maximum_steps_per_frame: u32)` (method, since 0.4.0): Sets the upper limit of steps per frame. (at least 1)
  - `maximum_steps_per_frame` u32: New limit. (raised to 1 if 0)
- `pub fn get_maximum_steps_per_frame(&self) -> u32` (method, since 0.4.0): Returns the maximum steps run per frame.
  - returns u32: Current limit. (at least 1)
- `pub fn advance(&mut self, time_delta: f64, mut step_function: impl FnMut(f64) -> bool) -> f64` (method, since 0.4.0): Advances. (runs steps for the given time and returns the remaining fraction (0 ~ 1). Negative or non-finite time counts as 0)
  - `time_delta` f64: This frame's time. (seconds)
  - `step_function` impl FnMut(f64) -> bool: Function to call for each step. Receives the step time (seconds), true continues and false stops this frame's steps there. (the stopped step is not counted and its time stays accumulated)
  - returns f64: Remaining fraction. (accumulated time / step time, 0 ~ 1) Draw by blending the previous and current step by this fraction.

  Example: Time over the limit is discarded, and kept when stopped with false

  ```rust
  use vanilla::FixedStepper;
  
  let mut fixed_stepper = FixedStepper::new(0.125, 3);
  let alpha = fixed_stepper.advance(1.0, |_| true);
  assert_eq!(fixed_stepper.get_last_step_count(), 3);
  assert_eq!(alpha, 0.0);
  
  let alpha = fixed_stepper.advance(0.25, |_| false);
  assert_eq!(fixed_stepper.get_last_step_count(), 0);
  assert_eq!(alpha, 1.0);
  ```

- `pub fn get_alpha(&self) -> f64` (method, since 0.4.0): Remaining fraction. (Accumulated time / one step, 0 ~ 1)
  - returns f64: Accumulated time divided by the step time, clamped to 0 ~ 1.
- `pub fn get_last_step_count(&self) -> u32` (method, since 0.4.0): Number of steps run in the last advance.
  - returns u32: Number of steps fully run in the last advance. (excluding steps stopped with false, 0 after reset)
- `pub fn reset(&mut self)` (method, since 0.4.0): Clears the accumulated time. (When starting a new round or releasing a hold)
