-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
451 lines (388 loc) · 10.8 KB
/
main.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
'use strict';
const context = canvas.getContext('2d');
// New keys, we assume the loader made the keys variable already, will need to chance once we split things.
keys[32] = start;
keys[49] = save;
keys[50] = load;
keys[80] = pause;
keys[77] = mouseNext;
/* TODO:
* - unit testing.
* + basic is implemented
* - something more fun, sync it up with a node server somehow?
* - multiple particle types:
* - default (atoms?)
* - mist????
* - walls, immutable, indistructable.
* - more shiny good stuff.
* - GUI
* - actual particle behaviour:
* - point gravity.
* - motion.
* - containment.
* - collicions.
* - steering behaviour:
* - AI in a particle engine? yes, go fuck yourself if you don't know about steering behaviour.
* - seriously go feel a wicked degree of shame mate.
* - mouse / pointer modes:
* - touchscreen.
* - create / emit.
* - gravitate.
* - contain.
* - grab.
* - editor.
* - json.
* - aspects? some drag and pull system, freeze, selecting, etc.
* - create gravity well.
* - Adding title randomizer.
* - Add angles.
* - Physics engine.
* - engine in webasm.
*/
/* Global variables.
********************/
const settings = {type: 'square'};
let actions = [];
let paused = false;
/* universal functions.
***********************/
/**
* returns a random number
* @param {Int} i
* @return {Int}
*/
function rand(i = 1) {
return i * Math.random();
}
/* Anything relating to the mouse events.
*****************************************/
const mouse = {
clone: {
clientX: 0,
clientY: 0,
},
modus: {
emit: ev => {
return function () {
particleArr.push(new Particle({x:mouse.clone.clientX, y:mouse.clone.clientY, type: config.type}));
};
},
// bow: ev => {
// return _ => {
// //particleArr.push(particle(mouse.clone.clientX, mouse.clone.clientY))
// }
// },
// create: ev => {
// return function () {
// particleArr.push(new Particle({x:mouse.clone.clientX, y:mouse.clone.clientY, type: config.type}))
// }
// },
// blackHole : ev => {
// particleArr.push(particle(ev.clientX,ev.clientY,-100))
// }
// pew : ev => {
// let i = 10
// while (i--){
// particleArr[i] = (particle(ev.clientX,ev.clientY))
// }
// },
gravitate: function(ev) {
return _ => {
gravity.boring({
coords: {
x: mouse.clone.clientX,
y: mouse.clone.clientY,
},
mass: 100000,
});
};
},
// edit : ev => {
// pause()
// edit(ev.clientX,ev.clientY)
// },
},
action: '',
};
/**
* Change the mousemode to the next in the list.
*/
function mouseNext() {
let flag;
for (let mode in mouse.modus) {
if (flag)
return mouse.action = mouse.modus[mode];
flag = mouse.modus[mode] === mouse.action;
}
return mouse.action = mouse.modus.emit;
}
mouse.action = mouse.modus.gravitate;
/**
* Scroll event handler.
* @param {scrollEvent} ev
*/
function onScroll(ev) {
console.log(ev);
mouseNext();
//scroller(ev.clientX,ev.clientY)
}
// This creates an... action emitter?
window.onmousedown = ev => actions.push(mouse.action(ev));
window.onmouseup = ev => actions.pop();
window.onmousemove = ev => mouse.clone = ev;
window.onclick = ev => mouse.clone = ev;
window.onscroll = onScroll;
/* Anything related to touch events.
************************************/
// Touchscreen handles very different from a mouse, make seperate actions!
const touch = {};
window.ontouchstart = window.onmousedown;
window.ontouchend = window.onmouseup;
//window.ontouchmove = ev => mouse.clone = ev;
// When does this even fire?
window.ontouchcancel = window.ontouchend;
// scroller window for picking mouse modes -fix this-
function scroller(x, y) {
const picker = document.createElement('ul');
let mode;
for (mode in mouse.modus) {
let item = document.createElement('li');
item.innerText = mode;
picker.appendChild(item);
}
select('body').appendChild(picker);
pause();
}
// Editor because we need to be able to edit particle properties ingame :p.
function edit(x, y) {
const editor = document.createElement('code');
editor.setAttribute('class', 'json');
let particle = findParticle(x, y);
console.log(particle);
if (!particle)
return;
editor.innerText = JSON.stringify(particle);
select('body').appendChild(editor);
}
/**
* Finds any particle at the X and Y coords given, returns the last particle found.
* If no particles are found, returns false.
* @param {Int} x
* @param {Int} y
*/
function findParticle(x, y) {
let temp;
particleArr.forEach(particle => {
if (x < particle.coords.x + particle.mass)
if (x > particle.coords.x)
if (y < particle.coords.y + particle.mass)
if (y > particle.coords.y)
temp = particle;
});
return temp || false;
}
/**
* Particle constructor.
*/
class Particle {
constructor(props = {x, y , mass, color, type}) {
this.coords = {
x: props.x || rand(canvas.width),
y: props.y || rand(canvas.height)
};
this.vol = {
x: rand(2) - 1,
y: rand(2) - 1
};
this.color = props.color || `hsla(${rand(360)},100%,58%,1)`;
this.mass = props.mass || 20 + rand(80);
this.type = props.type || 'particle';
this.shape = 'round';
}
}
// location to store the refrences to the particles
let particleArr = [];
// Overly big render object, might be smarter to do this as a function instead.
const render = {
particle: (particle, fill) => {
context.fillStyle = particle.color;
context.beginPath();
context.arc(particle.coords.x, particle.coords.y, particle.mass, 0, Math.PI * 2);
// Take a second look at below.
fill || context.fill();
context.closePath();
},
square: particle => {
context.fillStyle = particle.color;
context.fillRect(particle.coords.x, particle.coords.y, particle.mass, particle.mass);
context.fill();
},
point: (coords = {x:50, y: 50}) => {
},
game: _ => {
context.fillStyle = '#FFF';
context.fillRect(0, 0, canvas.width, canvas.height);
particleArr.forEach(particle => {
// make this a function that the particle has or refrences which contains these functions to allow for costum behaviour
gravity[config.gravity](particle);
collisions[config.collisions](particle);
checkWithinBounds(particle);
move(particle);
render[particle.type](particle);
});
}
};
/* Anything and everything related to moving stuff around.
**********************************************************/
const gravity = {
boring: entity => {
particleArr.forEach(particle => {
if (particle !== entity) {
let x = particle.coords.x - entity.coords.x;
let y = particle.coords.y - entity.coords.y;
let r = x * x + y * y;
let force = ((entity.mass + particle.mass) / r) * config.G;
particle.vol.x -= force * x;
particle.vol.y -= force * y;
}
});
},
wigglyInverse: entity => {
let force = 1;
particleArr.forEach(particle => {
if (particle !== entity) {
let xDistance = particle.coords.x - entity.coords.x;
let yDistance = particle.coords.y - entity.coords.y;
force *= entity.mass;
particle.vol.x += force / xDistance;
particle.vol.y += force / yDistance;
}
});
},
wiggly: entity => {
let force = 1;
particleArr.forEach(particle => {
if (particle !== entity) {
let x = particle.coords.x - entity.coords.x;
let y = particle.coords.y - entity.coords.y;
// force *= entity.mass
particle.vol.x -= force / x;
particle.vol.y -= force / y;
}
});
},
};
function move(particle) {
particle.coords.x += particle.vol.x / particle.mass;
particle.coords.y += particle.vol.y / particle.mass;
}
/* code relating to collisions here :D.
***************************************/
function checkWithinBounds(entity) {
if(config.bounds){
if (entity.coords.x < 0) {
entity.vol.x *= -1;
entity.coords.x++;
}
if (entity.coords.x + entity.mass > canvas.width) {
entity.vol.x *= -1;
entity.coords.x--;
}
if (entity.coords.y < 0) {
entity.vol.y *= -1;
entity.coords.y++;
}
if (entity.coords.y + entity.mass > canvas.height) {
entity.vol.y *= -1;
entity.coords.y--;
}
} else {
if (entity.coords.x + entity.mass < 0)
entity.coords.x = canvas.width;
if (entity.coords.x > canvas.width)
entity.coords.x = 0 - entity.mass;
if (entity.coords.y + entity.mass < 0)
entity.coords.y = canvas.height;
if (entity.coords.y > canvas.height)
entity.coords.y = 0 - entity.mass;
}
}
const collisions = {
boring: entity => {
particleArr.forEach(particle => {
// don't allow particles to collide with themselves
if (particle !== entity) {
const x = entity.coords.x + entity.vol.x / entity.mass;
const y = entity.coords.y + entity.vol.y / entity.mass;
if (x < particle.coords.x + particle.mass)
if (x > particle.coords.x - entity.mass)
if (y < particle.coords.y + particle.mass)
if (y > particle.coords.y - entity.mass) {
/* breaks down when more than 2 particles are involved
* scenario: particle A will hit B so it inverts its velocity.
* if afterwards it will hit C it inverts again, this causes weird behavior like particles phasing into eachother.
*/
entity.vol.x *= -1;
entity.vol.y *= -1;
const volX = (entity.vol.x + particle.vol.x) / 2;
const volY = (entity.vol.y + particle.vol.y) / 2;
entity.vol.x = volX;
entity.vol.y = volY;
// comment out the two lines below for a cool effect
// particle.vol.x = volX;
// particle.vol.y = volY;
}
}
});
},
care: entity => {
particleArr.forEach(particle => {
if (particle !== entity)
if (entity.coords.x < particle.coords.x + particle.mass)
if (entity.coords.x > particle.coords.x - entity.mass)
if (entity.coords.y < particle.coords.y + particle.mass)
if (entity.coords.y > particle.coords.y - entity.mass) {
// entity.coords.x *= -1
// entity.coords.y *= -1
const volX = (entity.vol.x + particle.vol.x) / 2;
const volY = (entity.vol.y + particle.vol.y) / 2;
entity.vol.x = volX;
entity.vol.y = volY;
particle.vol.x = volX;
particle.vol.y = volY;
}
});
}
};
function pause() {
paused = !paused;
}
/* saving and loading to and from the local storage.
****************************************************/
function save() {
localStorage.setItem('particles', JSON.stringify(particleArr));
}
function load() {
particleArr = JSON.parse(localStorage.getItem('particles'));
}
/* Essentials for running the program.
**************************************/
function start() {
let i = 20;
particleArr = [];
while (i--)
particleArr.push(new Particle({type: config.type}));
// console.log(particleArr)
}
function main() {
// Rate at which it runs doesn't matter.
requestAnimationFrame(main);
// If we want something to be done every frame, add it here.
actions.forEach(action => {
action();
});
if (!paused)
render.game();
}
start();
main();