Array2d Class
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
Parameters:
-
width
Number- Width of the 2d Array.
-
height
Number- Height of the 2d Array.
Item Index
Methods
Methods
each
-
func
-
[context]
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]
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:
An array of coord values matched by the filter function.
get
-
x
-
y
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:
getAdjacent
-
x
-
y
-
[settings]
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:
An array of adjacent coord values.
getLineThrough
-
x0
-
y0
-
x1
-
y1
-
[condition=false]
-
[withCoords=false]
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:
An array of coord values.
getNearest
-
tileX
-
tileY
-
[settings]
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:
An array of coord values within a square radius of the given coords.
getWithinSquareRadius
-
x
-
y
-
[settings]
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:
An array of coord values within a square radius of the given coords.
remove
-
x
-
y
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
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
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
Updates the size of this Array2d without destroying data.
Parameters:
-
width
Number- The new width.
-
height
Number- The new height.
Properties
data
Array
2d Array data
height
Number
Height of the 2d Array.
width
Number
Width of the 2d Array.