littlejsengine 1.18.29 → 1.20.0

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 (51) hide show
  1. package/FAQ.md +841 -815
  2. package/README.md +211 -199
  3. package/dist/littlejs.d.ts +4383 -812
  4. package/dist/littlejs.esm.js +31848 -16680
  5. package/dist/littlejs.esm.min.js +14 -1
  6. package/dist/littlejs.js +31230 -16166
  7. package/dist/littlejs.min.js +13 -1
  8. package/dist/littlejs.release.js +27419 -15457
  9. package/package.json +10 -5
  10. package/plugins/audioEffects.js +375 -0
  11. package/plugins/box2d.js +2516 -2073
  12. package/plugins/drawUtilities.js +206 -31
  13. package/plugins/gltf.js +740 -0
  14. package/plugins/jsconfig.json +19 -0
  15. package/plugins/lightSystem.js +719 -330
  16. package/plugins/math3d.js +911 -0
  17. package/plugins/medalSystem.js +118 -72
  18. package/plugins/newgrounds.js +400 -199
  19. package/plugins/pathFinder.js +208 -175
  20. package/plugins/pluginExport.js +196 -115
  21. package/plugins/postProcess.js +284 -166
  22. package/plugins/render3d.js +4072 -0
  23. package/plugins/render3dDebug.js +109 -0
  24. package/plugins/render3dExtras.js +1754 -0
  25. package/plugins/render3dVoxels.js +502 -0
  26. package/plugins/textureSheet.js +86 -48
  27. package/plugins/threejs.js +183 -158
  28. package/plugins/tweakables.js +411 -0
  29. package/plugins/tweenSystem.js +646 -526
  30. package/plugins/uiSystem.js +2024 -1687
  31. package/plugins/zzfxm.js +8 -15
  32. package/src/engine.js +345 -194
  33. package/src/engineAudio.js +1022 -777
  34. package/src/engineBuild.mjs +289 -253
  35. package/src/engineDebug.js +912 -735
  36. package/src/engineDraw.js +1774 -1510
  37. package/src/engineEditor.js +2431 -0
  38. package/src/engineExport.js +419 -396
  39. package/src/engineInput.js +1462 -1293
  40. package/src/engineLogo.js +4 -3
  41. package/src/engineMath.js +1274 -1183
  42. package/src/engineObject.js +674 -551
  43. package/src/engineParticles.js +583 -532
  44. package/src/engineRelease.js +67 -45
  45. package/src/engineSettings.js +808 -726
  46. package/src/engineTileLayer.js +995 -741
  47. package/src/engineUtilities.js +32 -13
  48. package/src/engineWebGL.js +1162 -941
  49. package/src/jsconfig.json +17 -13
  50. package/plugins/box2d.wasm.js +0 -630
  51. package/plugins/box2d.wasm.wasm +0 -0
