littlejsengine 1.7.21 → 1.8.3

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 (52) hide show
  1. package/README.md +28 -55
  2. package/build/littlejs.d.ts +595 -552
  3. package/build/littlejs.esm.js +626 -535
  4. package/build/littlejs.esm.min.js +1 -1
  5. package/build/littlejs.js +542 -264
  6. package/build/littlejs.min.js +1 -1
  7. package/build/littlejs.release.js +542 -264
  8. package/examples/breakout/game.js +17 -14
  9. package/examples/breakout/gameObjects.js +29 -8
  10. package/examples/breakout/index.html +3 -3
  11. package/examples/breakoutTutorial/README.md +3 -3
  12. package/examples/breakoutTutorial/game.js +4 -4
  13. package/examples/breakoutTutorial/index.html +2 -2
  14. package/examples/electron/game.js +3 -2
  15. package/examples/electron/index.html +2 -2
  16. package/examples/empty/game.js +1 -1
  17. package/examples/favicon.png +0 -0
  18. package/examples/js13k/game.js +20 -15
  19. package/examples/js13k/index.html +14 -14
  20. package/examples/module/game.js +5 -4
  21. package/examples/module/index.html +1 -1
  22. package/examples/particles/index.html +19 -22
  23. package/examples/platformer/game.js +3 -3
  24. package/examples/platformer/gameEffects.js +21 -19
  25. package/examples/platformer/gameLevel.js +6 -6
  26. package/examples/platformer/gameObjects.js +18 -18
  27. package/examples/platformer/gamePlayer.js +4 -3
  28. package/examples/platformer/index.html +6 -6
  29. package/examples/platformer/tiles.png +0 -0
  30. package/examples/platformer/tilesLevel.png +0 -0
  31. package/examples/puzzle/game.js +10 -8
  32. package/examples/puzzle/index.html +2 -2
  33. package/examples/screenshot.jpg +0 -0
  34. package/examples/starter/game.js +32 -21
  35. package/examples/starter/index.html +13 -13
  36. package/examples/starter/tiles.png +0 -0
  37. package/examples/stress/index.html +4 -4
  38. package/examples/typescript/game.js +4 -3
  39. package/examples/typescript/game.ts +5 -4
  40. package/examples/typescript/index.html +1 -1
  41. package/package.json +2 -1
  42. package/src/engine.js +59 -52
  43. package/src/engineAudio.js +2 -1
  44. package/src/engineDraw.js +171 -55
  45. package/src/engineExport.js +84 -271
  46. package/src/engineMedals.js +13 -45
  47. package/src/engineObject.js +13 -13
  48. package/src/engineParticles.js +17 -17
  49. package/src/engineSettings.js +195 -2
  50. package/src/engineTileLayer.js +23 -22
  51. package/src/engineUtilities.js +6 -6
  52. package/src/engineWebGL.js +43 -50
@@ -10,9 +10,9 @@
10
10
 
11
11
  class GameObject extends EngineObject
