littlejsengine 1.11.17 → 1.12.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/littlejs.d.ts +112 -43
  2. package/dist/littlejs.esm.js +586 -500
  3. package/dist/littlejs.esm.min.js +1 -1
  4. package/dist/littlejs.js +2993 -122
  5. package/dist/littlejs.min.js +1 -1
  6. package/dist/littlejs.release.js +2992 -119
  7. package/examples/box2d/game.js +70 -39
  8. package/examples/box2d/gameObjects.js +81 -77
  9. package/examples/box2d/index.html +2 -7
  10. package/examples/box2d/scenes.js +80 -90
  11. package/examples/breakout/game.js +63 -28
  12. package/examples/breakout/gameObjects.js +45 -47
  13. package/examples/breakout/index.html +1 -5
  14. package/examples/breakoutTutorial/game.js +36 -29
  15. package/examples/breakoutTutorial/index.html +1 -3
  16. package/examples/empty/game.js +5 -2
  17. package/examples/empty/index.html +1 -3
  18. package/examples/htmlMenu/game.js +24 -15
  19. package/examples/htmlMenu/index.html +5 -7
  20. package/examples/index.html +2 -3
  21. package/examples/module/game.js +32 -34
  22. package/examples/module/index.html +1 -1
  23. package/examples/particles/index.html +27 -22
  24. package/examples/platformer/game.js +71 -45
  25. package/examples/platformer/gameCharacter.js +42 -34
  26. package/examples/platformer/gameEffects.js +82 -75
  27. package/examples/platformer/gameLevel.js +67 -38
  28. package/examples/platformer/{gameLevelData.js → gameLevelData.json} +1 -11
  29. package/examples/platformer/gameObjects.js +70 -64
  30. package/examples/platformer/gamePlayer.js +12 -7
  31. package/examples/platformer/index.html +1 -9
  32. package/examples/puzzle/game.js +59 -40
  33. package/examples/puzzle/index.html +1 -3
  34. package/examples/shorts/base.html +2 -2
  35. package/examples/shorts/particles.js +1 -1
  36. package/examples/shorts/platformer.js +1 -1
  37. package/examples/shorts/tileLayer.js +5 -6
  38. package/examples/shorts/tiles.png +0 -0
  39. package/examples/starter/game.js +9 -12
  40. package/examples/starter/index.html +13 -13
  41. package/examples/stress/index.html +44 -44
  42. package/examples/typescript/build.js +17 -18
  43. package/examples/typescript/game.js +32 -34
  44. package/examples/typescript/game.ts +32 -34
  45. package/examples/typescript/index.html +1 -1
  46. package/examples/uiSystem/game.js +34 -31
  47. package/examples/uiSystem/index.html +1 -5
  48. package/package.json +1 -1
  49. package/plugins/box2d.js +6 -2
  50. package/plugins/pluginExport.js +1 -2
  51. package/reference.md +29 -27
  52. package/src/engine.js +1 -1
  53. package/src/engineBuild.js +13 -6
  54. package/src/engineDebug.js +1 -3
  55. package/src/engineExport.js +2 -6
  56. package/src/engineInput.js +3 -1
  57. package/src/engineObject.js +18 -14
  58. package/src/engineSettings.js +6 -6
  59. package/src/engineTileLayer.js +179 -96
  60. package/examples/typescript/build/dist/littlejs.esm.js +0 -4834
  61. package/examples/typescript/build/examples/typescript/build.js +0 -24
  62. package/examples/typescript/build/examples/typescript/game.js +0 -102
@@ -8,7 +8,13 @@
8
8
 
9
9
  'use strict';
10
10
 
