mwg API
    Preparing search index...

    Class TargetingController

    The input-facing half of targeting: a cursor a player moves over cells, the range and line-of-sight rule that decides whether the current aim is legal, an optional preview of the shape, and a confirm/cancel result. It carries no renderer: moveTo takes a cell, so a game converts its own pointer position through its own camera, and the controller hands back cells for the game to draw and resolve. Legality beyond range and sight is the optional validate hook's job, and damage is never the controller's business.

    import { Level, FLOOR, WALL, TargetingController } from '@datamoc/mw_games/roguelike';

    const level = new Level(12, 12, [WALL, FLOOR], 1);
    const aiming = new TargetingController(level, {
    origin: { x: 2, y: 2 },
    range: 6,
    shape: { kind: 'burst', radius: 1 },
    });

    aiming.move(1, 0); // keyboard: one cell east
    aiming.moveTo({ x: 5, y: 2 }); // pointer: the caller resolved the screen point first
    console.log(aiming.valid); // in range and in sight
    console.log(aiming.preview()); // the burst's cells, or [] while the aim is illegal

    const shot = aiming.confirm(); // null when illegal, the target cells when not
    Index
    onCancel: Signal<void> = ...
    onConfirm: Signal<TargetResult> = ...
    onMove: Signal<Step> = ...
    • get distance(): number

      how many cells the cursor is from the origin, in the level's own ruler

      Returns number

    • get valid(): boolean

      whether the current aim is legal: range, sight unless waived, and the game's own rule

      Returns boolean

    • Abandons the aim without confirming; the game closes its own overlay on onCancel.

      Returns void

    • Moves the cursor one cell, for keyboard and gamepad navigation. On a square level the offset is added directly; on a hex level it is looked up among the cursor's six neighbours, since only the level knows which offset means which direction there. A move that would leave the map is ignored rather than clamped to its edge.

      Parameters

      • dx: number
      • dy: number

      Returns void

    • Moves the cursor to a specific cell, for pointer navigation: the caller has already turned a screen point into a cell through its own camera. Off-map cells are ignored.

      Parameters

      Returns void