12
12
  {
13
- constructor(pos, size, tileIndex, tileSize, angle)
13
+ constructor(pos, size, tileInfo, angle)
14
14
  {
15
- super(pos, size, tileIndex, tileSize, angle);
15
+ super(pos, size, tileInfo, angle);
16
16
  this.health = 0;
17
17
  this.isGameObject = 1;
18
18
  this.damageTimer = new Timer;
@@ -66,7 +66,7 @@ class Crate extends GameObject
66
66
  {
67
67
  constructor(pos)
68
68
  {
69
- super(pos, vec2(1), 2, vec2(16), (randInt(4))*PI/2);
69
+ super(pos, vec2(1), tile(2), (randInt(4))*PI/2);
70
70
 
71
71
  this.color = (new Color).setHSLA(rand(),1,.8);
72
72
  this.health = 5;
@@ -81,7 +81,7 @@ class Crate extends GameObject
81
81
  return;
82
82
 
83
83
  sound_destroyObject.play(this.pos);
84
- makeDebris(this.pos, this.color);
84
+ makeDebris(this.pos, this.color, 200);
85
85
  this.destroy();
86
86
  }
87
87
  }
@@ -92,7 +92,7 @@ class Enemy extends GameObject
92
92
  {
93
93
  constructor(pos)
94
94
  {
95
- super(pos, vec2(.9,.9), 8, vec2(16));
95
+ super(pos, vec2(.9,.9), tile(5));
96
96
 
97
97
  this.drawSize = vec2(1);
98
98
  this.color = (new Color).setHSLA(rand(), 1, .7);
@@ -140,7 +140,7 @@ class Enemy extends GameObject
140
140
  // make bottom flush
141
141
  let bodyPos = this.pos;
142
142
  bodyPos = bodyPos.add(vec2(0,(this.drawSize.y-this.size.y)/2));
143
- drawTile(bodyPos, this.drawSize, this.tileIndex, this.tileSize, this.color, this.angle, this.mirror, this.additiveColor);
143
+ drawTile(bodyPos, this.drawSize, this.tileInfo, this.color, this.angle, this.mirror, this.additiveColor);
144
144
  }
145
145
  }
146
146
 
@@ -150,7 +150,7 @@ class Grenade extends GameObject
150
150
  {
151
151
  constructor(pos)
152
152
  {
153
- super(pos, vec2(.2), 3, vec2(8));
153
+ super(pos, vec2(.2), tile(3,8));
154
154
 
155
155
  this.beepTimer = new Timer(1);
156
156
  this.elasticity = .3;
@@ -178,12 +178,12 @@ class Grenade extends GameObject
178
178
 
179
179
  render()
180
180
  {
181
- drawTile(this.pos, vec2(.5), this.tileIndex, this.tileSize, this.color, this.angle);
181
+ drawTile(this.pos, vec2(.5), this.tileInfo, this.color, this.angle);
182
182
 
183
183
  // draw additive flash when damaged
184
184
  setBlendMode(1);
185
185
  const flash = Math.cos(this.getAliveTime()*2*PI);
186
- drawTile(this.pos, vec2(2), 0, vec2(16), new Color(1,0,0,.2-.2*flash));
186
+ drawTile(this.pos, vec2(2), tile(0, 16), new Color(1,0,0,.2-.2*flash));
187
187
  setBlendMode(0);
188
188
  }
189
189
  }
@@ -194,7 +194,7 @@ class Weapon extends EngineObject
194
194
  {
195
195
  constructor(pos, parent)
196
196
  {
197
- super(pos, vec2(.6), 2, vec2(8));
197
+ super(pos, vec2(.6), tile(2,8));
198
198
 
199
199
  // weapon settings
200
200
  this.fireRate = 8;
@@ -211,12 +211,12 @@ class Weapon extends EngineObject
211
211
  // shell effect
212
212
  this.addChild(this.shellEmitter = new ParticleEmitter(
213
213
  vec2(), 0, 0, 0, 0, .1, // pos, angle, emitSize, emitTime, emitRate, emiteCone
214
- undefined, undefined, // tileIndex, tileSize
214
+ 0, // tileInfo
215
215
  new Color(1,.8,.5), new Color(.9,.7,.5), // colorStartA, colorStartB
216
216
  new Color(1,.8,.5), new Color(.9,.7,.5), // colorEndA, colorEndB
217
- 3, .1, .1, .15, .1, // particleTime, sizeStart, sizeEnd, particleSpeed, particleAngleSpeed
218
- 1, .95, 1, 0, 0, // damping, angleDamping, gravityScale, particleCone, fadeRate,
219
- .1, 1 // randomness, collide, additive, randomColorLinear, renderOrder
217
+ 3, .1, .1, .15, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
218
+ 1, .95, 1, 0, 0, // damp, angleDamp, gravity, particleCone, fade
219
+ .1, 1 // randomness, collide, additive, colorLinear, renderOrder
220
220
  ), vec2(.1,0), -.8);
221
221
  this.shellEmitter.elasticity = .5;
222
222
  this.shellEmitter.particleDestroyCallback = persistentParticleDestroyCallback;
@@ -322,12 +322,12 @@ class Bullet extends EngineObject
322
322
  // spark effects
323
323
  const emitter = new ParticleEmitter(
324
324
  this.pos, 0, 0, .1, 100, .5, // pos, angle, emitSize, emitTime, emitRate, emiteCone
325
- undefined, undefined, // tileIndex, tileSize
325
+ 0, // tileInfo
326
326
  new Color(1,1,0), new Color(1,0,0), // colorStartA, colorStartB
327
327
  new Color(1,1,0), new Color(1,0,0), // colorEndA, colorEndB
328
- .2, .2, 0, .1, .1, // particleTime, sizeStart, sizeEnd, particleSpeed, particleAngleSpeed
329
- 1, 1, .5, PI, .1, // damping, angleDamping, gravityScale, particleCone, fadeRate,
330
- .5, 1, 1 // randomness, collide, additive, randomColorLinear, renderOrder
328
+ .2, .2, 0, .1, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
329
+ 1, 1, .5, PI, .1, // damp, angleDamp, gravityScale, particleCone, fade,
330
+ .5, 1, 1 // randomness, collide, additive, colorLinear, renderOrder
331
331
  );
332
332
  emitter.trailScale = 1;
333
333
  emitter.elasticity = .3;
@@ -13,7 +13,7 @@ class Character extends GameObject
13
13
  {
14
14
  constructor(pos)
15
15
  {
16
- super(pos, vec2(.6,.95), 32);
16
+ super(pos, vec2(.6,.95), tile());
17
17
 
18
18
  this.weapon = new Weapon(pos, this);
19
19
  this.lastPos = pos;
@@ -211,8 +211,9 @@ class Character extends GameObject
211
211
  this.mirror = moveInput.x < 0;
212
212
 
213
213
  // set tile to use
214
- this.tileIndex = this.isDead() ? 3 :
214
+ const tile = this.isDead() ? 3 :
215
215
  this.climbingLadder || this.groundTimer.active() ? 3 + 2*this.walkCyclePercent|0 : 4;
216
+ this.tileInfo.pos.x = tile * 16;
216
217
  }
217
218
 
218
219
  render()
@@ -226,7 +227,7 @@ class Character extends GameObject
226
227
  // make bottom flush
227
228
  bodyPos = bodyPos.add(vec2(0,(this.drawSize.y-this.size.y)/2));
228
229
  }
229
- drawTile(bodyPos, this.drawSize, this.tileIndex, this.tileSize, this.color, this.angle, this.mirror);
230
+ drawTile(bodyPos, this.drawSize, this.tileInfo, this.color, this.angle, this.mirror);
230
231
  }
231
232
 
232
233
  damage(damage, damagingObject)
@@ -6,9 +6,9 @@
6
6
  <link rel=icon type=image/png href=../favicon.png>
7
7
  </head><body>
8
8
 
9
- <script src=../../build/littlejs.js?1713></script>
10
- <script src=gameObjects.js?1713></script>
11
- <script src=gamePlayer.js?1713></script>
12
- <script src=gameEffects.js?1713></script>
13
- <script src=gameLevel.js?1713></script>
14
- <script src=game.js?1713></script>
9
+ <script src=../../build/littlejs.js?183></script>
10
+ <script src=gameObjects.js?183></script>
11
+ <script src=gamePlayer.js?183></script>
12
+ <script src=gameEffects.js?183></script>
13
+ <script src=gameLevel.js?183></script>
14
+ <script src=game.js?183></script>
Binary file
@@ -61,7 +61,7 @@ function gameInit()
61
61
  // setup game
62
62
  cameraPos = levelSize.scale(.5).add(cameraOffset);
63
63
  cameraScale = 900/levelSize.y;
64
- gravity = -.005;
64
+ gravity = -.004;
65
65
  fallTimer = new Timer;
66
66
  comboCount = score = 0;
67
67
  }
@@ -208,7 +208,7 @@ function gameRender()
208
208
 
209
209
  // use darker color for icon
210
210
  const color2 = color.scale(.8, 1);
211
- drawTile(drawPos, vec2(.5), data, vec2(64), color2);
211
+ drawTile(drawPos, vec2(.5), tile(data, 64), color2);
212
212
  }
213
213
 
214
214
  // draw a grey square at top to cover up incomming tiles
@@ -225,7 +225,7 @@ function gameRenderPost()
225
225
 
226
226
  ///////////////////////////////////////////////////////////////////////////////
227
227
  // Startup LittleJS Engine
228
- engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, 'tiles.png');
228
+ engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);
229
229
 
