forked from Code-Bullet/NEAT-Template-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTennisBall.js
More file actions
54 lines (42 loc) · 1.16 KB
/
TennisBall.js
File metadata and controls
54 lines (42 loc) · 1.16 KB
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
class TennisBall {
// Originally named/planned to be a "TennisBall" item, but is now sunglasses.
static originalSpawnPoints = [
{ x: 180, y: 400 },
{ x: 920, y: 600 },
{ x: 600, y: 640 },
{ x: 800, y: 240 }
];
static spawnPoints = [...TennisBall.originalSpawnPoints];
constructor() {
let spawn = random(TennisBall.spawnPoints);
this.x = spawn.x;
this.y = spawn.y;
this.r = 7.5;
this.life = millis() + 15000;
this.idList = [];
}
show() {
fill(120, 40, 255);
noStroke();
image(tennis, this.x, this.y, this.r*2, this.r*2);
}
static resetSpawns() {
TennisBall.spawnPoints = [...TennisBall.originalSpawnPoints];
}
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;
}
}