littlejsengine 1.9.11 → 1.10.2

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 (53) hide show
  1. package/README.md +4 -0
  2. package/dist/littlejs.d.ts +80 -9
  3. package/dist/littlejs.esm.js +193 -71
  4. package/dist/littlejs.esm.min.js +1 -1
  5. package/dist/littlejs.js +175 -69
  6. package/dist/littlejs.min.js +1 -1
  7. package/dist/littlejs.release.js +174 -68
  8. package/examples/box2d/game.js +1 -1
  9. package/examples/box2d/index.html +7 -6
  10. package/examples/breakout/game.js +1 -1
  11. package/examples/breakout/index.html +5 -4
  12. package/examples/breakoutTutorial/index.html +4 -3
  13. package/examples/electron/game.js +1 -1
  14. package/examples/electron/index.html +4 -3
  15. package/examples/empty/game.js +1 -1
  16. package/examples/empty/index.html +2 -1
  17. package/examples/htmlMenu/game.js +67 -0
  18. package/examples/htmlMenu/index.html +56 -0
  19. package/examples/htmlMenu/tiles.png +0 -0
  20. package/examples/index.html +33 -0
  21. package/examples/js13k/game.js +1 -1
  22. package/examples/js13k/index.html +17 -14
  23. package/examples/module/game.js +1 -1
  24. package/examples/module/index.html +3 -2
  25. package/examples/particles/index.html +4 -3
  26. package/examples/platformer/game.js +1 -1
  27. package/examples/platformer/gameLevel.js +2 -2
  28. package/examples/platformer/gameObjects.js +0 -1
  29. package/examples/platformer/index.html +10 -9
  30. package/examples/puzzle/game.js +1 -1
  31. package/examples/puzzle/index.html +4 -3
  32. package/examples/starter/game.js +4 -4
  33. package/examples/starter/index.html +15 -14
  34. package/examples/stress/index.html +3 -2
  35. package/examples/typescript/game.js +1 -1
  36. package/examples/typescript/index.html +3 -2
  37. package/examples/uiSystem/game.js +134 -0
  38. package/examples/uiSystem/index.html +25 -0
  39. package/examples/uiSystem/tiles.png +0 -0
  40. package/jsconfig.json +11 -0
  41. package/package.json +1 -1
  42. package/plugins/box2d.js +2 -1
  43. package/plugins/postProcess.js +6 -7
  44. package/plugins/uiSystem.js +243 -0
  45. package/src/engine.js +48 -20
  46. package/src/engineAudio.js +10 -13
  47. package/src/engineDebug.js +1 -1
  48. package/src/engineDraw.js +101 -27
  49. package/src/engineExport.js +18 -2
  50. package/src/engineInput.js +2 -2
  51. package/src/engineSettings.js +1 -1
  52. package/src/engineUtilities.js +1 -1
  53. package/src/engineWebGL.js +11 -4
package/README.md CHANGED
@@ -79,6 +79,10 @@ LittleJS is a small but powerful game engine with many features and no dependenc
79
79
 
80
80
  To get started download the latest LittleJS package from GitHub or install via npm: ```npm install littlejsengine```
81
81
 