230
230
  ///////////////////////////////////////////////////////////////////////////////
231
231
  // find and remove all runs of 3 or higher
@@ -288,13 +288,14 @@ function clearMatches()
288
288
  const color1 = tileColors[data];
289
289
  const color2 = color1.lerp(new Color, .5);
290
290
  new ParticleEmitter(
291
- pos.add(vec2(.5)), 0, 1, .1, 100, PI,// pos, angle, emitSize, emitTime, emitRate, emiteCone
292
- undefined, undefined, // tileIndex, tileSize
291
+ pos.add(vec2(.5)), 0, // pos, angle
292
+ .5, .1, 200, PI, // emitSize, emitTime, emitRate, emiteCone
293
+ 0, // tileInfo
293
294
  color1, color2, // colorStartA, colorStartB
294
295
  color1.scale(1,0), color2.scale(1,0),// colorEndA, colorEndB
295
- .5, .3, .3, .05, .05, // particleTime, sizeStart, sizeEnd, particleSpeed, particleAngleSpeed
296
- .99, 1, 1, PI, .05, // damping, angleDamping, gravityScale, particleCone, fadeRate,
297
- .5, 0, 1 // randomness, collide, additive, randomColorLinear, renderOrder
296
+ .5, .3, .2, .05, .05, // particleTime, sizeStart, sizeEnd, speed, angleSpeed
297
+ .99, 1, 1, PI, .05, // damp, angleDamp, gravityScale, particleCone, fadeRate
298
+ .5, 0, 1 // randomness, collide, additive, colorLinear, renderOrder
298
299
  );
