mwg API
    Preparing search index...

    Interface ReactionRule<TState>

    Custom actions/reactions to changes in any object's state - HP dropping past a threshold, a durability hitting zero, a quest flag flipping true - watched declaratively instead of as a branch cascade of if checks scattered through game code.

    A ReactionRule pairs a when predicate over some state with an action the framework calls the moment that predicate turns true; check() runs every rule against a fresh reading, edge-triggered so a condition that stays true never re-fires on every call. Rules make no assumption about what shape they watch - an actor's StatBlock values, a plain object's own fields (an item's durability, a door's open flag), whatever the game passes to check, so the same table works for a character or an inanimate object alike. A once: true rule (a bond breaking, a boss entering phase two) fires a single time ever and then retires; the default is edge-triggered, firing again each time the condition leaves and returns, the shape a recurring low-health warning needs.

    import { ReactionTable } from '@datamoc/mw_games/core';

    const reactions = new ReactionTable<{ hp: number; maxHp: number }>([
    { id: 'critical', when: (s) => s.hp / s.maxHp <= 0.25, action: () => console.log('low hp!') },
    { id: 'shattered', when: (s) => s.hp <= 0, action: () => console.log('broke'), once: true },
    ]);

    reactions.check({ hp: 40, maxHp: 100 }); // ['critical'] - fires once, not every call after
    reactions.check({ hp: 38, maxHp: 100 }); // [] - still true, already fired
    reactions.check({ hp: 90, maxHp: 100 }); // [] - condition left; 'critical' may fire again later
    interface ReactionRule<TState> {
        action: (state: Readonly<TState>) => void;
        id: string;
        once?: boolean;
        when: (state: Readonly<TState>) => boolean;
    }

    Type Parameters

    • TState
    Index
    action: (state: Readonly<TState>) => void
    id: string
    once?: boolean

    fires once ever, then is never checked again - a boss entering phase two, a bond broken

    when: (state: Readonly<TState>) => boolean