82
+ *You will need to run a local web server to run LittleJS games!*
83
+
84
+ This is because web browsers just have protection from loading local files. You may see a console error like "The image element contains cross-origin data." Don't panic, it's easy to fix! If you are using [Visual Studio Code](https://code.visualstudio.com/) there is a [Live Preview Extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.live-server) that will handle this for you automatically. Another option is to setup a simple local web server like [http-server](https://www.npmjs.com/package/http-server) via npm.
85
+
82
86
  Learn how to make a simple game from scratch with [The Breakout Tutorial.](https://github.com/KilledByAPixel/LittleJS/tree/main/examples/breakoutTutorial)
83
87
 
84
88
  [LittleJS Engine Quick Reference Sheet](https://github.com/KilledByAPixel/LittleJS/blob/main/reference.md) - This cheat sheet can help you get started.
@@ -70,8 +70,9 @@ declare module "littlejsengine" {
70
70
  * @param {Function} gameRender - Called before objects are rendered, draw any background effects that appear behind objects
71
71
  * @param {Function} gameRenderPost - Called after objects are rendered, draw effects or hud that appear above all objects
72
72
  * @param {Array} [imageSources=['tiles.png']] - Image to load
73
+ * @param {HTMLElement} [rootElement] - Root element to attach to, the document body by default
73
74
  * @memberof Engine */
74
- export function engineInit(gameInit: Function, gameUpdate: Function, gameUpdatePost: Function, gameRender: Function, gameRenderPost: Function, imageSources?: any[]): void;
75
+ export function engineInit(gameInit: Function, gameUpdate: Function, gameUpdatePost: Function, gameRender: Function, gameRenderPost: Function, imageSources?: any[], rootElement?: HTMLElement): void;
75
76
  /** Update each engine object, remove destroyed objects, and update time
76
77
  * @memberof Engine */
77
78
  export function engineObjectsUpdate(): void;
@@ -85,6 +86,13 @@ declare module "littlejsengine" {
85
86
  * @param {Array} [objects=engineObjects] - List of objects to check
86
87
  * @memberof Engine */
87
88
  export function engineObjectsCallback(pos?: Vector2, size?: number | Vector2, callbackFunction?: Function, objects?: any[]): void;
89
+ /** Return a list of objects intersecting a ray
90
+ * @param {Vector2} start
91
+ * @param {Vector2} end
92
+ * @param {Array} [objects=engineObjects] - List of objects to check
93
+ * @return {Array} - List of objects hit
94
+ * @memberof Engine */
95
+ export function engineObjectsRaycast(start: Vector2, end: Vector2, objects?: any[]): any[];
88
96
  /** Add a new update function for a plugin
89
97
  * @param {Function} [updateFunction]
90
98
  * @param {Function} [renderFunction]
@@ -664,6 +672,14 @@ declare module "littlejsengine" {
664
672
  * @return {Boolean} - True if overlapping
665
673
  * @memberof Utilities */
666
674
  export function isOverlapping(posA: Vector2, sizeA: Vector2, posB: Vector2, sizeB?: Vector2): boolean;
675
+ /** Returns true if a line segment is intersecting an axis aligned box
676
+ * @param {Vector2} start - Start of raycast
677
+ * @param {Vector2} end - End of raycast
678
+ * @param {Vector2} pos - Center of box
679
+ * @param {Vector2} size - Size of box
680
+ * @return {Boolean} - True if intersecting
681
+ * @memberof Utilities */
682
+ export function isIntersecting(start: Vector2, end: Vector2, pos: Vector2, size: Vector2): boolean;
667
683
  /** Returns an oscillating wave between 0 and amplitude with frequency of 1 Hz by default
668
684
  * @param {Number} [frequency] - Frequency of the wave in Hz
669
685
  * @param {Number} [amplitude] - Amplitude (max height) of the wave
@@ -1025,17 +1041,69 @@ declare module "littlejsengine" {
1025
1041
  * @memberof Utilities
1026
1042
  */
1027
1043
  export function hsl(h?: number, s?: number, l?: number, a?: number): Color;
1044
+ /**
1045
+ * Check if object is a valid Color
1046
+ * @param {any} c
1047
+ * @return {Boolean}
1048
+ * @memberof Utilities
1049
+ */
1050
+ export function isColor(c: any): boolean;
1051
+ /** Color - White
1052
+ * @type {Color}
1053
+ * @memberof Utilities */
1054
+ export const WHITE: Color;
1055
+ /** Color - Black
1056
+ * @type {Color}
1057
+ * @memberof Utilities */
1058
+ export const BLACK: Color;
1059
+ /** Color - Gray
1060
+ * @type {Color}
1061
+ * @memberof Utilities */
1062
+ export const GRAY: Color;
1063
+ /** Color - Red
1064
+ * @type {Color}
1065
+ * @memberof Utilities */
1066
+ export const RED: Color;
1067
+ /** Color - Orange
1068
+ * @type {Color}
1069
+ * @memberof Utilities */
1070
+ export const ORANGE: Color;
1071
+ /** Color - Yellow
1072
+ * @type {Color}
1073
+ * @memberof Utilities */
1074
+ export const YELLOW: Color;
1075
+ /** Color - Green
1076
+ * @type {Color}
1077
+ * @memberof Utilities */
1078
+ export const GREEN: Color;
1079
+ /** Color - Cyan
1080
+ * @type {Color}
1081
+ * @memberof Utilities */
1082
+ export const CYAN: Color;
1083
+ /** Color - Blue
1084
+ * @type {Color}
1085
+ * @memberof Utilities */
1086
+ export const BLUE: Color;
1087
+ /** Color - Purple
1088
+ * @type {Color}
1089
+ * @memberof Utilities */
1090
+ export const PURPLE: Color;
1091
+ /** Color - Magenta
1092
+ * @type {Color}
1093
+ * @memberof Utilities */
1094
+ export const MAGENTA: Color;
1028
1095
  /** Array containing texture info for batch rendering system
1029
1096
  * @type {Array}
1030
1097
  * @memberof Draw */
1031
1098
  export let textureInfos: any[];
1032
1099
  /**
1033
- * Create a tile info object
1100
+ * Create a tile info object using a grid based system
1034
1101
  * - This can take vecs or floats for easier use and conversion
1035
1102
  * - If an index is passed in, the tile size and index will determine the position
1036
- * @param {(Number|Vector2)} [pos=(0,0)] - Top left corner of tile in pixels or index
1103
+ * @param {(Number|Vector2)} [pos=0] - Index of tile in sheet
1037
1104
  * @param {(Number|Vector2)} [size=tileSizeDefault] - Size of tile in pixels
1038
1105
  * @param {Number} [textureIndex] - Texture index to use
1106
+ * @param {Number} [padding] - How many pixels padding around tiles
1039
1107
  * @return {TileInfo}
1040
1108
  * @example
1041
1109
  * tile(2) // a tile at index 2 using the default tile size of 16
@@ -1044,7 +1112,7 @@ declare module "littlejsengine" {
1044
1112
  * tile(vec2(4,8), vec2(30,10)) // a tile at pixel location (4,8) with a size of (30,10)
1045
1113
  * @memberof Draw
1046
1114
  */
1047
- export function tile(pos?: (number | Vector2), size?: (number | Vector2), textureIndex?: number): TileInfo;
1115
+ export function tile(pos?: (number | Vector2), size?: (number | Vector2), textureIndex?: number, padding?: number): TileInfo;
1048
1116
  /**
1049
1117
  * Tile Info - Stores info about how to draw a tile
1050
1118
  */
@@ -1053,14 +1121,17 @@ declare module "littlejsengine" {
1053
1121
  * @param {Vector2} [pos=(0,0)] - Top left corner of tile in pixels
1054
1122
  * @param {Vector2} [size=tileSizeDefault] - Size of tile in pixels
1055
1123
  * @param {Number} [textureIndex] - Texture index to use
1124
+ * @param {Number} [padding] - How many pixels padding around tiles
1056
1125
  */
1057
- constructor(pos?: Vector2, size?: Vector2, textureIndex?: number);
1126
+ constructor(pos?: Vector2, size?: Vector2, textureIndex?: number, padding?: number);
1058
1127
  /** @property {Vector2} - Top left corner of tile in pixels */
1059
1128
  pos: Vector2;
1060
1129
  /** @property {Vector2} - Size of tile in pixels */
1061
1130
  size: Vector2;
1062
1131
  /** @property {Number} - Texture index to use */
1063
1132
  textureIndex: number;
1133
+ /** @property {Number} - How many pixels padding around tiles */
1134
+ padding: number;
1064
1135
  /** Returns a copy of this tile offset by a vector
1065
1136
  * @param {Vector2} offset - Offset to apply in pixels
1066
1137
  * @return {TileInfo}
@@ -1089,8 +1160,6 @@ declare module "littlejsengine" {
1089
1160
  size: Vector2;
1090
1161
  /** @property {WebGLTexture} - webgl texture */
1091
1162
  glTexture: WebGLTexture;
1092
- /** @property {Vector2} - size to adjust tile to fix bleeding */
1093
- fixBleedSize: Vector2;
1094
1163
  }
1095
1164
  /**
1096
1165
  * LittleJS Drawing System
@@ -1203,8 +1272,9 @@ declare module "littlejsengine" {
1203
1272
  * @param {CanvasTextAlign} [textAlign]
1204
1273
  * @param {String} [font=fontDefault]
1205
1274
  * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context=overlayContext]
1275
+ * @param {Number} [maxWidth]
1206
1276
  * @memberof Draw */
1207
- export function drawTextScreen(text: string, pos: Vector2, size?: number, color?: Color, lineWidth?: number, lineColor?: Color, textAlign?: CanvasTextAlign, font?: string, context?: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D): void;
1277
+ export function drawTextScreen(text: string, pos: Vector2, size?: number, color?: Color, lineWidth?: number, lineColor?: Color, textAlign?: CanvasTextAlign, font?: string, context?: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, maxWidth?: number): void;
1208
1278
  /** Draw text on overlay canvas in world space
1209
1279
  * Automatically splits new lines into rows
1210
1280
  * @param {String} text
@@ -1216,8 +1286,9 @@ declare module "littlejsengine" {
1216
1286
  * @param {CanvasTextAlign} [textAlign='center']
1217
1287
  * @param {String} [font=fontDefault]
1218
1288
  * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context=overlayContext]
1289
+ * @param {Number} [maxWidth]
1219
1290
  * @memberof Draw */
1220
- export function drawText(text: string, pos: Vector2, size?: number, color?: Color, lineWidth?: number, lineColor?: Color, textAlign?: CanvasTextAlign, font?: string, context?: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D): void;
1291
+ export function drawText(text: string, pos: Vector2, size?: number, color?: Color, lineWidth?: number, lineColor?: Color, textAlign?: CanvasTextAlign, font?: string, context?: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D, maxWidth?: number): void;
1221
1292
  export let engineFontImage: any;
1222
1293
  /**
1223
1294
  * Font Image Object - Draw text on a 2D canvas by using characters in an image