299
300
  }
300
301
  }
@@ -303,6 +304,7 @@ function clearMatches()
303
304
  {
304
305
  score += ++comboCount*removedCount;
305
306
  fallTimer.set();
307
+ levelFall = [];
306
308
  }
307
309
  else
308
310
  comboCount = 0;
@@ -6,5 +6,5 @@
6
6
  <link rel=icon type=image/png href=../favicon.png>
7
7
  </head><body>
8
8
 
9
- <script src=../../build/littlejs.js?1713></script>
10
- <script src=game.js?1713></script>
9
+ <script src=../../build/littlejs.js?183></script>
10
+ <script src=game.js?183></script>
Binary file
@@ -21,41 +21,47 @@ let particleEmitter;
21
21
  function gameInit()
22
22
  {
23
23
  // create tile collision and visible tile layer
24
- initTileCollision(vec2(32, 16));
24
+ initTileCollision(vec2(32,16));
25
25
  const pos = vec2();
26
26
  const tileLayer = new TileLayer(pos, tileCollisionSize);
27
27
 
28
28
  // get level data from the tiles image
29
- const imageLevelDataRow = 1;
30
- mainContext.drawImage(tileImage, 0, 0);
29
+ const tileImage = textureInfos[0].image;
30
+ mainContext.drawImage(tileImage,0,0);
31
+ const imageData = mainContext.getImageData(0,0,tileImage.width,tileImage.height).data;
31
32
  for (pos.x = tileCollisionSize.x; pos.x--;)
32
33
  for (pos.y = tileCollisionSize.y; pos.y--;)
33
34
  {
34
- const imageData = mainContext.getImageData(pos.x, 16*(imageLevelDataRow+1)-pos.y-1, 1, 1).data;
35
- if (imageData[0])
36
- {
37
- const tileIndex = 1;
38
- const direction = randInt(4)
39
- const mirror = randInt(2);
40
- const color = randColor();
41
- const data = new TileLayerData(tileIndex, direction, mirror, color);
42
- tileLayer.setData(pos, data);
43
- setTileCollisionData(pos, 1);
44
- }
35
+ // check if this pixel is set
36
+ const i = pos.x + tileImage.width*(15 + tileCollisionSize.y - pos.y);
37
+ if (!imageData[4*i])
38
+ continue;
39
+
40
+ // set tile data
41
+ const tileIndex = 1;
42
+ const direction = randInt(4)
43
+ const mirror = randInt(2);
44
+ const color = randColor();
45
+ const data = new TileLayerData(tileIndex, direction, mirror, color);
46
+ tileLayer.setData(pos, data);
47
+ setTileCollisionData(pos, 1);
45
48
  }
49
+
50
+ // draw tile layer with new data
46
51
  tileLayer.redraw();
47
52
 
48
- // move camera to center of collision
49
- cameraPos = tileCollisionSize.scale(.5);
53
+ // setup camera
54
+ cameraPos = vec2(16,8);
55
+ cameraScale = 48;
50
56
 
51
57
  // enable gravity
52
58
  gravity = -.01;
53
59
 
54
60
  // create particle emitter
55
61
  particleEmitter = new ParticleEmitter(
56
- vec2(16), 0, // emitPos, emitAngle
62
+ vec2(16,15), 0, // emitPos, emitAngle
57
63
  1, 0, 500, PI, // emitSize, emitTime, emitRate, emiteCone
58
- 0, vec2(16), // tileIndex, tileSize
64
+ tile(0, 16), // tileIndex, tileSize
59
65
  new Color(1,1,1), new Color(0,0,0), // colorStartA, colorStartB
60
66
  new Color(1,1,1,0), new Color(0,0,0,0), // colorEndA, colorEndB
61
67
  2, .2, .2, .1, .05, // time, sizeStart, sizeEnd, speed, angleSpeed
@@ -99,16 +105,21 @@ function gameUpdatePost()
99
105
  function gameRender()
100
106
  {
101
107
  // draw a grey square in the background without using webgl
102
- drawRect(cameraPos, tileCollisionSize.add(vec2(5)), new Color(.4,.4,.4), 0, 0);
108
+ drawRect(vec2(16,8), vec2(20,14), hsl(0,0,.6), 0, 0);
109
+
110
+ // draw the logo as a tile
111
+ drawTile(vec2(21,5), vec2(4.5), tile(2));
103
112
  }
104
113
 
105
114
  ///////////////////////////////////////////////////////////////////////////////
106
115
  function gameRenderPost()
107
116
  {
108
117
  // draw to overlay canvas for hud rendering
109
- drawTextScreen('LittleJS Engine Demo', vec2(mainCanvasSize.x/2, 80), 80);
118
+ drawTextScreen('LittleJS Engine Demo',
119
+ vec2(mainCanvasSize.x/2, 70), 80, // position, size
120
+ hsl(0,0,1), 6, hsl(0,0,0)); // color, outline size and color
110
121
  }
111
122
 
112
123
  ///////////////////////////////////////////////////////////////////////////////
113
124
  // Startup LittleJS Engine
114
- engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, 'tiles.png');
125
+ engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);
@@ -7,18 +7,18 @@
7
7
  </head><body>
