littlejsengine 1.11.5 → 1.11.6

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 (39) hide show
  1. package/dist/littlejs.esm.js +11 -8
  2. package/dist/littlejs.esm.min.js +1 -1
  3. package/dist/littlejs.js +11 -8
  4. package/dist/littlejs.min.js +1 -1
  5. package/dist/littlejs.release.js +11 -8
  6. package/examples/box2d/index.html +5 -5
  7. package/examples/breakout/gameObjects.js +1 -1
  8. package/examples/breakout/index.html +4 -4
  9. package/examples/breakoutTutorial/README.md +1 -1
  10. package/examples/breakoutTutorial/game.js +1 -1
  11. package/examples/breakoutTutorial/index.html +2 -2
  12. package/examples/htmlMenu/index.html +2 -2
  13. package/examples/module/index.html +1 -1
  14. package/examples/particles/index.html +1 -1
  15. package/examples/platformer/index.html +8 -8
  16. package/examples/puzzle/index.html +2 -2
  17. package/examples/starter/game.js +3 -3
  18. package/examples/starter/index.html +13 -13
  19. package/examples/stress/index.html +1 -1
  20. package/examples/uiSystem/index.html +3 -3
  21. package/package.json +1 -1
  22. package/shortExamples/base.html +15 -12
  23. package/shortExamples/code/animation.js +18 -0
  24. package/shortExamples/code/blending.js +8 -4
  25. package/shortExamples/code/clock.js +21 -0
  26. package/shortExamples/code/colors.js +25 -0
  27. package/shortExamples/code/helloWorld.js +5 -4
  28. package/shortExamples/code/playSound.js +1 -3
  29. package/shortExamples/code/shapes.js +1 -1
  30. package/shortExamples/code/spriteAtlas.js +14 -7
  31. package/shortExamples/code/systemFont.js +10 -8
  32. package/shortExamples/code/tileLayer.js +1 -1
  33. package/shortExamples/code/timers.js +36 -0
  34. package/shortExamples/index.html +6 -2
  35. package/shortExamples/tiles.png +0 -0
  36. package/src/engine.js +1 -1
  37. package/src/engineDraw.js +4 -1
  38. package/src/engineObject.js +4 -4
  39. package/src/engineUtilities.js +2 -2
@@ -1,12 +1,16 @@
1
1
  function gameRender()
2
2
  {
3
+ const circleTile = tile(2, 128);
4
+
3
5
  // additive blend
4
6
  setBlendMode(1);
5
- drawTile(vec2(1, 3), vec2(6), tile(0), rgb(1,0,0));
6
- drawTile(vec2(-1,3), vec2(6), tile(0), rgb(0,0,1));
7
+ drawTile(vec2(-4,-2), vec2(7), circleTile, rgb(1,0,0));
8
+ drawTile(vec2(-6, 2), vec2(7), circleTile, rgb(0,1,0));
9
+ drawTile(vec2(-8,-2), vec2(7), circleTile, rgb(0,0,1));
7
10
 
8
11
  // alpha blend
9
12
  setBlendMode(0);
10
- drawTile(vec2(1, -3), vec2(6), tile(0), rgb(1,0,0,.5));
11
- drawTile(vec2(-1,-3), vec2(6), tile(0), rgb(0,0,1,.5));
13
+ drawTile(vec2(4,-2), vec2(7), circleTile, rgb(1,0,0,.5));
14
+ drawTile(vec2(6, 2), vec2(7), circleTile, rgb(0,1,0,.5));
15
+ drawTile(vec2(8,-2), vec2(7), circleTile, rgb(0,0,1,.5));
12
16
  }
@@ -0,0 +1,21 @@
1
+ function gameRender()
2
+ {
3
+ // draw background
4
+ for(let i=12; i--;)
5
+ {
6
+ const a = i/6*PI;
7
+ const pos = vec2(0,7).rotate(a);
8
+ drawRect(pos, vec2(.5,1), hsl(i/12,1,.5), a);
9
+ }
10
+
11
+ // get current time
12
+ const d = Date().slice(16,24);
13
+ const s = d.slice(6,8)|0;
14
+ const m = d.slice(3,5)|0;
15
+ const h = (d.slice(0,2)|0) + m/60;
16
+
17
+ // draw clock hands
18
+ drawLine(vec2(0,0), vec2(0,4).rotate(h/12*2*PI), 1);
19
+ drawLine(vec2(0,0), vec2(0,6).rotate(m/60*2*PI), .4);
20
+ drawLine(vec2(0,0), vec2(0,8).rotate(s/60*2*PI), .1);
21
+ }
@@ -0,0 +1,25 @@
1
+ function gameRender()
2
+ {
3
+ // color constants
4
+ drawRect(vec2(-8, 5), vec2(3), RED);
5
+ drawRect(vec2(-4, 5), vec2(3), YELLOW);
6
+ drawRect(vec2(-0, 5), vec2(3), GREEN);
7
+ drawRect(vec2( 4, 5), vec2(3), BLUE);
8
+ drawRect(vec2( 8, 5), vec2(3), WHITE);
9
+
10
+ // rgb and hsl color
11
+ drawRect(vec2(-8, 0), vec2(3), new Color(1,0,0));
12
+ drawRect(vec2(-4, 0), vec2(3), rgb(0,1,1));
13
+ drawRect(vec2(-0, 0), vec2(3), hsl(0,1,.5));
14
+ drawRect(vec2( 4, 0), vec2(3), hsl(.6,.5,.5));
15
+ drawRect(vec2( 8, 0), vec2(3), hsl(0,0,1));
16
+
17
+ // color lerpping
18
+ for(let i=5; i--;)
19
+ {
20
+ const color1 = RED;
21
+ const color2 = YELLOW;
22
+ const color = color1.lerp(color2, i/4);
23
+ drawRect(vec2(-8+i*4, -5), vec2(3), color);
24
+ }
25
+ }
@@ -1,7 +1,8 @@
1
1
  function gameRender()
