littlejsengine 1.14.10 → 1.14.11

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.
@@ -76,7 +76,7 @@ class NewgroundsPlugin
76
76
  // get medals
77
77
  const medalsResult = this.call('Medal.getList');
78
78
  this.medals = medalsResult ? medalsResult.result.data['medals'] : [];
79
- debugMedals && console.log(this.medals);
79
+ debugMedals && LOG(this.medals);
80
80
  for (const newgroundsMedal of this.medals)
81
81
  {
82
82
  const medal = medals[newgroundsMedal['id']];
@@ -99,7 +99,7 @@ class NewgroundsPlugin
99
99
  // get scoreboards
100
100
  const scoreboardResult = this.call('ScoreBoard.getBoards');
101
101
  this.scoreboards = scoreboardResult ? scoreboardResult.result.data.scoreboards : [];
102
- debugMedals && console.log(this.scoreboards);
102
+ debugMedals && LOG(this.scoreboards);
103
103
 
104
104
  // keep the session alive with a ping every minute
105
105
  const keepAliveMS = 60 * 1e3;
@@ -168,10 +168,10 @@ class NewgroundsPlugin
168
168
  try { xmlHttp.send(formData); }
169
169
  catch(e)
170
170
  {
171
- debugMedals && console.log('newgrounds call failed', e);
171
+ debugMedals && LOG('newgrounds call failed', e);
172
172
  return;
173
173
  }
174
- debugMedals && console.log(xmlHttp.responseText);
174
+ debugMedals && LOG(xmlHttp.responseText);
175
175
  return xmlHttp.responseText && JSON.parse(xmlHttp.responseText);
176
176
  }
177
177
  }
package/reference.md CHANGED
@@ -77,7 +77,6 @@ Vector2.normalize(length=1) // Normalize this vector to length
77
77
  Vector2.clampLength(length=1) // Clamp this vector to length
78
78
  Vector2.dot(v) // Dot product with vector
79
79
  Vector2.cross(v) // Cross product with vector
80
- Vector2.invert() // Invert this vector
81
80
  Vector2.floor() // Floor this vector
82
81
  Vector2.area() // Get area covered by this vector as a rectangle
83
82
  Vector2.lerp(v, percent) // Interpolate between vectors
@@ -442,7 +441,6 @@ debug // Is debug enabled?
442
441
  debugPointSize = .5 // Size to render debug points by default
443
442
  debugKey = 'Escape' // Key code used to toggle debug mode
444
443
  debugOverlay // True if the debug overlay is active
445
- enableAsserts // True if asserts are enabled
446
444
  showWatermark // True if watermark with FPS should be show