8
8
 
9
9
  <!-- LittleJS Engine -->
10
- <script src=../../src/engineDebug.js?1713></script>
11
- <script src=../../src/engineUtilities.js?1713></script>
12
- <script src=../../src/engineSettings.js?1713></script>
13
- <script src=../../src/engineObject.js?1713></script>
14
- <script src=../../src/engineDraw.js?1713></script>
15
- <script src=../../src/engineInput.js?1713></script>
16
- <script src=../../src/engineAudio.js?1713></script>
17
- <script src=../../src/engineTileLayer.js?1713></script>
18
- <script src=../../src/engineParticles.js?1713></script>
19
- <script src=../../src/engineMedals.js?1713></script>
20
- <script src=../../src/engineWebGL.js?1713></script>
21
- <script src=../../src/engine.js?1713></script>
10
+ <script src=../../src/engineDebug.js?183></script>
11
+ <script src=../../src/engineUtilities.js?183></script>
12
+ <script src=../../src/engineSettings.js?183></script>
13
+ <script src=../../src/engineObject.js?183></script>
14
+ <script src=../../src/engineDraw.js?183></script>
15
+ <script src=../../src/engineInput.js?183></script>
16
+ <script src=../../src/engineAudio.js?183></script>
17
+ <script src=../../src/engineTileLayer.js?183></script>
18
+ <script src=../../src/engineParticles.js?183></script>
19
+ <script src=../../src/engineMedals.js?183></script>
20
+ <script src=../../src/engineWebGL.js?183></script>
21
+ <script src=../../src/engine.js?183></script>
22
22
 
23
23
  <!-- Add your game scripts here -->
24
- <script src=game.js?1713></script>
24
+ <script src=game.js?183></script>
Binary file
@@ -6,7 +6,8 @@
6
6
  <link rel=icon type=image/png href=../favicon.png>
7
7
  </head><body>
8
8
 
9
- <script src=../../build/littlejs.min.js?1713></script>
9
+ <!-- LittleJS Engine -->
10
+ <script src=../../build/littlejs.min.js?183></script>
10
11
  <script>
11
12
 
