Talos docs

SharedParams

Since 0.7

{@link ViewsHandle.shared}'s options: the framing camera's, and when to split.

Plain text

Signature

export interface SharedParams extends FrameParams {
  /** `true` or absent: split with the defaults. `false`: never split (the same as `frame(1, …)`). */
  split?: SharedSplit | boolean
}

Import: import type { SharedParams } from "@argus/api"

Members

MemberSignatureDescription
splitsplit?: SharedSplit | booleantrue or absent: split with the defaults. false: never split (the same as frame(1, …)).

Example

The tested fixture fixtures/recipes/couch-coop/scenes/behaviors/couch.ts.

import type { Behavior } from "@argus/api"

/**
 * Two players on one keyboard, one screen split down the middle.
 *
 * The scene's `meta.players` (`max: 2`, `keyboard: "split"`) gives player 1 the keys on the left
 * half (WASD) and player 2 the right half (the arrows), and `spawn { for: "player", player: N }`
 * makes `p1` and `p2` theirs; the engine walks each one's controller from their own keys. This
 * behaviour does the rest: it seats player 2 and gives each player a view.
 *
 * Seating: `join: "script"` means only a script seats anyone. A lobby would wait for a press; this
 * seats the right half at once, which is also what lets a headless self-test drive player 2
 * (headless, no key or pad ever joins anyone).
 *
 * Both players home is a win: each pad emits `goal-reached` (`home.ts`), and this counts them.
 */
interface State {
  home: string[]
  won: boolean
  layout: string
  views: number
  seated: number
}

export default {
  init(ctx) {
    // No number is no seat: under `keyboard: "player"` (the default) the keyboard has no halves.
    if (ctx.players.count < 2 && typeof ctx.players.join("keyboard-right") !== "number") {
      throw new Error('player 2 was not seated: "keyboard-right" needs meta.players.keyboard "split"')
    }
    // View 1 is `ctx.camera`; view 2 has a rig of its own. Each follows its player from behind.
    ctx.views.setLayout("left-right")
    for (const [view, id] of [[1, "p1"], [2, "p2"]] as const) {
      ctx.views.set(view, "third-person", { distance: 6, yaw: 0, pitch: -15 })
      ctx.views.follow(view, id)
    }
    ctx.state.home = []
    ctx.state.won = false
    // Kept so a test can read what was set up.
    ctx.state.layout = ctx.views.layout
    ctx.state.views = ctx.views.count
    ctx.state.seated = ctx.players.count
    ctx.events.on("goal-reached", (event) => {
      const goal = (event.data as { goal?: string }).goal
      if (goal === undefined || ctx.state.home.includes(goal)) return
      ctx.state.home.push(goal)
      if (ctx.state.home.length === 2 && !ctx.state.won) {
        ctx.state.won = true
        ctx.events.emit("both-home", { home: ctx.state.home })
      }
    })
  },
} satisfies Behavior<State>