littlejsengine 1.9.11 → 1.10.4

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 (76) hide show
  1. package/README.md +9 -13
  2. package/dist/littlejs.d.ts +129 -10
  3. package/dist/littlejs.esm.js +220 -75
  4. package/dist/littlejs.esm.min.js +1 -1
  5. package/dist/littlejs.js +188 -73
  6. package/dist/littlejs.min.js +1 -1
  7. package/dist/littlejs.release.js +187 -72
  8. package/examples/box2d/game.js +4 -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/README.md +9 -7
  13. package/examples/breakoutTutorial/game.js +6 -6
  14. package/examples/breakoutTutorial/index.html +4 -3
  15. package/examples/electron/game.js +1 -1
  16. package/examples/electron/index.html +4 -3
  17. package/examples/empty/game.js +1 -1
  18. package/examples/empty/index.html +2 -1
  19. package/examples/htmlMenu/game.js +67 -0
  20. package/examples/htmlMenu/index.html +56 -0
  21. package/examples/htmlMenu/tiles.png +0 -0
  22. package/examples/index.html +33 -0
  23. package/examples/js13k/build/index.html +1 -0
  24. package/examples/js13k/build/index.js +1 -0
  25. package/examples/js13k/build/tiles.png +0 -0
  26. package/examples/js13k/game - Copy.zip +0 -0
  27. package/examples/js13k/game.js +1 -1
  28. package/examples/js13k/game.zip +0 -0
  29. package/examples/js13k/index.html +17 -14
  30. package/examples/module/game.js +1 -1
  31. package/examples/module/index.html +3 -2
  32. package/examples/particles/index.html +4 -3
  33. package/examples/platformer/game.js +7 -5
  34. package/examples/platformer/gameCharacter.js +12 -11
  35. package/examples/platformer/gameEffects.js +3 -4
  36. package/examples/platformer/gameLevel.js +18 -34
  37. package/examples/platformer/gameObjects.js +9 -15
  38. package/examples/platformer/index.html +10 -9
  39. package/examples/puzzle/game.js +1 -1
  40. package/examples/puzzle/index.html +4 -3
  41. package/examples/starter/build/index.html +2 -0
  42. package/examples/starter/build/index.js +1 -0
  43. package/examples/starter/build/tiles.png +0 -0
  44. package/examples/starter/game.js +4 -4
  45. package/examples/starter/game.zip +0 -0
  46. package/examples/starter/index.html +15 -14
  47. package/examples/stress/index.html +3 -2
  48. package/examples/typescript/build/build/littlejs.esm.js +4327 -0
  49. package/examples/typescript/build/dist/littlejs.esm.js +4425 -0
  50. package/examples/typescript/build/examples/typescript/build.js +24 -0
  51. package/examples/typescript/build/examples/typescript/game.js +100 -0
  52. package/examples/typescript/build/examples/typescript/test/build/littlejs.esm.js +3934 -0
  53. package/examples/typescript/build/examples/typescript/test/examples/typescript/build.js +86 -0
  54. package/examples/typescript/build/examples/typescript/test/examples/typescript/game.js +92 -0
  55. package/examples/typescript/game.js +1 -1
  56. package/examples/typescript/game.ts +1 -1
  57. package/examples/typescript/index.html +3 -2
  58. package/examples/uiSystem/game.js +134 -0
  59. package/examples/uiSystem/index.html +25 -0
  60. package/examples/uiSystem/tiles.png +0 -0
  61. package/jsconfig.json +11 -0
  62. package/package.json +1 -1
  63. package/plugins/box2d.js +2 -1
  64. package/plugins/postProcess.js +14 -8
  65. package/plugins/uiSystem.js +243 -0
  66. package/src/engine.js +42 -22
  67. package/src/engineAudio.js +10 -13
  68. package/src/engineDebug.js +1 -1
  69. package/src/engineDraw.js +102 -27
  70. package/src/engineExport.js +32 -2
  71. package/src/engineInput.js +1 -3
  72. package/src/engineObject.js +1 -0
  73. package/src/engineSettings.js +1 -1
  74. package/src/engineTileLayer.js +2 -1
  75. package/src/engineUtilities.js +2 -1
  76. package/src/engineWebGL.js +26 -4
package/README.md CHANGED
@@ -24,7 +24,7 @@ The code is very clean and well documented with some fun examples to get you sta
24
24
 
25
25
  <div align="center">
26
26
 
