Map Class
Manages map Tiles. Depends on Array2d (array-2d.js).
Constructor
Item Index
Methods
Methods
canMoveThroughTile
-
x
-
y
Checks if a map tile can be moved through.
Parameters:
-
x
Number- The x map tile coord to check.
-
y
Number- The y map tile coord to check.
Returns:
canSeeThroughTile
-
x
-
y
Checks if a map tile can be seen through.
Parameters:
-
x
Number- The x map tile coord to check.
-
y
Number- The y map tile coord to check.
Returns:
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.
loadTilesFromArrayString
-
mapData
-
charToType
-
[defaultTileType]
Loads Tile data from an array of strings
Parameters:
-
mapData
Array- The array of strings to load.
-
charToType
Object- An object mapping string characters to Tile types (see Tile.Types[type]).
-
[defaultTileType]
String optional- The tile type to use if a character is not in charToType. This is used to allow characters representing entites or non-tile objects to be included in the mapData.
Example:
// 'P' will be ignored and a floor tile will be placed at that position
var mapData = [
'####',
'#..#',
'#.P#',
'####',
],
charToType = {
'#': 'wall',
'.': 'floor'
},
defaultTileType = 'floor';
map.loadTilesFromArrayString(mapData, charToType, defaultTileType);
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
-
tile
Sets a value at given coords.
Parameters:
-
x
Number- Position on the x axis of the value being set.
-
y
Number- Position on the y axis of the value being set.
-
tile
Tile | String- The Tile being set at given coords. If Tile is a string a new tile will be created using the string as the Tile Type (see Tile.Types[type]).
Returns:
the tile added
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.