API Docs for:
Show:

Array2d Class

Defined in: src/array-2d.js:4

Manages a 2d array of values mapped to x,y coords. coord data methods to help with data maniuplation and searching (getAdjacent, findNearest, filter, getWithinSquareRadius etc.)

Constructor

Array2d

(
  • width
  • height
)

Defined in src/array-2d.js:4

Parameters:

  • width Number
    • Width of the 2d Array.
  • height Number
    • Height of the 2d Array.

Methods

copy

() Array2d

Defined in src/array-2d.js:437

Creates a copy of this Array2d. Shallow copies values.

Returns:

each

(
  • func
  • [context]
)
Array2d

Defined in src/array-2d.js:455

Loops over each coord value.

Parameters:

  • func Function
    • A function to call on each coord value. (function(value, x, y){})
  • [context] Object optional
    • Context to call the function with (func.call(context, val, x, y))

Returns:

filter

(
  • filter
  • [withCoords=false]
)
Array

Defined in src/array-2d.js:408

Retrieves an array of the filtered values.

Parameters:

  • filter Function
    • A function to determine if a value is to be included in results (returns true). (function(value, x, y){ return true;})
  • [withCoords=false] Bool optional
    • If true the returned array will include the coords of each value ([{x: 0, y: 0, value: 1}, ...])

Returns:

Array:

An array of coord values matched by the filter function.

get

(
  • x
  • y
)
Mixed

Defined in src/array-2d.js:89

Gets a value from given coords.

Parameters:

  • x Number
    • Map tile x coord of the value being retrieved.
  • y Number
    • Map tile y coord of the value being retrieved.

Returns:

Mixed:

getAdjacent

(
  • x
  • y
  • [settings]
)
Array

Defined in src/array-2d.js:113

Retrieves an array of values of adjacent coords.

Parameters:

  • x Number
    • Map tile x coord to get adjacent values of.
  • y Number
    • Map tile y coord to get adjacent values of.
  • [settings] Object optional

    -

    • [withCoords=false] Bool optional
      • If true the returned array will include the coords of each value ([{x: 0, y: 0, value: 1}, ...])
    • [withDiagonals=true] Bool optional
      • If true diagonals will be included.
    • [filter=false] Function optional
      • A function to filter the values returned (function(value, x, y){ return true;})

Returns:

Array:

An array of adjacent coord values.

getLineThrough

(
  • x0
  • y0
  • x1
  • y1
  • [condition=false]
  • [withCoords=false]
)
Array

Defined in src/array-2d.js:258

Retrieves an array of values of coords along a line starting at point 0 and crossing point 1 until it hits the edge of the 2d array or a coord value returning true when passed to the condtion function.

Parameters:

  • x0 Number
    • Map tile x coord of start.
  • y0 Number
    • Map tile y coord of start.
  • x1 Number
    • Map tile x coord of crossing.
  • y1 Number
    • Map tile y coord of crossing.
  • [condition=false] Function optional
    • A function to determine when to end the line. A coord value returning true when passed to the function will end the line. (function(value, x, y){ return true;})
  • [withCoords=false] Bool optional
    • If true the returned array will include the coords of each value ([{x: 0, y: 0, value: 1}, ...])

Returns:

Array:

An array of coord values.

getNearest

(
  • tileX
  • tileY
  • [settings]
)
Array

Defined in src/array-2d.js:313

Retrieves an array of the nearest coord values meeting checked requirements. If multiple coord values were matched at the same nearest distance, the returned array will contain multiple matched coord values. Used for projecting path of ranged attacks, pushed entities, ect.

Parameters:

  • tileX Number
    • Map tile x coord of the center of the radius.
  • tileY Number
    • Map tile x coord of the center of the radius.
  • [settings] Object optional

    -

    • [maxRadius=1] Number optional
      • Maxium search radius from given coord.
    • [filter=false] Function optional
      • A function to determine when the desired coord value is matched. A coord value returning true when passed to the function would be added to the list of results. (function(value, x, y){ return true;}) If no check function is provided any tile with a truthy value will be matched.
    • [withCoords=false] Bool optional
      • If true the returned array will include the coords of each value ([{x: 0, y: 0, value: 1}, ...])

Returns:

Array:

An array of coord values within a square radius of the given coords.

getWithinSquareRadius

(
  • x
  • y
  • [settings]
)
Array

Defined in src/array-2d.js:194

Retrieves an array of values of coords within a given radius.

Parameters:

  • x Number
    • Map tile x coord at the center of the radius.
  • y Number
    • Map tile x coord at the center of the radius.
  • [settings] Object optional

    -

    • [radius=1] Number optional
      • Radius of the area to retrieve tiles from.
    • [filter=false] Function optional
      • A function to filter the values returned (function(value, x, y){ return true;})
    • [withCoords=false] Bool optional
      • If true the returned array will include the coords of each value ([{x: 0, y: 0, value: 1}, ...])
    • [includeTarget=false] Bool optional
      • If true the value of the coordinates given will be included in the returned array.

Returns:

Array:

An array of coord values within a square radius of the given coords.

remove

(
  • x
  • y
)

Defined in src/array-2d.js:103

Removes a value from given coords.

Parameters:

  • x Number
    • Map tile x coord of the value being removed.
  • y Number
    • Map tile y coord of the value being removed.

reset

(
  • width
  • height
)

Defined in src/array-2d.js:42

Resets the 2d array, clearing all data and initializing with this.width and this.height.

Parameters:

  • width Number
    • The new width.
  • height Number
    • The new height.

set

(
  • x
  • y
  • value
)

Defined in src/array-2d.js:75

Sets a value at given coords.

Parameters:

  • x Number
    • Map tile x coord of the value being set.
  • y Number
    • Map tile y coord of the value being set.
  • value Mixed
    • The value being set at given coords.

setSize

(
  • width
  • height
)

Defined in src/array-2d.js:55

Updates the size of this Array2d without destroying data.

Parameters:

  • width Number
    • The new width.
  • height Number
    • The new height.

Properties

data

Array

Defined in src/array-2d.js:35

2d Array data

height

Number

Defined in src/array-2d.js:28

Height of the 2d Array.

width

Number

Defined in src/array-2d.js:21

Width of the 2d Array.