-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterrain.js
More file actions
195 lines (152 loc) · 5.82 KB
/
Copy pathterrain.js
File metadata and controls
195 lines (152 loc) · 5.82 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
class Terrain {
constructor(_width, _height) {
this.width = _width;
this.height = _height;
// indicates whether or not the terrain is currently stopped
this.isStopped = false;
// x-velocity of the terrain and obstacles
this.velocity = 5;
// stores calculated noises for each x-value
this.topTerrain = [];
this.bottomTerrain = [];
// maximum height of the bottom and top terrain
this.maxTerrainHeight = this.height * 0.25
// offsets for perlin noise function
this.topOffsetX = 0;
this.bottomOffsetX = 100;
// value that increments the noise x-offsets
this.noiseIncrement = 0.006;
// average radius of an obstacle (can be greater or smaller)
this.obstacleRadius = this.height * 0.08;
// maximum offset that is added/substracted to/from the obstacles vertices
this.obstacleOffset = this.obstacleRadius * 0.25;
// create obstacle each x frames
this.framesPerObstacle = 60;
this.obstacles = [];
}
show() {
fill(134, 204, 216);
stroke(113, 175, 186);
strokeWeight(4);
// Show top terrain
beginShape();
vertex(0, 0); // top left corner
for (let i = 0; i < this.width; i++) {
vertex(i, this.topTerrain[i]);
}
vertex(this.width, 0); // top right corner
endShape();
// Show bottom terrain
beginShape();
vertex(0, this.height) // bottom left corner
for (let i = 0; i < this.width; i++) {
vertex(i, this.bottomTerrain[i]);
}
vertex(this.width, this.height) // bottom right corner
endShape();
this.showObstacles();
}
showObstacles() {
for (let i = 0; i < this.obstacles.length; i++) {
this.obstacles[i].show();
}
}
update() {
if (this.isStopped) {
return;
}
for (let i = 0; i < this.width; i++) {
// calculate noises
let topNoise = noise(this.topOffsetX);
let bottomNoise = noise(this.bottomOffsetX);
// map noise values (0 - 1) to corresponding height
let topY = map(topNoise, 0, 1, 0, this.maxTerrainHeight);
let bottomY = map(bottomNoise, 0, 1, this.height, this.height - this.maxTerrainHeight);
// add the mapped values to the terrain arrays
this.topTerrain[i] = topY;
this.bottomTerrain[i] = bottomY;
// increment noise x-offsets
this.topOffsetX += this.noiseIncrement;
this.bottomOffsetX += this.noiseIncrement;
}
// restore noise x-offset and add velocity
this.topOffsetX -= (this.width - this.velocity) * this.noiseIncrement;
this.bottomOffsetX -= (this.width - this.velocity) * this.noiseIncrement;
this.updateObstacles();
}
updateObstacles() {
// add obstacle if frame count is reached
if (frameCount % this.framesPerObstacle == 0) {
this.obstacles.push(this.createObstacle());
}
// update x-position of obstacles
for (let i = this.obstacles.length - 1; i >= 0; i--) {
this.obstacles[i].update();
// remove obstacle when offscreen
if (this.obstacles[i].offscreen()) {
this.obstacles.splice(i, 1);
}
}
}
stop() {
this.isStopped = true;
}
continue() {
this.isStopped = false;
}
createObstacle() {
// number of vertices (points / edges) of the polygon
let vertexAmount = random(4, 10);
let vertices = Utilities.getCircumferencePoints(0, 0, this.obstacleRadius, vertexAmount);
for (let i = 0; i < vertexAmount; i++) {
// apply some randomness to make it noncircular
vertices[i].x += random(-this.obstacleOffset, this.obstacleOffset);
vertices[i].y += random(-this.obstacleOffset, this.obstacleOffset);
}
// get some min. and max. coordinates to determine obstacle position
let minX = 0;
let minY = 0;
let maxY = 0;
for (let i = 0; i < vertices.length; i++) {
minX = vertices[i].x < minX ? vertices[i].y : minX;
minY = vertices[i].y < minY ? vertices[i].y : minY;
maxY = vertices[i].y > maxY ? vertices[i].y : maxY;
}
// position that will be added to all vertices of the polygon
let obstaclePosition = createVector(
this.width + abs(minX),
random(this.maxTerrainHeight + abs(minY), this.height - this.maxTerrainHeight - abs(maxY))
);
for (let i = 0; i < vertices.length; i++) {
// add calculated obstacle position to the vertices
vertices[i].x += obstaclePosition.x;
vertices[i].y += obstaclePosition.y;
}
return new Obstacle(vertices, this.velocity);
}
collidesCircle(posX, posY, radius) {
// circle can potentially collide with top terrain
if (posY - radius <= this.maxTerrainHeight) {
if (posY - radius < this.topTerrain[int(posX)]) {
return true;
}
}
// circle can potentially collide with bottom terrain
if (posY + radius >= this.height - this.maxTerrainHeight) {
if (posY + radius > this.bottomTerrain[int(posX)]) {
return true;
}
}
// position out of view
if (posX < 0 || posX > this.width) {
return true;
}
// loop through all obstacles and check for collisions
for (let i = 0; i < this.obstacles.length; i++) {
if (this.obstacles[i].collidesPoint(posX, posY)) {
return true;
}
}
return false;
}
}