Modifying the Game State
To update the grid, add or remove elements, you can use the game
object, which provides a set of dedicated methods.
getCell
This method lets you access an element at a specific position on the grid and modify its properties:
const game = createGame()
const element = game.getCell(9, 4)
element.visible = false
Note
The available properties are the same as those listed for event targets.
setCell
This method modifies an element directly at a specific position.
It takes three arguments: x
, y
, and an object with the properties to update.
game.setCell(3, 4, {
visible: false,
dialog: 'I am invisible'
})
setAll
setAll
allows you to update all elements that share the same symbol:
game.setAll('#', {
visible: false
})
addToCell
Adds a new element to a given cell.
Parameters: position x
, position y
, and the symbol of the element to add.
game.addToCell(3, 2, '#')
Warning
player
The game.player
object gives you access to the player and lets you change their position
and sprite
:
game.player.position = [5, 6]
game.player.sprite = `
..1..
.111.
11111
.1.1.
.1.1.
`
width
and height
To get the dimensions of the world, use the game.width
and game.height
properties.
These are read-only values.
alert(`width: ${game.width}, height: ${game.height}`)
loadMap
To load a new map, use game.loadMap()
.
It takes two arguments:
- A new
map
string (multiline format), - An optional position to reset the player.
game.loadMap(
`
########
#......#
#......#
#......#
#......#
#......#
#......#
########
`,
[3, 5]
)
🎛️ updateFilter
You can update the current filter settings using the updateFilter
method.
It accepts an object containing only the settings to update (all other settings remain unchanged).
const game = createGame({
filter: {
name: 'fractal',
settings: {
sideCount: 12,
scale: 0.9,
rotation: 0
}
}
})
game.updateFilter({
scale: 0.3
})
Warning
updateFilter
cannot be used to change the filter name, only its settings.