-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ghost.js
54 lines (43 loc) · 1.42 KB
/
Ghost.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
import {OBJECT_TYPE, DIRECTIONS} from './setup.js';
import Character from './Character.js';
class Ghost extends Character {
constructor(speed, startPos, name, movement, scaryTarget, scaryFunc) {
super(speed, startPos, name);
this.movement = movement;
this.startPos = startPos;
this.dir = DIRECTIONS.ArrowRight;
this.isScared = false;
this.scaryTarget = scaryTarget;
this.scaryFunc = scaryFunc;
}
getDiv() {
const div = document.createElement("div");
div.classList.add("ghost", this.name);
document.getElementById("game").prepend(div);
return div;
}
setNextMove(objectExist, pacman, ghosts) {
let move;
if (this.isScared) {
move = this.scaryFunc(this.pos, this.dir, objectExist, this.scaryTarget);
} else {
move = this.movement(this.pos, this.dir, objectExist, pacman, ghosts);
}
this.setNextPositionForAnimation(move.nextMovePos);
this.pos = move.nextMovePos;
this.dir = move.direction;
}
setIsScared(value) {
if (value) {
this.div.classList.add(OBJECT_TYPE.SCARED)
} else {
this.div.classList.remove(OBJECT_TYPE.SCARED)
this.div.style.animation = "";
}
this.isScared = value;
}
moveDiv() {
super.moveDiv(this.dir);
}
}
export default Ghost;