Skip to content

Click to move and unit orders ​

Goal ​

Walk player or AI pawns to a point, preview a movement budget, and order selected units in a grid.

Files you will edit ​

  • src/game.ts — selection, orders and the move system.

Steps ​

  1. Give each pawn a characterMovement root. Add stepMoves to your defineGame({ systems: [stepMoves] }) declaration. Your input system supplies world points and entity ids; it decides which buttons select or move.

    ts
    import { createSelection, simpleMoveTo, stopMove, orderMove } from 'gameable';
    
    const selected = createSelection(64);
    // In your input handler, with ctx, pawn and goal supplied by the game:
    selected.set(pawn); // click
    selected.toggle(otherPawn); // shift-click
    simpleMoveTo(ctx, pawn, goal);
    orderMove(ctx, selected.entities, goal, { spacing: 2 });
    stopMove(ctx, pawn);

    Use selected.box(candidates, x0, y0, x1, y1, project, additive) for a screen rectangle. project(entity, out) writes x/y/visible and returns success. Double-click can add matching candidates. Call selected.prune with your alive predicate after deaths. Selection is game-owned: save its entity ids in your game's snapshot if needed.

  2. Allocate preview storage during setup, then reuse it when the goal changes:

    ts
    import { previewPath, type PathPreview } from 'gameable';
    
    const path = new Float32Array(768);
    const preview: PathPreview = {
      count: 0,
      length: 0,
      reachable: false,
      endX: 0,
      endZ: 0,
      budgetX: 0,
      budgetZ: 0,
      withinBudget: false,
    };
    // In your targeting handler:
    previewPath(ctx, from, goal, path, preview, { budget: 5 });

    Draw preview.count XYZ points and the budget marker. NavigationBounds supply the shared navigation grid; without bounds the path is straight. Pending bakes and unreachable or truncated routes report reachable: false. The budget measures the path, including elevation, and clamps at its end.

Verify ​

Order a possessed pawn around a NavigationModifier blocking box. Check moveState(pawn) becomes arrived; order into the box and expect failed. Shift-select several units and check their destinations differ. Preview a short budget and verify the marker lies along the route. Stop a moving pawn and confirm its input and velocity clear. Walking requests survive actor snapshot/restore; query scratch is rebuilt. Commands own walking input while active: avoid feeding manual or AI movement to the same pawn simultaneously.

See also ​