littlejsengine 1.14.11 → 1.14.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/dist/littlejs.d.ts +252 -109
  2. package/dist/littlejs.esm.js +559 -252
  3. package/dist/littlejs.esm.min.js +1 -1
  4. package/dist/littlejs.js +557 -251
  5. package/dist/littlejs.min.js +1 -1
  6. package/dist/littlejs.release.js +482 -206
  7. package/examples/box2d/gameObjects.js +2 -2
  8. package/examples/breakout/gameObjects.js +2 -2
  9. package/examples/breakoutTutorial/README.md +32 -32
  10. package/examples/breakoutTutorial/game.js +1 -1
  11. package/examples/electron/game.js +3 -3
  12. package/examples/electron/index.html +2 -2
  13. package/examples/electron/package.json +1 -8
  14. package/examples/index.html +10 -7
  15. package/examples/module/game.js +3 -3
  16. package/examples/platformer/gameEffects.js +4 -4
  17. package/examples/platformer/gameLevel.js +1 -1
  18. package/examples/platformer/gameObjects.js +4 -4
  19. package/examples/puzzle/game.js +1 -1
  20. package/examples/shorts/animation.js +1 -1
  21. package/examples/shorts/base.html +1 -1
  22. package/examples/shorts/blending.js +8 -14
  23. package/examples/shorts/box2d.js +7 -3
  24. package/examples/shorts/box2dCar.js +2 -1
  25. package/examples/shorts/empty.js +30 -0
  26. package/examples/shorts/helloWorld.js +1 -1
  27. package/examples/shorts/hillGlideGame.js +10 -4
  28. package/examples/shorts/landerGame.js +11 -8
  29. package/examples/shorts/medals.js +4 -4
  30. package/examples/shorts/music.js +29 -56
  31. package/examples/shorts/musicPlayer.js +134 -0
  32. package/examples/shorts/nineSlice.js +34 -15
  33. package/examples/shorts/parallax.js +4 -3
  34. package/examples/shorts/particles.js +15 -15
  35. package/examples/shorts/piano.js +15 -21
  36. package/examples/shorts/pongGame.js +6 -4
  37. package/examples/shorts/raycasting.js +9 -4
  38. package/examples/shorts/sequencer.js +122 -0
  39. package/examples/shorts/shapes.js +7 -4
  40. package/examples/shorts/slidingPuzzle.js +4 -2
  41. package/examples/shorts/sound.js +18 -9
  42. package/examples/shorts/spaceGame.js +12 -8
  43. package/examples/shorts/spriteAtlas.js +7 -7
  44. package/examples/shorts/starfield.js +1 -1
  45. package/examples/shorts/systemFont.js +2 -2
  46. package/examples/shorts/texture.js +2 -2
  47. package/examples/shorts/tileLayer.js +10 -10
  48. package/examples/shorts/tiltedView.js +14 -3
  49. package/examples/shorts/timers.js +10 -0
  50. package/examples/shorts/topDown.js +4 -1
  51. package/examples/shorts/uiSystem.js +8 -5
  52. package/examples/starter/game.js +3 -3
  53. package/examples/starter/index.html +2 -2
  54. package/examples/typescript/game.js +3 -3
  55. package/examples/typescript/game.ts +3 -3
  56. package/examples/uiSystem/game.js +3 -3
  57. package/package.json +4 -2
  58. package/plugins/box2d.js +17 -2
  59. package/plugins/newgrounds.js +7 -5
  60. package/plugins/postProcess.js +5 -2
  61. package/plugins/uiSystem.js +120 -34
  62. package/plugins/zzfxm.js +5 -1
  63. package/reference.md +1 -1
  64. package/src/engine.js +35 -12
  65. package/src/engineAudio.js +31 -14
  66. package/src/engineDebug.js +75 -45
  67. package/src/engineDraw.js +60 -31
  68. package/src/engineExport.js +2 -1
  69. package/src/engineMedals.js +10 -2
  70. package/src/engineObject.js +14 -10
  71. package/src/engineParticles.js +59 -46
  72. package/src/engineSettings.js +7 -7
  73. package/src/engineTileLayer.js +45 -20
  74. package/src/engineUtilities.js +67 -20
@@ -231,6 +231,8 @@ function formatTime(t)
231
231
  async function fetchJSON(url)
232
232
  {
233
233
  const response = await fetch(url);
234
+ if (!response.ok)
235
+ throw new Error(`Failed to fetch JSON from ${url}: ${response.status} ${response.statusText}`);
234
236
  return response.json();
235
237
  }
236
238
 
@@ -241,6 +243,13 @@ async function fetchJSON(url)
241
243
  * @memberof Utilities */
