Class CoordMap<D, V>

An abstraction on Map which provides helper functions and serialization for coordinate based storage and lookup.

Type Parameters

  • D extends number[]
  • V = unknown

Implements

Constructors

Properties

[toStringTag]: string = ...
coords: Map<string, V> = ...

Accessors

Methods

  • Delete the element at the coordinate.

    Parameters

    • coordinates: D

      The coordinates to delete from the space.

    Returns boolean

    true if an element in the Map existed and has been removed, or false if the element does not exist.

  • Executes a provided function once per each key/value pair in the CoordMap, in insertion order.

    Parameters

    • callbackfn: ((value, key, map) => void)

      The function to call over each element.

        • (value, key, map): void
        • Parameters

          • value: V
          • key: D
          • map: Map<D, V>

          Returns void

    • Optional thisArg: unknown

      the element representing this

    Returns void

  • Determine if coordinates are set with a value.

    Parameters

    • coordinates: D

      The coordinates to check.

    Returns boolean

    boolean indicating whether an element with the specified key exists or not.

  • Get the value at the provided point in space.

    Parameters

    • coordinates: D

      The coordinates to retrieve from the space.

    Returns undefined | V

    the value stored at the point.

  • Get the value at the provided offset.

    Parameters

    • dimensions: number

      The number of dimensions

    • dimension: number

      The dimension to get the offset

    • offset: -1 | 1

      The amount of offset

    Returns undefined | V

    the value stored at the offset

  • Set the value at the provided point in space.

    Parameters

    • coordinates: D

      The coordinates to set a value.

    • value: V

      The value to set.

    Returns this

    this CoordMap instance.

  • Fill the CoordinateMap with a predefined value. Always square.

    Parameters

    • generator: ((coord) => V)

      Function returning a value to fill.

        • (coord): V
        • Parameters

          • coord: D

          Returns V

    • start: D

      the minimum size of the area to fill.

    • end: D

      the maximum size of the area to fill.

    Returns this

    this CoordMap instance.

  • Convert coordinate map to a simple object for storage and testing.

    Returns Record<string, V>

    An object representing all coordinates in the map. Keys are comma delineated coordinates.

  • Take an array of numbers representing coordinates and convert them into a coordinate key.

    Parameters

    • coordinates: D

      the coordinates to convert.

    Returns string

    A string storing the coordinates of the provided array.

    Example

    // returns "1,-2,3"
    this.encodeCoordinate([1, -2, 3]);
  • Take a stored coordinate key and convert them back into numbers.

    Parameters

    • key: string

      Joined and stringified coordinates.

    Returns D

    An array of numbers representing the coordinates.

    Example

    // returns [1, -2, 3]
    this.decodeCoordinate("1,-2,3");
  • Recursively fill the coordinates with the

    Parameters

    • generator: ((coord) => V)

      Function returning a value to fill.

        • (coord): V
        • Parameters

          • coord: D

          Returns V

    • start: D

      the minimum size of the area to fill.

    • end: D

      the maximum size of the area to fill.

    • state: number[] = []

      the current coordinate state.

    Returns void