-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcactus.js
81 lines (74 loc) · 2.11 KB
/
cactus.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
class Cactus {
constructor(size) {
this.aspectRatio = 312 / 892;
const maxSize = raptor.h * 0.6;
this.h = maxSize; // min(size * this.aspectRatio, maxSize);
this.w = this.h * this.aspectRatio;
this.x = window.innerWidth;
this.y = GROUND - this.h;
}
collisionPolygon() {
const points = [
{ x: this.x + this.w, y: this.y + this.h * 0.28 },
{ x: this.x + this.w * 0.7, y: this.y + this.h * 0.28 },
{ x: this.x + this.w * 0.7, y: this.y },
{ x: this.x + this.w * 0.35, y: this.y },
{ x: this.x + this.w * 0.35, y: this.y + this.h * 0.43 },
{ x: this.x, y: this.y + this.h * 0.43 },
{ x: this.x, y: this.y + this.h },
{ x: this.x + this.w, y: this.y + this.h },
];
return points;
}
debugPolygon() {
beginShape();
this.collisionPolygon().forEach(({ x, y }) => {
vertex(x, y);
});
endShape(CLOSE);
}
update() {
this.x -= BACKGROUND_VELOCITY * (width / 1000);
}
}
function getNewMinWidth() {
return raptor.w * 1.5 + Math.floor(Math.random() * raptor.w * 10);
}
class Cactuses {
constructor(img) {
this.img = img;
this.cactuses = [];
}
get minWidthToSpawnNewCactus() {
return getNewMinWidth();
}
addCactus() {
const lastCactus = this.cactuses[this.cactuses.length - 1];
if (lastCactus) {
const distanceToLastCactus = window.innerWidth - lastCactus.x;
if (distanceToLastCactus >= this.minWidthToSpawnNewCactus) {
const randomSize = Math.floor(Math.random() * 30) + 10;
this.cactuses.push(new Cactus(randomSize));
BACKGROUND_VELOCITY += 0.1;
}
} else {
const randomSize = Math.floor(Math.random() * 30) + 10;
this.cactuses.push(new Cactus(randomSize));
}
}
update() {
cactuses.addCactus();
for (let cactus of this.cactuses) {
cactus.update();
image(this.img, cactus.x, cactus.y, cactus.w, cactus.h);
}
this.cactuses = this.cactuses.filter((cactus) => {
const outOfScreen = cactus.x < 0 - cactus.w;
if (outOfScreen) {
score++;
return false;
}
return true;
});
}
}