forked from Code-Bullet/NEAT-Template-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnti.js
More file actions
37 lines (29 loc) · 776 Bytes
/
Anti.js
File metadata and controls
37 lines (29 loc) · 776 Bytes
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
class Anti {
constructor(x,y) {;
this.x = x;
this.y = y;
this.r = 7.5;
this.life = millis() + 8000;
this.idList = [];
this.playInvin = false;
}
show() {
image(acorn, this.x, this.y, this.r * 2, this.r * 2);
}
checkCollision(player) {
let playerLeft = player.x;
let playerRight = player.x + player.w;
let playerTop = player.y;
let playerBottom = player.y + player.h;
let closestX = constrain(this.x, playerLeft, playerRight);
let closestY = constrain(this.y, playerTop, playerBottom);
let dx = this.x - closestX;
let dy = this.y - closestY;
let distanceSq = dx * dx + dy * dy;
if (distanceSq < this.r * this.r) {
this.collected = true;
return true;
}
return false;
}
}