Expand description
Timers a game’s rules set and forget, counted in steps or turns:
Countdown, which fires once and is the cooldown too, and Every,
which repeats. With them, the step rate (STEPS_PER_SECOND) and the
two conversions a length in milliseconds goes through
(steps_from_ms, ms_from_steps).
A timer says when; a tween (ease) says how far, drawn
from the time alone.
§Two kinds of time
Simulation time is steps or turns. A tick game steps its rules once
for each of input.ticks() and advances its timers by 1 in each step,
so a timer fires on the same step at 30 and at 60 frames a second. A
turn game advances by 1 where its turn or round ends, in its own
TurnRules::apply. Timers live in the state
the rules own, so they save, hash (state_hash)
and replay with it. A timer moves only when it is advanced, so a paused
game’s timers wait.
Presentation time is FrameInput::dt(), Ctx::clock_s and
HurryClock, in shuyi-ui: glides, blinks, text typing out, a clock on
screen. It may stay in floats, and no rule ever reads it.
use shuyi_core::timer::Every;
// Something that moves every 8 steps. `ticks` stands in for
// `input.ticks()`, the steps due this frame.
let mut mover = Every::new(8);
let mut x = 0;
for ticks in [1, 1, 2, 4, 1, 0, 2] {
for _ in 0..ticks {
// One step of the rules.
x += mover.advance(1);
}
}
assert_eq!(x, 1);
assert_eq!(mover.until_next(), 5);§Many timers, and things that happen later
Many timers are a BTreeMap<K, Countdown> in the state, never a
HashMap, whose order changes the hash (hash). An event
that happens later is a Countdown kept beside it, which sends it when
it fires.
§Values read from a file
Nothing checks a timer read from a hand-edited save. A value out of range
(left above length, since at or above period) still behaves by the
formulas on each method, and no method panics on it.
Structs§
- Countdown
- A timer that fires once, after
lengthsteps or turns, and waits to be restarted. - Every
- A timer that fires every
periodsteps or turns.
Constants§
- STEPS_
PER_ SECOND - Fixed simulation steps a second.
Functions§
- ms_
from_ steps - The milliseconds
stepssteps last atSTEPS_PER_SECOND, rounded down: 0, 16, 33, 50, … and exactly 1000 at step 60, so it never drifts as a whole number of milliseconds per step would.steps_from_msof it givesstepsback while that fits au32. - steps_
from_ ms - Whole steps in
msmilliseconds atSTEPS_PER_SECOND, rounded up, so any time above 0 is at least one step. It turns a length a designer wrote in milliseconds into steps, once, at load. A timer never counts milliseconds.