Cauldron2D.Test (Cauldron2D v0.1.3)

Copy Markdown View Source

Drives a Cauldron2D.Game in a test, with no world process and no clock: join players, hold actions, run ticks, read views and events.

driver = Cauldron2D.Test.new(MyGame, game_opts: [seed: 1], hz: 50)
{:ok, driver} = Cauldron2D.Test.join(driver, :alice, %{username: "alice"})
driver = driver |> Cauldron2D.Test.hold(:alice, [:thrust]) |> Cauldron2D.Test.tick(10)
Cauldron2D.Test.view(driver, :alice)
Cauldron2D.Test.events(driver)

A tick is what Cauldron2D.World does on one step: the input held since the last tick is applied, the game steps by 1 / hz, and its events are drained and kept, oldest first. until/3 ticks until a condition holds or a limit is reached.

Summary

Functions

Every event drained so far, oldest first.

What player id holds from the next tick: a list of actions, an aim, and optionally how hard each action is held.

Join player id with props; the game's refusal is returned as it gave it.

Player id leaves.

A driver for game; options :game_opts (default []) and :hz (default 50).

The game's state.

Run count ticks.

How many ticks have run.

Tick until condition holds of the driver, at most limit times; {:timeout, driver} when it never did.

The game's view for player id now.

Types

t()

@type t() :: %Cauldron2D.Test{
  dt: float(),
  events: [term()],
  game: module(),
  pending: %{required(term()) => Cauldron2D.Input.state()},
  state: term(),
  ticks: non_neg_integer()
}

Functions

events(test)

@spec events(t()) :: [term()]

Every event drained so far, oldest first.

hold(driver, id, actions, aim \\ nil, strength \\ %{})

@spec hold(t(), term(), [atom()], {number(), number()} | nil, %{
  required(atom()) => float()
}) :: t()

What player id holds from the next tick: a list of actions, an aim, and optionally how hard each action is held.

join(driver, id, props)

@spec join(t(), term(), map()) :: {:ok, t()} | {:error, term()}

Join player id with props; the game's refusal is returned as it gave it.

leave(driver, id)

@spec leave(t(), term()) :: t()

Player id leaves.

new(game, opts \\ [])

@spec new(module(), keyword()) :: t()

A driver for game; options :game_opts (default []) and :hz (default 50).

state(test)

@spec state(t()) :: term()

The game's state.

tick(driver, count \\ 1)

@spec tick(t(), pos_integer()) :: t()

Run count ticks.

tick_count(test)

@spec tick_count(t()) :: non_neg_integer()

How many ticks have run.

until(driver, condition, limit)

@spec until(t(), (t() -> boolean()), pos_integer()) :: {:ok, t()} | {:timeout, t()}

Tick until condition holds of the driver, at most limit times; {:timeout, driver} when it never did.

view(driver, id)

@spec view(t(), term()) :: term()

The game's view for player id now.