-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
543 lines (454 loc) · 18.2 KB
/
index.html
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding:0;
margin:0;
box-sizing: border-box;
}
body{
/* super responsive */
display:flex;
justify-content: center;
align-items: center;
background-color: black;
min-height: 100vh;
}
.root{
position:relative !important;
/* width:600px;
height:600px; */
border:1px solid white;
border-radius: 8px;
background-color: #555;
box-shadow:0px 0px 120px white;
/* display:flex; */
/* flex-wrap: wrap; */
}
.cell{
left:0;
top:0;
position:absolute !important;
/* width:100px; */
/* height:100px; */
background:black;
border-radius: 50%;
font-family: sans-serif;
font-size: 8px;
text-align: center;
padding:3px;
}
.radar{
left:0;
top:0;
position:absolute !important;
width:1px;
height:1px;
/* position: relative; */
background:blue;
}
</style>
</head>
<body>
<div class="root"></div>
<!--
TO GET A VISUAL EXAMPLE HOW THE RADAR WORKS:
1) COMMENT THE WHOLE GAME LOOP, ELEMENTS ARE FIRST PRINTED ANYWAY.
2) UNCOMMENT ALL THE COMMENTED LINES IN THE RADAR FUNCTION(), THAT HAVE TO DO WITH HTML ELEMENT, DOM, STYLE..
You can see how the radar is in exactness
PS- -> a <div> is created and printed for every pixel of the radar!! if you don't comment the game loop your browser can have a bed time calculating and printing all <div> s !!!
-->
</body>
<script id="config">
const CONFIG = {
"field_width":600,
"field_height":600,
"px_move":1, // how many px per game loop
"total_cells_n":100, // total count of cells
"game_tick":(1000 / 60), // game loop every n ms
"radar_distance":60, // how far cells find others
"radar_wave_px": 10, // distance in px every wave
"color_cell_true":"green", // background color
"color_cell_false":"darkgrey", // * *
"min_cell_size":20, // cells not smaller than
"max_cell_size":30, // * * bigger than
"min_false_to_listen":2, // when n true are around
"min_true_to_listen":1, // * * false are around
"print_lables":true // score is printed in cells
};
</script>
<script>
// INIT
const root = document.querySelector(".root");
root.style.width = CONFIG.field_width+"px";
root.style.height = CONFIG.field_height+"px";
const cells = [];
const radarDistance = CONFIG.radar_distance;
// CELLS CREATION
for (let i = 0; i <= CONFIG.total_cells_n; i++) {
const percentageS = CONFIG.min_cell_size + Math.floor(Math.random()*CONFIG.max_cell_size);
const protoCell = {
id : Math.random(),
x:Math.floor(Math.random()*CONFIG.field_width),
y:Math.floor(Math.random()*CONFIG.field_height),
size:CONFIG.min_cell_size,
type:Math.random()*10 < 5.000,
getCenter:function(){return {x:this.x + this.size/2 ,y:this.y + this.size/2}},
knowledge:255,
near:[]
}
printHtmlCell(protoCell);
cells.push(protoCell);
}
// radar for all cells
cells.forEach(cell => emitRadar(radarDistance,cell));
// PRINTS cell in HTML
function printHtmlCell(protoCell){
const exampleCell = document.createElement("span");
exampleCell.classList.add("cell");
exampleCell.setAttribute("id", protoCell.id);
// guarding for right
if(protoCell.x + protoCell.size > CONFIG.field_width){
exampleCell.style.left = "20px";
protoCell.x = 20;
}else{
exampleCell.style.left = protoCell.x + "px";
}
// guarding for bottom
if(protoCell.y + protoCell.size > CONFIG.field_height){
exampleCell.style.top = "20px";
protoCell.y = 20;
}else{
exampleCell.style.top = protoCell.y + "px";
}
// background color
if(protoCell.type){
exampleCell.style.backgroundColor = CONFIG.color_cell_true;
}else{
exampleCell.style.backgroundColor = CONFIG.color_cell_false;
}
exampleCell.style.width = protoCell.size + "px";
exampleCell.style.height = protoCell.size + "px";
// print single cell in root
root.appendChild(exampleCell);
}
// MOVE CELLS
function moveCell(cell){
const x_y_randomDirection = () => {
return Math.random()*10 < 5.000 ? CONFIG.px_move : -CONFIG.px_move;
}
// in every case , random movement
let directionX = x_y_randomDirection();
let directionY = x_y_randomDirection();
// when radar detects near cells
if(cell.near.length > 0){
let averagePositionX = 0;
let averagePositionY = 0;
const nearFalse = cell.near.filter(near=>!near.type);
const nearTrue = cell.near.filter(near=>near.type);
if(cell.type === true){
if(nearFalse.length > 0){
//priority true: move ALWAYS away from false
const directions = getAwayFrom(nearFalse , cell);
// get other direction
directionX = directions.directionX;
directionY = directions.directionY;
}
// if radar true has no false, go random
}
if(cell.type === false){
if(nearTrue.length > 0){
if(Math.random()*10 < 7.500){
//**PROBABILITY to go towards true**
const directions = getTowardsTo(nearTrue , cell);
directionX = directions.directionX;
directionY = directions.directionY;
}else if(nearFalse.length > 0){
//**PROBABILITY to search false**
const directions = getTowardsTo(nearFalse , cell);
directionX = directions.directionX;
directionY = directions.directionY;
}
}else if(nearFalse.length > 0){
// NO TRUE near
const directions = getTowardsTo(nearFalse , cell);
directionX = directions.directionX;
directionY = directions.directionY;
}
}
}//end radar detects something
// avoid overlapping with every other cell
const radarSize = Math.floor((cell.size/2 - (CONFIG.min_cell_size/2)));
const overlappingCells = radar(radarSize,cell);
if(overlappingCells.length){
const directions = getAwayFrom(overlappingCells , cell);
directionX = directions.directionX;
directionY = directions.directionY;
}
//**PROBABILITY direction ALWAYS random**
if(Math.random()*10 < 4.000){
directionX = x_y_randomDirection();
directionY = x_y_randomDirection();
}
// guarding for right, and left
if( directionX + (cell.x + cell.size) > CONFIG.field_width ){
cell.x += -directionX;
}else if(directionX + cell.x < 0){
cell.x += -directionX;
}else{
cell.x += directionX;
}
// guarding bottom, and top
if( directionY + (cell.y + cell.size) > CONFIG.field_height){
cell.y += -directionY;
}else if(directionY + cell.y < 0){
cell.y += -directionY;
}
else{
cell.y += directionY;
}
}
// return opposite direction of near cells
function getAwayFrom(detectedNear , cell){
let accumulatorX = 0, accumulatorY = 0;
detectedNear.forEach(nf => {
accumulatorX += nf.x;
accumulatorY += nf.y;
});
averagePositionX = accumulatorX / detectedNear.length ;
averagePositionY = accumulatorY / detectedNear.length ;
// get other direction
directionX = averagePositionX < cell.x ? CONFIG.px_move : -CONFIG.px_move
// get other direction
directionY = averagePositionY < cell.y ? CONFIG.px_move : -CONFIG.px_move
return{directionX,directionY}
}
// returns same direction as the cells near
function getTowardsTo(detectedNear , cell){
let accumulatorX = 0, accumulatorY = 0;
detectedNear.forEach(nf => {
accumulatorX += nf.getCenter().x;
accumulatorY += nf.getCenter().y;
});
averagePositionX = accumulatorX / detectedNear.length ;
averagePositionY = accumulatorY / detectedNear.length ;
// get other direction
directionX = cell.getCenter().x < averagePositionX ? CONFIG.px_move : -CONFIG.px_move
// get other direction
directionY = cell.getCenter().y < averagePositionY ? CONFIG.px_move : -CONFIG.px_move
return{directionX,directionY}
}
// CHANGE POSITION HTML
function moveHtmlCell(cell){
cell_ = document.getElementById(cell.id);
cell_.style.left = cell.x+"px";
cell_.style.top = cell.y+"px";
}
//
//
//
//
// ##### # # # # # # GAME LOOP
// ##### # # # # # # GAME LOOP
// ##### # # # # # # GAME LOOP
setInterval(()=>{
cells.forEach(cell=>{
// empty near before radar
cell.near = [];
// spread radar
emitRadar(radarDistance,cell);
//give new coordinates
moveCell(cell);
// translate
moveHtmlCell(cell);
//listen around
opinionExchange(cell);
if(CONFIG.print_lables) scoreLabel(cell);
});
}, CONFIG.game_tick );
// ##### # # # # # # GAME LOOP
// ##### # # # # # # GAME LOOP
// ##### # # # # # # GAME LOOP
//
// displays kind of cell and knowledge
function scoreLabel(cell){
const A = document.getElementById(cell.id);
A.innerHTML = cell.type + "<br>" +cell.knowledge;
}
//
//
//
// add or diminishes the knowledge of single cell when other cell are near checking for kind , changes the color of the cell according to the knowledge
// if the cell stays too long near other kinds of cell changes kind itself
function opinionExchange(cell){
const nearFalse = cell.near.filter(near=>!near.type);
const nearTrue = cell.near.filter(near=>near.type);
if(cell.near.length > 0){
if (cell.type === true){
// true are less
if(nearTrue.length < nearFalse.length){
// enought false
if(nearFalse.length > CONFIG.min_false_to_listen){
// knowledge becomes less
cell.knowledge -= 2 * nearFalse.length;
// near false become bigger
nearFalse.forEach(near=>{
if(near.size < CONFIG.max_cell_size)near.size++;
});
}
}else{
cell.knowledge += 2 * nearTrue.length;
}
}
if(cell.type === false){
// false are less
if(nearFalse.length < nearTrue.length){
// enought true
if(nearTrue.length > CONFIG.min_true_to_listen){
// knowledge becomes less
cell.knowledge -= 2 * nearTrue.length;
// near false become bigger
nearTrue.forEach(near=>{
if(near.size < CONFIG.max_cell_size)near.size++;
});
}
}else{
cell.knowledge += 2 * nearFalse.length;
}
}
}//if near cells
// guarding
if(cell.knowledge > 255) cell.knowledge = 255;
// changing cell type if too little knowledge
if(cell.knowledge < 20){
cell.type = !cell.type;
cell.knowledge = 200;
}
// Select and change background color to cell from his type
const htmlCell = document.getElementById(cell.id);
let rgb = "rgb(ab,cd,ef)";
if (cell.type === true){
//-> more knowlege more green
// true = 255-knowledge green 255-knowledge
rgb = "rgb(";
rgb += `${(255 - cell.knowledge)},240,${(255 - cell.knowledge)}`;
rgb += ")";
}
if (cell.type === false){
// -> more knowlege more grey
// false = +knowledge green +knowledge
rgb = "rgb(";
rgb += `${cell.knowledge},240,${cell.knowledge}`;
rgb += ")";
}
// change style in HTML
htmlCell.style.backgroundColor = rgb;
htmlCell.style.width = cell.size+"px";
htmlCell.style.height = cell.size+"px";
}
// updates the array cell.near[]
function emitRadar(radarDistance, cell){
const cellsFound = []
// every 3 pixel a radar wave
for(let distance=Math.floor((cell.size/2)); distance<= radarDistance; distance += CONFIG.radar_wave_px){
// one radar "turn around"
const foundCells = radar(distance, cell);
//add to array cells found from this cell
foundCells.forEach(cell =>{
if(!cellsFound.includes(cell)) cellsFound.push(cell);
});
}
cell.near.push(...cellsFound);
}
// searches point per point in skew square around the cell for other cells
function radar(radarDistance, cell){
// create variables
const cellSize = cell.size;
const cellCenter = cell.getCenter();
// directions
const points = [[1,1],[1,-1],[-1,-1],[-1,1]];
let pointsCounter = 0;
// number radar particles increasing
let counterRadarPoints = 0;
// cells found
const cellsFound = [];
// starting from top
const startingPoints = [
[-radarDistance, 0],
[0, +radarDistance],
[+radarDistance,0],
[0, -radarDistance]
];
// or the radar ends to add points before the end..
const _offsetOfWidthFromCenter_ = (cell.size / 4) -1;
// 4 times.. one per side f radar drop
for (i = 0; i <= radarDistance * points.length + _offsetOfWidthFromCenter_; i++){
if(counterRadarPoints > radarDistance){
// change direction!!
counterRadarPoints = 0;
pointsCounter++;
}else{
// get another radar point
counterRadarPoints ++;
}
// define radar position of next point
// print first starting top..
const top = (cellCenter.y + startingPoints[pointsCounter][0]) +
(points[pointsCounter][0] * counterRadarPoints) ;
const left = (cellCenter.x + startingPoints[pointsCounter][1]) +
(points[pointsCounter][1] * counterRadarPoints) ;
//////////////////////////// radar // visualization ///////
// const radar = document.createElement("span");
// radar.classList.add("radar");
// // // // // // // // // // // // // // // // // // // //
// array of cells found with this point
const pointFoundCells = collisionDetection(left,top,cell);
if(pointFoundCells.length > 0){
// add cells to the found array
pointFoundCells.forEach(cell =>{
if(!cellsFound.includes(cell)) cellsFound.push(cell);
});
// // //radar visualization.. yes is only a wave, but we sww also that..
//////////////////////////// radar // visualization ///////
// radar.style.backgroundColor = "red";
// radar.style.left = left + "px";
// radar.style.top = top + "px";
// // //append to.. HTML ELEMENT!!!
// root.appendChild(radar);
// // // // // // // // // // // // // // // // // // // //
}else{
//////////////////////////// radar // visualization ///////
// radar.style.backgroundColor = "green";
// // // // // // // // // // // // // // // // // // // //
}
////radar visualization, yes is only a wave, but we see also that:)
//////////////////////////// radar // visualization ///////
// radar.style.left = left + "px";
// radar.style.top = top + "px";
// //append to.. HTML ELEMENT!!!
// root.appendChild(radar);
// // // // // // // // // // // // // // // // // // // //
}
// .... radar found cells [] around this cell ...
return cellsFound;
}
// returns array of cells different from thisCell found in the point x , y
function collisionDetection(x,y,thisCell){
let x_ = false;
let y_ = false;
let cellsFoundByPoint = [];
cells.forEach(cell =>{
if(cell.id != thisCell.id){
cell.x < x && cell.x + cell.size > x ? x_=true : x_ = false;
cell.y < y && cell.y + cell.size > y ? y_=true : y_ = false;
if( x_ && y_ ) cellsFoundByPoint.push(cell);
}
});
return cellsFoundByPoint;
}
</script>
</html>