-
Notifications
You must be signed in to change notification settings - Fork 0
/
gapStream.js
50 lines (44 loc) · 1.43 KB
/
gapStream.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
class GapStream {
constructor() {
this.gapArray;
this.currentGapIndex = 0;
this.currentGap = null;
this.initArray();
}
initArray() {
this.gapArray = new Array(GAPARRAYLENGTH);
this.gapArray[0] = new Gap(null);
for (let i = 1; i < GAPARRAYLENGTH; i++) {
var newGap = new Gap(this.gapArray[i - 1]);
this.gapArray[i] = newGap;
}
this.currentGap = this.gapArray[this.currentGapIndex];
}
update() {
this.gapArray.forEach(element => {
element.move();
});
this.updateCurrentGap();
}
draw() {
this.gapArray.forEach(element => {
element.draw();
});
}
updateCurrentGap() {
if (this.currentGap.y - GAPHEIGHT / 2 - 20 > HEIGHT) {
score++;
let nextIndex = (this.currentGapIndex + 1) % GAPARRAYLENGTH;
let previousGap = this.gapArray[(this.currentGapIndex - 1 + GAPARRAYLENGTH) % GAPARRAYLENGTH];
this.gapArray[this.currentGapIndex] = new Gap(previousGap);
this.currentGapIndex = (this.currentGapIndex + 1) % GAPARRAYLENGTH;
this.currentGap = this.gapArray[this.currentGapIndex];
}
}
checkCollition(player) {
return this.currentGap.checkCollition(player);
}
currentGapY() {
return this.currentGap.y;
}
}