242
244
  function isNumber(n) { return typeof n === 'number' && !isNaN(n); }
243
245
 
246
+ /**
247
+ * Check if object is a valid string or can be converted to one
248
+ * @param {any} s
249
+ * @return {boolean}
250
+ * @memberof Utilities */
251
+ function isString(s) { return s !== undefined && s !== null && typeof s.toString() === 'string'; }
252
+
244
253
  ///////////////////////////////////////////////////////////////////////////////
245
254
 
246
255
  /** Random global functions
@@ -303,6 +312,7 @@ function randColor(colorA=new Color, colorB=new Color(0,0,0,1), linear=false)
303
312
  /**
304
313
  * Seeded random number generator
305
314
  * - Can be used to create a deterministic random number sequence
315
+ * @memberof Engine
306
316
  * @example
307
317
  * let r = new RandomGenerator(123); // random number generator with seed 123
308
318
  * let a = r.float(); // random value between 0 and 1
@@ -399,6 +409,7 @@ function ASSERT_VECTOR2_NORMAL(v)
399
409
  /**
400
410
  * 2D Vector object with vector math library
401
411
  * - Functions do not change this so they can be chained together
412
+ * @memberof Engine
402
413
  * @example
403
414
  * let a = new Vector2(2, 3); // vector with coordinates (2, 3)
404
415
  * let b = new Vector2; // vector with coordinates (0, 0)
@@ -539,9 +550,10 @@ class Vector2
539
550
  return new Vector2(this.x*c - this.y*s, this.x*s + this.y*c);
540
551
  }
541
552
 
542
- /** Set the integer direction of this vector, corresponding to multiples of 90 degree rotation (0-3)
553
+ /** Sets this this vector to point in the specified integer direction (0-3), corresponding to multiples of 90 degree rotation
543
554
  * @param {number} [direction]
544
- * @param {number} [length] */
555
+ * @param {number} [length]
556
+ * @return {Vector2} */
545
557
  setDirection(direction, length=1)
546
558
  {
547
559
  ASSERT_NUMBER_VALID(direction);
@@ -549,8 +561,10 @@ class Vector2
549
561
  direction = mod(direction, 4);
550
562
  ASSERT(direction===0 || direction===1 || direction===2 || direction===3,
551
563
  'Vector2.setDirection() direction must be an integer between 0 and 3.');
552
- return vec2(direction%2 ? direction-1 ? -length : length : 0,
553
- direction%2 ? 0 : direction ? -length : length);
564
+
565
+ this.x = direction%2 ? direction-1 ? -length : length : 0;
566
+ this.y = direction%2 ? 0 : direction ? -length : length;
567
+ return this;
554
568
  }
555
569
 
556
570
  /** Returns the integer direction of this vector, corresponding to multiples of 90 degree rotation (0-3)
@@ -646,6 +660,7 @@ function ASSERT_COLOR_VALID(c) { ASSERT(isColor(c), 'Color is invalid.', c); }
646
660
 
647
661
  /**
648
662
  * Color object (red, green, blue, alpha) with some helpful functions
663
+ * @memberof Engine
649
664
  * @example
650
665
  * let a = new Color; // white
651
666
  * let b = new Color(1, 0, 0); // red
@@ -823,7 +838,8 @@ class Color
823
838
  * @return {Color} */
824
839
  setHex(hex)
825
840
  {
826
- ASSERT(typeof hex === 'string' && hex[0] === '#', 'Color hex code must be a string starting with #');
841
+ ASSERT(isString(hex), 'Color hex code must be a string');
842
+ ASSERT(hex[0] === '#', 'Color hex code must start with #');
827
843
  ASSERT([4,5,7,9].includes(hex.length), 'Invalid hex');
828
844
 
829
845
  if (hex.length < 6)
@@ -865,77 +881,78 @@ class Color
865
881
  }
866
882
 
867
883
  ///////////////////////////////////////////////////////////////////////////////
868
- // default colors
884
+ // Default Colors
869
885
 
870
886
  /** Color - White #ffffff
871
887
  * @type {Color}
872
888
  * @memberof Utilities */
873
- const WHITE = rgb();
889
+ const WHITE = protectEngineConstant(rgb());
874
890
 
875
- /** Color - Clear White #ffffff with 0 alpha
891
+ /** Color - Clear White #757474ff with 0 alpha
876
892
  * @type {Color}
877
893
  * @memberof Utilities */
878
- const CLEAR_WHITE = rgb(1,1,1,0);
894
+ const CLEAR_WHITE = protectEngineConstant(rgb(1,1,1,0));
879
895
 
