mwg API
    Preparing search index...

    Class EventPresentation<State, Command, Event, A>

    The documented integration between a turn-based simulation and its presentation, over the two pieces that already exist (simulation.SimulationRuntime and core.PresentationQueue) rather than a third kind of thing. It fixes the five interactions that are easy to get subtly wrong when a game wires them by hand:

    • A command result: submit dispatches through the runtime and hands the returned events to the queue, in one call, so the state commit and the animation are never started from two places that can drift apart.
    • An animation lock: locked is the queue's own isBusy, and submit refuses while it is true - a click during an attack cannot commit a second attack on top of the first.
    • A scheduled secondary actor: followUp returns the commands that follow an outcome, and they are dispatched only once the batch before them has finished presenting, so the counter-attack animation starts after the attack animation, not underneath it.
    • Cancellation: cancel drops everything not yet played. The simulation state is not rolled back, because the commands it already committed did happen; cancelling stops the show, it does not undo the turn.
    • Save/load: snapshot saves the simulation only, deliberately not the queue. A restored run comes back idle, so loading mid-animation skips the rest of that batch instead of replaying it on top of the loaded state.
    import { EventPresentation } from '@datamoc/mw_games/simulation';
    import type { SimulationRuntime } from '@datamoc/mw_games/simulation';
    import type { Actor } from '@datamoc/mw_games/roguelike';

    interface Fighter extends Actor {
    id: string;
    }

    declare const runtime: SimulationRuntime<{ hp: number }, 'attack' | 'counter', { amount: number }, Fighter>;

    const presentation = new EventPresentation({
    runtime,
    play: (event) => 0.25 * event.amount, // a hit is a quarter second per point
    followUp: (outcome): readonly ('attack' | 'counter')[] =>
    outcome.events.length > 0 ? ['counter'] : [],
    });

    presentation.submit('attack'); // commits the attack and starts its animation
    presentation.submit('attack'); // null - the first is still playing
    presentation.update(1 / 60); // drains the queue, then the scheduled counter
    presentation.cancel(); // abandons whatever is left; the committed state stands
    const saved = presentation.snapshot(); // the simulation only

    Type Parameters

    Index
    • Abandons every event not yet presented, current one included, and any scheduled follow-ups. The simulation's committed state is untouched: cancellation stops the presentation, not the turn.

      Returns void