Cauldron2D.Camera.Follow (Cauldron2D v0.1.4)

Copy Markdown View Source

A camera that trails its target and can be shaken, as a value the game steps.

follow = Cauldron2D.Camera.Follow.new(ship.pos)
follow = follow |> Cauldron2D.Camera.Follow.follow(ship.pos, dt, 0.3) |> Cauldron2D.Camera.Follow.step(dt)
Cauldron2D.Camera.view(focus: Cauldron2D.Camera.Follow.focus(follow), ...)

follow/4 closes on the target so that the distance halves every lag seconds; a lag of 0 snaps to it. shake/3 starts a shake of an amplitude in tiles that fades to nothing over its time; step/2 advances it. The shake is a fixed pattern of the time elapsed, so a replay shakes the same way.

Summary

Functions

Where the camera looks: its position plus the shake.

Move toward target for dt seconds, the distance halving every lag seconds.

A camera at pos.

The shake's offset now, {0.0, 0.0} when there is none.

Start a shake of amplitude tiles fading over duration seconds; a shake already going is replaced.

Advance the shake by dt seconds; it ends when its time is up.

Types

point()

@type point() :: {float(), float()}

t()

@type t() :: %Cauldron2D.Camera.Follow{
  pos: point(),
  shake: nil | %{amplitude: float(), duration: float(), elapsed: float()}
}

Functions

focus(follow)

@spec focus(t()) :: point()

Where the camera looks: its position plus the shake.

follow(follow, arg, dt, lag)

@spec follow(t(), {number(), number()}, number(), number()) :: t()

Move toward target for dt seconds, the distance halving every lag seconds.

new(arg)

@spec new({number(), number()}) :: t()

A camera at pos.

offset(follow)

@spec offset(t()) :: point()

The shake's offset now, {0.0, 0.0} when there is none.

shake(follow, amplitude, duration)

@spec shake(t(), number(), number()) :: t()

Start a shake of amplitude tiles fading over duration seconds; a shake already going is replaced.

step(follow, dt)

@spec step(t(), number()) :: t()

Advance the shake by dt seconds; it ends when its time is up.