880
896
  /** Color - Black #000000
881
897
  * @type {Color}
882
898
  * @memberof Utilities */
883
- const BLACK = rgb(0,0,0);
899
+ const BLACK = protectEngineConstant(rgb(0,0,0));
884
900
 
885
901
  /** Color - Clear Black #000000 with 0 alpha
886
902
  * @type {Color}
887
903
  * @memberof Utilities */
888
- const CLEAR_BLACK = rgb(0,0,0,0);
904
+ const CLEAR_BLACK = protectEngineConstant(rgb(0,0,0,0));
889
905
 
890
906
  /** Color - Gray #808080
891
907
  * @type {Color}
892
908
  * @memberof Utilities */
893
- const GRAY = rgb(.5,.5,.5);
909
+ const GRAY = protectEngineConstant(rgb(.5,.5,.5));
894
910
 
895
911
  /** Color - Red #ff0000
896
912
  * @type {Color}
897
913
  * @memberof Utilities */
898
- const RED = rgb(1,0,0);
914
+ const RED = protectEngineConstant(rgb(1,0,0));
899
915
 
900
916
  /** Color - Orange #ff8000
901
917
  * @type {Color}
902
918
  * @memberof Utilities */
903
- const ORANGE = rgb(1,.5,0);
919
+ const ORANGE = protectEngineConstant(rgb(1,.5,0));
904
920
 
905
921
  /** Color - Yellow #ffff00
906
922
  * @type {Color}
907
923
  * @memberof Utilities */
908
- const YELLOW = rgb(1,1,0);
924
+ const YELLOW = protectEngineConstant(rgb(1,1,0));
909
925
 
910
926
  /** Color - Green #00ff00
911
927
  * @type {Color}
912
928
  * @memberof Utilities */
913
- const GREEN = rgb(0,1,0);
929
+ const GREEN = protectEngineConstant(rgb(0,1,0));
914
930
 
915
931
  /** Color - Cyan #00ffff
916
932
  * @type {Color}
917
933
  * @memberof Utilities */
918
- const CYAN = rgb(0,1,1);
934
+ const CYAN = protectEngineConstant(rgb(0,1,1));
919
935
 
920
936
  /** Color - Blue #0000ff
921
937
  * @type {Color}
922
938
  * @memberof Utilities */
923
- const BLUE = rgb(0,0,1);
939
+ const BLUE = protectEngineConstant(rgb(0,0,1));
924
940
 
925
941
  /** Color - Purple #8000ff
926
942
  * @type {Color}
927
943
  * @memberof Utilities */
928
- const PURPLE = rgb(.5,0,1);
944
+ const PURPLE = protectEngineConstant(rgb(.5,0,1));
929
945
 
930
946
  /** Color - Magenta #ff00ff
931
947
  * @type {Color}
932
948
  * @memberof Utilities */
933
- const MAGENTA = rgb(1,0,1);
949
+ const MAGENTA = protectEngineConstant(rgb(1,0,1));
934
950
 
935
951
  ///////////////////////////////////////////////////////////////////////////////
936
952
 
937
953
  /**
938
954
  * Timer object tracks how long has passed since it was set
955
+ * @memberof Engine
939
956
  * @example
940
957
  * let a = new Timer; // creates a timer that is not set
941
958
  * a.set(3); // sets the timer to 3 seconds
@@ -997,4 +1014,34 @@ class Timer
997
1014
  /** Get how long since elapsed, returns 0 if not set (returns negative if currently active)
998
1015
  * @return {number} */
999
1016
  valueOf() { return this.get(); }
1017
+ }
1018
+
1019
+ ///////////////////////////////////////////////////////////////////////////////
1020
+ // Helper functions used by the engine
1021
+
1022
+ // make color constants immutable with debug assertions
1023
+ function protectEngineConstant(obj)
1024
+ {
1025
+ if (debug)
1026
+ {
1027
+ // get properties and store original values
1028
+ const props = Object.keys(obj), values = {};
1029
+ props.forEach(prop => values[prop] = obj[prop]);
1030
+
1031
+ // replace with getters/setters that assert
1032
+ props.forEach(prop =>
1033
+ {
1034
+ Object.defineProperty(obj, prop, {
1035
+ get: () => values[prop],
1036
+ set: (value) =>
1037
+ {
1038
+ ASSERT(false, `Cannot modify engine constant. Attempted to set constant (${obj}) property '${prop}' to '${value}'.`);
1039
+ },
1040
+ enumerable: true
1041
+ });
1042
+ });
1043
+ }
1044
+
1045
+ // freeze the object to prevent adding new properties
1046
+ return Object.freeze(obj);
1000
1047
  }