-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet.js
96 lines (72 loc) · 1.85 KB
/
bullet.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class Bullet {
constructor() {
this.r = 20;
this.pos = createVector(width / 2, height / 2);
this.vel = createVector(random() + 0.3, random() + 0.5);
this.vel.normalize();
this.vel.mult(sml / 100);
this.life = 3;
this.score = 0;
this.diamond = 0;
}
update() {
if (this.pos.y > height) {
this.gameover();
return;
}
if (this.pos.x > width - this.r) {
this.vel.x = -abs(this.vel.x);
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
} else if (this.pos.x < this.r) {
this.vel.x = abs(this.vel.x);
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
} else if (this.pos.y < this.r) {
this.vel.y = abs(this.vel.y);
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
} else {
this.pos.x += this.vel.x;
this.pos.y += this.vel.y;
}
}
render() {
colorMode(RGB);
fill(col);
ellipse(this.pos.x, this.pos.y, this.r * 2);
}
hits(ship) {
if (ship.pos.y - ship.h / 2 < this.pos.y + this.r && this.pos.x < ship.pos.x + ship.w / 2 && this.pos.x > ship.pos.x - ship.w / 2 && this.pos.y < ship.pos.y) {
snap.play();
this.vel.y = -abs(this.vel.y);
this.pos.y += this.vel.y;
}
}
hitstar(star) {
if (dist(this.pos.x, this.pos.y, star.pos.x, star.pos.y) <= this.r + star.r) {
if (abs(this.pos.y - star.pos.y) < this.r + this.r) {
// this.vel.y *= -1;
this.vel.x *= -1;
} else {
this.vel.y *= -1;
}
this.score += floor(star.r / 5);
pling.stop();
pling.play();
return true;
} else
return false;
}
gameover() {
this.life--;
losetrack.play();
if (this.life > 0) {
this.pos = createVector(width / 2, height / 2);
} else {
gameovershow();
toggle();
//game is over
}
}
}