-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathscript.js
37 lines (32 loc) · 854 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { directions, keys } from './constants.js'
import Sokoban from './Sokoban.js'
// init
const sokoban = new Sokoban({ level: 1 })
sokoban.render({ restart: true })
// re-render
document.addEventListener('keydown', (event) => {
const playerCoords = sokoban.findPlayerCoords()
switch (event.key) {
case keys.up:
case keys.w:
sokoban.move(playerCoords, directions.up)
break
case keys.down:
case keys.s:
sokoban.move(playerCoords, directions.down)
break
case keys.left:
case keys.a:
sokoban.move(playerCoords, directions.left)
break
case keys.right:
case keys.d:
sokoban.move(playerCoords, directions.right)
break
default:
}
sokoban.render()
})
document.querySelector('button').addEventListener('click', (event) => {
sokoban.render({ restart: true })
})