-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pacman.js
59 lines (48 loc) · 1.38 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
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;
if(this.timer === this.speed) {
this.timer = 0;
return true;
}
this.timer++
}
getNextMove(objectExists) {
let nextMovePos = this.pos + this.dir.movement;
if( objectExists(nextMovePos, OBJECT_TYPE.WALL) ||
objectExists(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, objectExists) => {
let dir;
if(e.keyCode >= 37 && e.keyCode <= 40) {
dir = DIRECTIONS[e.key];
} else {
return;
}
const nextMovePos = this.pos + dir.movement;
if(objectExists(nextMovePos, OBJECT_TYPE.WALL)) return;
this.dir = dir;
};
}
export default Pacman;