-
Notifications
You must be signed in to change notification settings - Fork 0
/
PipePair.js
80 lines (64 loc) · 2.27 KB
/
PipePair.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
class PipePair {
// Construct pipe pair object
constructor(firstPipe, previousPipe, upToRandNo) {
// Min distance of top pipe from floor
var minDistFromEdge = 50;
// Gap between pipe pairs
this.gap = 160;
this.maxPipeDifference = 300;
this.passed = false;
// Pandomise pipe hights
if (firstPipe) {
this.topHeight = (canvas.height - 30) / 2 - this.gap / 2;
} else {
if (randomPipeHeights.length >= upToRandNo) {
randomPipeHeights.push(floor(random(minDistFromEdge, canvas.height - minDistFromEdge - 30 - this.gap)));
}
this.topHeight = randomPipeHeights[upToRandNo]; //floor(random(minDistFromEdge, canvas.height - minDistFromEdge - 30 - this.gap));
if (previousPipe) {
while (abs(this.topHeight - previousPipe.topHeight) > this.maxPipeDifference) {
randomPipeHeights[upToRandNo] = floor(random(minDistFromEdge, canvas.height - minDistFromEdge - 30 - this.gap));
this.topHeight = randomPipeHeights[upToRandNo];
}
}
}
// Set pipe geometry
this.bottomHeight = canvas.height - this.topHeight - this.gap;
this.bottomPipe = new Pipe(false, this.bottomHeight);
this.topPipe = new Pipe(true, this.topHeight);
}
// Display both pipes
show() {
this.topPipe.show();
this.bottomPipe.show();
}
// Update pipes possition
update(pause) {
this.topPipe.update(pause);
this.bottomPipe.update(pause);
}
// Ensure pipe is on screen
offScreen() {
if (this.bottomPipe.x + this.bottomPipe.width < 0) {
return true;
}
return false;
}
// Cheack if player has passed pipe
playerPassed(playerX) {
if (!this.passed && playerX > this.bottomPipe.x + this.bottomPipe.width) {
this.passed = true;
return true;
}
return false;
}
// Check if player collided with pipe
collided(p) {
return this.bottomPipe.collided(p) || this.topPipe.collided(p);
}
// Update pipe location
setX(newX) {
this.bottomPipe.x = newX;
this.topPipe.x = newX;
}
}