2
2
  {
3
- // draw to overlay canvas
4
- drawTextScreen('Hello World',
5
- mainCanvasSize.scale(.5), 80, // position, size
6
- hsl(0,0,1), 6, hsl(0,0,0)); // color, outline size and color
3
+ // draw text
4
+ drawText('Hello World', vec2(0,2), 3);
5
+
6
+ // draw a tile
7
+ drawTile(vec2(0,-2), vec2(7), tile(3,128));
7
8
  }
@@ -25,11 +25,9 @@ function gameInit()
25
25
  new SoundButton(vec2(-6, 5),'💰', new Sound([,,1675,,.06,.24,1,1.82,,,837,.06]));
26
26
  new SoundButton(vec2( 0, 5),'🥊', new Sound([,,925,.04,.3,.6,1,.3,,6.27,-184,.09,.17]));
27
27
  new SoundButton(vec2( 6, 5),'✨', new Sound([,,539,0,.04,.29,1,1.92,,,567,.02,.02,,,,.04]));
28
-
29
28
  new SoundButton(vec2(-6, 0),'🐁', new Sound([,.2,1e3,.02,,.01,2,,18,,475,.01,.01]));
30
29
  new SoundButton(vec2( 0, 0),'🎹', new Sound([1.5,.5,270,,.1,,1,1.5,,,,,,,,.1,.01]));
31
- new SoundButton(vec2( 6, 0),'🏌️', new Sound([,,150,.05,,.05,,1.3,,,,,,3]));
32
-
30
+ new SoundButton(vec2( 6, 0),'🏌️', new Sound([,,150,.05,,.05,,1.3,,,,,,3]));
33
31
  new SoundButton(vec2(-6,-5),'🌊', new Sound([,.2,40,.5,,1.5,,11,,,,,,199]));
34
32
  new SoundButton(vec2( 0,-5),'🛰️', new Sound([,.5,847,.02,.3,.9,1,1.67,,,-294,.04,.13,,,,.1]));
35
33
  new SoundButton(vec2( 6,-5),'⚡', new Sound([,,471,,.09,.47,4,1.06,-6.7,,,,,.9,61,.1,,.82,.1]));
@@ -17,7 +17,7 @@ function gameRender()
17
17
  drawEllipse(vec2(-5,5), 2, 1, 0, hsl(0,1,.5));
18
18
  drawEllipse(vec2(5,5), 1, .5, 0, hsl(.55,1,.5));
19
19
 
20
- // lines and rects
20
+ // rects and lines
21
21
  drawRect(vec2(0,-5), vec2(13,2), hsl(.6,1,.5));
22
22
  drawLine(vec2(-5,-7), vec2(5,-3), 1, hsl(0,0,1));
23
23
  drawLine(vec2(-5,-3), vec2(5,-7), .2, hsl(1,1,.5));
@@ -9,7 +9,8 @@ function gameInit()
9
9
  circle: gameTile(0),
10
10
  crate: gameTile(1),
11
11
  icon: gameTile(2),
12
- largeIcon: gameTile(vec2(1,1),128),
12
+ circleBig: gameTile(2, 128),
13
+ iconBig: gameTile(3, 128),
13
14
  };
14
15
  }
15
16
 
@@ -18,10 +19,16 @@ function gameRender()
18
19
  drawRect(vec2(0,0), vec2(100), GRAY); // draw background
19
20
 
20
21
  // draw a sprite from the atlas
