-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
328 lines (275 loc) · 8.18 KB
/
sketch.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* ____ _____ _
* | _ \ | __ (_)
* | |_) | _____ _| |__) | _ __ ___ ___
* | _ < / _ \ \/ / ___/ | '_ \ / _ \/ __|
* | |_) | (_) > <| | | | |_) | __/\__ \
* |____/ \___/_/\_\_| |_| .__/ \___||___/ by Alon Dattner
* | |
* |_|
*
* Inspired by "3D Pipes" screensaver for Windows
**/
// You can adjust the colors to your preference
let boxColors = ["#f94144", "#f3722c", "#f8961e", "#f9844a", "#f9c74f", "#90be6d", "#43aa8b", "#4d908e", "#577590", "#277da1"];
// DON'T CHANGE THESE!
let blockSize = 50;
let gridWidth = 1000;
let gridHeight = 500;
let pipes = [];
let stars = [];
/**
* Setup scene
*/
function setup() {
// General settings
createCanvas(1280, 720, WEBGL);
angleMode(DEGREES);
frameRate(20);
// Add pipe
pipes.push(new Pipe());
// Add stars
for(let i = 0; i<100; i++) {
stars.push(new Star());
}
}
/**
* Draw scene
*/
function draw() {
// Set background, camera and lighting
background(0);
camera(gridWidth/6,-gridHeight/4,gridWidth*2.25);
ambientLight(150,150,150);
directionalLight(255,255,255,-0.5,0.5,-0.5);
// Draw stars
for(let i = stars.length-1; i>=0; i--) {
stars[i].render();
stars[i].update();
}
// Draw pipes
for(let i = pipes.length-1; i >= 0; i--) {
pipes[i].render();
pipes[i].update();
}
}
/**
* Block Class
*/
class Block {
constructor(position, blockColor) {
this.position = createVector(position.x, position.y, position.z);
this.blockColor = blockColor;
}
/**
* Renders the block
*/
render() {
push();
strokeWeight(5);
fill(this.blockColor);
translate(this.position.x,this.position.y,this.position.z);
box(blockSize);
pop();
}
/**
* Returns the position
*/
getPosition() {
return this.position.copy();
}
}
/**
* Star Class
*/
class Star {
constructor() {
this.position = createVector(random(-gridWidth*4, gridWidth*4), random(-gridHeight*4, gridHeight*4), -gridWidth*1.5);
this.size = random(3,10);
}
/**
* Updates the star
*/
update() {
// Updates position based on mouse position
this.position.add(createVector(this.size*(mouseX-1280/2)*0.001, this.size*(mouseY-720/2)*0.001,0));
// Updates position if star leaves screen
if(this.position.x > gridWidth*4 || this.position.x < -gridWidth*4) this.position.x = -this.position.x;
if(this.position.y > gridHeight*4 || this.position.y < -gridHeight*4) this.position.y = -this.position.y;
}
/**
* Renders the star
*/
render() {
push();
noStroke();
fill("white");
translate(this.position.x, this.position.y, this.position.z);
sphere(this.size);
pop();
}
}
/**
* Pipe Class
*/
class Pipe {
constructor() {
// Array will contain all blocks of pipe
this.blocks = [];
// Find random possible starting position
do {
this.position = createVector(
this.getRandomStartCoordinate(-gridWidth, gridWidth),
this.getRandomStartCoordinate(-gridHeight, gridHeight),
this.getRandomStartCoordinate(-gridWidth, gridWidth));
} while(!this.isPossibleNewPosition(this.position))
// Find random possible starting velocity (moving direction)
let possibleVelocities = [
createVector(50,0,0),
createVector(0,50,0),
createVector(0,0,50),
createVector(-50,0,0),
createVector(0,-50,0),
createVector(0,0,-50)
];
this.velocity = random(possibleVelocities);
// Set random starting section length
this.sectionLength = floor(random(5,20));
// Set random color
let boxColorIndex = floor(random(boxColors.length));
this.boxColor = boxColors[boxColorIndex];
// Remove color from possible box colors so there are no pipes with same color at the same time
boxColors.splice(boxColorIndex, 1);
// Add first block
let newBlock = new Block(this.position.copy(), this.boxColor)
this.blocks.push(newBlock);
}
/**
* Updates the pipe
*/
update() {
// Remove pipe if delete = true
if(this.delete) {
// Remove blocks while deleting
if(this.blocks.length>0) {
this.blocks.splice(0,1);
}
// Remove pipe when fully deleted
else {
// Add color back to possible box colors
boxColors.push(this.boxColor);
// Remove pipe completely
pipes.splice(0,1);
}
return;
}
// Set delete = true when pipe reached max length
if(!this.delete && this.blocks.length >= 200) {
this.delete = true;
}
// Calculate new position
let calculatedNewPosition = this.position.copy().add(this.velocity);
// Check if new position is possible
if(!this.isPossibleNewPosition(calculatedNewPosition)) {
// All possible velocities
let possibleVelocities = [
createVector(50,0,0),
createVector(0,50,0),
createVector(0,0,50),
createVector(-50,0,0),
createVector(0,-50,0),
createVector(0,0,-50)
];
// Loop through each possible velocity
for(let i = possibleVelocities.length-1; i >= 0; i--) {
// Calculate new position with possible velocity
calculatedNewPosition = this.position.copy().add(possibleVelocities[i]);
// Remove velocity from possible velocities if new position is not possible
if(!this.isPossibleNewPosition(calculatedNewPosition) ||
possibleVelocities[i].equals(this.velocity.copy().mult(-1))) {
possibleVelocities.splice(i, 1);
}
}
// Set new random velocity from possible velocities
this.velocity = random(possibleVelocities);
// Set delete = true and return if there is no possible velocity
if(!this.velocity) {
this.delete = true;
return;
}
}
// Add velocity to position and create new block
this.position.add(this.velocity);
let newBlock = new Block(this.position, this.boxColor);
this.blocks.push(newBlock);
// Reduce sectionLength
this.sectionLength--;
// Create new pipe if pipe length is 100
if(this.blocks.length == 100) {
pipes.push(new Pipe());
}
}
/**
* Renders the pipe
*/
render() {
// Render all blocks of pipe
for(let block of this.blocks) {
block.render();
}
}
/**
* Returns wether a new position is possible
*/
isPossibleNewPosition(newPosition) {
// Return false if section length is reached
if(this.sectionLength <= 0) {
this.sectionLength = floor(random(5,20));
return false;
}
// Return false if grid limits are reached
if(newPosition.x > gridWidth ||
newPosition.x < -gridWidth ||
newPosition.y > gridHeight ||
newPosition.y < -gridHeight ||
newPosition.z > gridWidth ||
newPosition.z < -gridWidth) {
return false;
}
// Return false if pipe collides with a block
for(let pipe of pipes) {
for(let block of pipe.getAllBlocks()) {
let blockPosition = block.getPosition();
if(newPosition.x == blockPosition.x && newPosition.y == blockPosition.y && newPosition.z == blockPosition.z) return false;
}
}
return true;
}
/**
* Returns all blocks
*/
getAllBlocks() {
return this.blocks;
}
/**
* Returns a random start coordinate inside the min and max (considering the grid and block size)
*
* Disclamer:
* ChatGPT was used to "generate a function to find a random number between two numbers which can be divided by 50"
* I then customized the function to fit my needs (considering the grid and block size)
*/
getRandomStartCoordinate(min, max) {
min = Math.ceil(min / blockSize) * blockSize;
max = Math.floor(max / blockSize) * blockSize;
return Math.floor(random((max - min) / blockSize + 1)) * blockSize + min;
}
}
/**
* Pauses loop (for debugging only)
*/
/*function mousePressed() {
if(isLooping()) {
noLoop();
}
else loop();
}*/