Skip to main content

Module tick

Module tick 

Source
Expand description

A stage’s rules in fixed steps (ADR-0003 §2): TickDriver steps a TickSim as many times as a frame has steps due (FrameInput::ticks), gives each step what the player did since the last one, and hands each step’s events on once. ActionIds turns a game’s action into the id a step’s input holds, and back.

A tick stage reads input from its steps’ TickInput, never from FrameInput::actions: frames and steps don’t line up (a 120 Hz display runs a step every other frame; a late frame runs up to 4), so a press read straight from a frame would be lost on a frame with no step and seen again on a frame with several, and a key’s repeats would count as presses.

use shuyi_core::tick::TickRecord;
use shuyi_core::toy::{Dot, DotEvent};
use shuyi_ui::input::sample::Action;
use shuyi_ui::screen::FrameInput;
use shuyi_ui::tick::TickDriver;

let mut dot = Dot::default();
let mut driver = TickDriver::new().recording(TickRecord::new(made, 1));
// A tap on a frame with no step due: kept for the next step.
let tap = FrameInput::new(vec![Action::CursorRight], 0.004, vec![]);
assert!(driver.update(&mut dot, &tap).is_empty());
// Two steps due: the tap moves the dot in the first, and it stops in
// the second.
let late = FrameInput::new(vec![], 0.033, vec![]).with_ticks(2);
let events = driver.update(&mut dot, &late);
assert_eq!(events, [(2, DotEvent::Stopped { x: 1, y: 0 })]);
assert_eq!(driver.record().map(TickRecord::len), Some(2));

Structs§

TickDriver
Runs a stage’s TickSim in fixed steps: as many a frame as FrameInput::ticks says are due (counted by the platform’s App, 60 a second, at most 4 a frame). It keeps no clock of its own.

Traits§

ActionIds
A game’s action as a step’s input holds it (ActionId): its place in ActionSet::ALL. Every ActionSet has it.