21
- let pos = vec2(0,0); // world position to draw
22
- let angle = 0; // world space angle to draw the tile
23
- let size = vec2(15); // world size of the tile
24
- let mirror = 0; // should tile be mirrored?
25
- let color = hsl(0,0,1); // color to multiply the tile by
26
- drawTile(pos, size, spriteAtlas.largeIcon, color, angle, mirror);
22
+ let pos = vec2(Math.sin(time)*5,-3);// world position to draw
23
+ let angle = 0; // world space angle to draw the tile
24
+ let size = vec2(9); // world size of the tile
25
+ let mirror = 0; // should tile be mirrored?
26
+ let color = hsl(0,0,1); // color to multiply the tile by
27
+ let additiveColor = hsl(0,0,0,0); // color to add to
28
+ drawTile(pos, size, spriteAtlas.iconBig, color, angle, mirror, additiveColor);
29
+
30
+ // draw more sprites from the atlas
31
+ drawTile(vec2(-7,4), vec2(5), spriteAtlas.crate);
32
+ drawTile(vec2( 0,4), vec2(5), spriteAtlas.circle);
33
+ drawTile(vec2( 7,4), vec2(5), spriteAtlas.circleBig);
27
34
  }
@@ -1,14 +1,16 @@
1
1
  function gameRender()
