forked from stackup-dev/immutable-stackupinvaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Debris.js
55 lines (47 loc) · 1.11 KB
/
Debris.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
class Debris {
constructor() {
this.r = 5;
this.resetDebris();
}
resetDebris() {
this.y = random(height - 10);
this.r = random(5,10)
let spawnLeftSide = random(1) < 0.5;
if (spawnLeftSide) {
this.x = random(-width, 0);
this.isGoingLeft = false;
} else {
this.x = random(width, width * 2);
this.isGoingLeft = true;
}
}
update() {
if (this.isGoingLeft) {
this.x --;
} else {
this.x ++;
}
if (this.isOffScreen()) {
this.resetDebris();
}
}
isOffScreen() {
if (this.isGoingLeft && this.x < -5) {
return true;
} else if(!this.isGoingLeft && this.x > width + 5) {
return true;
}
return false;
}
display() {
fill(100);
noStroke();
ellipse(this.x, this.y, this.r * 2, this.r * 2);
}
hasHitPlayer(player) {
if (dist(this.x, this.y, player.x, player.y) < this.r + player.r) {
return true;
}
return false
}
}