-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacman.js
70 lines (57 loc) · 1.54 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
65
66
67
68
69
70
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;
this.bugStatus = false
}
shouldMove() {
// Don't move before a key is pressed
if (!this.dir) return;
if (this.timer === this.speed) {
this.timer = 0;
return true;
}
let percentage = Math.round(Math.random() * 99) + 1
this.timer++
}
getNextMove(objectExist) {
let nextMovePos = this.pos + this.dir.movement;
// Do we collide with a wall?
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) {
let percentage = Math.round(Math.random() * 99) + 1
dir = DIRECTIONS[e.key];
if(this.bugStatus == false){
this.dir = dir;
}
if (percentage >= 14 && this.bugStatus == true){
this.dir = dir;
}
}
const nextMovePos = this.pos + dir.movement;
if (objectExist(nextMovePos, OBJECT_TYPE.WALL)) return;
};
}
export default Pacman;