2
2
  {
3
+ // draw text with built in font image system font
3
4
  const font = new FontImage;
4
- font.drawText('System Font Test ', cameraPos.add(vec2(0,3)), .2, true);
5
+ font.drawText('System Font Test', cameraPos.add(vec2(0,3)), .2, true);
5
6
 
6
- for(let j=1; j<4; j++)
7
+ // show every character in the system font
8
+ let s = '';
9
+ for(let i=32; i<128; ++i)
7
10
  {
8
- let s = '';
9
- for(let i=0; i<32; ++i)
10
- s += String.fromCharCode(32*j + i);
11
- font.drawText(s, cameraPos.add(vec2(0,1-j)), .1, true);
11
+ if (i%32 == 0)
12
+ s += '\n';
13
+ s += String.fromCharCode(i);
12
14
  }
13
- }
14
-
15
+ font.drawText(s, cameraPos, .1, true);
16
+ }
@@ -34,7 +34,7 @@ function gameUpdate()
34
34
  {
35
35
  // create particle emitter
36
36
  const hue = rand();
37
- let particleEmitter = new ParticleEmitter(
37
+ const particleEmitter = new ParticleEmitter(
38
38
  mousePos, 0, // emitPos, emitAngle
39
39
  0, 0.1, 500, PI, // emitSize, emitTime, emitRate, emitCone
40
40
  tile(0, 16), // tileIndex, tileSize
@@ -0,0 +1,36 @@
1
+ class TimerButton extends EngineObject
2
+ {
3
+ constructor(pos, time)
4
+ {
5
+ super(pos, vec2(5.5));
6
+ this.time = time;
7
+ this.timer = new Timer;
8
+ }
9
+
10
+ update()
11
+ {
12
+ if (mouseWasPressed(0) && isOverlapping(this.pos, this.size, mousePos))
13
+ this.timer.isSet() ? this.timer.unset() : this.timer.set(this.time);
14
+ }
15
+
16
+ render()
17
+ {
18
+ this.color = this.timer.isSet() ? this.timer.active() ? BLUE : RED : GRAY;
19
+ drawRect(this.pos, this.size, this.color);
20
+ if (this.timer.isSet())
21
+ {
22
+ drawTextOverlay(this.timer.get().toFixed(1), this.pos.add(vec2(0,1)), 2);
23
+ const percent = this.timer.getPercent()*100|0;
24
+ drawTextOverlay(percent+'%', this.pos.add(vec2(0,-1)), 2);
25
+ }
26
+ else
27
+ drawTextOverlay('Click\nto set', this.pos, 2);
28
+ }
29
+ }
30
+
31
+ function gameInit()
32
+ {
33
+ new TimerButton(vec2(-7, 0), 3);
34
+ new TimerButton(vec2( 0, 0), 1);
35
+ new TimerButton(vec2( 7, 0), 0);
36
+ }
@@ -44,7 +44,7 @@ iframe
44
44
  }
45
45
  #textareaError
46
46
  {
47
- flex-grow: 1;
47
+ flex-grow: .3;
48
48
  padding: 5px;
49
49
  resize: none;
50
50
  display: none;
@@ -108,12 +108,16 @@ const exampleList =
108
108
  [
109
109
  new ExampleInfo('Hello World', 'helloWorld.js', 'Simplest example'),
110
110
  new ExampleInfo('Shapes', 'shapes.js', 'How to draw geometric shapes'),
111
+ new ExampleInfo('Colors', 'colors.js', 'How to use the color object'),
111
112
  new ExampleInfo('Blending', 'blending.js', 'Shows different blending modes'),
113
+ new ExampleInfo('Timers', 'timers.js', 'How to use the timer object'),
112
114
  new ExampleInfo('Texture', 'texture.js', 'How to display a full texture'),
113
115
  new ExampleInfo('Particles', 'particles.js', 'How to use the particle system'),
114
116
  new ExampleInfo('Play Sound', 'playSound.js', 'How to play sounds with zzfx'),
115
117
  new ExampleInfo('System Font', 'systemFont.js', 'Demo of the included system font'),
116
118
  new ExampleInfo('Sprite Atlas', 'spriteAtlas.js', 'How to set up a simple sprite atlas'),
119
+ new ExampleInfo('Animation', 'animation.js', 'How to animate sprites'),
120
+ new ExampleInfo('Clock', 'clock.js', 'A simple clock showing the time'),
117
121
  new ExampleInfo('Tile Layer', 'tileLayer.js', 'How to use a tile layer for collision and rendering'),
118
122
  new ExampleInfo('Pong Game', 'pong.js', 'A simple pong game using LittleJS physics'),
119
123
 
@@ -226,7 +230,7 @@ function setCode(code)
226
230
  return; // stop if script error
227
231
 
228
232
  // start LittleJS engine
229
- iframeContent.engineInit(iframeContent.gameInit, iframeContent.gameUpdate, iframeContent.gameUpdatePost, iframeContent.gameRender, iframeContent.gameRenderPost, ['tiles.png']);
233
+ iframeContent.engineInit(iframeContent.gameInit, iframeContent.gameUpdate, iframeContent.gameUpdatePost, iframeContent.gameRender, iframeContent.gameRenderPost, ['tiles.png?1']);
230
234
  }
231
235
  iframeExample.src = 'base.html';
232
236
  }
Binary file
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.11.5';
33
+ const engineVersion = '1.11.6';
34
34
 
35
35
  /** Frames per second to update
36
36
  * @type {Number}
package/src/engineDraw.js CHANGED
@@ -458,7 +458,10 @@ function drawTextScreen(text, pos, size=1, color=new Color, lineWidth=0, lineCol
458
458
  context.lineJoin = 'round';
459
459
 
460
460
  pos = pos.copy();
461
- (text+'').split('\n').forEach(line=>
461
+
462
+ const lines = (text+'').split('\n');
463
+ pos.y -= (lines.length-1) * size/2; // center text vertically
464
+ lines.forEach(line=>
462
465
  {
463
466
  lineWidth && context.strokeText(line, pos.x, pos.y, maxWidth);
464
467
  context.fillText(line, pos.x, pos.y, maxWidth);
@@ -338,19 +338,19 @@ class EngineObject
338
338
 
339
339
  /** Convert from local space to world space
340
340
  * @param {Vector2} pos - local space point */
341
- localToWorld(pos) { return this.pos.add(pos.rotate(-this.angle)); }
341
+ localToWorld(pos) { return this.pos.add(pos.rotate(this.angle)); }
342
342
 
343
343
  /** Convert from world space to local space
344
344
  * @param {Vector2} pos - world space point */
345
- worldToLocal(pos) { return pos.subtract(this.pos).rotate(this.angle); }
345
+ worldToLocal(pos) { return pos.subtract(this.pos).rotate(-this.angle); }
346
346
 
347
347
  /** Convert from local space to world space for a vector (rotation only)
348
348
  * @param {Vector2} vec - local space vector */
349
- localToWorldVector(vec) { return vec.rotate(this.angle); }
349
+ localToWorldVector(vec) { return vec.rotate(-this.angle); }
350
350
 
351
351
  /** Convert from world space to local space for a vector (rotation only)
352
352
  * @param {Vector2} vec - world space vector */
353
- worldToLocalVector(vec) { return vec.rotate(-this.angle); }
353
+ worldToLocalVector(vec) { return vec.rotate(this.angle); }
354
354
 
355
355
  /** Called to check if a tile collision should be resolved
356
356
  * @param {Number} tileData - the value of the tile at the position
@@ -482,7 +482,7 @@ class Vector2
482
482
  rotate(angle)
483
483
  {
484
484
  const c = Math.cos(angle), s = Math.sin(angle);
485
- return new Vector2(this.x*c - this.y*s, this.x*s + this.y*c);
485
+ return new Vector2(this.x*c + this.y*s, this.x*s + this.y*c);
486
486
  }
487
487
 
488
488
  /** Set the integer direction of this vector, corresponding to multiples of 90 degree rotation (0-3)
@@ -914,7 +914,7 @@ class Timer
914
914
 
915
915
  /** Get percentage elapsed based on time it was set to, returns 0 if not set
916
916
  * @return {Number} */
917
- getPercent() { return this.isSet()? percent(this.time - time, this.setTime, 0) : 0; }
917
+ getPercent() { return this.isSet()? 1-percent(this.time - time, 0, this.setTime) : 0; }
918
918
 
919
919
  /** Returns this timer expressed as a string
920
920
  * @return {String} */