-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacMan.js
64 lines (52 loc) · 1.45 KB
/
PacMan.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { OBJECT_TYPE, DIRECTIONS } from './setup'
class PacMan {
constructor(speed, startPos) {
this.pos = startPos
this.speed = speed
this.dir = null
this.timer = 0
this.powerPill = false
this.rotation = true
}
shouldMove() {
if (! this.dir) return false
if (this.timer === this.speed) {
this.timer = 0
return true
}
this.timer++
}
getNextMove(objectExist) {
let nextMovePos = this.pos + this.dir.movement
if (
objectExist(nextMovePos, OBJECT_TYPE.WALL)
|| objectExist(nextMovePos, OBJECT_TYPE.GHOSTLAIR)
) {
nextMovePos = this.pos
}
return { nextMovePos, direction: this.dir }
}
makeMove() {
const classesToRemove = [OBJECT_TYPE.PACMAN]
const classesToAdd = [OBJECT_TYPE.PACMAN]
return { classesToRemove, classesToAdd }
}
setNewPos(nextMovePos) {
this.pos = nextMovePos
}
handleKeyInput(e, objectExist) {
let dir
if (! e.keyCode >= 37 && ! e.keyCode <= 40) {
return
} else {
dir = DIRECTIONS[e.key]
}
const nextMovePos = this.pos + dir.movement
if (
objectExist(nextMovePos, OBJECT_TYPE.WALL)
|| objectExist(nextMovePos, OBJECT_TYPE.GHOSTLAIR)
) return
this.dir = dir
}
}
export default PacMan