447
445
  ```
448
446
 
package/src/engine.js CHANGED
@@ -30,7 +30,7 @@ const engineName = 'LittleJS';
30
30
  * @type {string}
31
31
  * @default
32
32
  * @memberof Engine */
33
- const engineVersion = '1.14.10';
33
+ const engineVersion = '1.14.11';
34
34
 
35
35
  /** Frames per second to update
36
36
  * @type {number}
@@ -119,12 +119,12 @@ function engineAddPlugin(updateFunction, renderFunction)
119
119
  * @example
120
120
  * // Basic engine startup
121
121
  * engineInit(
122
- * () => { console.log('Game initialized!'); }, // gameInit
123
- * () => { updateGameLogic(); }, // gameUpdate
124
- * () => { updateUI(); }, // gameUpdatePost
125
- * () => { drawBackground(); }, // gameRender
126
- * () => { drawHUD(); }, // gameRenderPost
127
- * ['tiles.png', 'tilesLevel.png'] // images to load
122
+ * () => { LOG('Game initialized!'); }, // gameInit
123
+ * () => { updateGameLogic(); }, // gameUpdate
124
+ * () => { updateUI(); }, // gameUpdatePost
125
+ * () => { drawBackground(); }, // gameRender
126
+ * () => { drawHUD(); }, // gameRenderPost
127
+ * ['tiles.png', 'tilesLevel.png'] // images to load
128
128
  * );
129
129
  * @memberof Engine */
130
130
  async function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources=[], rootElement=document.body)
@@ -170,16 +170,20 @@ async function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, game
170
170
  if (!debugSpeedUp)
171
171
  frameTimeBufferMS = min(frameTimeBufferMS, 50); // clamp min framerate
172
172
 
173
+ let wasUpdated = false;
173
174
  if (paused)
174
175
  {
176
+ // update everything except the game and objects
177
+ wasUpdated = true;
175
178
  updateCanvas();
179
+ inputUpdate();
180
+ pluginUpdateList.forEach(f=>f());
176
181
 
177
182
  // update object transforms even when paused
178
183
  for (const o of engineObjects)
179
184
  o.parent || o.updateTransforms();
180
185
 
181
- inputUpdate();
182
- pluginUpdateList.forEach(f=>f());
186
+ // do post update
183
187
  debugUpdate();
184
188
  gameUpdatePost();
185
189
  inputUpdatePost();
@@ -196,12 +200,13 @@ async function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, game
196
200
  }
197
201
 
198
202
  // update multiple frames if necessary in case of slow framerate
199
- for (;frameTimeBufferMS >= 0; frameTimeBufferMS -= 1e3 / frameRate)
203
+ for (; frameTimeBufferMS >= 0; frameTimeBufferMS -= 1e3 / frameRate)
200
204
  {
201
205
  // increment frame and update time
202
206
  time = frame++ / frameRate;
203
207
 
204
208
  // update game and objects
209
+ wasUpdated = true;
205
210
  updateCanvas();
206
211
  inputUpdate();
207
212
  gameUpdate();
@@ -212,7 +217,6 @@ async function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, game
212
217
  debugUpdate();
213
218
  gameUpdatePost();
214
219
  inputUpdatePost();
215
-
216
220
  if (debugVideoCaptureIsActive())
217
221
  renderFrame();
218
222
  }
@@ -229,6 +233,10 @@ async function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, game
229
233
  {
230
234
  if (headlessMode) return;
231
235
 
236
+ // canvas must be updated before rendering
237
+ if (!wasUpdated)
238
+ updateCanvas();
239
+
232
240
  // render sort then render while removing destroyed objects
233
241
  enginePreRender();
234
242
  gameRender();
@@ -388,7 +396,7 @@ async function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, game
388
396
  promises.push(new Promise(resolve =>
389
397
  {
390
398
  let t = 0;
391
- console.log(`${engineName} Engine v${engineVersion}`);
399
+ LOG(`${engineName} Engine v${engineVersion}`);
392
400
  updateSplash();
393
401
  function updateSplash()
394
402
  {
@@ -16,12 +16,6 @@
16
16
  * @memberof Debug */
17
17
  const debug = true;
18
18
 
19
- /** True if asserts are enabled
20
- * @type {boolean}
21
- * @default
22
- * @memberof Debug */
23
- const enableAsserts = true;
24
-
25
19
  /** Size to render debug points by default
26
20
  * @type {number}
27
21
  * @default
@@ -52,15 +46,16 @@ let debugPrimitives = [], debugPhysics = false, debugRaycast = false, debugParti
52
46
  ///////////////////////////////////////////////////////////////////////////////
53
47
  // Debug helper functions
54
48
 
55
- /** Asserts if the expression is false, does not do anything in release builds
49
+ /** Asserts if the expression is false, does nothing in release builds
56
50
  * @param {boolean} assert
57
51
  * @param {...Object} [output] - error message output
58
52
  * @memberof Debug */
59
- function ASSERT(assert, ...output)
60
- {
61
- if (enableAsserts)
62
- console.assert(assert, ...output);
63
- }
53
+ function ASSERT(assert, ...output) { console.assert(assert, ...output); }
54
+
55
+ /** Log to console if debug is enabled, does nothing in release builds
56
+ * @param {...Object} [output] - message output
57
+ * @memberof Debug */
58
+ function LOG(...output) { console.log(...output); }
64
59
 
65
60
  /** Draw a debug rectangle in world space
66
61
  * @param {Vector2} pos
@@ -554,7 +549,7 @@ function debugVideoCaptureStart()
554
549
  }
555
550
 
556
551
  // start recording
557
- console.log('Video capture started.');
552
+ LOG('Video capture started.');
558
553
  debugVideoCapture.start();
559
554
  debugVideoCaptureTimer = new Timer(0);
560
555
 
@@ -581,7 +576,7 @@ function debugVideoCaptureStop()
581
576
  return; // not recording
582
577
 
583
578
  // stop recording
584
- console.log(`Video capture ended. ${debugVideoCaptureTimer.get().toFixed(2)} seconds recorded.`);
579
+ LOG(`Video capture ended. ${debugVideoCaptureTimer.get().toFixed(2)} seconds recorded.`);
585
580
  debugVideoCapture.stop();
586
581
  debugVideoCapture = 0;
587
582
  debugVideoCaptureIcon.style.display = 'none';
@@ -31,6 +31,7 @@ export
31
31
 
32
32
  // Debug
33
33
  ASSERT,
34
+ LOG,
34
35
  debugRect,
35
36
  debugPoly,
36
37
  debugCircle,
@@ -18,6 +18,7 @@ const debugMedals = 0;
18
18
 
19
19
  // debug commands are automatically removed from the final build
20
20
  function ASSERT (){}
21
+ function LOG (){}
21
22
  function debugInit (){}
22
23
  function debugUpdate (){}
23
24
  function debugRender (){}
@@ -108,6 +108,12 @@ function tileCollisionLoad(tileMapData, tileInfo=tile(), renderOrder=0, collisio
108
108
  const tileLayer = new TileCollisionLayer(vec2(), levelSize, tileInfo, layerRenderOrder);
109
109
  tileLayers[layerIndex] = tileLayer;
110
110
 
111
+ // apply layer color
112
+ const layerColor = dataLayer.color || WHITE;
113
+ if (dataLayer.tintcolor)
114
+ layerColor.setHex(dataLayer.tintcolor);
115
+ ASSERT(isColor(layerColor), 'layer color is not a color');
116
+
111
117
  for (let x=levelSize.x; x--;)
112
118
  for (let y=levelSize.y; y--;)
113
119
  {
@@ -115,7 +121,7 @@ function tileCollisionLoad(tileMapData, tileInfo=tile(), renderOrder=0, collisio
115
121
  const data = dataLayer.data[x + y*levelSize.x];
116
122
  if (data)
117
123
  {
118
- const layerData = new TileLayerData(data-1);
124
+ const layerData = new TileLayerData(data-1, 0, false, layerColor);
119
125
  tileLayer.setData(pos, layerData);
120
126
 
121
127
  // set collision for top layer
@@ -217,7 +217,12 @@ function wave(frequency=1, amplitude=1, t=time, offset=0)
217
217
  * @param {number} t - time in seconds
218
218
  * @return {string}
219
219
  * @memberof Utilities */
220
- function formatTime(t) { return (t/60|0) + ':' + (t%60<10?'0':'') + (t%60|0); }
220
+ function formatTime(t)
221
+ {
222
+ const sign = t < 0 ? '-' : '';
223
+ t = abs(t)|0;
224
+ return sign + (t/60|0) + ':' + (t%60<10?'0':'') + t%60;
225
+ }
221
226
 
222
227
  /** Fetches a JSON file from a URL and returns the parsed JSON object. Must be used with await!
223
228
  * @param {string} url - URL of JSON file
@@ -553,11 +558,7 @@ class Vector2
553
558
  direction()
554
559
  { return abs(this.x) > abs(this.y) ? this.x < 0 ? 3 : 1 : this.y < 0 ? 2 : 0; }
555
560
 
556
- /** Returns a copy of this vector that has been inverted
557
- * @return {Vector2} */
558
- invert() { return new Vector2(this.y, -this.x); }
559
-
560
- /** Returns a copy of this vector absolute values
561
+ /** Returns a copy of this vector with absolute values
561
562
  * @return {Vector2} */
562
563
  abs() { return new Vector2(abs(this.x), abs(this.y)); }
563
564
 
@@ -599,13 +600,10 @@ class Vector2
599
600
  toString(digits=3)
600
601
  {
601
602
  ASSERT_NUMBER_VALID(digits);
602
- if (debug)
603
- {
604
- if (this.isValid())
605
- return `(${(this.x<0?'':' ') + this.x.toFixed(digits)},${(this.y<0?'':' ') + this.y.toFixed(digits)} )`;
606
- else
607
- return `(${this.x}, ${this.y})`;
608
- }
603
+ if (this.isValid())
604
+ return `(${(this.x<0?'':' ') + this.x.toFixed(digits)},${(this.y<0?'':' ') + this.y.toFixed(digits)} )`;
605
+ else
606
+ return `(${this.x}, ${this.y})`;
609
607
  }
610
608
 
611
609
  /** Checks if this is a valid vector
@@ -994,7 +992,7 @@ class Timer
994
992
 
995
993
  /** Returns this timer expressed as a string
996
994
  * @return {string} */
997
- toString() { if (debug) { return this.isSet() ? Math.abs(this.get()) + ' seconds ' + (this.get()<0 ? 'before' : 'after' ) : 'unset'; }}
995
+ toString() { return this.isSet() ? Math.abs(this.get()) + ' seconds ' + (this.get()<0 ? 'before' : 'after' ) : 'unset'; }
998
996
 
999
997
  /** Get how long since elapsed, returns 0 if not set (returns negative if currently active)
1000
998
  * @return {number} */