12
13
  /*
@@ -27,8 +28,7 @@ class TestObject extends EngineObject
27
28
  {
28
29
  constructor(pos)
29
30
  {
30
- super(pos, vec2(1), 0, vec2(16), 0, spriteColor);
31
-
31
+ super(pos, vec2(1), tile(0), 0, spriteColor);
32
32
  this.additiveColor = spriteAdditiveColor;
33
33
  this.setCollision();
34
34
  }
@@ -147,7 +147,7 @@ const tilesImageData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCA
147
147
 
148
148
  ///////////////////////////////////////////////////////////////////////////////
149
149
  // Startup LittleJS Engine
150
- engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, tilesImageData);
150
+ engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, [tilesImageData]);
151
151
 
152
152
  ///////////////////////////////////////////////////////////////////////////////
153
153
  // music - Depp Loop
@@ -24,7 +24,8 @@ function gameInit() {
24
24
  // get level data from the tiles image
25
25
  const imageLevelDataRow = 1;
26
26
  const mainContext = LittleJS.mainContext;
27
- mainContext.drawImage(LittleJS.tileImage, 0, 0);
27
+ const tileImage = LittleJS.textureInfos[0].image;
28
+ mainContext.drawImage(tileImage, 0, 0);
28
29
  for (pos.x = LittleJS.tileCollisionSize.x; pos.x--;)
29
30
  for (pos.y = LittleJS.tileCollisionSize.y; pos.y--;) {
30
31
  const imageData = mainContext.getImageData(pos.x, 16 * (imageLevelDataRow + 1) - pos.y - 1, 1, 1).data;
@@ -47,7 +48,7 @@ function gameInit() {
47
48
  const center = LittleJS.tileCollisionSize.scale(.5).add(vec2(0, 9));
48
49
  particleEmitter = new LittleJS.ParticleEmitter(center, 0, // emitPos, emitAngle
49
50
  1, 0, 500, Math.PI, // emitSize, emitTime, emitRate, emiteCone
50
- 0, vec2(16), // tileIndex, tileSize
51
+ LittleJS.tile(0, 16), // tileIndex, tileSize
51
52
  new Color(1, 1, 1), new Color(0, 0, 0), // colorStartA, colorStartB
52
53
  new Color(1, 1, 1, 0), new Color(0, 0, 0, 0), // colorEndA, colorEndB
53
54
  2, .2, .2, .1, .05, // time, sizeStart, sizeEnd, speed, angleSpeed
@@ -91,4 +92,4 @@ function gameRenderPost() {
91
92
  }
92
93
  ///////////////////////////////////////////////////////////////////////////////
93
94
  // Startup LittleJS Engine
94
- LittleJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, 'tiles.png');
95
+ LittleJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);
@@ -8,7 +8,7 @@
8
8
 
9
9
  // import module
10
10
  import * as LittleJS from '../../build/littlejs.esm.js';
11
- const {Vector2, Color, Timer, vec2} = LittleJS;
11
+ const {Vector2, Color, Timer, vec2, tile} = LittleJS;
12
12
 
13
13
  // sound effects
14
14
  const sound_click = new LittleJS.Sound([1,.5]);
@@ -32,7 +32,8 @@ function gameInit()
32
32
  // get level data from the tiles image
33
33
  const imageLevelDataRow = 1;
34
34
  const mainContext = LittleJS.mainContext;
35
- mainContext.drawImage(LittleJS.tileImage, 0, 0);
35
+ const tileImage = LittleJS.textureInfos[0].image;
36
+ mainContext.drawImage(tileImage, 0, 0);
36
37
  for (pos.x = LittleJS.tileCollisionSize.x; pos.x--;)
37
38
  for (pos.y = LittleJS.tileCollisionSize.y; pos.y--;)
38
39
  {
@@ -61,7 +62,7 @@ function gameInit()
61
62
  particleEmitter = new LittleJS.ParticleEmitter(
62
63
  center, 0, // emitPos, emitAngle
63
64
  1, 0, 500, Math.PI, // emitSize, emitTime, emitRate, emiteCone
64
- 0, vec2(16), // tileIndex, tileSize
65
+ LittleJS.tile(0, 16), // tileIndex, tileSize
65
66
  new Color(1,1,1), new Color(0,0,0), // colorStartA, colorStartB
66
67
  new Color(1,1,1,0), new Color(0,0,0,0), // colorEndA, colorEndB
67
68
  2, .2, .2, .1, .05, // time, sizeStart, sizeEnd, speed, angleSpeed
@@ -120,4 +121,4 @@ function gameRenderPost()
120
121
 
121
122
  ///////////////////////////////////////////////////////////////////////////////
122
123
  // Startup LittleJS Engine
123
- LittleJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, 'tiles.png');
124
+ LittleJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);
@@ -7,4 +7,4 @@
7
7
  </head><body>
8
8
 
9
9
  <!-- Add your game scripts here -->
10
- <script src=game.js?1713 type=module></script>
10
+ <script src=game.js?183 type=module></script>
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "littlejsengine",
3
- "version": "1.7.21",
3
+ "version": "1.8.3",
4
4
  "description": "LittleJS - Tiny and Fast HTML5 Game Engine",
5
5
  "main": "build/littlejs.esm.js",
6
+ "types": "build/littlejs.esm.js",
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "git+https://github.com/KilledByAPixel/LittleJS.git"
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.7.21';
33
+ const engineVersion = '1.8.3';
34
34
 
35
35
  /** Frames per second to update objects
36
36
  * @type {Number}
@@ -80,6 +80,9 @@ let paused = 0;
80
80
  * @memberof Engine */