11
- class GameObject extends EngineObject
11
+ // import LittleJS module
12
+ import * as LJS from '../../dist/littlejs.esm.js';
13
+ import * as GameEffects from './gameEffects.js';
14
+ import * as Game from './game.js';
15
+ const {vec2, hsl, rgb, Timer} = LJS;
16
+
17
+ export class GameObject extends LJS.EngineObject
12
18
  {
13
19
  constructor(pos, size, tileInfo, angle)
14
20
  {
@@ -25,7 +31,7 @@ class GameObject extends EngineObject
25
31
  // flash white when damaged
26
32
  let brightness = 0;
27
33
  if (!this.isDead() && this.damageTimer.isSet())
28
- brightness = .5*percent(this.damageTimer, .15, 0);
34
+ brightness = .5*LJS.percent(this.damageTimer, .15, 0);
29
35
  this.additiveColor = hsl(0,0,brightness,0);
30
36
 
31
37
  // kill if below level
@@ -35,7 +41,7 @@ class GameObject extends EngineObject
35
41
 
36
42
  damage(damage, damagingObject)
37
43
  {
38
- ASSERT(damage >= 0);
44
+ LJS.ASSERT(damage >= 0);
39
45
  if (this.isDead())
40
46
  return 0;
41
47
 
@@ -45,7 +51,7 @@ class GameObject extends EngineObject
45
51
  child.damageTimer && child.damageTimer.set();
46
52
 
47
53
  // apply damage and kill if necessary
48
- const newHealth = max(this.health - damage, 0);
54
+ const newHealth = LJS.max(this.health - damage, 0);
49
55
  if (!newHealth)
50
56
  this.kill(damagingObject);
51
57
 
@@ -59,13 +65,13 @@ class GameObject extends EngineObject
59
65
 
60
66
  ///////////////////////////////////////////////////////////////////////////////
61
67
 
62
- class Crate extends GameObject
68
+ export class Crate extends GameObject
63
69
  {
64
70
  constructor(pos)
65
71
  {
66
- super(pos, vec2(.999), spriteAtlas.crate, (randInt(4))*PI/2);
72
+ super(pos, vec2(.999), Game.spriteAtlas.crate, (LJS.randInt(4))*LJS.PI/2);
67
73
 
68
- this.color = hsl(rand(),1,.8);
74
+ this.color = hsl(LJS.rand(),1,.8);
69
75
  this.health = 5;
70
76
 
71
77
  // make it a solid object for collision
@@ -77,59 +83,59 @@ class Crate extends GameObject
77
83
  if (this.destroyed)
78
84
  return;
79
85
 
80
- sound_destroyObject.play(this.pos);
81
- makeDebris(this.pos, this.color, 30);
86
+ GameEffects.sound_destroyObject.play(this.pos);
87
+ GameEffects.makeDebris(this.pos, this.color, 30);
82
88
  this.destroy();
83
89
  }
84
90
  }
85
91
 
86
92
  ///////////////////////////////////////////////////////////////////////////////
87
93
 
88
- class Coin extends EngineObject
94
+ export class Coin extends LJS.EngineObject
89
95
  {
90
96
  constructor(pos)
91
97
  {
92
- super(pos, vec2(1), spriteAtlas.coin);
98
+ super(pos, vec2(1), Game.spriteAtlas.coin);
93
99
  this.color = hsl(.15,1,.5);
94
100
  }
95
101
 
96
102
  render()
97
103
  {
98
104
  // make it appear to spin
99
- const t = time+this.pos.x/4+this.pos.y/4;
100
- drawTile(this.pos, vec2(.1, .6), 0, this.color); // edge of coin
101
- drawTile(this.pos, vec2(.5+.5*Math.sin(t*2*PI), 1), this.tileInfo, this.color);
105
+ const t = LJS.time+this.pos.x/4+this.pos.y/4;
106
+ LJS.drawTile(this.pos, vec2(.1, .6), 0, this.color); // edge of coin
107
+ LJS.drawTile(this.pos, vec2(.5+.5*Math.sin(t*2*LJS.PI), 1), this.tileInfo, this.color);
102
108
  }
103
109
 
104
110
  update()
105
111
  {
106
- if (!player)
112
+ if (!Game.player)
107
113
  return;
108
114
 
109
115
  // check if player in range
110
- const d = this.pos.distanceSquared(player.pos);
116
+ const d = this.pos.distanceSquared(Game.player.pos);
111
117
  if (d > .5)
112
118
  return;
113
119
 
114
120
  // award points and destroy
115
- ++score;
116
- sound_score.play(this.pos);
121
+ Game.addToScore();
122
+ GameEffects.sound_score.play(this.pos);
117
123
  this.destroy();
118
124
  }
119
125
  }
120
126
 
121
127
  ///////////////////////////////////////////////////////////////////////////////
122
128
 
123
- class Enemy extends GameObject
129
+ export class Enemy extends GameObject
124
130
  {
125
131
  constructor(pos)
126
132
  {
127
- super(pos, vec2(.9,.9), spriteAtlas.enemy);
133
+ super(pos, vec2(.9,.9), Game.spriteAtlas.enemy);
128
134
 
129
135
  this.drawSize = vec2(1);
130
- this.color = hsl(rand(), 1, .7);
136
+ this.color = hsl(LJS.rand(), 1, .7);
131
137
  this.health = 5;
132
- this.bounceTime = new Timer(rand(1e3));
138
+ this.bounceTime = new Timer(LJS.rand(1e3));
133
139
  this.setCollision(true, false);
134
140
  }
135
141
 
@@ -137,16 +143,16 @@ class Enemy extends GameObject
137
143
  {
138
144
  super.update();
139
145
 
140
- if (!player)
146
+ if (!Game.player)
141
147
  return;
142
148
 
143
149
  // jump around randomly
144
- if (this.groundObject && rand() < .01 && this.pos.distance(player.pos) < 20)
145
- this.velocity = vec2(rand(.1,-.1), rand(.4,.2));
150
+ if (this.groundObject && LJS.rand() < .01 && this.pos.distance(Game.player.pos) < 20)
151
+ this.velocity = vec2(LJS.rand(.1,-.1), LJS.rand(.4,.2));
146
152
 
147
153
  // damage player if touching
148
- if (isOverlapping(this.pos, this.size, player.pos, player.size))
149
- player.damage(1, this);
154
+ if (LJS.isOverlapping(this.pos, this.size, Game.player.pos, Game.player.size))
155
+ Game.player.damage(1, this);
150
156
  }
151
157
 
152
158
  kill()
@@ -154,9 +160,9 @@ class Enemy extends GameObject
154
160
  if (this.destroyed)
155
161
  return;
156
162
 
157
- ++score;
158
- sound_score.play(this.pos);
159
- makeDebris(this.pos, this.color);
163
+ Game.addToScore();
164
+ GameEffects.sound_score.play(this.pos);
165
+ GameEffects.makeDebris(this.pos, this.color);
160
166
  this.destroy();
161
167
  }
162
168
 
@@ -169,17 +175,17 @@ class Enemy extends GameObject
169
175
  // make bottom flush
170
176
  let bodyPos = this.pos;
171
177
  bodyPos = bodyPos.add(vec2(0,(this.drawSize.y-this.size.y)/2));
172
- drawTile(bodyPos, this.drawSize, this.tileInfo, this.color, this.angle, this.mirror, this.additiveColor);
178
+ LJS.drawTile(bodyPos, this.drawSize, this.tileInfo, this.color, this.angle, this.mirror, this.additiveColor);
173
179
  }
174
180
  }
175
181
 
176
182
  ///////////////////////////////////////////////////////////////////////////////
177
183
 
178
- class Grenade extends GameObject
184
+ export class Grenade extends GameObject
179
185
  {
180
186
  constructor(pos)
181
187
  {
182
- super(pos, vec2(.2), spriteAtlas.grenade);
188
+ super(pos, vec2(.2), Game.spriteAtlas.grenade);
183
189
 
184
190
  this.beepTimer = new Timer(1);
185
191
  this.elasticity = .3;
@@ -195,35 +201,35 @@ class Grenade extends GameObject
195
201
 
196
202
  if (this.getAliveTime() > 3)
197
203
  {
198
- explosion(this.pos);
204
+ GameEffects.explosion(this.pos);
199
205
  this.destroy();
200
206
  }
201
207
  else if (this.beepTimer.elapsed())
202
208
  {
203
- sound_grenade.play(this.pos)
209
+ GameEffects.sound_grenade.play(this.pos)
204
210
  this.beepTimer.set(1);
205
211
  }
206
212
  }
207
213
 
208
214
  render()
209
215
  {
210
- drawTile(this.pos, vec2(.5), this.tileInfo, this.color, this.angle);
216
+ LJS.drawTile(this.pos, vec2(.5), this.tileInfo, this.color, this.angle);
211
217
 
212
218
  // draw additive flash exploding
213
- setBlendMode(true);
214
- const flash = Math.cos(this.getAliveTime()*2*PI);
215
- drawTile(this.pos, vec2(2), spriteAtlas.circle, hsl(0,1,.5,.2-.2*flash));
216
- setBlendMode(false);
219
+ LJS.setBlendMode(true);
220
+ const flash = Math.cos(this.getAliveTime()*2*LJS.PI);
221
+ LJS.drawTile(this.pos, vec2(2), Game.spriteAtlas.circle, hsl(0,1,.5,.2-.2*flash));
222
+ LJS.setBlendMode(false);
217
223
  }
218
224
  }
219
225
 
220
226
  ///////////////////////////////////////////////////////////////////////////////
221
227
 
222
- class Weapon extends EngineObject
228
+ export class Weapon extends LJS.EngineObject
223
229
  {
224
230
  constructor(pos, parent)
225
231
  {
226
- super(pos, vec2(.6), spriteAtlas.gun);
232
+ super(pos, vec2(.6), Game.spriteAtlas.gun);
227
233
 
228
234
  // weapon settings
229
235
  this.fireRate = 8;
@@ -238,7 +244,7 @@ class Weapon extends EngineObject
238
244
  parent.addChild(this, vec2(.6,0));
239
245
 
240
246
  // shell effect
241
- this.addChild(this.shellEmitter = new ParticleEmitter(
247
+ this.addChild(this.shellEmitter = new LJS.ParticleEmitter(
242
248
  vec2(), 0, 0, 0, 0, .1, // pos, angle, emitSize, emitTime, emitRate, emitCone
243
249
  0, // tileInfo
244
250
  rgb(1,.8,.5), rgb(.9,.7,.5), // colorStartA, colorStartB
@@ -248,7 +254,7 @@ class Weapon extends EngineObject
248
254
  .1, 1 // randomness, collide, additive, colorLinear, renderOrder
249
255
  ), vec2(.1,0), -.8);
250
256
  this.shellEmitter.elasticity = .5;
251
- this.shellEmitter.particleDestroyCallback = persistentParticleDestroyCallback;
257
+ this.shellEmitter.particleDestroyCallback = GameEffects.persistentParticleDestroyCallback;
252
258
  }
253
259
 
254
260
  update()
@@ -257,21 +263,21 @@ class Weapon extends EngineObject
257
263
 
258
264
  // update recoil
259
265
  if (this.recoilTimer.active())
260
- this.localAngle = lerp(this.recoilTimer.getPercent(), this.localAngle, 0);
266
+ this.localAngle = LJS.lerp(this.recoilTimer.getPercent(), this.localAngle, 0);
261
267
 
262
268
  this.mirror = this.parent.mirror;
263
- this.fireTimeBuffer += timeDelta;
269
+ this.fireTimeBuffer += LJS.timeDelta;
264
270
  if (this.triggerIsDown)
265
271
  {
266
272
  // try to fire
267
273
  for (; this.fireTimeBuffer > 0; this.fireTimeBuffer -= 1/this.fireRate)
268
274
  {
269
275
  // create bullet
270
- sound_shoot.play(this.pos);
271
- this.localAngle = -rand(.2,.25);
276
+ GameEffects.sound_shoot.play(this.pos);
277
+ this.localAngle = -LJS.rand(.2,.25);
272
278
  this.recoilTimer.set(.1);
273
279
  const direction = vec2(this.bulletSpeed*this.getMirrorSign(), 0);
274
- const velocity = direction.rotate(rand(-1,1)*this.bulletSpread);
280
+ const velocity = direction.rotate(LJS.rand(-1,1)*this.bulletSpread);
275
281
  new Bullet(this.pos, this.parent, velocity, this.damage);
276
282
 
277
283
  // spawn shell particle
@@ -279,13 +285,13 @@ class Weapon extends EngineObject
279
285
  }
280
286
  }
281
287
  else
282
- this.fireTimeBuffer = min(this.fireTimeBuffer, 0);
288
+ this.fireTimeBuffer = LJS.min(this.fireTimeBuffer, 0);
283
289
  }
284
290
  }
285
291
 
286
292
  ///////////////////////////////////////////////////////////////////////////////
287
293
 
288
- class Bullet extends EngineObject
294
+ export class Bullet extends LJS.EngineObject
289
295
  {
290
296
  constructor(pos, attacker, velocity, damage)
291
297
  {
@@ -305,7 +311,7 @@ class Bullet extends EngineObject
305
311
  update()
306
312
  {
307
313
  // check if hit someone
308
- engineObjectsCallback(this.pos, this.size, (o)=>
314
+ LJS.engineObjectsCallback(this.pos, this.size, (o)=>
309
315
  {
310
316
  if (o.isGameObject)
311
317
  this.collideWithObject(o)
@@ -317,12 +323,12 @@ class Bullet extends EngineObject
317
323
  this.range -= this.velocity.length();
318
324
  if (this.range < 0)
319
325
  {
320
- new ParticleEmitter(
321
- this.pos, 0, .2, .1, 50, PI, spriteAtlas.circle, // pos, emit info, tileInfo
322
- rgb(1,1,.1), rgb(1,1,1), // colorStartA, colorStartB
323
- rgb(1,1,.1,0), rgb(1,1,1,0),// colorEndA, colorEndB
326
+ new LJS.ParticleEmitter(
327
+ this.pos, 0, .2, .1, 50, 3.14, Game.spriteAtlas.circle, // pos, emit info, tileInfo
328
+ rgb(1,1,.1), rgb(1,1,1), // colorStartA, colorStartB
329
+ rgb(1,1,.1,0), rgb(1,1,1,0), // colorEndA, colorEndB
324
330
  .1, .5, .1, .05, 0, // particleTime, sizeStart, sizeEnd, speed, angleSpeed
325
- 1, 1, .5, PI, .1, // damping, angleDamping, gravityScale, cone, fadeRate,
331
+ 1, 1, .5, 3.14, .1, // damping, angleDamping, gravityScale, cone, fadeRate,
326
332
  .5, 0, 1 // randomness, collide, additive, randomColorLinear
327
333
  );
328
334
 
@@ -347,7 +353,7 @@ class Bullet extends EngineObject
347
353
  if (data <= 0)
348
354
  return false;
349
355
 
350
- destroyTile(pos);
356
+ GameEffects.destroyTile(pos);
351
357
  this.kill();
352
358
  return true;
353
359
  }
@@ -359,17 +365,17 @@ class Bullet extends EngineObject
359
365
  this.destroy();
360
366
 
361
367
  // spark effects
362
- const emitter = new ParticleEmitter(
368
+ const emitter = new LJS.ParticleEmitter(
363
369
  this.pos, 0, 0, .1, 100, .5, // pos, angle, emitSize, emitTime, emitRate, emitCone
364
- 0, // tileInfo
370
+ 0, // tileInfo
365
371
  rgb(1,1,0), rgb(1,0,0), // colorStartA, colorStartB
366
372
  rgb(1,1,0), rgb(1,0,0), // colorEndA, colorEndB
367
- .2, .2, 0, .1, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
368
- 1, 1, .5, PI, .1, // damp, angleDamp, gravityScale, particleCone, fade,
369
- .5, 1, 1 // randomness, collide, additive, colorLinear, renderOrder
373
+ .2, .2, 0, .1, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
374
+ 1, 1, .5, 3.14, .1, // damp, angleDamp, gravityScale, particleCone, fade,
375
+ .5, 1, 1 // randomness, collide, additive, colorLinear, renderOrder
370
376
  );
371
377
  emitter.trailScale = 1;
372
378
  emitter.elasticity = .3;
373
- emitter.angle = this.velocity.angle() + PI;
379
+ emitter.angle = this.velocity.angle() + LJS.PI;
374
380
  }
375
381
  }
@@ -9,22 +9,27 @@
9
9
 
10
10
  'use strict';
11
11
 
12
- class Player extends Character
12
+ // import LittleJS module
13
+ import * as LJS from '../../dist/littlejs.esm.js';
14
+ import * as GameCharacter from './gameCharacter.js';
15
+ import * as Game from './game.js';
16
+
17
+ export class Player extends GameCharacter.Character
13
18
  {
14
19
  update()
15
20
  {
16
21
  // movement control
17
- this.moveInput = isUsingGamepad ? gamepadStick(0) : keyDirection();
18
- this.holdingJump = keyIsDown('ArrowUp') || gamepadIsDown(0);
19
- this.holdingShoot = !isUsingGamepad && mouseIsDown(0) || keyIsDown('KeyZ') || gamepadIsDown(2);
20
- this.pressingThrow = keyIsDown('KeyC') || mouseIsDown(1) || gamepadIsDown(1);
21
- this.pressedDodge = keyIsDown('KeyX') || mouseIsDown(2) || gamepadIsDown(3);
22
+ this.moveInput = LJS.isUsingGamepad ? LJS.gamepadStick(0) : LJS.keyDirection();
23
+ this.holdingJump = LJS.keyIsDown('ArrowUp') || LJS.gamepadIsDown(0);
24
+ this.holdingShoot = !LJS.isUsingGamepad && LJS.mouseIsDown(0) || LJS.keyIsDown('KeyZ') || LJS.gamepadIsDown(2);
25
+ this.pressingThrow = LJS.keyIsDown('KeyC') || LJS.mouseIsDown(1) || LJS.gamepadIsDown(1);
26
+ this.pressedDodge = LJS.keyIsDown('KeyX') || LJS.mouseIsDown(2) || LJS.gamepadIsDown(3);
22
27
  super.update();
23
28
  }
24
29
 
25
30
  kill()
26
31
  {
27
- ++deaths;
32
+ Game.addToDeaths();
28
33
  super.kill();
29
34
  }
30
35
  }
@@ -6,12 +6,4 @@
6
6
  <meta name=mobile-web-app-capable content=yes>
7
7
  <link rel=icon type=image/png href=../favicon.png>
8
8
  </head><body>
9
-
10
- <script src=../../dist/littlejs.js?11115></script>
11
- <script src=gameObjects.js?11115></script>
12
- <script src=gameCharacter.js?11115></script>
13
- <script src=gamePlayer.js?11115></script>
14
- <script src=gameEffects.js?11115></script>
15
- <script src=gameLevel.js?11115></script>
16
- <script src=gameLevelData.js?11115></script>
17
- <script src=game.js?11115></script>
9
+ <script src=game.js type=module></script>
@@ -8,9 +8,14 @@
8
8
 
9
9
  'use strict';
10
10
 
11
+ // import LittleJS module
12
+ import * as LJS from '../../dist/littlejs.esm.js';
13
+ const {vec2, rgb, hsl, tile} = LJS;
14
+
15
+ ///////////////////////////////////////////////////////////////////////////////
11
16
  // do not use pixelated rendering
12
- setCanvasPixelated(false);
13
- setTilesPixelated(false);
17
+ LJS.setCanvasPixelated(false);
18
+ LJS.setTilesPixelated(false);
14
19
 
15
20
  const fallTime = .2;
16
21
  const cameraOffset = vec2(0,-.5);
@@ -19,9 +24,9 @@ const minMatchCount = 3;
19
24
  const highScoreKey = 'puzzleBestScore';
20
25
 
21
26
  // sound effects
22
- const sound_goodMove = new Sound([.4,.2,250,.04,,.04,,,1,,,,,3]);
23
- const sound_badMove = new Sound([,,700,,,.07,,,,3.7,,,,3,,,.1]);
24
- const sound_fall = new Sound([.2,,1900,,,.01,,1.4,,91,,,,,,,,,,.7]);
27
+ const sound_goodMove = new LJS.Sound([.4,.2,250,.04,,.04,,,1,,,,,3]);
28
+ const sound_badMove = new LJS.Sound([,,700,,,.07,,,,3.7,,,,3,,,.1]);
29
+ const sound_fall = new LJS.Sound([.2,,1900,,,.01,,1.4,,91,,,,,,,,,,.7]);
25
30
 
26
31
  let level, levelSize, levelFall, fallTimer, dragStartPos, comboCount, score, bestScore;
27
32
 
@@ -43,29 +48,40 @@ const getTile = (pos) => level[pos.x + pos.y * levelSize.x];
43
48
  const setTile = (pos, data) => level[pos.x + pos.y * levelSize.x] = data;
44
49
 
45
50
  ///////////////////////////////////////////////////////////////////////////////
46
- function gameInit()
51
+ function gameReset()
47
52
  {
48
- // setup canvas
49
- canvasFixedSize = vec2(1920, 1080); // 1080p
50
- mainCanvas.style.background = backgroundColor;
51
-
52
- // load high score
53
- bestScore = localStorage[highScoreKey] || 0;
53
+ // reset game objects
54
+ LJS.engineObjectsDestroy();
54
55
 
55
56
  // randomize level
56
57
  level = [];
57
- levelSize = vec2(12,6);
58
58
  const pos = vec2();
59
59
  for (pos.x = levelSize.x; pos.x--;)
60
60
  for (pos.y = levelSize.y; pos.y--;)
61
- setTile(pos, randInt(tileTypeCount));
61
+ setTile(pos, LJS.randInt(tileTypeCount));
62
62
 
63
- // setup game
64
- cameraPos = levelSize.scale(.5).add(cameraOffset);
65
- cameraScale = 900/levelSize.y;
66
- gravity = -.004;
67
- fallTimer = new Timer;
68
63
  comboCount = score = 0;
64
+ fallTimer = new LJS.Timer;
65
+ }
66
+
67
+ ///////////////////////////////////////////////////////////////////////////////
68
+ function gameInit()
69
+ {
70
+ // setup canvas
71
+ LJS.setCanvasFixedSize(vec2(1920, 1080)); // 1080p
72
+ LJS.mainCanvas.style.background = backgroundColor;
73
+
74
+ // load high score
75
+ bestScore = localStorage[highScoreKey] || 0;
76
+
77
+ // setup game
78
+ levelSize = vec2(12,6);
79
+ LJS.setCameraPos(levelSize.scale(.5).add(cameraOffset));
80
+ LJS.setCameraScale(900/levelSize.y);
81
+ LJS.setGravity(vec2(0,-.004));
82
+
83
+ // start a new game
84
+ gameReset();
69
85
  }
70
86
 
71
87
  ///////////////////////////////////////////////////////////////////////////////
@@ -78,7 +94,7 @@ function gameUpdate()
78
94
  {
79
95
  // add more blocks in the top
80
96
  for (let x = 0; x < levelSize.x; ++x)
81
- setTile(vec2(x,levelSize.y),randInt(tileTypeCount));
97
+ setTile(vec2(x,levelSize.y), LJS.randInt(tileTypeCount));
82
98
  }
83
99
 
84
100
  // allow blocks to fall
@@ -104,7 +120,7 @@ function gameUpdate()
104
120
 
105
121
  if (keepFalling)
106
122
  {
107
- const p = percent(comboCount, 9, 0);
123
+ const p = LJS.percent(comboCount, 9, 0);
108
124
  fallTimer.set(fallTime*p);
109
125
  sound_fall.play();
110
126
  }
@@ -119,22 +135,22 @@ function gameUpdate()
119
135
  if (!fallTimer.isSet())
120
136
  {
121
137
  // mouse/touch control
122
- const mouseTilePos = mousePos.floor();
123
- if (!mousePos.arrayCheck(levelSize))
138
+ const mouseTilePos = LJS.mousePos.floor();
139
+ if (!LJS.mousePos.arrayCheck(levelSize))
124
140
  {
125
141
  // cancel drag if mouse is not in the level bounds
126
142
  dragStartPos = 0;
127
143
  }
128
- else if (mouseWasPressed(0) && !dragStartPos)
144
+ else if (LJS.mouseWasPressed(0) && !dragStartPos)
129
145
  {
130
146
  // start drag
131
147
  dragStartPos = mouseTilePos.copy();
132
148
  }
133
- else if (mouseIsDown(0) && dragStartPos)
149
+ else if (LJS.mouseIsDown(0) && dragStartPos)
134
150
  {
135
151
  // if dragging to a neighbor tile
136
- const dx = abs(dragStartPos.x - mouseTilePos.x);
137
- const dy = abs(dragStartPos.y - mouseTilePos.y);
152
+ const dx = LJS.abs(dragStartPos.x - mouseTilePos.x);
153
+ const dy = LJS.abs(dragStartPos.y - mouseTilePos.y);
138
154
  if (dx == 1 && dy == 0 || dx == 0 && dy == 1)
139
155
  {
140
156
  const startTile = getTile(dragStartPos);
@@ -172,6 +188,9 @@ function gameUpdate()
172
188
  bestScore = score;
173
189
  localStorage[highScoreKey] = bestScore;
174
190
  }
191
+
192
+ if (LJS.keyWasPressed('KeyR'))
193
+ gameReset();
175
194
  }
176
195
 
177
196
  ///////////////////////////////////////////////////////////////////////////////
@@ -184,7 +203,7 @@ function gameUpdatePost()
184
203
  function gameRender()
185
204
  {
186
205
  // draw a black square for the background
187
- drawRect(cameraPos.subtract(cameraOffset), levelSize, hsl(0,0,0));
206
+ LJS.drawRect(LJS.cameraPos.subtract(cameraOffset), levelSize, hsl(0,0,0));
188
207
 
189
208
  // draw the blocks
190
209
  const pos = vec2();
@@ -198,7 +217,7 @@ function gameRender()
198
217
  // highlight drag start
199
218
  const drawPos = pos.add(vec2(.5));
200
219
  if (dragStartPos && pos.x == dragStartPos.x && pos.y == dragStartPos.y)
201
- drawRect(drawPos, vec2(1.05));
220
+ LJS.drawRect(drawPos, vec2(1.05));
202
221
 
203
222
  // make pieces fall gradually
204
223
  if (fallTimer.active() && levelFall[pos.x + pos.y*levelSize.x])
@@ -206,28 +225,28 @@ function gameRender()
206
225
 
207
226
  // draw background
208
227
  const color = tileColors[data];
209
- drawRect(drawPos, vec2(.95), color);
228
+ LJS.drawRect(drawPos, vec2(.95), color);
210
229
 
211
230
  // use darker color for icon
212
231
  const color2 = color.scale(.8, 1);
213
- drawTile(drawPos, vec2(.5), tile(data, 64), color2);
232
+ LJS.drawTile(drawPos, vec2(.5), tile(data, 64), color2);
214
233
  }
215
234
 
216
235
  // draw a grey square at top to cover up incoming tiles
217
- drawRect(cameraPos.subtract(cameraOffset).add(vec2(0,levelSize.y)), levelSize, backgroundColor);
236
+ LJS.drawRect(LJS.cameraPos.subtract(cameraOffset).add(vec2(0,levelSize.y)), levelSize, backgroundColor);
218
237
  }
219
238
 
220
239
  ///////////////////////////////////////////////////////////////////////////////
221
240
  function gameRenderPost()
222
241
  {
223
242
  // draw text on top of everything
224
- drawTextOverlay('Score: ' + score, cameraPos.add(vec2(-3,-3.1)), .9, hsl(), .1);
225
- drawTextOverlay('Best: ' + bestScore, cameraPos.add(vec2( 3,-3.1)), .9, hsl(), .1);
243
+ LJS.drawTextOverlay('Score: ' + score, LJS.cameraPos.add(vec2(-3,-3.1)), .9, hsl(), .1);
244
+ LJS.drawTextOverlay('Best: ' + bestScore, LJS.cameraPos.add(vec2( 3,-3.1)), .9, hsl(), .1);
226
245
  }
227
246
 
228
247
  ///////////////////////////////////////////////////////////////////////////////
229
248
  // Startup LittleJS Engine
230
- engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
249
+ LJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
231
250
 
232
251
  ///////////////////////////////////////////////////////////////////////////////
233
252
  // find and remove all runs of 3 or higher
@@ -289,14 +308,14 @@ function clearMatches()
289
308
  // spawn particles
290
309
  const color1 = tileColors[data];
291
310
  const color2 = color1.lerp(hsl(), .5);
292
- new ParticleEmitter(
293
- pos.add(vec2(.5)), 0, // pos, angle
294
- .5, .1, 200, PI, // emitSize, emitTime, emitRate, emitCone
295
- 0, // tileInfo
311
+ new LJS.ParticleEmitter(
312
+ pos.add(vec2(.5)), 0, // pos, angle
313
+ .5, .1, 200, 3.14, // emitSize, emitTime, emitRate, emitCone
314
+ 0, // tileInfo
296
315
  color1, color2, // colorStartA, colorStartB
297
316
  color1.scale(1,0), color2.scale(1,0),// colorEndA, colorEndB
298
317
  .5, .3, .2, .05, .05, // particleTime, sizeStart, sizeEnd, speed, angleSpeed
299
- .99, 1, 1, PI, .05, // damp, angleDamp, gravityScale, particleCone, fadeRate
318
+ .99, 1, 1, 3.14, .05, // damp, angleDamp, gravityScale, particleCone, fadeRate
300
319
  .5, 0, 1 // randomness, collide, additive, colorLinear, renderOrder
301
320
  );
302
321
  }
@@ -5,6 +5,4 @@
5
5
  <meta name=mobile-web-app-capable content=yes>
6
6
  <link rel=icon type=image/png href=../favicon.png>
7
7
  </head><body>
8
-
9
- <script src=../../dist/littlejs.js?11115></script>
10
- <script src=game.js?11115></script>
8
+ <script src=game.js type=module></script>
@@ -1,5 +1,5 @@
1
1
  <!DOCTYPE html><meta charset=utf-8><body>
2
- <script src=../../dist/littlejs.js?11115></script>
2
+ <script src=../../dist/littlejs.js?1120></script>
3
3
  <script>
4
4
 
5
5
  'use strict';
@@ -23,4 +23,4 @@ function gameUpdatePost() {}
23
23
  function gameRender() {}
24
24
  function gameRenderPost() {}
25
25
 
26
- </script>
26
+ </script>
@@ -1,6 +1,6 @@
1
1
  function gameInit()
2
2
  {
3
- gravity = -.01; // set default gravity
3
+ gravity.y = -.01; // set default gravity
4
4
 
5
5
  // fire
6
6
  new ParticleEmitter(
@@ -32,7 +32,7 @@ class Player extends PhysicsObject
32
32
  function gameInit()
33
33
  {
34
34
  // setup level
35
- gravity = -.05;
35
+ gravity.y = -.05;
36
36
  new Player(vec2(0,4));
37
37
 
38
38
  // create random objects