27
- ## [Demo](https://killedbyapixel.github.io/LittleJS/examples/starter/) | [Docs](https://killedbyapixel.github.io/LittleJS/docs) | [Trailer](https://youtu.be/chuBzGjv7Ms) | [Discord](https://discord.gg/zb7hcGkyZe) | [Tutorial](https://github.com/KilledByAPixel/LittleJS/blob/main/examples/breakoutTutorial/README.md) | [Quick Reference](https://github.com/KilledByAPixel/LittleJS/blob/main/reference.md)
27
+ ## [Demo](https://killedbyapixel.github.io/LittleJS/examples/starter/) | [Docs](https://killedbyapixel.github.io/LittleJS/docs) | [Trailer](https://youtu.be/chuBzGjv7Ms) | [Discord](https://discord.gg/zb7hcGkyZe) | [Tutorial](https://github.com/KilledByAPixel/LittleJS/blob/main/examples/breakoutTutorial/README.md) | [FAQ](https://github.com/KilledByAPixel/LittleJS/blob/main/FAQ.md)
28
28
 
29
29
  </div>
30
30
 
@@ -79,13 +79,15 @@ 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
- Learn how to make a simple game from scratch with [The Breakout Tutorial.](https://github.com/KilledByAPixel/LittleJS/tree/main/examples/breakoutTutorial)
82
+ *You will need to run a local web server to run LittleJS games!*
83
83
 
84
- [LittleJS Engine Quick Reference Sheet](https://github.com/KilledByAPixel/LittleJS/blob/main/reference.md) - This cheat sheet can help you get started.
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
85
 
86
- [Watch this GitNation talk](https://youtu.be/_dXKU0WgAj8?si=ZDXLYAFDWp54hrGT) to hear more about LittleJS works and get some tips on how to use it.
87
-
88
- Join our vibrant community on [Discord](https://discord.gg/zb7hcGkyZe) to get help, share your projects, and collaborate with others!
86
+ - Learn how to make a simple game from scratch with [The Breakout Tutorial.](https://github.com/KilledByAPixel/LittleJS/tree/main/examples/breakoutTutorial)
87
+ - [LittleJS Engine Quick Reference Sheet](https://github.com/KilledByAPixel/LittleJS/blob/main/reference.md) - This cheat sheet can help you get started.
88
+ - [Watch this GitNation talk](https://youtu.be/_dXKU0WgAj8?si=ZDXLYAFDWp54hrGT) to hear more about LittleJS works and get some tips on how to use it.
89
+ - Join our vibrant community on [Discord](https://discord.gg/zb7hcGkyZe) to get help, share your projects, and collaborate with others!
90
+ - [Check out The Little JS FAQ for more help getting started](https://github.com/KilledByAPixel/LittleJS/blob/main/FAQ.md), and let us know if there's anything you'd like to see added.
89
91
 
90
92
  ## Examples
91
93
 
@@ -98,6 +100,7 @@ These demos are for both learning and using as starter projects to create your o
98
100
  - [Box2D Demo](https://killedbyapixel.github.io/LittleJS/examples/box2d/) - Box2D plugin demonstration and testbed
99
101
  - [Stress Test](https://killedbyapixel.github.io/LittleJS/examples/stress/) - Max sprite/object test and music system demo
100
102
  - [Particle System Designer](https://killedbyapixel.github.io/LittleJS/examples/particles/) - Particle system editor and visualizer
103
+ - [All Examples](https://killedbyapixel.github.io/LittleJS/examples/) - Links to all the example projects on one page
101
104
 
102
105
  ## Builds
103
106
 
@@ -185,13 +188,6 @@ class MyObject extends EngineObject
185
188
 
186
189
  Debug builds of LittleJS have a special menu that can be opened by pressing the Esc key.
187
190
 
188
- - Esc: Debug Overlay
189
- - 1: Debug Physics
190
- - 2: Debug Particles
191
- - 3: Debug Gamepads
192
- - 4: Debug Raycasts
193
- - 5: Save Screenshot
194
-
195
191
  ## Games Made With LittleJS
196
192
 
197
193
  Here are a few of the amazing games people made using LittleJS...
@@ -69,9 +69,10 @@ declare module "littlejsengine" {
69
69
  * @param {Function} gameUpdatePost - Called after physics and objects are updated, setup camera and prepare for render
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
- * @param {Array} [imageSources=['tiles.png']] - Image to load
72
+ * @param {Array} [imageSources=[]] - 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
@@ -1176,6 +1245,37 @@ declare module "littlejsengine" {
1176
1245
  * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context]
1177
1246
  * @memberof Draw */
1178
1247
  export function drawLine(posA: Vector2, posB: Vector2, thickness?: number, color?: Color, useWebGL?: boolean, screenSpace?: boolean, context?: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D): void;
1248
+ /** Draw colored polygon using passed in points
1249
+ * @param {Array} points - Array of Vector2 points
1250
+ * @param {Color} [color=(1,1,1,1)]
1251
+ * @param {Number} [lineWidth=0]
1252
+ * @param {Color} [lineColor=(0,0,0,1)]
1253
+ * @param {Boolean} [screenSpace=false]
1254
+ * @param {CanvasRenderingContext2D} [context=mainContext]
1255
+ * @memberof Draw */
1256
+ export function drawPoly(points: any[], color?: Color, lineWidth?: number, lineColor?: Color, screenSpace?: boolean, context?: CanvasRenderingContext2D): void;
1257
+ /** Draw colored ellipse using passed in point
1258
+ * @param {Vector2} pos
1259
+ * @param {Number} [width=1]
1260
+ * @param {Number} [height=1]
1261
+ * @param {Number} [angle=0]
1262
+ * @param {Color} [color=(1,1,1,1)]
1263
+ * @param {Number} [lineWidth=0]
1264
+ * @param {Color} [lineColor=(0,0,0,1)]
1265
+ * @param {Boolean} [screenSpace=false]
1266
+ * @param {CanvasRenderingContext2D} [context=mainContext]
1267
+ * @memberof Draw */
1268
+ export function drawEllipse(pos: Vector2, width?: number, height?: number, angle?: number, color?: Color, lineWidth?: number, lineColor?: Color, screenSpace?: boolean, context?: CanvasRenderingContext2D): void;
1269
+ /** Draw colored circle using passed in point
1270
+ * @param {Vector2} pos
1271
+ * @param {Number} [radius=1]
1272
+ * @param {Color} [color=(1,1,1,1)]
1273
+ * @param {Number} [lineWidth=0]
1274
+ * @param {Color} [lineColor=(0,0,0,1)]
1275
+ * @param {Boolean} [screenSpace=false]
1276
+ * @param {CanvasRenderingContext2D} [context=mainContext]
1277
+ * @memberof Draw */
1278
+ export function drawCircle(pos: Vector2, radius?: number, color?: Color, lineWidth?: number, lineColor?: Color, screenSpace?: boolean, context?: CanvasRenderingContext2D): void;
1179
1279
  /** Draw directly to a 2d canvas context in world space
1180
1280
  * @param {Vector2} pos
1181
1281
  * @param {Vector2} size
@@ -1203,8 +1303,9 @@ declare module "littlejsengine" {
1203
1303
  * @param {CanvasTextAlign} [textAlign]
1204
1304
  * @param {String} [font=fontDefault]
1205
1305
  * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context=overlayContext]
1306
+ * @param {Number} [maxWidth]
1206
1307
  * @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;
1308
+ 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
1309
  /** Draw text on overlay canvas in world space
1209
1310
  * Automatically splits new lines into rows
1210
1311
  * @param {String} text
@@ -1216,8 +1317,9 @@ declare module "littlejsengine" {
1216
1317
  * @param {CanvasTextAlign} [textAlign='center']
1217
1318
  * @param {String} [font=fontDefault]
1218
1319
  * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context=overlayContext]
1320
+ * @param {Number} [maxWidth]
1219
1321
  * @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;
1322
+ 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
1323
  export let engineFontImage: any;
1222
1324
  /**
1223
1325
  * Font Image Object - Draw text on a 2D canvas by using characters in an image
@@ -1332,6 +1434,23 @@ declare module "littlejsengine" {
1332
1434
  * @param {WebGLTexture} texture
1333
1435
  * @memberof WebGL */
1334
1436
  export function glSetTexture(texture: WebGLTexture): void;
1437
+ /** Set antialiasing for webgl canvas
1438
+ * @param {Boolean} [antialias]
1439
+ * @memberof WebGL */
1440
+ export function glSetAntialias(antialias?: boolean): void;
1441
+ /** Shoule webgl be setup with antialiasing, must be set before calling engineInit
1442
+ * @type {Boolean}
1443
+ * @memberof WebGL */
1444
+ export let glAntialias: boolean;
1445
+ export let glShader: any;
1446
+ export let glActiveTexture: any;
1447
+ export let glArrayBuffer: any;
1448
+ export let glGeometryBuffer: any;
1449
+ export let glPositionData: any;
1450
+ export let glColorData: any;
1451
+ export let glInstanceCount: any;
1452
+ export let glAdditive: any;
1453
+ export let glBatchAdditive: any;
1335
1454
  /**
1336
1455
  * LittleJS Input System
1337
1456
  * - Tracks keyboard down, pressed, and released