81
81
  function setPaused(_paused) { paused = _paused; }
82
82
 
83
+ // Frame time tracking
84
+ let frameTimeLastMS = 0, frameTimeBufferMS = 0, averageFPS = 0;
85
+
83
86
  ///////////////////////////////////////////////////////////////////////////////
84
87
 
85
88
  /** Start up LittleJS engine with your callback functions
@@ -88,52 +91,13 @@ function setPaused(_paused) { paused = _paused; }
88
91
  * @param {Function} gameUpdatePost - Called after physics and objects are updated, setup camera and prepare for render
89
92
  * @param {Function} gameRender - Called before objects are rendered, draw any background effects that appear behind objects
90
93
  * @param {Function} gameRenderPost - Called after objects are rendered, draw effects or hud that appear above all objects
91
- * @param {String} [tileImageSource] - Tile image to use, everything starts when the image is finished loading
94
+ * @param {Array} [imageSources=['tiles.png']] - Image to load
92
95
  * @memberof Engine */
93
- function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, tileImageSource)
96
+ function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources=['tiles.png'])
94
97
  {
95
- // init engine when tiles load or fail to load
96
- tileImage.onerror = tileImage.onload = ()=>
97
- {
98
- // save tile image info
99
- tileImageFixBleed = vec2(tileFixBleedScale).divide(tileImageSize = vec2(tileImage.width, tileImage.height));
100
- debug && (tileImage.onload=()=>ASSERT(1)); // tile sheet can not reloaded
101
-
102
- // setup html
103
- const styleBody =
104
- 'margin:0;overflow:hidden;' + // fill the window
105
- 'background:#000;' + // set background color
106
- 'touch-action:none;' + // prevent mobile pinch to resize
107
- 'user-select:none;' + // prevent mobile hold to select
108
- '-webkit-user-select:none;' + // compatibility for ios
109
- '-webkit-touch-callout:none'; // compatibility for ios
110
- document.body.style = styleBody;
111
- document.body.appendChild(mainCanvas = document.createElement('canvas'));
112
- mainContext = mainCanvas.getContext('2d');
113
-
114
- // init stuff and start engine
115
- debugInit();
116
- glEnable && glInit();
117
-
118
- // create overlay canvas for hud to appear above gl canvas
119
- document.body.appendChild(overlayCanvas = document.createElement('canvas'));
120
- overlayContext = overlayCanvas.getContext('2d');
121
-
122
- // set canvas style
123
- const styleCanvas =
124
- 'position:absolute;' + // position canvas
125
- 'top:50%;left:50%;transform:translate(-50%,-50%);' + // center the canvas
126
- (canvasPixelated?'image-rendering:pixelated':''); // set pixelated rendering
127
- (glCanvas||mainCanvas).style = mainCanvas.style = overlayCanvas.style = styleCanvas;
128
-
129
- gameInit();
130
- engineUpdate();
131
- };
132
-
133
- // frame time tracking
134
- let frameTimeLastMS = 0, frameTimeBufferMS = 0, averageFPS = 0;
98
+ ASSERT(Array.isArray(imageSources)); // pass in images as array
135
99
 
136
- // main update loop
100
+ // internal update loop for engine
137
101
  function engineUpdate(frameTimeMS=0)
138
102
  {
139
103
  // update time keeping
@@ -164,9 +128,9 @@ function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRender
164
128
  }
165
129
  else
166
130
  {
167
- // clear canvas and set size to same as window
168
- mainCanvas.width = min(innerWidth, canvasMaxSize.x);
169
- mainCanvas.height = min(innerHeight, canvasMaxSize.y);
131
+ // clear canvas and set size to same as window
132
+ mainCanvas.width = min(innerWidth, canvasMaxSize.x);
133
+ mainCanvas.height = min(innerHeight, canvasMaxSize.y);
170
134
  }
171
135
 
172
136
  // clear overlay canvas and set size
@@ -198,6 +162,9 @@ function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRender
198
162
  // update multiple frames if necessary in case of slow framerate
199
163
  for (;frameTimeBufferMS >= 0; frameTimeBufferMS -= 1e3 / frameRate)
200
164
  {
165
+ // increment frame and update time
166
+ time = frame++ / frameRate;
167
+
201
168
  // update game and objects
202
169
  inputUpdate();
203
170
  gameUpdate();
@@ -245,8 +212,51 @@ function engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRender
245
212
  requestAnimationFrame(engineUpdate);
246
213
  }
247
214
 
248
- // set tile image source to load the image and start the engine
249
- tileImageSource ? tileImage.src = tileImageSource : tileImage.onload();
215
+ // setup html
216
+ const styleBody =
217
+ 'margin:0;overflow:hidden;' + // fill the window
218
+ 'background:#000;' + // set background color
219
+ 'touch-action:none;' + // prevent mobile pinch to resize
220
+ 'user-select:none;' + // prevent mobile hold to select
221
+ '-webkit-user-select:none;' + // compatibility for ios
222
+ '-webkit-touch-callout:none'; // compatibility for ios
223
+ document.body.style = styleBody;
224
+ document.body.appendChild(mainCanvas = document.createElement('canvas'));
225
+ mainContext = mainCanvas.getContext('2d');
226
+
227
+ // init stuff and start engine
228
+ debugInit();
229
+ glEnable && glInit();
230
+
231
+ // create overlay canvas for hud to appear above gl canvas
232
+ document.body.appendChild(overlayCanvas = document.createElement('canvas'));
233
+ overlayContext = overlayCanvas.getContext('2d');
234
+
235
+ // set canvas style
236
+ const styleCanvas =
237
+ 'position:absolute;' + // position
238
+ 'top:50%;left:50%;transform:translate(-50%,-50%);' + // center
239
+ (canvasPixelated?'image-rendering:pixelated':''); // pixelated rendering
240
+ (glCanvas||mainCanvas).style = mainCanvas.style = overlayCanvas.style = styleCanvas;
241
+
242
+ // load all of the images
243
+ Promise.all(imageSources.map((src, textureIndex)=>
244
+ new Promise((resolve, reject)=>
245
+ {
246
+ const image = new Image;
247
+ image.onerror = image.onload = ()=>
248
+ {
249
+ textureInfos[textureIndex] = new TextureInfo(image);
250
+ resolve();
251
+ }
252
+ image.src = src;
253
+ })
254
+ )).then(()=>
255
+ {
256
+ // start the engine
257
+ gameInit();
258
+ engineUpdate();
259
+ });
250
260
  }
251
261
 
252
262
  // Called automatically by engine to setup render system
@@ -284,9 +294,6 @@ function engineObjectsUpdate()
284
294
 
285
295
  // remove destroyed objects
286
296
  engineObjects = engineObjects.filter(o=>!o.destroyed);
287
-
288
- // increment frame and update time
289
- time = ++frame / frameRate;
290
297
  }
291
298
 
292
299
  /** Destroy and remove all objects
@@ -45,7 +45,8 @@ class Sound
45
45
  if (zzfxSound)
46
46
  {
47
47
  // generate zzfx sound now for fast playback
48
- this.randomness = zzfxSound[1] || (zzfxSound[1] = 0);
48
+ this.randomness = zzfxSound[1];
49
+ zzfxSound[1] = 0; // generate without randomness
49
50
  this.sampleChannels = [zzfxG(...zzfxSound)];
50
51
  this.sampleRate = zzfxR;
51
52
  }