forked from CodingTrain/Flappy-Bird-Clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.js
76 lines (66 loc) · 1.94 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
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/cXgA1d_E-jY
// Pipe is exported (eslint flags)
/* exported Pipe */
class Pipe {
constructor() {
this.spacing = 125;
this.top = random(height / 6, 3 / 4 * height);
this.bottom = this.top + this.spacing;
this.x = width;
this.w = 80;
this.speed = 2;
this.passed = false;
this.highlight = false;
}
hits(bird) {
if (bird.y - bird.height / 2 < this.top || bird.y + bird.height / 2 > this.bottom) {
//if this.w is huge, then we need different collision model
if (bird.x + bird.width / 2 > this.x && bird.x - bird.width / 2 < this.x + this.w) {
this.highlight = true;
this.passed = true;
return true;
}
}
this.highlight = false;
return false;
}
//this function is used to calculate scores and checks if we've went through the pipes
pass(bird) {
if (bird.x > this.x && !this.passed) {
this.passed = true;
return true;
}
return false;
}
drawHalf() {
let howManyNedeed = 0;
let peakRatio = pipePeakSprite.height / pipePeakSprite.width;
let bodyRatio = pipeBodySprite.height / pipeBodySprite.width;
//this way we calculate, how many tubes we can fit without stretching
howManyNedeed = Math.round(height / (this.w * bodyRatio));
//this <= and start from 1 is just my HACK xD But it's working
for (let i = 0; i < howManyNedeed; ++i) {
let offset = this.w * (i * bodyRatio + peakRatio);
image(pipeBodySprite, -this.w / 2, offset, this.w, this.w * bodyRatio);
}
image(pipePeakSprite, -this.w / 2, 0, this.w, this.w * peakRatio);
}
show() {
push();
translate(this.x + this.w / 2, this.bottom);
this.drawHalf();
translate(0, -this.spacing);
rotate(PI);
this.drawHalf();
pop();
}
update() {
this.x -= this.speed;
}
offscreen() {
return (this.x < -this.w);
}
}