Cauldron2D.Sectors.Blockmap (Cauldron2D v0.2.2)

Copy Markdown View Source

A sector map's lines, and the bodies moving through it, sorted into square blocks so that movement and traces only look at what is near.

blockmap =
  map
  |> Cauldron2D.Sectors.Blockmap.build(size: 128)
  |> Cauldron2D.Sectors.Blockmap.with_bodies([{:player, {1056.0, -3616.0}, 16.0}, ...])

Cauldron2D.Sectors.Blockmap.lines_near(blockmap, {x, y}, 16.0)
Cauldron2D.Sectors.Blockmap.bodies_near(blockmap, {x, y}, 16.0)

Lines are filed once, when the blockmap is built; bodies move, so with_bodies/2 files a fresh set and replaces the last, and put_body/2 and remove_body/2 file or unfile one as it moves. A body is {id, {x, y}, radius}.

Summary

Functions

The bodies whose box overlaps a box of radius round point.

File the lines of map (a Cauldron2D.Sectors.Map). Option :size, the block's side, default 128.

The blocks a segment passes through, in order from from to to, as block numbers.

The indexes of lines filed in the blocks a box of radius round point touches.

File body, moving it if a body with its id is filed already.

Unfile the body with id, if one is filed.

The blockmap with bodies filed, replacing the ones filed before.

Types

body()

@type body() :: {term(), {number(), number()}, number()}

t()

@type t() :: %Cauldron2D.Sectors.Blockmap{
  bodies: %{required(integer()) => [body()]},
  cells: tuple(),
  columns: pos_integer(),
  filed: %{required(term()) => integer()},
  origin: {float(), float()},
  rows: pos_integer(),
  size: number(),
  widest: float()
}

Functions

bodies_near(blockmap, arg, radius)

@spec bodies_near(t(), {number(), number()}, number()) :: [body()]

The bodies whose box overlaps a box of radius round point.

build(map, opts \\ [])

@spec build(Cauldron2D.Sectors.Map.t(), keyword()) :: t()

File the lines of map (a Cauldron2D.Sectors.Map). Option :size, the block's side, default 128.

cells_of_segment(blockmap, arg)

@spec cells_of_segment(t(), {{number(), number()}, {number(), number()}}) :: [
  non_neg_integer()
]

The blocks a segment passes through, in order from from to to, as block numbers.

lines_near(blockmap, arg, radius)

@spec lines_near(t(), {number(), number()}, number()) :: [non_neg_integer()]

The indexes of lines filed in the blocks a box of radius round point touches.

put_body(blockmap, body)

@spec put_body(t(), body()) :: t()

File body, moving it if a body with its id is filed already.

remove_body(blockmap, id)

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

Unfile the body with id, if one is filed.

with_bodies(blockmap, bodies)

@spec with_bodies(t(), [body()]) :: t()

The blockmap with bodies filed, replacing the ones filed before.