-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.js
56 lines (55 loc) · 1.41 KB
/
enemy.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
class enemy {
constructor() {
this.r = 7;
this.width = this.r * 2;
this.pos = createVector(
random(this.r, width - this.r),
random(20 + this.r, height - this.r)
);
this.vel = createVector(0, 0);
this.speed = random(0.4, 0.9);
this.pause = createVector(0, 0);
}
display() {
noStroke();
fill(255, 0, 0);
ellipse(this.pos.x, this.pos.y, this.width, this.width);
}
attack() {
if (length > 0) {
// dtp (Distance To Player)
let dtp = p5.Vector.dist(this.pos, player[0].pos);
if (dtp <= this.r + player[0].r) {
trail.shift();
length -= 1;
}
}
}
move() {
if (ghostPause == false) {
for (let p of player) {
let dtp = p5.Vector.dist(this.pos, player[0].pos);
if (dtp < width / 2) {
this.vel = createVector(p.pos.x - this.pos.x, p.pos.y - this.pos.y);
this.vel.limit(this.speed);
this.pos.add(this.vel);
}
}
}
}
intersects(other) {
for (var i = 0; i < ghost.length; i++) {
if (
this.pos != ghost[i].pos &&
this.pos.dist(ghost[i].pos) < this.width
) {
this.vel = createVector(
ghost[i].pos.x - this.pos.x,
ghost[i].pos.y - this.pos.y
);
ghost[i].vel.limit(ghost[i].speed);
ghost[i].pos.sub(ghost[i].vel);
}
}
}
}