-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.js
77 lines (64 loc) · 1.98 KB
/
pipe.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
class Pipe {
constructor() {
// ou seja a altura é dividida em 4 partes, sendo que o espaço ficara sempre nos quadrantes centrais do canvas
let randomValue = Math.floor(Math.random() * (height * (3/4) - height * (1/4))) + (height * (1/4));
this.space = 130;
if(randomValue + this.space >= height){
this.top = randomValue - this.space;
this.bottom = height - randomValue;
} else {
this.top = randomValue;
this.bottom = height - (randomValue + this.space);
}
this.x = width;
this.w = 30;
this.speed = 2;
this.highlight = false;
}
hits(bird) {
// hits on the left wall
if (this.inside(this.x, this.top, bird) || this.inside(this.x + this.w, this.top, bird)) {
// hits on the corners of the top pipe
this.highlight = true;
} else if (this.inside(this.x, height - this.bottom, bird) || this.inside(this.x + this.w, height - this.bottom, bird)){
// hits on the corners of the bottom pipe
this.highlight = true;
} else if (bird.y - bird.radius <= this.top || bird.y + bird.radius >= height - this.bottom){
if(bird.x + bird.radius >= this.x && bird.x + bird.radius <= this.x + this.w)
this.highlight = true;
}
else {
this.highlight = false;
}
if(this.highlight){
bird.dead();
return true;
}
else
return false;
}
show() {
fill(0,160,68);
if (this.highlight) {
fill(255, 0, 0);
}
rect(this.x, 0, this.w, this.top);
rect(this.x, height-this.bottom, this.w, this.bottom);
}
update() {
this.x -= this.speed;
}
offscreen() {
if (this.x < -this.w) {
return true;
} else {
return false;
}
}
inside(x, y, bird) {
if ( (x - bird.x) ** 2 + (y - bird.y) ** 2 <= bird.radius ** 2)
return true;
else
return false;
}
}