Cauldron2D.Thinkers (Cauldron2D v0.2.2)

Copy Markdown View Source

The things in a world that change by themselves each tick — a door closing, a lift waiting, a light flickering, a creature — kept in a fixed order so a world run twice from the same start runs the same.

thinkers = Cauldron2D.Thinkers.new()
{door, thinkers} = Cauldron2D.Thinkers.add(thinkers, %{kind: :door, sector: 12, speed: 2.0})

{thinkers, world} =
  Cauldron2D.Thinkers.run(thinkers, world, fn thinker, id, world ->
    MyGame.think(thinker, id, world)
  end)

A thinker is any term, filed under the id add/2 gives it. run/3 calls the function for each thinker in the order they were added; it answers {:keep, thinker, world} to carry on or {:done, world} to be removed. A run visits the thinkers that existed when it began; thinkers to start during a run are added with add/2 to the thinkers it returns.

Summary

Functions

Add thinker; gives its id and the thinkers.

How many thinkers there are.

Thinker id, or nil.

No thinkers.

Remove thinker id, if it is there.

Run every thinker once, in order. See the moduledoc.

Every thinker with its id, in order.

Replace thinker id with fun.(thinker); nothing if it is not there.

Types

id()

@type id() :: pos_integer()

t()

@type t() :: %Cauldron2D.Thinkers{
  entries: :gb_trees.tree(id(), term()),
  next: pos_integer()
}

Functions

add(thinkers, thinker)

@spec add(t(), term()) :: {id(), t()}

Add thinker; gives its id and the thinkers.

count(thinkers)

@spec count(t()) :: non_neg_integer()

How many thinkers there are.

get(thinkers, id)

@spec get(t(), id()) :: term() | nil

Thinker id, or nil.

new()

@spec new() :: t()

No thinkers.

remove(thinkers, id)

@spec remove(t(), id()) :: t()

Remove thinker id, if it is there.

run(thinkers, world, fun)

@spec run(t(), world, (term(), id(), world -> {:keep, term(), world} | {:done, world})) ::
  {t(), world}
when world: term()

Run every thinker once, in order. See the moduledoc.

to_list(thinkers)

@spec to_list(t()) :: [{id(), term()}]

Every thinker with its id, in order.

update(thinkers, id, fun)

@spec update(t(), id(), (term() -> term())) :: t()

Replace thinker id with fun.(thinker); nothing if it is not there.