@@ -1,726 +1,808 @@
1
- /**
2
- * LittleJS Engine Settings
3
- * - All settings for the engine are here
4
- * @namespace Settings
5
- */
6
-
7
- 'use strict';
8
-
9
- ///////////////////////////////////////////////////////////////////////////////
10
- // Camera settings
11
-
12
- /** Position of camera in world space
13
- * @type {Vector2}
14
- * @default Vector2()
15
- * @memberof Settings */
16
- let cameraPos = vec2();
17
-
18
- /** Rotation angle of camera in world space
19
- * @type {number}
20
- * @default
21
- * @memberof Settings */
22
- let cameraAngle = 0;
23
-
24
- /** Scale of camera in world space
25
- * @type {number}
26
- * @default
27
- * @memberof Settings */
28
- let cameraScale = 32;
29
-
30
- ///////////////////////////////////////////////////////////////////////////////
31
- // Time settings
32
-
33
- /** Scale applied to engine time, can be used for slow motion or fast forward
34
- * - 1 is normal speed, 2 is double speed, 0.5 is half speed
35
- * - 0 freezes the simulation without setting the paused flag
36
- * - Should be >= 0; stacks multiplicatively with the debug +/- shortcut
37
- * @type {number}
38
- * @default
39
- * @memberof Settings */
40
- let timeScale = 1;
41
-
42
- ///////////////////////////////////////////////////////////////////////////////
43
- // Display settings
44
-
45
- /** Enable applying color to tiles when using canvas2d
46
- * - This is slower but should be the same as WebGL rendering
47
- * @type {boolean}
48
- * @default
49
- * @memberof Settings */
50
- let canvasColorTiles = true;
51
-
52
- /** Color to clear the canvas to before render, does not clear if alpha is 0
53
- * @type {Color}
54
- * @memberof Settings */
55
- let canvasClearColor = CLEAR_BLACK;
56
-
57
- /** The max size of the canvas, centered if window is larger
58
- * @type {Vector2}
59
- * @default Vector2(1920,1080)
60
- * @memberof Settings */
61
- let canvasMaxSize = vec2(1920, 1080);
62
-
63
- /** Minimum aspect ratio of the canvas (width/height), unused if 0
64
- * Can be used with canvasMaxAspect to limit aspect ratio
65
- * @type {number}
66
- * @default
67
- * @memberof Settings */
68
- let canvasMinAspect = 0;
69
-
70
- /** Maximum aspect ratio of the canvas (width/height), unused if 0
71
- * Can be used with canvasMinAspect to limit aspect ratio
72
- * @type {number}
73
- * @default
74
- * @memberof Settings */
75
- let canvasMaxAspect = 0;
76
-
77
- /** Fixed size of the canvas, if enabled canvas size never changes
78
- * - you may also need to set mainCanvasSize if using screen space coords in startup
79
- * @type {Vector2}
80
- * @default Vector2()
81
- * @memberof Settings */
82
- let canvasFixedSize = vec2();
83
-
84
- /** Use nearest canvas scaling for more pixelated look
85
- * - If enabled sets css image-rendering:pixelated
86
- * @type {boolean}
87
- * @default
88
- * @memberof Settings */
89
- let canvasPixelated = false;
90
-
91
- /** Disables texture filtering for crisper pixel art
92
- * - Leave true for pixel art so sprites stay sharp when scaled (uses NEAREST filtering)
93
- * - Set false for smooth/high-resolution art to enable bilinear filtering and mipmaps
94
- * @type {boolean}
95
- * @default
96
- * @memberof Settings */
97
- let tilesPixelated = true;
98
-
99
- /** Scale factor applied to the canvas backing store for native-resolution rendering.
100
- * Pass 1 for no scaling, a number for an explicit ratio, or undefined to track devicePixelRatio each frame.
101
- * @type {number|undefined}
102
- * @default
103
- * @memberof Settings */
104
- let canvasPixelRatio = 1;
105
-
106
- /** Default font used for text rendering
107
- * @type {string}
108
- * @default
109
- * @memberof Settings */
110
- let fontDefault = 'arial';
111
-
112
- /** Enable to show the LittleJS splash screen on startup
113
- * @type {boolean}
114
- * @default
115
- * @memberof Settings */
116
- let showSplashScreen = false;
117
-
118
- /** Disables all rendering, audio, and input for servers
119
- * @type {boolean}
120
- * @default
121
- * @memberof Settings */
122
- let headlessMode = false;
123
-
124
- /** Disables the automatic requestAnimationFrame loop so the engine only
125
- * advances when engineStep is called, for tests and frame-stepping tools
126
- * @type {boolean}
127
- * @default
128
- * @memberof Settings */
129
- let engineManualStep = false;
130
-
131
- ///////////////////////////////////////////////////////////////////////////////
132
- // WebGL settings
133
-
134
- /** Enable WebGL accelerated rendering
135
- * @type {boolean}
136
- * @default
137
- * @memberof Settings */
138
- let glEnable = true;
139
-
140
- /** How many sided poly to use when drawing circles and ellipses with WebGL
141
- * @type {number}
142
- * @default
143
- * @memberof Settings */
144
- let glCircleSides = 32;
145
-
146
- ///////////////////////////////////////////////////////////////////////////////
147
- // Tile sheet settings
148
-
149
- /** Default size of tiles in pixels
150
- * @type {Vector2}
151
- * @default Vector2(16,16)
152
- * @memberof Settings */
153
- let tileDefaultSize = vec2(16);
154
-
155
- /** Default padding pixels around tiles
156
- * @type {number}
157
- * @default
158
- * @memberof Settings */
159
- let tileDefaultPadding = 0;
160
-
161
- /** Default amount of pixels smaller to draw tiles to prevent neighbor bleeding
162
- * @type {number}
163
- * @default
164
- * @memberof Settings */
165
- let tileDefaultBleed = 0;
166
-
167
- ///////////////////////////////////////////////////////////////////////////////
168
- // Object settings
169
-
170
- /** Enable physics solver for collisions between objects
171
- * @type {boolean}
172
- * @default
173
- * @memberof Settings */
174
- let enablePhysicsSolver = true;
175
-
176
- /** Default object mass for collision calculations (how heavy objects are)
177
- * @type {number}
178
- * @default
179
- * @memberof Settings */
180
- let objectDefaultMass = 1;
181
-
182
- /** How much to slow velocity by each frame (0-1)
183
- * @type {number}
184
- * @default
185
- * @memberof Settings */
186
- let objectDefaultDamping = 1;
187
-
188
- /** How much to slow angular velocity each frame (0-1)
189
- * @type {number}
190
- * @default
191
- * @memberof Settings */
192
- let objectDefaultAngleDamping = 1;
193
-
194
- /** How much to bounce when a collision occurs (0-1)
195
- * @type {number}
196
- * @default
197
- * @memberof Settings */
198
- let objectDefaultRestitution = 0;
199
-
200
- /** How much to slow when touching (0-1)
201
- * @type {number}
202
- * @default
203
- * @memberof Settings */
204
- let objectDefaultFriction = .8;
205
-
206
- /** Clamp max speed to avoid fast objects missing collisions
207
- * @type {number}
208
- * @default
209
- * @memberof Settings */
210
- let objectMaxSpeed = 1;
211
-
212
- /** How much gravity to apply to objects, negative Y is down
213
- * @type {Vector2}
214
- * @default
215
- * @memberof Settings */
216
- let gravity = vec2();
217
-
218
- /** Scales emit rate of particles, useful for low graphics mode (0 disables particle emitters)
219
- * @type {number}
220
- * @default
221
- * @memberof Settings */
222
- let particleEmitRateScale = 1;
223
-
224
- ///////////////////////////////////////////////////////////////////////////////
225
- // Input settings
226
-
227
- /** Should gamepads be allowed
228
- * @type {boolean}
229
- * @default
230
- * @memberof Settings */
231
- let gamepadsEnable = true;
232
-
233
- /** If true, the dpad input is also routed to the left analog stick (for better accessibility)
234
- * @type {boolean}
235
- * @default
236
- * @memberof Settings */
237
- let gamepadDirectionEmulateStick = true;
238
-
239
- /** If true, axes that do not rest near center are ignored on gamepads without
240
- * standard mapping. Steering wheels and flight sticks report pedal and throttle
241
- * axes that rest at full deflection, which otherwise reads as a stick held down.
242
- * @type {boolean}
243
- * @default
244
- * @memberof Settings */
245
- let gamepadAxisFilterEnable = true;
246
-
247
- /** If true the WASD keys are also routed to the direction keys (for better accessibility)
248
- * @type {boolean}
249
- * @default
250
- * @memberof Settings */
251
- let inputWASDEmulateDirection = true;
252
-
253
- /** True if touch input is enabled for mobile devices
254
- * - Touch events will be routed to mouse events
255
- * @type {boolean}
256
- * @default
257
- * @memberof Settings */
258
- let touchInputEnable = true;
259
-
260
- /** True if touch gamepad should appear on mobile devices
261
- * - Supports left analog stick, 4 face buttons and start button (button 9)
262
- * - setTouchGamepadButtonCount(1) to use face buttons as right analog stick
263
- * - Analog stick buttons 10 and 11 are also activated when virtual sticks are touched
264
- * - Rendered as a full-viewport HTML/SVG overlay, so controls may sit outside the game canvas
265
- * @type {boolean}
266
- * @default
267
- * @memberof Settings */
268
- let touchGamepadEnable = false;
269
-
270
- /** True if touches outside the gamepad controls should still drive mouse/touch input
271
- * - When false (the default), enabling the touch gamepad suppresses touch-to-mouse input entirely
272
- * - Set true to also pass touches outside the controls through to the game as mouse/touch input
273
- * - Touches on the gamepad controls never drive the mouse regardless of this setting
274
- * @type {boolean}
275
- * @default
276
- * @memberof Settings */
277
- let touchGamepadPassthrough = false;
278
-
279
- /** Size of center button if touch gamepad should have start button in the center
280
- * - Prevents activating when pressed near virtual stick or face buttons
281
- * - When the game is paused, any touch will press the button
282
- * - Measured in viewport CSS pixels
283
- * @type {number}
284
- * @default
285
- * @memberof Settings */
286
- let touchGamepadCenterButtonSize = 0;
287
-
288
- /** Number of buttons on the right side of the touch gamepad (0-4), using gamepad buttons 0-3
289
- * - A count of 1 is a single large button (the size of a stick)
290
- * - Ignored when touchGamepadRightStick is set (the right side is a stick instead)
291
- * @type {number}
292
- * @default
293
- * @memberof Settings */
294
- let touchGamepadButtonCount = 4;
295
-
296
- /** True if the touch gamepad should have a left analog stick (or dpad)
297
- * - When false, the left side is face buttons (touchGamepadLeftButtonCount) or nothing
298
- * @type {boolean}
299
- * @default
300
- * @memberof Settings */
301
- let touchGamepadLeftStick = true;
302
-
303
- /** Number of buttons on the left side of the touch gamepad (0-4), using gamepad buttons 4-7
304
- * - Only used when touchGamepadLeftStick is false (otherwise the left side is a stick)
305
- * - A count of 1 is a single large button (the size of a stick)
306
- * @type {number}
307
- * @default
308
- * @memberof Settings */
309
- let touchGamepadLeftButtonCount = 0;
310
-
311
- /** True if the touch gamepad right side should be an analog stick (or dpad) instead of face buttons
312
- * - When set, touchGamepadButtonCount is ignored and the right side is a stick
313
- * - Uses an analog stick when touchGamepadAnalog is true, otherwise an 8 way dpad
314
- * @type {boolean}
315
- * @default
316
- * @memberof Settings */
317
- let touchGamepadRightStick = false;
318
-
319
- /** True if touch gamepad should be analog stick or false to use if 8 way dpad
320
- * @type {boolean}
321
- * @default
322
- * @memberof Settings */
323
- let touchGamepadAnalog = true;
324
-
325
- /** True if touch gamepad directional controls should float to where you press
326
- * - Only affects analog sticks and dpads, not face buttons
327
- * - Directional controls re-anchor to where you press within the bottom ~60% of their screen half; the top ~40% passes through to the game
328
- * - The right side floats only when it acts as the right analog stick (touchGamepadRightStick is set)
329
- * - A center button (touchGamepadCenterButtonSize) still works since it ignores touches near the sticks
330
- * @type {boolean}
331
- * @default
332
- * @memberof Settings */
333
- let touchGamepadFloating = false;
334
-
335
- /** Size of virtual gamepad for touch devices in viewport CSS pixels
336
- * @type {number}
337
- * @default
338
- * @memberof Settings */
339
- let touchGamepadSize = 100;
340
-
341
- /** Transparency of touch gamepad overlay
342
- * @type {number}
343
- * @default
344
- * @memberof Settings */
345
- let touchGamepadAlpha = .3;
346
-
347
- /** How long to display the touch gamepad on screen in seconds, set to 0 to always display
348
- * @type {number}
349
- * @default
350
- * @memberof Settings */
351
- let touchGamepadDisplayTime = 3;
352
-
353
- /** Duration in ms to vibrate when a touch gamepad face button or start button is pressed
354
- * - Set to 0 to disable, also requires vibrateEnable and hardware support (ignored on iOS)
355
- * @type {number}
356
- * @default
357
- * @memberof Settings */
358
- let touchGamepadVibration = 0;
359
-
360
- /** Allow vibration hardware if it exists
361
- * @type {boolean}
362
- * @default
363
- * @memberof Settings */
364
- let vibrateEnable = true;
365
-
366
- ///////////////////////////////////////////////////////////////////////////////
367
- // Audio settings
368
-
369
- /** All audio code can be disabled and removed from build
370
- * @type {boolean}
371
- * @default
372
- * @memberof Settings */
373
- let soundEnable = true;
374
-
375
- /** Volume scale to apply to all sound, music and speech
376
- * Use setSoundVolume to also update the audio master gain immediately
377
- * @type {number}
378
- * @default
379
- * @memberof Settings */
380
- let soundVolume = .3;
381
-
382
- /** Default range where sound no longer plays
383
- * @type {number}
384
- * @default
385
- * @memberof Settings */
386
- let soundDefaultRange = 40;
387
-
388
- /** Default range percent to start tapering off sound (0-1)
389
- * @type {number}
390
- * @default
391
- * @memberof Settings */
392
- let soundDefaultTaper = .7;
393
-
394
- ///////////////////////////////////////////////////////////////////////////////
395
- // Setters for global variables
396
-
397
- /** Set position of camera in world space
398
- * @param {Vector2} pos
399
- * @memberof Settings */
400
- function setCameraPos(pos) { cameraPos = pos.copy(); }
401
-
402
- /** Set angle of camera in world space
403
- * @param {number} angle
404
- * @memberof Settings */
405
- function setCameraAngle(angle) { cameraAngle = angle; }
406
-
407
- /** Set scale of camera in world space
408
- * @param {number} scale
409
- * @memberof Settings */
410
- function setCameraScale(scale) { cameraScale = scale; }
411
-
412
- /** Set scale applied to engine time
413
- * @param {number} scale
414
- * @memberof Settings */
415
- function setTimeScale(scale) { timeScale = scale; }
416
-
417
- /** Set if tiles should be colorized when using canvas2d
418
- * This can be slower but results should look nearly identical to WebGL rendering
419
- * It can be enabled/disabled at any time
420
- * Optimized for performance, and will use faster method if color is white or untextured
421
- * @param {boolean} colorTiles
422
- * @memberof Settings */
423
- function setCanvasColorTiles(colorTiles) { canvasColorTiles = colorTiles; }
424
-
425
- /** Set color to clear the canvas to before render, does not clear if alpha is 0
426
- * @param {Color} color
427
- * @memberof Settings */
428
- function setCanvasClearColor(color) { canvasClearColor = color.copy(); }
429
-
430
- /** Set max size of the canvas
431
- * @param {Vector2} size
432
- * @memberof Settings */
433
- function setCanvasMaxSize(size) { canvasMaxSize = size.copy(); }
434
-
435
- /** Set minimum aspect ratio of the canvas (width/height), unused if 0
436
- * @param {number} aspect
437
- * @memberof Settings */
438
- function setCanvasMinAspect(aspect) { canvasMinAspect = aspect; }
439
-
440
- /** Set maximum aspect ratio of the canvas (width/height), unused if 0
441
- * @param {number} aspect
442
- * @memberof Settings */
443
- function setCanvasMaxAspect(aspect) { canvasMaxAspect = aspect; }
444
-
445
- /** Set fixed size of the canvas
446
- * @param {Vector2} size
447
- * @memberof Settings */
448
- function setCanvasFixedSize(size) { canvasFixedSize = size.copy(); }
449
-
450
- /** Use nearest scaling algorithm for canvas for more pixelated look
451
- * @param {boolean} pixelated
452
- * @memberof Settings */
453
- function setCanvasPixelated(pixelated)
454
- {
455
- canvasPixelated = pixelated;
456
- if (mainCanvas)
457
- mainCanvas.style.imageRendering = pixelated ? 'pixelated' : '';
458
- if (glCanvas)
459
- glCanvas.style.imageRendering = pixelated ? 'pixelated' : '';
460
- }
461
-
462
- /** Disables texture filtering for crisper pixel art
463
- * - Leave true for pixel art; set false for smooth/high-resolution art
464
- * @param {boolean} pixelated
465
- * @memberof Settings */
466
- function setTilesPixelated(pixelated) { tilesPixelated = pixelated; }
467
-
468
- /** Set the canvas pixel ratio.
469
- * Pass a number for an explicit ratio, or call with no argument to track devicePixelRatio each frame.
470
- * @param {number} [pixelRatio]
471
- * @memberof Settings */
472
- function setCanvasPixelRatio(pixelRatio) { canvasPixelRatio = pixelRatio; }
473
-
474
- /** Set default font used for text rendering
475
- * @param {string} font
476
- * @memberof Settings */
477
- function setFontDefault(font) { fontDefault = font; }
478
-
479
- /** Set if the LittleJS splash screen should be shown on startup
480
- * @param {boolean} show
481
- * @memberof Settings */
482
- function setShowSplashScreen(show) { showSplashScreen = show; }
483
-
484
- /** Set to disable rendering, audio, and input for servers
485
- * @param {boolean} headless
486
- * @memberof Settings */
487
- function setHeadlessMode(headless) { headlessMode = headless; }
488
-
489
- /** Set if the engine only advances when engineStep is called
490
- * Must be set before engineInit
491
- * @param {boolean} [enable]
492
- * @memberof Settings */
493
- function setEngineManualStep(enable=true) { engineManualStep = enable; }
494
-
495
- /** Set if WebGL rendering is enabled
496
- * @param {boolean} enable
497
- * @memberof Settings */
498
- function setGLEnable(enable)
499
- {
500
- if (enable && !glCanBeEnabled)
501
- {
502
- console.warn('Can not enable WebGL if it was disabled on start.');
503
- return;
504
- }
505
- glEnable = enable;
506
- if (glCanvas) // hide glCanvas if WebGL is disabled
507
- glCanvas.style.display = enable ? '' : 'none';
508
- }
509
-
510
- /** Set how many sided polygons to use when drawing circles and ellipses with WebGL
511
- * @param {number} sides
512
- * @memberof Settings */
513
- function setGLCircleSides(sides) { glCircleSides = sides; }
514
-
515
- /** Set default size of tiles in pixels
516
- * @param {Vector2} size
517
- * @memberof Settings */
518
- function setTileDefaultSize(size) { tileDefaultSize = size.copy(); }
519
-
520
- /** Default padding pixels around tiles
521
- * @param {number} padding
522
- * @memberof Settings */
523
- function setTileDefaultPadding(padding) { tileDefaultPadding = padding; }
524
-
525
- /** Default amount of pixels smaller to draw tiles to prevent neighbor bleeding
526
- * @param {number} bleed
527
- * @memberof Settings */
528
- function setTileDefaultBleed(bleed) { tileDefaultBleed = bleed; }
529
-
530
- /** Set if collisions between objects are enabled
531
- * @param {boolean} enable
532
- * @memberof Settings */
533
- function setEnablePhysicsSolver(enable) { enablePhysicsSolver = enable; }
534
-
535
- /** Set default object mass for collision calculations
536
- * @param {number} mass
537
- * @memberof Settings */
538
- function setObjectDefaultMass(mass) { objectDefaultMass = mass; }
539
-
540
- /** Set how much to slow velocity by each frame
541
- * @param {number} damp
542
- * @memberof Settings */
543
- function setObjectDefaultDamping(damp) { objectDefaultDamping = damp; }
544
-
545
- /** Set how much to slow angular velocity each frame
546
- * @param {number} damp
547
- * @memberof Settings */
548
- function setObjectDefaultAngleDamping(damp) { objectDefaultAngleDamping = damp; }
549
-
550
- /** Set how much to bounce when a collision occurs
551
- * @param {number} restitution
552
- * @memberof Settings */
553
- function setObjectDefaultRestitution(restitution) { objectDefaultRestitution = restitution; }
554
-
555
- /** Set how much to slow when touching
556
- * @param {number} friction
557
- * @memberof Settings */
558
- function setObjectDefaultFriction(friction) { objectDefaultFriction = friction; }
559
-
560
- /** Set max speed to avoid fast objects missing collisions
561
- * @param {number} speed
562
- * @memberof Settings */
563
- function setObjectMaxSpeed(speed) { objectMaxSpeed = speed; }
564
-
565
- /** Set how much gravity to apply to objects
566
- * @param {Vector2} newGravity
567
- * @memberof Settings */
568
- function setGravity(newGravity) { gravity = newGravity.copy(); }
569
-
570
- /** Set to scales emit rate of particles
571
- * @param {number} scale
572
- * @memberof Settings */
573
- function setParticleEmitRateScale(scale) { particleEmitRateScale = scale; }
574
-
575
- /** Set if gamepads are enabled
576
- * @param {boolean} enable
577
- * @memberof Settings */
578
- function setGamepadsEnable(enable) { gamepadsEnable = enable; }
579
-
580
- /** Set if the dpad input is also routed to the left analog stick
581
- * @param {boolean} enable
582
- * @memberof Settings */
583
- function setGamepadDirectionEmulateStick(enable) { gamepadDirectionEmulateStick = enable; }
584
-
585
- /** Set if axes that do not rest near center are ignored on non-standard gamepads
586
- * @param {boolean} enable
587
- * @memberof Settings */
588
- function setGamepadAxisFilterEnable(enable) { gamepadAxisFilterEnable = enable; }
589
-
590
- /** Set if true the WASD keys are also routed to the direction keys
591
- * @param {boolean} enable
592
- * @memberof Settings */
593
- function setInputWASDEmulateDirection(enable) { inputWASDEmulateDirection = enable; }
594
-
595
- /** Set if touch input is allowed
596
- * @param {boolean} enable
597
- * @memberof Settings */
598
- function setTouchInputEnable(enable) { touchInputEnable = enable; }
599
-
600
- /** Set if touch gamepad should appear on mobile devices
601
- * @param {boolean} enable
602
- * @memberof Settings */
603
- function setTouchGamepadEnable(enable) { touchGamepadEnable = enable; }
604
-
605
- /** Set if touches outside the gamepad controls should still drive mouse/touch input
606
- * @param {boolean} passthrough
607
- * @memberof Settings */
608
- function setTouchGamepadPassthrough(passthrough) { touchGamepadPassthrough = passthrough; }
609
-
610
- /** Set if touch gamepad should have start button in the center
611
- * - Set size to enable the center button
612
- * - When the game is paused, any touch will press the button
613
- * @param {number} size
614
- * @memberof Settings */
615
- function setTouchGamepadCenterButtonSize(size) { touchGamepadCenterButtonSize = size; }
616
-
617
- /** Set number of buttons on the right side of the touch gamepad (0-4, gamepad buttons 0-3)
618
- * @param {number} count
619
- * @memberof Settings */
620
- function setTouchGamepadButtonCount(count)
621
- {
622
- touchGamepadButtonCount = count;
623
- if (count > 0)
624
- touchGamepadRightStick = false;
625
- }
626
-
627
- /** Set if the touch gamepad should have a left analog stick (or dpad)
628
- * @param {boolean} enable
629
- * @memberof Settings */
630
- function setTouchGamepadLeftStick(enable)
631
- {
632
- touchGamepadLeftStick = enable;
633
- if (enable)
634
- touchGamepadLeftButtonCount = 0;
635
- }
636
-
637
- /** Set number of buttons on the left side of the touch gamepad (0-4, gamepad buttons 4-7)
638
- * - Only used when touchGamepadLeftStick is false
639
- * @param {number} count
640
- * @memberof Settings */
641
- function setTouchGamepadLeftButtonCount(count)
642
- {
643
- touchGamepadLeftButtonCount = count;
644
- if (count > 0)
645
- touchGamepadLeftStick = false;
646
- }
647
-
648
- /** Set if the touch gamepad right side is an analog stick (or dpad) instead of face buttons
649
- * @param {boolean} rightStick
650
- * @memberof Settings */
651
- function setTouchGamepadRightStick(rightStick)
652
- {
653
- touchGamepadRightStick = rightStick;
654
- if (rightStick)
655
- touchGamepadButtonCount = 0;
656
- }
657
-
658
- /** Set if touch gamepad should be analog stick or 8 way dpad
659
- * @param {boolean} analog
660
- * @memberof Settings */
661
- function setTouchGamepadAnalog(analog) { touchGamepadAnalog = analog; }
662
-
663
- /** Set if touch gamepad directional controls should float to where you press
664
- * @param {boolean} floating
665
- * @memberof Settings */
666
- function setTouchGamepadFloating(floating) { touchGamepadFloating = floating; }
667
-
668
- /** Set size of virtual gamepad for touch devices in pixels
669
- * @param {number} size
670
- * @memberof Settings */
671
- function setTouchGamepadSize(size) { touchGamepadSize = size; }
672
-
673
- /** Set transparency of touch gamepad overlay
674
- * @param {number} alpha
675
- * @memberof Settings */
676
- function setTouchGamepadAlpha(alpha) { touchGamepadAlpha = alpha; }
677
-
678
- /** Set how long to display the touch gamepad on screen in seconds, set to 0 to always display
679
- * @param {number} time
680
- * @memberof Settings */
681
- function setTouchGamepadDisplayTime(time) { touchGamepadDisplayTime = time; }
682
-
683
- /** Set duration in ms to vibrate when a touch gamepad face or start button is pressed (0 disables)
684
- * @param {number} ms
685
- * @memberof Settings */
686
- function setTouchGamepadVibration(ms) { touchGamepadVibration = ms; }
687
-
688
- /** Set to allow vibration hardware if it exists
689
- * @param {boolean} enable
690
- * @memberof Settings */
691
- function setVibrateEnable(enable) { vibrateEnable = enable; }
692
-
693
- /** Set to disable all audio code
694
- * @param {boolean} enable
695
- * @memberof Settings */
696
- function setSoundEnable(enable) { soundEnable = enable; }
697
-
698
- /** Set volume scale to apply to all sound, music and speech
699
- * @param {number} volume
700
- * @memberof Settings */
701
- function setSoundVolume(volume)
702
- {
703
- soundVolume = volume;
704
- if (soundEnable && !headlessMode && audioMasterGain)
705
- audioMasterGain.gain.value = volume; // update gain immediately
706
- }
707
-
708
- /** Set default range where sound no longer plays
709
- * @param {number} range
710
- * @memberof Settings */
711
- function setSoundDefaultRange(range) { soundDefaultRange = range; }
712
-
713
- /** Set default range percent to start tapering off sound
714
- * @param {number} taper
715
- * @memberof Settings */
716
- function setSoundDefaultTaper(taper) { soundDefaultTaper = taper; }
717
-
718
- /** Set if watermark with FPS should be shown
719
- * @param {boolean} show
720
- * @memberof Debug */
721
- function setDebugWatermark(show) { debugWatermark = show; }
722
-
723
- /** Set key code used to toggle debug mode, Esc by default
724
- * @param {string} key
725
- * @memberof Debug */
726
- function setDebugKey(key) { debugKey = key; }
1
+ /**
2
+ * LittleJS Engine Settings
3
+ * - All settings for the engine are here
4
+ * @namespace Settings
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ ///////////////////////////////////////////////////////////////////////////////
10
+ // Camera settings
11
+
12
+ /** Position of camera in world space
13
+ * @type {Vector2}
14
+ * @default Vector2()
15
+ * @memberof Settings */
16
+ let cameraPos = vec2();
17
+
18
+ /** Rotation angle of camera in world space
19
+ * @type {number}
20
+ * @default
21
+ * @memberof Settings */
22
+ let cameraAngle = 0;
23
+
24
+ /** Scale of camera in world space
25
+ * @type {number}
26
+ * @default
27
+ * @memberof Settings */
28
+ let cameraScale = 32;
29
+
30
+ ///////////////////////////////////////////////////////////////////////////////
31
+ // Time settings
32
+
33
+ /** Scale applied to engine time, can be used for slow motion or fast forward
34
+ * - 1 is normal speed, 2 is double speed, 0.5 is half speed
35
+ * - 0 freezes the game like a pause without setting the paused flag, gameUpdatePost and input still run
36
+ * - Should be >= 0; stacks multiplicatively with the debug +/- shortcut
37
+ * @type {number}
38
+ * @default
39
+ * @memberof Settings */
40
+ let timeScale = 1;
41
+
42
+ ///////////////////////////////////////////////////////////////////////////////
43
+ // Display settings
44
+
45
+ /** Enable applying color to tiles when using canvas2d
46
+ * - This is slower but should be the same as WebGL rendering
47
+ * @type {boolean}
48
+ * @default
49
+ * @memberof Settings */
50
+ let canvasColorTiles = true;
51
+
52
+ /** Color to clear the canvas to before render, does not clear if alpha is 0
53
+ * @type {Color}
54
+ * @memberof Settings */
55
+ let canvasClearColor = CLEAR_BLACK;
56
+
57
+ /** The max size of the canvas in css pixels, centered if window is larger
58
+ * - Not affected by canvasPixelRatio, the backing store may be larger than this
59
+ * @type {Vector2}
60
+ * @default Vector2(3840,2160)
61
+ * @memberof Settings */
62
+ let canvasMaxSize = vec2(3840, 2160);
63
+
64
+ /** Minimum aspect ratio of the canvas (width/height), unused if 0
65
+ * Can be used with canvasMaxAspect to limit aspect ratio
66
+ * @type {number}
67
+ * @default
68
+ * @memberof Settings */
69
+ let canvasMinAspect = 0;
70
+
71
+ /** Maximum aspect ratio of the canvas (width/height), unused if 0
72
+ * Can be used with canvasMinAspect to limit aspect ratio
73
+ * @type {number}
74
+ * @default
75
+ * @memberof Settings */
76
+ let canvasMaxAspect = 0;
77
+
78
+ /** Fixed size of the canvas in css pixels, if enabled canvas size never changes
79
+ * - canvasPixelRatio still applies, it only scales the backing store
80
+ * @type {Vector2}
81
+ * @default Vector2()
82
+ * @memberof Settings */
83
+ let canvasFixedSize = vec2();
84
+
85
+ /** Use nearest canvas scaling for more pixelated look
86
+ * - If enabled sets css image-rendering:pixelated
87
+ * @type {boolean}
88
+ * @default
89
+ * @memberof Settings */
90
+ let canvasPixelated = false;
91
+
92
+ /** Disables texture filtering for crisper pixel art
93
+ * - Leave true for pixel art so sprites stay sharp when scaled (uses NEAREST filtering)
94
+ * - Set false for smooth/high-resolution art to enable bilinear filtering and mipmaps; those textures upload
95
+ * premultiplied, so filtering does not pull dark from see through texels into the edges
96
+ * @type {boolean}
97
+ * @default
98
+ * @memberof Settings */
99
+ let tilesPixelated = true;
100
+
101
+ /** Scale factor applied to the canvas resolution for sharper rendering
102
+ * Pass 1 for no scaling, a number for an explicit ratio, or undefined to track devicePixelRatio each frame.
103
+ * - Only the backing store scales, so this changes sharpness and nothing else
104
+ * - mainCanvasSize, cameraScale, mousePos and screen space stay in css pixels,
105
+ * so the same code draws the same size at any ratio
106
+ * - Pixel art usually looks best left at 1 or set to whole numbers,
107
+ * a fractional ratio samples texels unevenly
108
+ * @type {number|undefined}
109
+ * @default
110
+ * @memberof Settings */
111
+ let canvasPixelRatio = 1;
112
+
113
+ /** Default font used for text rendering
114
+ * @type {string}
115
+ * @default
116
+ * @memberof Settings */
117
+ let fontDefault = 'arial';
118
+
119
+ /** Enable to show the LittleJS splash screen on startup, must be set before engineInit
120
+ * @type {boolean}
121
+ * @default
122
+ * @memberof Settings */
123
+ let showSplashScreen = false;
124
+
125
+ /** Disables all rendering, audio, and input for servers, must be set before engineInit
126
+ * @type {boolean}
127
+ * @default
128
+ * @memberof Settings */
129
+ let headlessMode = false;
130
+
131
+ /** Disables the automatic requestAnimationFrame loop so the engine only
132
+ * advances when engineStep is called, for tests and frame-stepping tools
133
+ * @type {boolean}
134
+ * @default
135
+ * @memberof Settings */
136
+ let engineManualStep = false;
137
+
138
+ ///////////////////////////////////////////////////////////////////////////////
139
+ // WebGL settings
140
+
141
+ /** Enable WebGL accelerated rendering
142
+ * @type {boolean}
143
+ * @default
144
+ * @memberof Settings */
145
+ let glEnable = true;
146
+
147
+ /** Draw in screen space by default: what the screenSpace parameter of the draw and debug functions defaults to, so a
148
+ * heads up display, or a 3D game that only draws 2D on the screen, can turn it on and leave the flag out
149
+ * - The engine says its own space when it draws, so objects, tile layers and particles stay in world space, but a
150
+ * game's own draws follow it, a render() override included: turn it on around the hud, or pass false there
151
+ * - Sizes and widths keep their defaults, which are world sized, so pass them in pixels
152
+ * - drawText has no screenSpace and stays in world space, drawTextScreen is the one for the screen
153
+ * @type {boolean}
154
+ * @default
155
+ * @memberof Settings */
156
+ let drawScreenSpace = false;
157
+
158
+ /** How many sided poly to use when drawing circles and ellipses with WebGL
159
+ * @type {number}
160
+ * @default
161
+ * @memberof Settings */
162
+ let glCircleSides = 32;
163
+
164
+ ///////////////////////////////////////////////////////////////////////////////
165
+ // Tile sheet settings
166
+
167
+ /** Default size of tiles in pixels
168
+ * @type {Vector2}
169
+ * @default Vector2(16,16)
170
+ * @memberof Settings */
171
+ let tileDefaultSize = vec2(16);
172
+
173
+ /** Default padding pixels around tiles
174
+ * @type {number}
175
+ * @default
176
+ * @memberof Settings */
177
+ let tileDefaultPadding = 0;
178
+
179
+ /** Default amount of pixels smaller to draw tiles to prevent neighbor bleeding
180
+ * @type {number}
181
+ * @default
182
+ * @memberof Settings */
183
+ let tileDefaultBleed = 0;
184
+
185
+ ///////////////////////////////////////////////////////////////////////////////
186
+ // Object settings
187
+
188
+ /** Enable physics solver for collisions, between objects and with tiles
189
+ * @type {boolean}
190
+ * @default
191
+ * @memberof Settings */
192
+ let enablePhysicsSolver = true;
193
+
194
+ /** Default object mass for collision calculations (how heavy objects are)
195
+ * @type {number}
196
+ * @default
197
+ * @memberof Settings */
198
+ let objectDefaultMass = 1;
199
+
200
+ /** Fraction of velocity objects keep each frame, 1 keeps all of it, 0 stops at once
201
+ * @type {number}
202
+ * @default
203
+ * @memberof Settings */
204
+ let objectDefaultDamping = 1;
205
+
206
+ /** Fraction of angular velocity objects keep each frame, 1 keeps all of it, 0 stops at once
207
+ * @type {number}
208
+ * @default
209
+ * @memberof Settings */
210
+ let objectDefaultAngleDamping = 1;
211
+
212
+ /** How much to bounce when a collision occurs (0-1)
213
+ * @type {number}
214
+ * @default
215
+ * @memberof Settings */
216
+ let objectDefaultRestitution = 0;
217
+
218
+ /** Fraction of sliding speed objects keep each frame on the ground, 1 is no friction, 0 stops at once
219
+ * - The more slippery of an object and its ground is used
220
+ * @type {number}
221
+ * @default
222
+ * @memberof Settings */
223
+ let objectDefaultFriction = .8;
224
+
225
+ /** Clamp max speed to avoid fast objects missing collisions, in world units per frame on each axis
226
+ * @type {number}
227
+ * @default
228
+ * @memberof Settings */
229
+ let objectMaxSpeed = 1;
230
+
231
+ /** How much gravity to apply to objects, negative Y is down, in world units per frame per frame
232
+ * @type {Vector2}
233
+ * @default
234
+ * @memberof Settings */
235
+ let gravity = vec2();
236
+
237
+ /** Scales emit rate of particles, useful for low graphics mode (0 disables particle emitters)
238
+ * @type {number}
239
+ * @default
240
+ * @memberof Settings */
241
+ let particleEmitRateScale = 1;
242
+
243
+ ///////////////////////////////////////////////////////////////////////////////
244
+ // Input settings
245
+
246
+ /** Should gamepads be allowed
247
+ * @type {boolean}
248
+ * @default
249
+ * @memberof Settings */
250
+ let gamepadsEnable = true;
251
+
252
+ /** If true, the dpad input is also routed to the left analog stick (for better accessibility)
253
+ * @type {boolean}
254
+ * @default
255
+ * @memberof Settings */
256
+ let gamepadDirectionEmulateStick = true;
257
+
258
+ /** If true, axes that do not rest near center are ignored on gamepads without
259
+ * standard mapping. Steering wheels and flight sticks report pedal and throttle
260
+ * axes that rest at full deflection, which otherwise reads as a stick held down.
261
+ * @type {boolean}
262
+ * @default
263
+ * @memberof Settings */
264
+ let gamepadAxisFilterEnable = true;
265
+
266
+ /** If true the WASD keys are also routed to the direction keys (for better accessibility)
267
+ * - Turn it off for a game with two players on one keyboard, one on WASD and one on the arrows
268
+ * @type {boolean}
269
+ * @default
270
+ * @memberof Settings */
271
+ let inputWASDEmulateDirection = true;
272
+
273
+ /** True if touch input is enabled for mobile devices
274
+ * - Touch events will be routed to mouse events
275
+ * @type {boolean}
276
+ * @default
277
+ * @memberof Settings */
278
+ let touchInputEnable = true;
279
+
280
+ /** True if touch gamepad should appear on mobile devices
281
+ * - Supports left analog stick, 4 face buttons and start button (button 9)
282
+ * - setTouchGamepadRightStick(true) for a right analog stick in place of the face buttons
283
+ * - Analog stick buttons 10 and 11 are also activated when virtual sticks are touched
284
+ * - Rendered as a full-viewport HTML/SVG overlay, so controls may sit outside the game canvas
285
+ * - It is gamepad 0 once touched; a real gamepad being used takes over and hides it until the screen is touched again
286
+ * @type {boolean}
287
+ * @default
288
+ * @memberof Settings */
289
+ let touchGamepadEnable = false;
290
+
291
+ /** True if touches outside the gamepad controls should still drive mouse/touch input
292
+ * - When false (the default), enabling the touch gamepad suppresses touch-to-mouse input entirely
293
+ * - Set true to also pass touches outside the controls through to the game as mouse/touch input
294
+ * - Touches on the gamepad controls never drive the mouse regardless of this setting
295
+ * @type {boolean}
296
+ * @default
297
+ * @memberof Settings */
298
+ let touchGamepadPassthrough = false;
299
+
300
+ /** Size of center button if touch gamepad should have start button in the center
301
+ * - Prevents activating when pressed near virtual stick or face buttons
302
+ * - When the game is paused, any touch will press the button
303
+ * - Measured in viewport CSS pixels
304
+ * @type {number}
305
+ * @default
306
+ * @memberof Settings */
307
+ let touchGamepadCenterButtonSize = 0;
308
+
309
+ /** Number of buttons on the right side of the touch gamepad (0-4), using gamepad buttons 0-3
310
+ * - A count of 1 is a single large button (the size of a stick)
311
+ * - Ignored when touchGamepadRightStick is set (the right side is a stick instead)
312
+ * @type {number}
313
+ * @default
314
+ * @memberof Settings */
315
+ let touchGamepadButtonCount = 4;
316
+
317
+ /** True if the touch gamepad should have a left analog stick (or dpad)
318
+ * - When false, the left side is face buttons (touchGamepadLeftButtonCount) or nothing
319
+ * @type {boolean}
320
+ * @default
321
+ * @memberof Settings */
322
+ let touchGamepadLeftStick = true;
323
+
324
+ /** Number of buttons on the left side of the touch gamepad (0-4), using gamepad buttons 4-7
325
+ * - Only used when touchGamepadLeftStick is false (otherwise the left side is a stick)
326
+ * - A count of 1 is a single large button (the size of a stick)
327
+ * @type {number}
328
+ * @default
329
+ * @memberof Settings */
330
+ let touchGamepadLeftButtonCount = 0;
331
+
332
+ /** True if the touch gamepad right side should be an analog stick (or dpad) instead of face buttons
333
+ * - When set, touchGamepadButtonCount is ignored and the right side is a stick
334
+ * - Uses an analog stick when touchGamepadAnalog is true, otherwise an 8 way dpad
335
+ * @type {boolean}
336
+ * @default
337
+ * @memberof Settings */
338
+ let touchGamepadRightStick = false;
339
+
340
+ /** True if touch gamepad should be analog stick or false to use if 8 way dpad
341
+ * @type {boolean}
342
+ * @default
343
+ * @memberof Settings */
344
+ let touchGamepadAnalog = true;
345
+
346
+ /** True if touch gamepad directional controls should float to where you press
347
+ * - Only affects analog sticks and dpads, not face buttons
348
+ * - Directional controls re-anchor to where you press within the bottom ~60% of their screen half; the top ~40% passes through to the game
349
+ * - The right side floats only when it acts as the right analog stick (touchGamepadRightStick is set)
350
+ * - A center button (touchGamepadCenterButtonSize) still works since it ignores touches near the sticks
351
+ * @type {boolean}
352
+ * @default
353
+ * @memberof Settings */
354
+ let touchGamepadFloating = false;
355
+
356
+ /** Size of virtual gamepad for touch devices in viewport CSS pixels
357
+ * @type {number}
358
+ * @default
359
+ * @memberof Settings */
360
+ let touchGamepadSize = 100;
361
+
362
+ /** Transparency of touch gamepad overlay
363
+ * @type {number}
364
+ * @default
365
+ * @memberof Settings */
366
+ let touchGamepadAlpha = .3;
367
+
368
+ /** How long to display the touch gamepad on screen in seconds, set to 0 to always display
369
+ * @type {number}
370
+ * @default
371
+ * @memberof Settings */
372
+ let touchGamepadDisplayTime = 3;
373
+
374
+ /** Duration in ms to vibrate when a touch gamepad face button or start button is pressed
375
+ * - Set to 0 to disable, also requires vibrateEnable and hardware support (ignored on iOS)
376
+ * @type {number}
377
+ * @default
378
+ * @memberof Settings */
379
+ let touchGamepadVibration = 0;
380
+
381
+ /** Allow vibration hardware if it exists
382
+ * @type {boolean}
383
+ * @default
384
+ * @memberof Settings */
385
+ let vibrateEnable = true;
386
+
387
+ ///////////////////////////////////////////////////////////////////////////////
388
+ // Audio settings
389
+
390
+ /** All audio code can be disabled and removed from build
391
+ * @type {boolean}
392
+ * @default
393
+ * @memberof Settings */
394
+ let soundEnable = true;
395
+
396
+ /** Volume scale to apply to all sound, music and speech
397
+ * Use setSoundVolume to also update the audio master gain immediately
398
+ * @type {number}
399
+ * @default
400
+ * @memberof Settings */
401
+ let soundVolume = .3;
402
+
403
+ /** Default range where sound no longer plays
404
+ * @type {number}
405
+ * @default
406
+ * @memberof Settings */
407
+ let soundDefaultRange = 40;
408
+
409
+ /** Default range percent to start tapering off sound (0-1)
410
+ * @type {number}
411
+ * @default
412
+ * @memberof Settings */
413
+ let soundDefaultTaper = .7;
414
+
415
+ /** Pause all sound while the page is hidden, and pick up where it was when it shows again
416
+ * - A hidden page stops the game, so without this a looping sound plays on over a frozen game
417
+ * - Turn it off to keep music playing in a background tab
418
+ * @type {boolean}
419
+ * @default
420
+ * @memberof Settings */
421
+ let soundPauseWhenHidden = true;
422
+
423
+ ///////////////////////////////////////////////////////////////////////////////
424
+ // Setters for global variables
425
+
426
+ /** Set position of camera in world space
427
+ * @param {Vector2} pos
428
+ * @memberof Settings */
429
+ function setCameraPos(pos) { cameraPos = pos.copy(); }
430
+
431
+ /** Set angle of camera in world space
432
+ * @param {number} angle
433
+ * @memberof Settings */
434
+ function setCameraAngle(angle) { cameraAngle = angle; }
435
+
436
+ /** Set scale of camera in world space
437
+ * @param {number} scale
438
+ * @memberof Settings */
439
+ function setCameraScale(scale) { cameraScale = scale; }
440
+
441
+ /** Set scale applied to engine time
442
+ * - 0 freezes the game like a pause, gameUpdatePost and input still run so the game can set it back
443
+ * @param {number} scale - 0 or more
444
+ * @memberof Settings */
445
+ function setTimeScale(scale)
446
+ {
447
+ ASSERT(scale >= 0, 'time scale must be 0 or more');
448
+ timeScale = max(0, scale); // a negative scale would build a debt of time the game then waits out
449
+ }
450
+
451
+ /** Set if tiles should be colorized when using canvas2d
452
+ * This can be slower but results should look nearly identical to WebGL rendering
453
+ * It can be enabled/disabled at any time
454
+ * Optimized for performance, and will use faster method if color is white or untextured
455
+ * @param {boolean} colorTiles
456
+ * @memberof Settings */
457
+ function setCanvasColorTiles(colorTiles) { canvasColorTiles = colorTiles; }
458
+
459
+ /** Set color to clear the canvas to before render, does not clear if alpha is 0
460
+ * @param {Color} color
461
+ * @memberof Settings */
462
+ function setCanvasClearColor(color) { canvasClearColor = color.copy(); }
463
+
464
+ /** Set max size of the canvas
465
+ * @param {Vector2} size
466
+ * @memberof Settings */
467
+ function setCanvasMaxSize(size) { canvasMaxSize = size.copy(); }
468
+
469
+ /** Set minimum aspect ratio of the canvas (width/height), unused if 0
470
+ * @param {number} aspect
471
+ * @memberof Settings */
472
+ function setCanvasMinAspect(aspect) { canvasMinAspect = aspect; }
473
+
474
+ /** Set maximum aspect ratio of the canvas (width/height), unused if 0
475
+ * @param {number} aspect
476
+ * @memberof Settings */
477
+ function setCanvasMaxAspect(aspect) { canvasMaxAspect = aspect; }
478
+
479
+ /** Set fixed size of the canvas
480
+ * @param {Vector2} size
481
+ * @memberof Settings */
482
+ function setCanvasFixedSize(size) { canvasFixedSize = size.copy(); }
483
+
484
+ /** Use nearest scaling algorithm for canvas for more pixelated look
485
+ * @param {boolean} pixelated
486
+ * @memberof Settings */
487
+ function setCanvasPixelated(pixelated)
488
+ {
489
+ canvasPixelated = pixelated;
490
+ if (mainCanvas)
491
+ mainCanvas.style.imageRendering = pixelated ? 'pixelated' : '';
492
+ if (glCanvas)
493
+ glCanvas.style.imageRendering = pixelated ? 'pixelated' : '';
494
+ }
495
+
496
+ /** Disables texture filtering for crisper pixel art
497
+ * - Leave true for pixel art; set false for smooth/high-resolution art
498
+ * - Set it before engineInit, a texture already loaded keeps the filtering it was made with
499
+ * @param {boolean} pixelated
500
+ * @memberof Settings */
501
+ function setTilesPixelated(pixelated) { tilesPixelated = pixelated; }
502
+
503
+ /** Set the canvas pixel ratio, scales the render resolution for sharper output
504
+ * Pass a number for an explicit ratio, or call with no argument to track devicePixelRatio each frame.
505
+ * - The canvas stays the same size on screen and everything draws the same
506
+ * size, it just renders at a higher resolution so nothing looks blurry
507
+ * - Game code is unaffected, it always works in css pixels
508
+ * @param {number} [pixelRatio]
509
+ * @example
510
+ * // render at native resolution, capped so phones don't pay for 3x
511
+ * setCanvasPixelRatio(min(devicePixelRatio, 2));
512
+ * @memberof Settings */
513
+ function setCanvasPixelRatio(pixelRatio) { canvasPixelRatio = pixelRatio; }
514
+
515
+ /** Get the pixel ratio currently applied to the canvas backing store
516
+ * - Resolves canvasPixelRatio, falling back to devicePixelRatio when it is undefined
517
+ * - Game code works in css pixels so this is rarely needed, it is for sizing
518
+ * render targets and viewports that must match the backing store
519
+ * @return {number}
520
+ * @memberof Settings */
521
+ function getCanvasPixelRatio() { return canvasPixelRatio ?? (devicePixelRatio || 1); }
522
+
523
+ /** Set default font used for text rendering
524
+ * @param {string} font
525
+ * @memberof Settings */
526
+ function setFontDefault(font) { fontDefault = font; }
527
+
528
+ /** Set if the LittleJS splash screen should be shown on startup, must be set before engineInit
529
+ * @param {boolean} show
530
+ * @memberof Settings */
531
+ function setShowSplashScreen(show) { showSplashScreen = show; }
532
+
533
+ /** Set to disable rendering, audio, and input for servers, must be set before engineInit
534
+ * @param {boolean} headless
535
+ * @memberof Settings */
536
+ function setHeadlessMode(headless) { headlessMode = headless; }
537
+
538
+ /** Set if the engine only advances when engineStep is called
539
+ * Set before engineInit for a test or a server; turned on in a running game it stops the loop, and turned off
540
+ * again it starts the loop from real time
541
+ * @param {boolean} [enable]
542
+ * @memberof Settings */
543
+ function setEngineManualStep(enable=true)
544
+ {
545
+ const resume = engineManualStep && !enable;
546
+ if (enable && !engineManualStep)
547
+ frameTimeBufferMS = -.5e3 / frameRate; // half a frame short, so each engineStep frame runs one update,
548
+ // not one more from time the last real frame left over
549
+ engineManualStep = enable;
550
+ if (resume && engineUpdateInternal)
551
+ {
552
+ // engineStep may have run the clock ahead of real time, start from now so that is not waited out
553
+ frameTimeLastMS = performance.now();
554
+ engineScheduleFrame();
555
+ }
556
+ }
557
+
558
+ /** Set whether the draw and debug functions draw in screen space when a call leaves screenSpace out
559
+ * @param {boolean} screenSpace
560
+ * @memberof Settings */
561
+ function setDrawScreenSpace(screenSpace) { drawScreenSpace = screenSpace; }
562
+
563
+ /** Set if WebGL rendering is enabled
564
+ * @param {boolean} enable
565
+ * @memberof Settings */
566
+ function setGLEnable(enable)
567
+ {
568
+ if (enable && !glCanBeEnabled)
569
+ {
570
+ console.warn('Can not enable WebGL if it was disabled on start.');
571
+ return;
572
+ }
573
+ glEnable = enable;
574
+ if (glCanvas) // hide glCanvas if WebGL is disabled
575
+ glCanvas.style.display = enable ? '' : 'none';
576
+ }
577
+
578
+ /** Set how many sided polygons to use when drawing circles and ellipses with WebGL
579
+ * @param {number} sides
580
+ * @memberof Settings */
581
+ function setGLCircleSides(sides) { glCircleSides = sides; }
582
+
583
+ /** Set default size of tiles in pixels
584
+ * @param {Vector2} size
585
+ * @memberof Settings */
586
+ function setTileDefaultSize(size) { tileDefaultSize = size.copy(); }
587
+
588
+ /** Default padding pixels around tiles
589
+ * @param {number} padding
590
+ * @memberof Settings */
591
+ function setTileDefaultPadding(padding) { tileDefaultPadding = padding; }
592
+
593
+ /** Default amount of pixels smaller to draw tiles to prevent neighbor bleeding
594
+ * @param {number} bleed
595
+ * @memberof Settings */
596
+ function setTileDefaultBleed(bleed) { tileDefaultBleed = bleed; }
597
+
598
+ /** Set if collisions are enabled, between objects and with tiles
599
+ * @param {boolean} enable
600
+ * @memberof Settings */
601
+ function setEnablePhysicsSolver(enable) { enablePhysicsSolver = enable; }
602
+
603
+ /** Set default object mass for collision calculations
604
+ * @param {number} mass
605
+ * @memberof Settings */
606
+ function setObjectDefaultMass(mass) { objectDefaultMass = mass; }
607
+
608
+ /** Set the fraction of velocity objects keep each frame, 1 keeps all of it, 0 stops at once
609
+ * @param {number} damp
610
+ * @memberof Settings */
611
+ function setObjectDefaultDamping(damp) { objectDefaultDamping = damp; }
612
+
613
+ /** Set the fraction of angular velocity objects keep each frame, 1 keeps all of it, 0 stops at once
614
+ * @param {number} damp
615
+ * @memberof Settings */
616
+ function setObjectDefaultAngleDamping(damp) { objectDefaultAngleDamping = damp; }
617
+
618
+ /** Set how much to bounce when a collision occurs
619
+ * @param {number} restitution
620
+ * @memberof Settings */
621
+ function setObjectDefaultRestitution(restitution) { objectDefaultRestitution = restitution; }
622
+
623
+ /** Set the fraction of sliding speed objects keep each frame on the ground, 1 is no friction, 0 stops at once
624
+ * @param {number} friction
625
+ * @memberof Settings */
626
+ function setObjectDefaultFriction(friction) { objectDefaultFriction = friction; }
627
+
628
+ /** Set max speed to avoid fast objects missing collisions
629
+ * @param {number} speed
630
+ * @memberof Settings */
631
+ function setObjectMaxSpeed(speed) { objectMaxSpeed = speed; }
632
+
633
+ /** Set how much gravity to apply to objects
634
+ * @param {Vector2} newGravity
635
+ * @memberof Settings */
636
+ function setGravity(newGravity) { gravity = newGravity.copy(); }
637
+
638
+ /** Set the scale for the emit rate of particles, 0 disables particle emitters
639
+ * @param {number} scale
640
+ * @memberof Settings */
641
+ function setParticleEmitRateScale(scale) { particleEmitRateScale = scale; }
642
+
643
+ /** Set if gamepads are enabled
644
+ * @param {boolean} enable
645
+ * @memberof Settings */
646
+ function setGamepadsEnable(enable) { gamepadsEnable = enable; }
647
+
648
+ /** Set if the dpad input is also routed to the left analog stick
649
+ * @param {boolean} enable
650
+ * @memberof Settings */
651
+ function setGamepadDirectionEmulateStick(enable) { gamepadDirectionEmulateStick = enable; }
652
+
653
+ /** Set if axes that do not rest near center are ignored on non-standard gamepads
654
+ * @param {boolean} enable
655
+ * @memberof Settings */
656
+ function setGamepadAxisFilterEnable(enable) { gamepadAxisFilterEnable = enable; }
657
+
658
+ /** Set if the WASD keys are also routed to the direction keys
659
+ * @param {boolean} enable
660
+ * @memberof Settings */
661
+ function setInputWASDEmulateDirection(enable) { inputWASDEmulateDirection = enable; }
662
+
663
+ /** Set if touch input is allowed
664
+ * @param {boolean} enable
665
+ * @memberof Settings */
666
+ function setTouchInputEnable(enable) { touchInputEnable = enable; }
667
+
668
+ /** Set if touch gamepad should appear on mobile devices
669
+ * @param {boolean} enable
670
+ * @memberof Settings */
671
+ function setTouchGamepadEnable(enable) { touchGamepadEnable = enable; }
672
+
673
+ /** Set if touches outside the gamepad controls should still drive mouse/touch input
674
+ * @param {boolean} passthrough
675
+ * @memberof Settings */
676
+ function setTouchGamepadPassthrough(passthrough) { touchGamepadPassthrough = passthrough; }
677
+
678
+ /** Set if touch gamepad should have start button in the center
679
+ * - Set size to enable the center button
680
+ * - When the game is paused, any touch will press the button
681
+ * @param {number} size
682
+ * @memberof Settings */
683
+ function setTouchGamepadCenterButtonSize(size) { touchGamepadCenterButtonSize = size; }
684
+
685
+ /** Set number of buttons on the right side of the touch gamepad (0-4, gamepad buttons 0-3)
686
+ * @param {number} count
687
+ * @memberof Settings */
688
+ function setTouchGamepadButtonCount(count)
689
+ {
690
+ touchGamepadButtonCount = count;
691
+ if (count > 0)
692
+ touchGamepadRightStick = false;
693
+ }
694
+
695
+ /** Set if the touch gamepad should have a left analog stick (or dpad)
696
+ * @param {boolean} enable
697
+ * @memberof Settings */
698
+ function setTouchGamepadLeftStick(enable)
699
+ {
700
+ touchGamepadLeftStick = enable;
701
+ if (enable)
702
+ touchGamepadLeftButtonCount = 0;
703
+ }
704
+
705
+ /** Set number of buttons on the left side of the touch gamepad (0-4, gamepad buttons 4-7)
706
+ * - Only used when touchGamepadLeftStick is false
707
+ * @param {number} count
708
+ * @memberof Settings */
709
+ function setTouchGamepadLeftButtonCount(count)
710
+ {
711
+ touchGamepadLeftButtonCount = count;
712
+ if (count > 0)
713
+ touchGamepadLeftStick = false;
714
+ }
715
+
716
+ /** Set if the touch gamepad right side is an analog stick (or dpad) instead of face buttons
717
+ * @param {boolean} rightStick
718
+ * @memberof Settings */
719
+ function setTouchGamepadRightStick(rightStick)
720
+ {
721
+ touchGamepadRightStick = rightStick;
722
+ if (rightStick)
723
+ touchGamepadButtonCount = 0;
724
+ }
725
+
726
+ /** Set if touch gamepad should be analog stick or 8 way dpad
727
+ * @param {boolean} analog
728
+ * @memberof Settings */
729
+ function setTouchGamepadAnalog(analog) { touchGamepadAnalog = analog; }
730
+
731
+ /** Set if touch gamepad directional controls should float to where you press
732
+ * @param {boolean} floating
733
+ * @memberof Settings */
734
+ function setTouchGamepadFloating(floating) { touchGamepadFloating = floating; }
735
+
736
+ /** Set size of virtual gamepad for touch devices in viewport CSS pixels
737
+ * @param {number} size
738
+ * @memberof Settings */
739
+ function setTouchGamepadSize(size) { touchGamepadSize = size; }
740
+
741
+ /** Set transparency of touch gamepad overlay
742
+ * @param {number} alpha
743
+ * @memberof Settings */
744
+ function setTouchGamepadAlpha(alpha) { touchGamepadAlpha = alpha; }
745
+
746
+ /** Set how long to display the touch gamepad on screen in seconds, set to 0 to always display
747
+ * @param {number} time
748
+ * @memberof Settings */
749
+ function setTouchGamepadDisplayTime(time) { touchGamepadDisplayTime = time; }
750
+
751
+ /** Set duration in ms to vibrate when a touch gamepad face or start button is pressed (0 disables)
752
+ * @param {number} ms
753
+ * @memberof Settings */
754
+ function setTouchGamepadVibration(ms) { touchGamepadVibration = ms; }
755
+
756
+ /** Set to allow vibration hardware if it exists
757
+ * @param {boolean} enable
758
+ * @memberof Settings */
759
+ function setVibrateEnable(enable) { vibrateEnable = enable; }
760
+
761
+ /** Set if audio is enabled, false turns all sound off
762
+ * @param {boolean} enable
763
+ * @memberof Settings */
764
+ function setSoundEnable(enable) { soundEnable = enable; }
765
+
766
+ /** Set volume scale to apply to all sound, music and speech
767
+ * @param {number} volume
768
+ * @memberof Settings */
769
+ function setSoundVolume(volume)
770
+ {
771
+ soundVolume = volume;
772
+ audioUpdateVolume(); // update gain immediately, sound off or not
773
+ }
774
+
775
+ /** Set default range where sound no longer plays
776
+ * @param {number} range
777
+ * @memberof Settings */
778
+ function setSoundDefaultRange(range) { soundDefaultRange = range; }
779
+
780
+ /** Set default range percent to start tapering off sound
781
+ * @param {number} taper
782
+ * @memberof Settings */
783
+ function setSoundDefaultTaper(taper) { soundDefaultTaper = taper; }
784
+
785
+ /** Set if all sound pauses while the page is hidden
786
+ * @param {boolean} pause
787
+ * @memberof Settings */
788
+ function setSoundPauseWhenHidden(pause) { soundPauseWhenHidden = pause; }
789
+
790
+ /** Set if watermark with FPS should be shown
791
+ * @param {boolean} show
792
+ * @memberof Debug */
793
+ function setDebugWatermark(show) { debugWatermark = show; }
794
+
795
+ /** Set key code used to toggle debug mode, Esc by default
796
+ * @param {string} key
797
+ * @memberof Debug */
798
+ function setDebugKey(key) { debugKey = key; }
799
+
800
+ /** Set if the debug keys work while the overlay is closed, the number keys and the +/- time keys
801
+ * @param {boolean} [enable]
802
+ * @memberof Debug */
803
+ function setDebugKeysAlways(enable=true) { debugKeysAlways = enable; }
804
+
805
+ /** Set if the tweakables panel is shown, as 9 does while the debug overlay is open
806
+ * @param {boolean} [show]
807
+ * @memberof Debug */
808
+ function setDebugTweakables(show=true) { debugTweakables = show; }