-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1859 lines (1639 loc) · 48.6 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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Immunity Collapse</title>
<style>
body {
margin:0;
overflow:hidden;
font-family:Arial;
}
#c {
position:absolute;
top:0;
left:0;
}
</style>
<script id="vsSource" type="x-shader/x-vertex">
precision mediump int;
precision highp float;
attribute vec4 aVertexPosition;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
uniform float uRadius;
uniform int uType;
uniform float iTime;
uniform float meRadius;
uniform float collid;
uniform float uvBack;
uniform float fade;
varying vec4 vColor;
void main() {
gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
vColor = aVertexPosition;
}
</script>
<script id="fsSource" type="x-shader/x-fragment">
// Fragment shader program
precision mediump int;
precision highp float;
varying vec4 vColor;
uniform float uRadius;
uniform int uType;
uniform float iTime;
uniform float meRadius;
uniform float collid;
uniform float uvBack;
uniform float fade;
const float dihedIcos = 0.364863828;
const float pentaHeight = 1.5*acos(dot(normalize(vec3(1.6180339+1., 1., 1.6180339)), normalize(vec3(.5))));
vec2 Rot2D (vec2 q, float a) {return q * cos (a) + q.yx * sin (a) * vec2 (-1., 1.);}
vec4 qmult(vec4 p, vec4 q) {
vec3 pv = p.xyz;
vec3 qv = q.xyz;
return vec4(
p.w * qv + q.w * pv + cross(pv, qv),
p.w * q.w - dot(pv, qv)
);
}
vec3 rotate(vec3 point, vec4 qrotor) {
vec3 rv = qrotor.xyz;
return qmult(qrotor, vec4(point * qrotor.w - cross(point, rv), dot(point, rv))).xyz;
}
float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) {
vec3 pa = p - a, ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h ) - r;
}
float opUnion( float d1, float d2, float k ) {
float h = clamp( 0.5 + 0.5*(d2-d1)/k, 0.0, 1.0 );
return mix( d2, d1, h ) - k*h*(1.0-h);
}
//
// CELL
//
float smin( float a, float b, float k ) {
float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 );
return mix( b, a, h ) - k*h*(1.0-h);
}
float fbm(vec2 uv) {
float f = 200.0;
vec2 tmp;
float T = 100.0 + iTime * 0.3;
T += sin(iTime * 4.0) * .1 + cos(iTime) * .15;
// layers of cells with some scaling and rotation applied.
for (int i = 1; i < 8; ++i) {
uv.y -= T * .5;
uv.x -= T * .4;
tmp = uv;
uv.x = tmp.x * .9 - tmp.y * .45;
uv.y = tmp.x * .45 + tmp.y * .9;
vec2 uv2 = mix(sin(uv + vec2(1.57, 0)), sin(uv.yx*1.4 + vec2(1.57, 0)), .75);
f = smin(f, uv2.x*uv2.y*.3 + .7, .07);
}
return 1. - f;
}
// BACKGROUND
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
// Rough Value noise implementation
float valueNoiseSimple(vec2 vl) {
float minStep = 1.0 ;
vec2 grid = floor(vl);
vec2 gridPnt1 = grid;
vec2 gridPnt2 = vec2(grid.x, grid.y + minStep);
vec2 gridPnt3 = vec2(grid.x + minStep, grid.y);
vec2 gridPnt4 = vec2(gridPnt3.x, gridPnt2.y);
float s = rand(grid);
float t = rand(gridPnt3);
float u = rand(gridPnt2);
float v = rand(gridPnt4);
float x1 = smoothstep(0., 1., fract(vl.x));
float interpX1 = mix(s, t, x1);
float interpX2 = mix(u, v, x1);
float y = smoothstep(0., 1., fract(vl.y));
float interpY = mix(interpX1, interpX2, y);
return interpY;
}
float fractalNoise(vec2 vl) {
float amplitude = .5;
float rez = 0.;
for (float i = 0.0; i < 8.0; i++) {
rez += amplitude * valueNoiseSimple(vl);
amplitude /= 2.;
vl *= 2.;
}
return rez;
}
float complexFBM(vec2 p) {
float slow = iTime / 20.;
float fast = iTime / 2.05;
vec2 offset1 = vec2(cos(slow) + slow , 0.); // Main front
vec2 offset2 =vec2(sin(fast ), 0.); // sub fronts
return fractalNoise(p + offset1 + fractalNoise(
p + fractalNoise(
p + 2. * fractalNoise(p - offset2)
)
));
}
void main() {
vec2 iResolution = vec2(uRadius*2.0, uRadius*2.0);
vec2 fragCoord = vColor.xy;
vec2 pc = vColor.xy * 2.0;
if (uType==2) { //BACKGROUND BLOOD
vec2 uv = fragCoord.xy / iResolution.xy-0.5;
vec3 color2 = vec3(0.3, 0., 0.05);
vec3 color1 = vec3(0.);
vec3 rez = mix(color1, color2, complexFBM(uv*uvBack)); //40
gl_FragColor = vec4(rez, 1.0);
float mult = 40.0;
float dist;
dist = abs((uv.x+0.5)*2.0);
if (dist>0.5) {
gl_FragColor.rgb*= max(0., 0.55 - dist)*mult;
mult /= 2.0;
}
dist = abs((uv.y+0.5)*2.0);
if (dist>0.5) {
gl_FragColor.rgb*= max(0., 0.55 - dist)*mult;
}
} else if (uType==0) { //CELL
float distance = length(vColor.xy);
vec2 uv = fragCoord.xy / iResolution.xy;
uv.y /= iResolution.x / iResolution.y;
vec2 ouv = uv;
float B = sin(iTime * 4.0 + uRadius);
uv = mix(uv, uv * sin(B), .035);
vec2 _uv = uv * 25.;
float f = fbm(_uv);
if (distance>uRadius) {
gl_FragColor.a = 0.0;
} else {
gl_FragColor.a = 1.0;
vec2 off = vec2(0.0, .03);
gl_FragColor.rgb = mix(vec3(1.*f,(.3 + B * .05)*f,(0.1 + B * .05)*f) + pow(max(0., dot(normalize(cross(vec3(.0025, f - fbm(_uv + off.yx), 0.), -vec3(0, f - fbm(_uv + off), .0025))).xzy, normalize(normalize(vec3(uv, 1.))))), 4.) * .2 * vec3(.4, .7, .7), vec3(0.), smoothstep(.45, .55, (max(abs(ouv.y * iResolution.x / iResolution.y), abs(ouv.x)))));
// contrast
gl_FragColor = smoothstep(.0, 1., gl_FragColor);
//
// Crop en forme de cercle et colors en fonction de la taille
//
float fadeLength = uRadius*0.25;
if (distance>(uRadius-fadeLength) && distance<uRadius) {
float a = 1.-((distance-(uRadius-fadeLength)) / fadeLength);
if (a>0.5) { // Premiere partie en fondu au noir
gl_FragColor.rgb*= (a*2.0)-1.0;
gl_FragColor.a = 1.0;
} else {
float f = abs(a-0.5);
if (uRadius>meRadius) {
gl_FragColor.rgba = mix(vec4(1.,.0,.0,1.),vec4(1.,.0,.0,.5), f*4.);
} else {
gl_FragColor.rgba = mix(vec4(1.), vec4(1.,1.,1.,.5), f*4.);
}
}
}
}
} else { // VIRUS
fragCoord.x+= uRadius;
fragCoord.y+= uRadius;
vec4 rotY = vec4(vec3(0., sin(iTime*0.5), 0.), cos(iTime*0.5));
vec4 rotX = vec4(vec3(sin(iTime*0.5), 0., 0.), cos(iTime*0.5));
vec4 dirA = qmult(rotY, rotX);
vec3 dir = rotate(normalize(vec3((2. * fragCoord - iResolution.xy) / iResolution.y, -2.35)), dirA);
vec3 camera = rotate(vec3(0., 0., 1.7748), dirA);
float dist, t = 0.;
vec4 col;
float maxDist = 0.;
float alpha = 0.;
vec3 pos;
float l = length(camera)-.5;
for(float i = 0.; i < 100.; i++) {
pos = camera + t * dir;
vec3 p = pos;
p.x = 32.0 * (fract(p.x / 32.0 - .5) - .5);
float a, w;
w = 2.09;
p.z = abs (p.z);
p.yz = Rot2D (p.yz, - dihedIcos);
p.x = - abs (p.x);
for (int k = 0; k < 4; k ++) {
p.zy = Rot2D (p.zy, - dihedIcos);
p.y = - abs (p.y);
p.zy = Rot2D (p.zy, dihedIcos);
if (k < 3) p.xy = Rot2D (p.xy, - w);
}
p.z = - p.z;
a = mod (atan (p.x, p.y) + .5 * w, w) - .5 * w;
p.yx = vec2 (cos (a), sin (a)) * length (p.xy);
p.x -= 2. * p.x * step (0., p.x);
p.yz = Rot2D (p.yz, -dihedIcos);
float d = length(vec3(p.x, p.y, p.z + .65)) - .045;
float d2 = sdCapsule(p, vec3(0), vec3(0., 0., -.65), .02);
p.xz = Rot2D (p.xz, pentaHeight);
d = min(d, length(vec3(p.x, p.y, p.z + .60)) - .045);
d2 = min(d2, sdCapsule(p, vec3(0), vec3(0., 0., -.60), .02));
d = min(d, length(p)-.45); //Sphere centrale
float h = clamp( .5 + .5*(d-d2)/.05, .0, 1. );
d = mix( d, d2, h ) - .05*h*(1.-h);
d = max(d, -length(p)+.40);
dist = d;
col.rgb += pow(.002/max(0., dist + .065), 1.1)
* pow(1. - (t - l) / 2.5, 6.)
* mix(
vec3(collid, .03, 10. - (10. * collid)),
vec3(.005, 1.-collid ,.01),
length(pos)
);
if (dist<0. && alpha<=0.1) {
alpha = 1.;
}
t += max(.001, dist);
if(t > 2.5) break;
maxDist = max(maxDist, dist);
}
col.a = alpha;
gl_FragColor = col;
}
gl_FragColor.rgb=mix(vec3(0.0),gl_FragColor.rgb,fade);
}
</script>
</head>
<body>
<canvas id="glCanvas" width="1900" height="800"></canvas>
<canvas id="c" width="1900" height="800"></canvas>
<script>
glCanvas.style.width = "100%";
glCanvas.style.height = "100%";
c.style.width = "100%";
c.style.height = "100%";
//
// Méthodes 3D
//
glMatrixArrayType="undefined"!=typeof Float32Array?Float32Array:"undefined"!=typeof WebGLFloatArray?WebGLFloatArray:Array;
var mat44={};
mat44.create=function(r){var t = new glMatrixArrayType(16);t[0] = t[5] = t[10] = t[15] = 1;return t;};
mat44.translate=function(out,a ,v){var x = v[0],y = v[1],z = v[2];var a00,a01,a02,a03,a10,a11,a12,a13,a20,a21,a22,a23;if(a===out){out[12]=a[0]*x+a[4]*y+a[8]*z+a[12];out[13]=a[1]*x+a[5]*y+a[9]*z+a[13];out[14]=a[2]*x+a[6]*y+a[10]*z+a[14];out[15]=a[3]*x+a[7]*y+a[11]*z+a[15];}else{a00=a[0];a01=a[1];a02=a[2];a03=a[3];a10=a[4];a11=a[5];a12=a[6];a13=a[7];a20=a[8];a21=a[9];a22=a[10];a23=a[11];out[0]=a00;out[1]=a01;out[2]=a02;out[3]=a03;out[4]=a10;out[5]=a11;out[6]=a12;out[7]=a13;out[8]=a20;out[9]=a21;out[10]=a22;out[11]=a23;out[12]=a00*x+a10*y+a20*z+a[12];out[13]=a01*x+a11*y+a21*z+a[13];out[14]=a02*x+a12*y+a22*z+a[14];out[15]=a03*x+a13*y+a23*z+a[15];}return out;};
mat44.rotate=function(r,t,a,n){var e=a[0],u=a[1];a=a[2];var i=Math.sqrt(e*e+u*u+a*a);if(!i)return null;1!=i&&(e*=i=1/i,u*=i,a*=i);var o=Math.sin(t),f=Math.cos(t),m=1-f;t=r[0],i=r[1];var c=r[2],v=r[3],y=r[4],l=r[5],s=r[6],M=r[7],p=r[8],A=r[9],d=r[10],h=r[11],F=e*e*m+f,g=u*e*m+a*o,x=a*e*m-u*o,T=e*u*m-a*o,b=u*u*m+f,w=a*u*m+e*o,G=e*a*m+u*o;return e=u*a*m-e*o,u=a*a*m+f,n?r!=n&&(n[12]=r[12],n[13]=r[13],n[14]=r[14],n[15]=r[15]):n=r,n[0]=t*F+y*g+p*x,n[1]=i*F+l*g+A*x,n[2]=c*F+s*g+d*x,n[3]=v*F+M*g+h*x,n[4]=t*T+y*b+p*w,n[5]=i*T+l*b+A*w,n[6]=c*T+s*b+d*w,n[7]=v*T+M*b+h*w,n[8]=t*G+y*e+p*u,n[9]=i*G+l*e+A*u,n[10]=c*G+s*e+d*u,n[11]=v*G+M*e+h*u,n};
mat44.perspective=function(out,fovy,aspect,near,far){var f =1.0/Math.tan(fovy/2);out[0]=f/aspect;out[1]=0;out[2]=0;out[3]=0;out[4]=0;out[5]=f;out[6]=0;out[7]=0;out[8]=0;out[9]=0;out[11]=-1;out[12]=0;out[13]=0;out[15]=0;if(far!=null && far!==Infinity){var nf=1/(near-far);out[10]=(far+near)*nf;out[14]=2*far*near*nf;}else{out[10]=-1;out[14]=-2*near;}return out;};
GOOD = 0;
ME = 1;
BACKGROUND = 2;
var muted = false;
var speed = 1;
var extraDecal={x:0,y:0};
class CELL {
constructor(type, x, y, radius, vX, vY) {
this.x = x;
this.y = y;
this.radius = radius;
this.setVelocity(vX ? vX : 0, vY ? vY : 0);
this.type=type;
this.collid = false;
this.collidPercent = 0.0;
this.contacts = [];
}
setVelocity(x, y) {
this.velocity = {
x:x,
y:y,
speed:Math.hypot(x, y)
};
}
update() {
if (this.radius<0) {
return false;
}
this.x+=this.velocity.x * (speed/(16.66/nbMs));
this.y+=this.velocity.y * (speed/(16.66/nbMs));
//Gère les rebons sur les bords
if (this.x-this.radius<0) {
this.x = this.radius * 2 - this.x;
this.velocity.x = -this.velocity.x;
}
if (this.y-this.radius<0) {
this.y = this.radius * 2 - this.y;
this.velocity.y = -this.velocity.y;
}
if (this.x+this.radius>stage.width) {
this.x = -2*this.radius + 2*stage.width - this.x;
this.velocity.x = -this.velocity.x;
}
if (this.y+this.radius>stage.height) {
this.y = -2*this.radius + 2*stage.height - this.y;
this.velocity.y = -this.velocity.y;
}
}
impulse(x, y) {
playNote(12, song.tracks[3].instrument, 0.45);
console.log("impulse",x,y);
this.setVelocity(this.velocity.x+x/10, this.velocity.y+y/10);
// Il faut calculer la surface de la projection
// Puis déduire cette surface de l'aire actuel
var S = Math.PI * (this.radius*this.radius);
var projection = S*0.025;
this.radius = Math.sqrt(S*0.95 / Math.PI); //FIXME, quand on est gros c'est beaucoup trop
var impulseRadius = Math.sqrt(projection / Math.PI);
//Créer un nouvel élément du même type que moi dans le sens inverse
stage.elements.push(new CELL(
this.type,
this.x-((this.radius+impulseRadius)*x)*1.1,
this.y-((this.radius+impulseRadius)*y)*1.1,
impulseRadius,
-x+this.velocity.x, //-x/100.0,
-y+this.velocity.y, //-y/100.0
));
}
}
class STAGE {
constructor(width, height) {
this.width = width;
this.height = height;
this.elements = [];
this.elements.push(new CELL(BACKGROUND,this.width/2,this.height/2,Math.max(this.width, this.height)));
this.goal = "";
this.ended = false;
}
checkGoal() {
switch (this.goal) {
case "onlyVirus": //Seulement des virus
var onlyVirus = true;
for (var i=1; i<this.elements.length; i++) {
if (this.elements[i].type!=ME) {
onlyVirus = false;
break;
}
}
return onlyVirus;
break;
case "getAll": //Être le seul
if (this.elements.length==2 && !me.isDied) {
return true;
}
break;
case "bigest": //Le plus gros
var maxRadius = 0;
for (var i=1; i<this.elements.length; i++) {
if (this.elements[i].type!=ME) {
maxRadius = Math.max(maxRadius, this.elements[i].radius);
}
}
if (me.radius>maxRadius) {
return true;
}
break;
}
return false;
}
}
rand = function(min, max) {
return (Math.random()*(max-min)) + min;
};
window.onresize = function() {
initCamera();
};
var stage, background;
var me;
var gl;
var programInfo;
var buffers;
var projectionMatrix = mat44.create();
var fieldOfView = 45 * Math.PI / 180;
var ratioText;
var uvBack = 40;
var ctx2D = c.getContext("2d");
var unitInPx;
var vFOV = 45 * Math.PI / 180; // convert vertical fov to radians
var maxZoom;
var oldDT=0;
var nbMs = 16;
var level = -1;
var tuto=[];
var tutoLeft;
var heightHUD = 198;
var mouse = {
pressed:false,
x:0,
y:0
};
var minY, maxY;
var frameStart;
var levelStart = false;
var homeLoaded = 0;
initCamera = function() {
c.height = document.body.clientHeight;
c.width = document.body.clientWidth;
ratioText = c.width/1920;
minY = document.body.clientHeight/2 - heightHUD/2 - 30;
maxY = document.body.clientHeight/2 + heightHUD/2 + 10;
projectionMatrix = mat44.create();
mat44.perspective(
projectionMatrix,
fieldOfView,
gl.canvas.clientWidth / gl.canvas.clientHeight,
0.1,
10000.0
);
};
/**
* Creates a pseudo-random value generator. The seed must be an integer.
*
* Uses an optimized version of the Park-Miller PRNG.
* http://www.firstpr.com.au/dsp/rand31/
*/
function Random(seed) {
this._seed = seed % 2147483647;
if (this._seed <= 0) this._seed += 2147483646;
};
/**
* Returns a pseudo-random value between 1 and 2^32 - 2.
*/
Random.prototype.next = function () {
return this._seed = this._seed * 16807 % 2147483647;
};
/**
* Returns a pseudo-random floating point number in range [0, 1).
*/
Random.prototype.nextFloat = function () {
// We know that result of next() will be 1 to 2147483646 (inclusive).
return (this.next() - 1) / 2147483646;
};
init = function() {
const canvas = document.querySelector('#glcanvas');
gl = canvas.getContext('webgl');
gl.clearColor(0.0, 0.0, 0.0, 1.0); // Clear to black, fully opaque
gl.clearDepth(1.0); // Clear everything
gl.enable(gl.DEPTH_TEST); // Enable depth testing
gl.depthFunc(gl.LEQUAL); // Near things obscure far things
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
const shaderProgram = initShaderProgram(gl, document.getElementById('vsSource').innerText, document.getElementById('fsSource').innerText);
programInfo = {
program: shaderProgram,
attribLocations: {
vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'),
},
uniformLocations: {
projectionMatrix: gl.getUniformLocation(shaderProgram, 'uProjectionMatrix'),
modelViewMatrix: gl.getUniformLocation(shaderProgram, 'uModelViewMatrix'),
radius: gl.getUniformLocation(shaderProgram, 'uRadius'),
type: gl.getUniformLocation(shaderProgram, 'uType'),
iTime: gl.getUniformLocation(shaderProgram, 'iTime'),
meRadius: gl.getUniformLocation(shaderProgram, 'meRadius'),
collid: gl.getUniformLocation(shaderProgram, 'collid'),
uvBack:gl.getUniformLocation(shaderProgram, 'uvBack'),
fade:gl.getUniformLocation(shaderProgram, 'fade')
}
};
initLevel(-1);
// Draw the scene
drawScene(gl, programInfo, buffers);
initCamera();
};
drawTuto = function() {
if (tuto.length==0) { return; }
var t = tuto[0];
if (t.t) {
//Ecrire le texte : Exécuté à chaque frame
drawText(
c.width/2 - ((12*(t.t.length*1.5*(c.width/1920))/2)),
c.height - (6*12*1.5*(c.width/1920)),
t.t,
1.5*(c.width/1920)
);
var text = "CLICK TO CONTINUE";
drawText(
c.width/2 - ((12*(text.length*1.0*(c.width/1920))/2)),
c.height - (3*12*1.0*(c.width/1920)),
text,
1.0*(c.width/1920),
false,
"#888"
);
}
if (typeof(t.z)!="undefined") { //Zoom sur : Executé une seul fois
targetextraDecal.x = stage.elements[t.z].x - stage.elements[1].x;
targetextraDecal.y = stage.elements[t.z].y - stage.elements[1].y;
if (t.z==0) {
targetZoom = maxZoom;
}
delete t.z;
}
if (t.a) { //Action : Executé une seul fois
SHOW_TARGET = true;
delete t.a;
}
};
tutoNext = function() {
tuto.splice(0,1);
if (tuto.length==0) {
levelStart = true;
SHOW_TARGET = true;
}
};
fadeToLevel = function(level) {
//initLevel(level);
fade.inProgress = true;
fade.out = 1;
fade.level = level;
}
initLevel = function(l, rand) {
mouse.over = false;
extraDecal = {x:0,y:0};
targetextraDecal = {x:0, y:0};
zoom = targetZoom = -100;
level = l;
SHOW_TARGET = false;
levelStart = false;
uvBack = 40;
tuto = [];
switch (level) {
case -1: //Home
stage = new STAGE(800,600);
me = new CELL(ME, 400, 300, 5);
targetZoom = -100;
break;
case 0: //Main menu
stage = new STAGE(800,600);
me = new CELL(ME, 400, 300, 5);
targetZoom = -100;
break;
case 1:
uvBack = 10;
stage = new STAGE(200,200);
stage.goal = "onlyVirus";
me = new CELL(ME, 50, 100, 10);
targetZoom = -40;
tuto = [
{t:"THAT IS YOU, A VIRUS", z:1},
{t:"THIS IS A RED BLOOD CELL", z:2},
{t:"YOUR GOAL ? ABSORD IT", z:0},
{t:"MOVE YOUR MOUSE AROUND YOU AND CLICK TO IMPULSE", z:0, a:true}
];
stage.elements.push(new CELL(GOOD,150,100,8));
break;
case 2:
uvBack = 10;
stage = new STAGE(200,200);
stage.goal = "onlyVirus";
me = new CELL(ME, 50, 100, 10);
targetZoom = -100;
tuto = [
{t:"YOU CAN ONLY ABSORB CELLS IF THEY ARE SMALLER THAN YOU", z:0},
{t:"IF YOU TOUCH A CELL BIGGER THAN YOU ? DEATH...", z:0},
{t:"THE CELLS SURROUNDED BY WHITE ARE SMALLER. OF RED, BIGGER", z:0},
];
stage.elements.push(new CELL(GOOD,150,100,10.25));
stage.elements.push(new CELL(GOOD,100,50,6));
stage.elements.push(new CELL(GOOD,100,150,6));
break;
case 3:
rand = 1;
levelRand = new Random(rand);
stage = new STAGE(800,800);
stage.goal = "bigest";
tuto = [
{t:"SPEED UP THE TIME OR SLOW IT DOWN WITH THE LEFT PANEL FOR MORE PRECISION", z:1},
];
me = new CELL(ME,400,400,5);
for (var i=0; i<600; i++) {
var radius = 1+(levelRand.nextFloat()*10);
var x = radius/2 + (levelRand.nextFloat()*(stage.width-radius/2));
var y = radius/2 + (levelRand.nextFloat()*(stage.height-radius/2));
stage.elements.push(new CELL(GOOD,x,y,radius,(levelRand.nextFloat()*0.02) - 0.01,(levelRand.nextFloat()*0.02) - 0.01));
}
break;
case 4:
rand = 56;
levelStart = true;
SHOW_TARGET = true;
levelRand = new Random(rand);
stage = new STAGE(800,800);
stage.goal = "bigest";
me = new CELL(ME,400,300,5);
for (var i=0; i<600; i++) {
var radius = 1+(levelRand.nextFloat()*12);
var x = radius/2 + (levelRand.nextFloat()*(stage.width-radius/2));
var y = radius/2 + (levelRand.nextFloat()*(stage.height-radius/2));
stage.elements.push(new CELL(GOOD,x,y,radius,));
}
stage.elements.push(new CELL(GOOD,390,265,30));
stage.elements.push(new CELL(GOOD,415,330,20));
stage.elements.push(new CELL(GOOD,370,325,20));
stage.elements.push(new CELL(GOOD,430,280,10));
break;
case 5:
if (!rand) {
rand = 1;
}
levelStart = true;
SHOW_TARGET = true;
levelRand = new Random(rand);
stage = new STAGE(800,800);
stage.goal = "bigest";
me = new CELL(ME,400,300,5);
for (var i=0; i<600; i++) {
var radius = 1+(levelRand.nextFloat()*12);
var x = radius/2 + (levelRand.nextFloat()*(stage.width-radius/2));
var y = radius/2 + (levelRand.nextFloat()*(stage.height-radius/2));
stage.elements.push(new CELL(GOOD,x,y,radius,levelRand.nextFloat()/8.0,levelRand.nextFloat()/8.0));
}
break;
case 6:
case 7:
rand = Math.floor(Math.random()*10000);
levelStart = true;
SHOW_TARGET = true;
levelRand = new Random(rand);
stage = new STAGE(800,800);
stage.goal = "bigest";
me = new CELL(ME,400,300,5);
for (var i=0; i<600; i++) {
var radius = 1+(levelRand.nextFloat()*12);
var collidWithMe = true;
while (collidWithMe) {
var x = radius/2 + (levelRand.nextFloat()*(stage.width-radius/2));
var y = radius/2 + (levelRand.nextFloat()*(stage.height-radius/2));
if (Math.hypot(x-me.x, y-me.y)>radius*2.0) {
collidWithMe = false;
}
}
stage.elements.push(new CELL(GOOD,x,y,radius,level==6 ? 0 : (levelRand.nextFloat()/8.0)-0.0625,level==6 ? 0 : (levelRand.nextFloat()/8.0)-0.0625));
}
break;
}
me.isDied = false;
stage.elements.push(me);
tutoLeft = tuto.length;
if (tutoLeft) {
SHOW_TARGET = false;
}
};
init();
window.addEventListener("wheel", event => {
var speedDelta = 1+((Math.sign(event.deltaY))/10);
targetZoom*= speedDelta;
return false;
});
document.onmousemove = function(e) {
c.style.cursor = "default";
mouse.x = e.clientX;
mouse.y = e.clientY;
if (e.clientX>13 && e.clientX<32 && e.clientY>minY && e.clientY<minY+20) {
c.style.cursor = "pointer";
} else if (e.clientX>13 && e.clientX<32 && e.clientY>maxY && e.clientY<maxY+20) {
c.style.cursor = "pointer";
}
};
document.onmousedown = function(e) {
if (level==-1) {
initLevel(0);
context.resume();
frameStart = frame;
requestAnimationFrame(playSong);
return;
}
if (level==0 && homeLoaded<0.97) {
homeLoaded = 1;
return;
}
if (mouse.over) {
ctx2D.globalAlpha = 1;
fadeToLevel(mouse.over);
return;
}
if (e.clientX>13 && e.clientX<32 && e.clientY>minY && e.clientY<minY+20) {
speed = speed>0.5 ? speed/2 : speed;
} else if (e.clientX>13 && e.clientX<32 && e.clientY>maxY && e.clientY<maxY+20) {
speed = speed<2.0 ? speed*2 : speed;
} else {
mouse.pressed = true;
mouse.pressedFrame = 0;
}
};
document.onmouseup = function() {
mouse.pressed = false;
};
document.onkeydown = function(e) {
switch(e.keyCode) {
case 77:
muted= !muted;
if (!muted) {
timeStart=-1;
}
break;
case 82:
initLevel(level);
break;
case 27:
fadeToLevel(0);
break;
default:
return true;
break;
}
return false;
};
var levelStart = false;
var homeLoaded = 0;
var fade = {
inProgress:false,
out:0,
in:0,
level:0
};
gameLoop = function(dt) {
window.requestAnimationFrame(gameLoop);
nbMs = dt-oldDT;
frame+=(speed/(16.66/nbMs));
oldDT = dt;
drawScene(gl, programInfo, buffers);
var oldMeRadius = me.radius;
for (i=1; i<stage.elements.length; i++) {
if (!stage.elements[i].radius) {
continue;
}
stage.elements[i].update();
stage.elements[i].collid = false;
}
unitInPx = 2 * Math.tan( vFOV / 2 ) * (-zoom);
unitInPx= c.height / unitInPx;
ctx2D.clearRect(0,0,c.width,c.height);
ctx2D.strokeStyle = '#FFF';
if (fade.inProgress) {
if (fade.out) {
fade.out-=0.025/(16.66/nbMs);
if (fade.out<=0) {
fade.out = 0;
initLevel(fade.level);
fade.in = 1;
} else {
ctx2D.globalAlpha = fade.out;
}
} else if (fade.in) {
fade.in-=0.025/(16.66/nbMs);
if (fade.in<=0) {
fade.inProgress = false;
ctx2D.globalAlpha = 1;
} else {
ctx2D.globalAlpha = 1-fade.in;
}
}
}
if (level==-1) {
ctx2D.globalAlpha = Math.max(0,Math.min(1.0, frame/360));
drawText(
c.width/2 - ((12*(14*1*(c.width/1920))/2)),
c.height/2 - 7,
"CLICK TO ENTER",
Math.max(0.8,1*(c.width/1920))
);
ctx2D.globalAlpha = 1;
return;
} else if (level==0) {
mouse.over = null;
c.style.cursor = "default";
var text = "IMMUNITY COLLAPSE";
for (var i=0; i<text.length; i++) {
if (!fade.inProgress) {
ctx2D.globalAlpha = Math.max(homeLoaded, Math.max(0,Math.min(1.0, (frame - frameStart - (6*i))/120)));
}
drawText(
c.width/2 - ((12*(17*5*(c.width/1920))/2)),
c.height/8,
text[i].padStart(i+1," "),
5*(c.width/1920),
true,
"#DDD"
);
}
if (!fade.inProgress) {
ctx2D.globalAlpha = Math.max(homeLoaded ? 0.5 : 0, Math.max(0,Math.min(0.5, (frame - frameStart - 180)/180)));
} else {
ctx2D.globalAlpha /=2;
}
drawText(
c.width/2 - ((12*(29*2*(c.width/1920))/2)),
c.height/8 + (12*9),
"- THE ART OF KILLING SLOWLY - ",
2*(c.width/1920),
true,
"#DCB",
"#FFF"
);
if (fade.inProgress) {
ctx2D.globalAlpha *=2;
}
var globalOpacity = Math.max(homeLoaded, Math.max(0,Math.min(1, (frame - frameStart - 300)/180)));
if (!fade.inProgress) {
ctx2D.globalAlpha = globalOpacity;
}
homeLoaded = globalOpacity;
for (var i=0; i<7; i++) {
var y = c.height/8 + (20*9)+(i*c.height/20);
if (i>1) {
y+=c.height/20;
}
if (i>4) {
y+=c.height/20;
}
var co = "#DDD";
if (mouse.x > c.width/2 - ((12*(9*1.5*(c.height/1000))/2))) {
if (mouse.x < c.width/2 + ((12*(9*1.5*(c.height/1000))/2))) {
if (mouse.y > y) {
if (mouse.y < y + (c.height/40)) {
var co = "#F33";
c.style.cursor = "pointer";
mouse.over = i+1;
}
}
}
}
var text = "";
switch (i) {
case 0:
text = "TUTO 1";
break;
case 1:
text = "TUTO 2";
break;
case 2:
text = "EASY";
break;
case 3:
text = "MEDIUM";
break;
case 4:
text = "HARD";
break;
case 5:
text = "RANDOM (MEDIUM)";
break;
case 6:
text = "RANDOM (HARD)";
break;
}
drawText(c.width/2 - ((12*(text.length*1.5*(c.height/1000))/2)),