-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappLittleConstructor.js
6023 lines (4919 loc) · 211 KB
/
appLittleConstructor.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
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
/*
First: thanks to @Javidx9, this was inspired in his youtube tutorials
OneLoneCoder.com - 3D Graphics Part #1 - Triangles & Projections
"Tredimensjonal Grafikk" - @Javidx9
Second: license
Third: about this and me.
License
~~~~~~~
JSEngine Senoid (Playing with hyper minimal 3dEngineJS).
Copyright (C) 2020 mfontanadev.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; See license for details.
Original works located at:
https://github.com/mfontanadev
From mfontanadev :)
~~~~~~~~~~~~~~~
Hello! Use this as like as you want, I hope you can create amazing things
and have a lot of fun. Let me know. You acknowledge
that I am not responsible for anything bad that happens as a result of
your actions. However this code is protected by GNU GPLv3, see the license in the
github repo. This means you must attribute me if you use it. You can view this
license here: https://github.com/mfontanadev/POCs/blob/master/LICENSE
Cheers!
Background
~~~~~~~~~~
Sources: https://github.com/mfontanadev/appLittleConstructor - Little Constructor - 3dEngineJS hyper minimal.
This programm has two layers, first you can use it like a 3dEngine, second,
you can use it to build virtual houses. I hope this programm can inspire you
to make your own one.
Video
~~~~~
PENDING
Author
~~~~~~
site: https://mfontanadev.github.io
twitter: https://twitter.com/mfontanadev
git: https://github.com/mfontanadev
linkedin: https://www.linkedin.com/in/mauricio-fontana-8285681b/?originalSubdomain=ar
Last Updated: 20/04/2020
*/
// ---------------------------------------------------------------
// CLASS: jsGameEngine.js
// ---------------------------------------------------------------
// --------------------------------------------------------------------
// Minimal engine to render points, lines, keyboard, save files,
// and some usefull functions.
//
/* thanks to @Javidx9, this was inspired in his youtube tutorials
OneLoneCoder.com - 3D Graphics Part #1 - Triangles & Projections
"Tredimensjonal Grafikk" - @Javidx9
*/
JSGameEngine.C_FPS = 24;
JSGameEngine.C_TEXTURE_QUALITY = 2;
JSGameEngine.C_VIEWPORT_SCALE = 0.5; //one unit in world = 0.5 unit in viewport.
JSGameEngine.C_VIEWPORT_CURSOR_LENGTH = 20; //px;
JSGameEngine.C_SKEEP_TEST = true;
JSGameEngine.PIXEL_SOLID = 1;
JSGameEngine.FG_WHITE = "white";
var C_KEY_SHIFT = 16;
var C_KEY_CTRL = 17;
var C_KEY_ALT = 18;
var C_KEY_LEFT = 37;
var C_KEY_DOWN = 40;
var C_KEY_RIGHT = 39;
var C_KEY_UP = 38;
var C_KEY_CHAR_A = 65;
var C_KEY_CHAR_B = 66;
var C_KEY_CHAR_C = 67;
var C_KEY_CHAR_D = 68;
var C_KEY_CHAR_E = 69;
var C_KEY_CHAR_F = 70;
var C_KEY_CHAR_G = 71;
var C_KEY_CHAR_H = 72;
var C_KEY_CHAR_I = 73;
var C_KEY_CHAR_J = 74;
var C_KEY_CHAR_K = 75;
var C_KEY_CHAR_L = 76;
var C_KEY_CHAR_M = 77;
var C_KEY_CHAR_N = 78;
var C_KEY_CHAR_O = 79;
var C_KEY_CHAR_P = 80;
var C_KEY_CHAR_Q = 81;
var C_KEY_CHAR_R = 82;
var C_KEY_CHAR_S = 83;
var C_KEY_CHAR_T = 84;
var C_KEY_CHAR_U = 85;
var C_KEY_CHAR_V = 86;
var C_KEY_CHAR_W = 87;
var C_KEY_CHAR_X = 88;
var C_KEY_CHAR_Y = 89;
var C_KEY_CHAR_Z = 90;
var C_KEY_CHAR_ADD = 187;
var C_KEY_CHAR_SUB = 189;
var C_KEY_INSERT = 45;
var C_KEY_DELETE = 46;
var C_KEY_RETURN = 13;
var C_KEY_SPACE = 32;
var C_KEY_CHAR_0 = 48;
var C_KEY_CHAR_1 = 49;
var C_KEY_CHAR_2 = 50;
var C_KEY_CHAR_3 = 51;
var C_KEY_CHAR_4 = 52;
var C_KEY_CHAR_5 = 53;
var C_KEY_CHAR_6 = 54;
var C_KEY_CHAR_7 = 55;
var C_KEY_CHAR_8 = 56;
var C_KEY_CHAR_9 = 57;
function JSGameEngine(_canvas, _context)
{
this.canvas = _canvas;
this.context = this.canvas.getContext("2d");
this.fontSize = 9;
this.font = this.fontSize + "px Verdana";
this.canvasWidth = this.canvas.width;
this.halfCanvasWidth = this.canvasWidth * 0.5;
this.canvasHeight = this.canvas.height;
this.halfCanvasHeight = this.canvasHeight * 0.5;
this.aspectRatio = this.canvasWidth / this.canvasHeight;
this.aspectRatio = 1;
this.keyboardeventPropagation = true;
this.keyPressed = [];
this.keyWaitRelease = [];
this.onKeyDownCallback = null;
this.onKeyUpCallback = null;
this.initKeyboard();
this.times = new Array();
this.logTimes = false;
this.px = this.context.createImageData(1, 1);
this.pxData = this.px.data;
this.setFontSize(this.fontSize);
console.log("js game engine created, widht x height:", this.canvasWidth, " x ", this.canvasHeight);
this.pDepthBuffer = new Array();
for (var y = 0; y < this.canvasHeight; y++)
for (var x = 0; x < this.canvasWidth; x++)
this.pDepthBuffer.push(0);
if (JSGameEngine.C_SKEEP_TEST === false)
JSGameEngine.tests(this);
}
JSGameEngine.prototype.start = function()
{
onUserCreate();
setInterval('onUserUpdate()', 1000 / JSGameEngine.C_FPS);
}
JSGameEngine.prototype.setKeyboardCallbacks = function(_onKeyDownFunction, _onKeyUpFunction)
{
this.onKeyDownCallback = _onKeyDownFunction;
this.onKeyUpCallback = _onKeyUpFunction;
}
JSGameEngine.prototype.initKeyboard = function()
{
var finalThis = this;
for (var i = 0; i < 256; i++)
{
this.keyPressed.push(false);
this.keyWaitRelease.push(false);
}
document.onkeydown = function(event)
{
finalThis.keyPressed[event.keyCode] = true;
if (finalThis.onKeyDownCallback)
finalThis.onKeyDownCallback(event.keyCode);
return finalThis.keyboardeventPropagation;
};
document.onkeyup = function(event)
{
finalThis.keyPressed[event.keyCode] = false;
finalThis.keyWaitRelease[event.keyCode] = false;
if (finalThis.onKeyUpCallback)
finalThis.onKeyUpCallback(event.keyCode);
return finalThis.keyboardeventPropagation;
}
}
JSGameEngine.prototype.isKeyPressed = function(_scanCode)
{
return this.keyPressed[_scanCode];
}
JSGameEngine.prototype.satKeyWaitRelease = function(_scanCode)
{
this.keyWaitRelease[_scanCode] = true;
}
JSGameEngine.prototype.getKeyWaitingRelease = function(_scanCode)
{
return this.keyWaitRelease[_scanCode];
}
JSGameEngine.prototype.clearScreen = function()
{
gEngine.context.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
}
JSGameEngine.prototype.drawTriangle = function(x1, y1, x2, y2, x3, y3, style, fillColor)
{
this.renderLine(x1, y1, x2, y2, style, fillColor);
this.renderLine(x2, y2, x3, y3, style, fillColor);
this.renderLine(x3, y3, x1, y1, style, fillColor);
}
JSGameEngine.prototype.fillTriangle = function(x1, y1, x2, y2, x3, y3, style, fillColor)
{
this.context.save();
this.context.beginPath();
this.context.moveTo(x1, y1);
this.context.lineTo(x2, y2);
this.context.lineTo(x3, y3);
this.context.closePath();
this.context.strokeStyle = fillColor;
this.context.stroke();
this.context.fillStyle = fillColor;
this.context.fill();
this.context.restore();
}
JSGameEngine.prototype.putPixel = function( x, y, r, g, b, a)
{
this.context.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + (a / 255) + ')';
this.context.fillRect(x, y, 1, 1);
}
JSGameEngine.prototype.putPixelSize = function( x, y, r, g, b, a, size)
{
this.context.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + (a / 255) + ')';
this.context.fillRect(x, y, size, size);
}
JSGameEngine.prototype.texturedTriangle = function( x1, y1, u1, v1, w1,
x2, y2, u2, v2, w2,
x3, y3, u3, v3, w3, texture)
{
if (texture === null)
return;
var detail = 0;
var pixColour = null;
var t = 0;
if (y2 < y1)
{
t=y1; y1=y2; y2=t;
t=x1; x1=x2; x2=t;
t=u1; u1=u2; u2=t;
t=v1; v1=v2; v2=t;
t=w1; w1=w2; w2=t;
}
if (y3 < y1)
{
t=y1; y1=y3; y3=t;
t=x1; x1=x3; x3=t;
t=u1; u1=u3; u3=t;
t=v1; v1=v3; v3=t;
t=w1; w1=w3; w3=t;
}
if (y3 < y2)
{
t=y2; y2=y3; y3=t;
t=x2; x2=x3; x3=t;
t=u2; u2=u3; u3=t;
t=v2; v2=v3; v3=t;
t=w2; w2=w3; w3=t;
}
var dy1 = y2 - y1;
var dx1 = x2 - x1;
var dv1 = v2 - v1;
var du1 = u2 - u1;
var dw1 = w2 - w1;
var dy2 = y3 - y1;
var dx2 = x3 - x1;
var dv2 = v3 - v1;
var du2 = u3 - u1;
var dw2 = w3 - w1;
var tex_u, tex_v, tex_w;
var dax_step = 0, dbx_step = 0,
du1_step = 0, dv1_step = 0,
du2_step = 0, dv2_step = 0,
dw1_step = 0, dw2_step = 0;
if (dy1) dax_step = dx1 / Math.abs(dy1);
if (dy2) dbx_step = dx2 / Math.abs(dy2);
if (dy1) du1_step = du1 / Math.abs(dy1);
if (dy1) dv1_step = dv1 / Math.abs(dy1);
if (dy1) dw1_step = dw1 / Math.abs(dy1);
if (dy2) du2_step = du2 / Math.abs(dy2);
if (dy2) dv2_step = dv2 / Math.abs(dy2);
if (dy2) dw2_step = dw2 / Math.abs(dy2);
if (dy1)
{
for (var i = y1; i <= y2; i+=JSGameEngine.C_TEXTURE_QUALITY)
{
var ax = x1 + (i - y1) * dax_step;
var bx = x1 + (i - y1) * dbx_step;
var tex_su = u1 + (i - y1) * du1_step;
var tex_sv = v1 + (i - y1) * dv1_step;
var tex_sw = w1 + (i - y1) * dw1_step;
var tex_eu = u1 + (i - y1) * du2_step;
var tex_ev = v1 + (i - y1) * dv2_step;
var tex_ew = w1 + (i - y1) * dw2_step;
if (ax > bx)
{
t=ax; ax=bx; bx=t;
t=tex_su; tex_su=tex_eu; tex_eu=t;
t=tex_sv; tex_sv=tex_ev; tex_ev=t;
t=tex_sw; tex_sw=tex_ew; tex_ew=t;
}
tex_u = tex_su;
tex_v = tex_sv;
tex_w = tex_sw;
var tstep = 1 / (bx - ax);
var t = 0;
for (var j = ax; j < bx; j+=JSGameEngine.C_TEXTURE_QUALITY)
{
tex_u = (1 - t) * tex_su + t * tex_eu;
tex_v = (1 - t) * tex_sv + t * tex_ev;
tex_w = (1 - t) * tex_sw + t * tex_ew;
pixColour = this.getTexturePixelScaled(texture, tex_u / tex_w, tex_v / tex_w);
this.putPixelSize(j, i, pixColour[0], pixColour[1], pixColour[2], pixColour[3], JSGameEngine.C_TEXTURE_QUALITY);
t += tstep * JSGameEngine.C_TEXTURE_QUALITY;
}
}
}
dy1 = y3 - y2;
dx1 = x3 - x2;
dv1 = v3 - v2;
du1 = u3 - u2;
dw1 = w3 - w2;
if (dy1) dax_step = dx1 / Math.abs(dy1);
if (dy2) dbx_step = dx2 / Math.abs(dy2);
du1_step = 0, dv1_step = 0;
if (dy1) du1_step = du1 / Math.abs(dy1);
if (dy1) dv1_step = dv1 / Math.abs(dy1);
if (dy1) dw1_step = dw1 / Math.abs(dy1);
if (dy1)
{
for (var i = y2; i <= y3; i+=JSGameEngine.C_TEXTURE_QUALITY)
//for (var i = y2; i <= y2; i++)
{
var ax = x2 + (i - y2) * dax_step;
var bx = x1 + (i - y1) * dbx_step;
var tex_su = u2 + (i - y2) * du1_step;
var tex_sv = v2 + (i - y2) * dv1_step;
var tex_sw = w2 + (i - y2) * dw1_step;
var tex_eu = u1 + (i - y1) * du2_step;
var tex_ev = v1 + (i - y1) * dv2_step;
var tex_ew = w1 + (i - y1) * dw2_step;
if (ax > bx)
{
t=ax; ax=bx; bx=t;
t=tex_su; tex_su=tex_eu; tex_eu=t;
t=tex_sv; tex_sv=tex_ev; tex_ev=t;
t=tex_sw; tex_sw=tex_ew; tex_ew=t;
}
tex_u = tex_su;
tex_v = tex_sv;
tex_w = tex_sw;
var tstep = 1 / (bx - ax);
var t = 0;
for (var j = ax; j < bx; j+=JSGameEngine.C_TEXTURE_QUALITY)
{
tex_u = (1 - t) * tex_su + t * tex_eu;
tex_v = (1 - t) * tex_sv + t * tex_ev;
tex_w = (1 - t) * tex_sw + t * tex_ew;
pixColour = this.getTexturePixelScaled(texture, tex_u / tex_w, tex_v / tex_w);
this.putPixelSize(j, i, pixColour[0], pixColour[1], pixColour[2], pixColour[3], JSGameEngine.C_TEXTURE_QUALITY);
t += tstep * JSGameEngine.C_TEXTURE_QUALITY;
}
}
}
}
JSGameEngine.prototype.getTexturePixelScaled = function(imgData, x, y)
{
var px = x * imgData.width;
var py = y * imgData.height;
return this.getTexturePixel(imgData, px, py);
}
JSGameEngine.prototype.getTexturePixel = function(imgData, x, y)
{
x = Math.round(x, 2) % imgData.width;
y = Math.round(y, 2) % imgData.height;
var index = (y * imgData.width + x) * 4;
return ([ imgData.data[index],
imgData.data[index + 1],
imgData.data[index + 2],
imgData.data[index + 3]
]);
}
JSGameEngine.prototype.fillTriangleCustom = function( x1, y1, w1, x2, y2, w2, x3, y3, w3, fillColor)
{
var pixColour = [fillColor.r, fillColor.g, fillColor.b, fillColor.a * 255];
var t = 0;
var index = 0;
if (y2 < y1)
{
t=y1; y1=y2; y2=t;
t=x1; x1=x2; x2=t;
t=w1; w1=w2; w2=t;
}
if (y3 < y1)
{
t=y1; y1=y3; y3=t;
t=x1; x1=x3; x3=t;
t=w1; w1=w3; w3=t;
t=w2; w2=w3; w3=t;
}
if (y3 < y2)
{
t=y2; y2=y3; y3=t;
t=x2; x2=x3; x3=t;
}
var dy1 = y2 - y1;
var dx1 = x2 - x1;
var dw1 = w2 - w1;
var dy2 = y3 - y1;
var dx2 = x3 - x1;
var dw2 = w3 - w1;
var tex_w;
var dax_step = 0, dbx_step = 0,
dw1_step = 0, dw2_step = 0;
if (dy1) dax_step = dx1 / Math.abs(dy1);
if (dy2) dbx_step = dx2 / Math.abs(dy2);
if (dy1) dw1_step = dw1 / Math.abs(dy1);
if (dy2) dw2_step = dw2 / Math.abs(dy2);
if (dy1)
{
for (var i = y1; i <= y2; i+=JSGameEngine.C_TEXTURE_QUALITY)
{
var ax = x1 + (i - y1) * dax_step;
var bx = x1 + (i - y1) * dbx_step;
var tex_sw = w1 + (i - y1) * dw1_step;
var tex_ew = w1 + (i - y1) * dw2_step;
if (ax > bx)
{
t=ax; ax=bx; bx=t;
t=tex_sw; tex_sw=tex_ew; tex_ew=t;
}
tex_w = tex_sw;
var tstep = 1 / (bx - ax);
var t = 0;
for (var j = ax; j < bx; j+=JSGameEngine.C_TEXTURE_QUALITY)
{
tex_w = (1 - t) * tex_sw + t * tex_ew;
//index = Math.round(i * this.canvasWidth + j, 0);
//if (tex_w > this.pDepthBuffer[index])
{
this.putPixelSize(j, i, pixColour[0], pixColour[1], pixColour[2], pixColour[3], JSGameEngine.C_TEXTURE_QUALITY);
//this.pDepthBuffer[index] = tex_w;
}
t += tstep * JSGameEngine.C_TEXTURE_QUALITY;
}
}
}
dy1 = y3 - y2;
dx1 = x3 - x2;
dw1 = w3 - w2;
if (dy1) dax_step = dx1 / Math.abs(dy1);
if (dy2) dbx_step = dx2 / Math.abs(dy2);
if (dy1) dw1_step = dw1 / Math.abs(dy1);
if (dy1)
{
for (var i = y2; i <= y3; i+=JSGameEngine.C_TEXTURE_QUALITY)
//for (var i = y2; i <= y2; i++)
{
var ax = x2 + (i - y2) * dax_step;
var bx = x1 + (i - y1) * dbx_step;
var tex_sw = w2 + (i - y2) * dw1_step;
var tex_ew = w1 + (i - y1) * dw2_step;
if (ax > bx)
{
t=ax; ax=bx; bx=t;
t=tex_sw; tex_sw=tex_ew; tex_ew=t;
}
tex_w = tex_sw;
var tstep = 1 / (bx - ax);
var t = 0;
for (var j = ax; j < bx; j+=JSGameEngine.C_TEXTURE_QUALITY)
{
tex_w = (1 - t) * tex_sw + t * tex_ew;
//index = Math.round(i * this.canvasWidth + j, 0);
//if (tex_w > this.pDepthBuffer[index])
{
this.putPixelSize(j, i, pixColour[0], pixColour[1], pixColour[2], pixColour[3], JSGameEngine.C_TEXTURE_QUALITY);
//this.pDepthBuffer[index] = tex_w;
}
t += tstep * JSGameEngine.C_TEXTURE_QUALITY;
}
}
}
}
JSGameEngine.prototype.renderLine = function(x1, y1, x2, y2, style, fillColor)
{
this.context.save();
this.context.beginPath();
this.context.lineWidth = style;
this.context.strokeStyle = fillColor;
this.context.moveTo(x1, y1);
this.context.lineTo(x2, y2);
this.context.stroke();
this.context.restore();
}
JSGameEngine.prototype.renderCircle = function(x1, y1, r, lineWidth, fillColor)
{
this.context.save();
this.context.beginPath();
this.context.strokeStyle = fillColor;
this.context.lineWidth = lineWidth;
this.context.fillStyle = fillColor;
this.context.arc(x1, y1, r, 0, 2 * Math.PI, false);
this.context.fill();
this.context.stroke();
this.context.restore();
}
JSGameEngine.prototype.draw2DAxis = function(fillColor)
{
this.renderLine(0, this.halfCanvasHeight, this.canvasWidth, this.halfCanvasHeight, 1, fillColor);
this.renderLine(this.halfCanvasWidth, 0, this.halfCanvasWidth, this.canvasHeight, 1, fillColor);
}
JSGameEngine.prototype.renderText = function(_cenX, _cenY, _text, _fillColor)
{
if (typeof _fillColor === "undefined")
_fillColor = "white";
this.context.save();
this.context.font = this.font;
this.context.fillStyle = _fillColor;
this.context.fillText(_text, _cenX, _cenY);
this.context.restore();
}
JSGameEngine.prototype.locateText = function(_col, _row, _text, _fillColor)
{
this.renderText(_col * this.fontSize, _row * this.fontSize * 1.5, _text, _fillColor);
}
JSGameEngine.prototype.getRowNumberGivenYPosition = function(_yPosition)
{
return Math.ceil(_yPosition / (this.fontSize * 1.5));
}
JSGameEngine.prototype.getColNumberGivenXPosition = function(_xPosition)
{
return Math.ceil(_xPosition / this.fontSize);
}
JSGameEngine.prototype.renderRectangle = function(_x1, _y1, _w, _h, lineWidth, fillColor)
{
this.context.save();
this.context.beginPath();
this.context.lineWidth = lineWidth;
this.context.strokeStyle = fillColor;
this.context.rect(_x1, _y1, _w, _h);
this.context.stroke();
this.context.restore();
}
JSGameEngine.prototype.rgbaToColor = function(_r, _g, _b, _a)
{
var r = _r % 256;
var g = _g % 256;
var b = _b % 256;
var result = "rgba(" + r.toString() + "," + g.toString() + "," + b.toString() + "," + _a.toString()+")";
return result;
}
JSGameEngine.st_rgbaToColor = function(_r, _g, _b)
{
var r = _r % 256;
var g = _g % 256;
var b = _b % 256;
var result = "rgb(" + r.toString() + "," + g.toString() + "," + b.toString() + ")";
return result;
}
JSGameEngine.prototype.startTime = function(_key)
{
var foundItem = this.chFoundTimeKey(_key);
if (foundItem === null)
{
foundItem = {key:_key, date: null, totalTime: 0};
this.times.push(foundItem);
}
foundItem.totalTime = 0;
foundItem.date = Date.now();
}
JSGameEngine.prototype.showTimes = function(_value)
{
this.logTimes = _value;
}
JSGameEngine.prototype.showTimeDiff = function(_key)
{
if (this.logTimes === false)
return;
this.addTimeDiff(_key);
var foundItem = this.chFoundTimeKey(_key);
if (foundItem !== null)
{
console.log("Timediff ", foundItem.key, ":", foundItem.totalTime);
}
else
{
console.log("Timediff ", _key, ": not found");
}
}
JSGameEngine.prototype.addTimeDiff = function(_key)
{
var foundItem = this.chFoundTimeKey(_key);
if (foundItem !== null)
{
foundItem.totalTime += (Date.now() - foundItem.date);
foundItem.date = Date.now();
}
}
JSGameEngine.prototype.chFoundTimeKey = function(_key)
{
var foundItem = null;
for (var i = 0; i < this.times.length && foundItem === null; i++)
if (this.times[i].key === _key)
foundItem = this.times[i];
return foundItem;
}
JSGameEngine.prototype.clearBuffer = function()
{
for (let index = 0; index < this.pDepthBuffer.length; index++)
{
this.pDepthBuffer[index] = 0;
}
}
JSGameEngine.prototype.swap = function(_array)
{
if (_array != null && _array.length > 0)
{
_array.splice(0, _array.length);
}
}
JSGameEngine.prototype.setFontSize = function(_size)
{
this.font = _size + "px Verdana";
}
JSGameEngine.prototype.getFontSize = function()
{
return this.fontSize;
}
JSGameEngine.tests = function(_this)
{
console.log("\n");
console.log("******** TESTS START ********" );
console.log("******** TESTS END ********" );
console.log("\n");
}
JSGameEngine.download = function(filename, text)
{
//Inline option with no name setted.
//uriContent = "data:application/octet-stream," + encodeURIComponent(json);
//newWindow = window.open(uriContent, 'nuevoDocumento');
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
}
else {
pom.click();
}
}
JSGameEngine.padChar = function(num, size, char)
{
var numLenght = num.toString().length;
if (numLenght < size)
return char.repeat(size - numLenght) + num.toString();
else
return num;
}
JSGameEngine.chClearArray = function(_array)
{
if (_array != null && _array.length > 0)
{
_array.splice(0, _array.length);
}
}
JSGameEngine.graToRad = function(grados)
{
return grados * Math.PI / 180;
}
JSGameEngine.radToGra = function(radians)
{
return 180 * radians / Math.PI;
}
// Get an URL to the OBJ folder, adding SERVER IP and PORT previous to the filename.
JSGameEngine.resolveURLToResourceFolder = function(_filename)
{
return 'http://'+ C_SERVER_IP + '/obj/' + _filename;
}
// ---------------------------------------------------------------
// CLASS: jsGameEngine.js
// ---------------------------------------------------------------
// --------------------------------------------------------------------
// Minimal engine to render points, lines, keyboard, save files,
// and some usefull functions.
//
/* thanks to @Javidx9, this was inspired in his youtube tutorials
OneLoneCoder.com - 3D Graphics Part #1 - Triangles & Projections
"Tredimensjonal Grafikk" - @Javidx9
*/
JSGameEngine.C_FPS = 24;
JSGameEngine.C_TEXTURE_QUALITY = 2;
JSGameEngine.C_VIEWPORT_SCALE = 0.5; //one unit in world = 0.5 unit in viewport.
JSGameEngine.C_VIEWPORT_CURSOR_LENGTH = 20; //px;
JSGameEngine.C_SKEEP_TEST = true;
JSGameEngine.PIXEL_SOLID = 1;
JSGameEngine.FG_WHITE = "white";
var C_KEY_SHIFT = 16;
var C_KEY_CTRL = 17;
var C_KEY_ALT = 18;
var C_KEY_LEFT = 37;
var C_KEY_DOWN = 40;
var C_KEY_RIGHT = 39;
var C_KEY_UP = 38;
var C_KEY_CHAR_A = 65;
var C_KEY_CHAR_B = 66;
var C_KEY_CHAR_C = 67;
var C_KEY_CHAR_D = 68;
var C_KEY_CHAR_E = 69;
var C_KEY_CHAR_F = 70;
var C_KEY_CHAR_G = 71;
var C_KEY_CHAR_H = 72;
var C_KEY_CHAR_I = 73;
var C_KEY_CHAR_J = 74;
var C_KEY_CHAR_K = 75;
var C_KEY_CHAR_L = 76;
var C_KEY_CHAR_M = 77;
var C_KEY_CHAR_N = 78;
var C_KEY_CHAR_O = 79;
var C_KEY_CHAR_P = 80;
var C_KEY_CHAR_Q = 81;
var C_KEY_CHAR_R = 82;
var C_KEY_CHAR_S = 83;
var C_KEY_CHAR_T = 84;
var C_KEY_CHAR_U = 85;
var C_KEY_CHAR_V = 86;
var C_KEY_CHAR_W = 87;
var C_KEY_CHAR_X = 88;
var C_KEY_CHAR_Y = 89;
var C_KEY_CHAR_Z = 90;
var C_KEY_CHAR_ADD = 187;
var C_KEY_CHAR_SUB = 189;
var C_KEY_INSERT = 45;
var C_KEY_DELETE = 46;
var C_KEY_RETURN = 13;
var C_KEY_SPACE = 32;
var C_KEY_CHAR_0 = 48;
var C_KEY_CHAR_1 = 49;
var C_KEY_CHAR_2 = 50;
var C_KEY_CHAR_3 = 51;
var C_KEY_CHAR_4 = 52;
var C_KEY_CHAR_5 = 53;
var C_KEY_CHAR_6 = 54;
var C_KEY_CHAR_7 = 55;
var C_KEY_CHAR_8 = 56;
var C_KEY_CHAR_9 = 57;
function JSGameEngine(_canvas, _context)
{
this.canvas = _canvas;
this.context = this.canvas.getContext("2d");
this.fontSize = 9;
this.font = this.fontSize + "px Verdana";
this.canvasWidth = this.canvas.width;
this.halfCanvasWidth = this.canvasWidth * 0.5;
this.canvasHeight = this.canvas.height;
this.halfCanvasHeight = this.canvasHeight * 0.5;
this.aspectRatio = this.canvasWidth / this.canvasHeight;
this.aspectRatio = 1;
this.keyboardeventPropagation = true;
this.keyPressed = [];
this.keyWaitRelease = [];
this.onKeyDownCallback = null;
this.onKeyUpCallback = null;
this.initKeyboard();
this.times = new Array();
this.logTimes = false;
this.px = this.context.createImageData(1, 1);
this.pxData = this.px.data;
this.setFontSize(this.fontSize);
console.log("js game engine created, widht x height:", this.canvasWidth, " x ", this.canvasHeight);
this.pDepthBuffer = new Array();
for (var y = 0; y < this.canvasHeight; y++)
for (var x = 0; x < this.canvasWidth; x++)
this.pDepthBuffer.push(0);
if (JSGameEngine.C_SKEEP_TEST === false)
JSGameEngine.tests(this);
}
JSGameEngine.prototype.start = function()
{
onUserCreate();
setInterval('onUserUpdate()', 1000 / JSGameEngine.C_FPS);
}
JSGameEngine.prototype.setKeyboardCallbacks = function(_onKeyDownFunction, _onKeyUpFunction)
{
this.onKeyDownCallback = _onKeyDownFunction;
this.onKeyUpCallback = _onKeyUpFunction;
}
JSGameEngine.prototype.initKeyboard = function()
{
var finalThis = this;
for (var i = 0; i < 256; i++)
{
this.keyPressed.push(false);
this.keyWaitRelease.push(false);
}
document.onkeydown = function(event)
{
finalThis.keyPressed[event.keyCode] = true;
if (finalThis.onKeyDownCallback)
finalThis.onKeyDownCallback(event.keyCode);
return finalThis.keyboardeventPropagation;
};
document.onkeyup = function(event)
{
finalThis.keyPressed[event.keyCode] = false;
finalThis.keyWaitRelease[event.keyCode] = false;
if (finalThis.onKeyUpCallback)
finalThis.onKeyUpCallback(event.keyCode);
return finalThis.keyboardeventPropagation;
}
}
JSGameEngine.prototype.isKeyPressed = function(_scanCode)
{
return this.keyPressed[_scanCode];
}
JSGameEngine.prototype.satKeyWaitRelease = function(_scanCode)
{