littlejsengine 1.9.9 → 1.9.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.
@@ -1,6 +1,18 @@
1
1
  /*
2
2
  LittleJS Box2D Example - Secenes
3
3
  - Each scene demonstrates a feature of Box2D
4
+ - Feel free to use tis code in your own projects
5
+ - 0 = Shapes: box, circle, poly, and edge
6
+ - 1 = Pyramid stack
7
+ - 2 = Dominoes chain reaction
8
+ - 3 = Car with controls
9
+ - 4 = Rope with box attached
10
+ - 5 = Raycasts in all directions
11
+ - 6 = Joints demo with every joint type
12
+ - 7 = Contacts begin and end callbacks
13
+ - 8 = Mobile object with multiple bodies
14
+ - 9 = Cloth object grid using rope joints
15
+ - 10 = Softbody object grid using weld joints
4
16
  */
5
17
 
6
18
  'use strict';
@@ -15,8 +27,8 @@ function loadScene(_scene)
15
27
 
16
28
  // destroy old scene
17
29
  engineObjectsDestroy();
18
- carWheelJoint = 0;
19
30
  mouseJoint = 0;
31
+ car = 0;
20
32
 
21
33
  // create walls
22
34
  groundObject = spawnBox(vec2(0,-4), vec2(1e3,8), hsl(0,0,.2), box2dBodyTypeStatic);
@@ -54,7 +66,7 @@ function loadScene(_scene)
54
66
  if (scene == 3)
55
67
  {
56
68
  sceneName = 'Car';
57
- spawnCar(vec2(10,2));
69
+ car = new CarObject(vec2(10,2));
58
70
  spawnBox(vec2(20,0), vec2(10,2), randColor(), box2dBodyTypeStatic, false, -.2);
59
71
  spawnPyramid(vec2(32,0), 6);
60
72
  }
@@ -99,29 +111,11 @@ function loadScene(_scene)
99
111
  l1.renderOrder = l2.renderOrder = -2;
100
112
  }
101
113
  {
102
- // pulley object renders a rope to connected point
103
- class PulleyObjects extends Box2dObject
104
- {
105
- constructor(pos, size, color, connectionPos)
106
- {
107
- super(pos, size, tile(3), 0, color);
108
- this.addBox(size);
109
- this.connectionPos = connectionPos;
110
- }
111
- render()
112
- {
113
- // draw rope
114
- const topPos = this.localToWorld(vec2(0,this.size.y/2));
115
- drawLine(topPos, this.connectionPos, .2, BLACK);
116
- super.render();
117
- }
118
- }
119
-
120
114
  // pulley joint
121
115
  const anchorA = vec2(14,15);
122
116
  const anchorB = vec2(26,15);
123
- const oA = new PulleyObjects(vec2(15,8), vec2(1,2), randColor(), anchorA);
124
- const oB = new PulleyObjects(vec2(25,8), vec2(1,2), randColor(), anchorB);
117
+ const oA = new PulleyJointObjects(vec2(15,8), vec2(1,2), randColor(), anchorA);
118
+ const oB = new PulleyJointObjects(vec2(25,8), vec2(1,2), randColor(), anchorB);
125
119
  const aA = spawnCircle(anchorA, 2, randColor(), box2dBodyTypeStatic);
126
120
  const aB = spawnCircle(anchorB, 2, randColor(), box2dBodyTypeStatic);
127
121
  const oaA = oA.localToWorld(vec2(0,oA.size.y/2));
@@ -156,31 +150,10 @@ function loadScene(_scene)
156
150
  box2dCreateDistanceJoint(o1, o2);
157
151
  }
158
152
  {
159
- // motor object renders a rope to connected point
160
- class MotorObject extends Box2dObject
161
- {
162
- constructor(pos, size, color, otherObject)
163
- {
164
- super(pos, size, tile(4), 0, color);
165
- this.addCircle(size.x);
166
- this.connectionPos = pos;
167
- const joint = box2dCreateMotorJoint(otherObject, this);
168
- joint.SetMaxForce(500);
169
- joint.SetMaxTorque(500);
170
- }
171
- render()
172
- {
173
- // draw rope
174
- const width = 2/(1+this.pos.distance(this.connectionPos));
175
- drawLine(this.pos, this.connectionPos, width, BLACK);
176
- super.render();
177
- }
178
- }
179
-
180
153
  // motor joint
181
154
  const o = new Box2dObject(vec2(10,8), vec2(4), tile(2), 0, randColor(), box2dBodyTypeStatic);
182
155
  o.renderOrder = -2;
183
- new MotorObject(vec2(10,8), vec2(2), randColor(), o);
156
+ new MotorJointObject(vec2(10,8), vec2(2), randColor(), o);
184
157
  }
185
158
  {
186
159
  // friction joint
@@ -194,26 +167,8 @@ function loadScene(_scene)
194
167
  if (scene == 7)
195
168
  {
196
169
  sceneName = 'Contacts';
197
- // changes objects color when touched
198
- class ContactTester extends Box2dObject
199
- {
200
- constructor(pos, size, color, contactColor, isSensor=false)
201
- {
202
- super(pos, size, 0, 0, color, box2dBodyTypeStatic);
203
- if (isSensor)
204
- {
205
- this.addCircle(size.x);
206
- this.setSensor(isSensor);
207
- }
208
- else
209
- this.addBox(size);
210
- this.contactColor = contactColor;
211
- }
212
- beginContact(other) { other.color = this.contactColor; }
213
- endContact(other) { other.color = WHITE; }
214
- }
215
- new ContactTester(vec2(15,8), vec2(5), hsl(0,0,0,.3), RED, true);
216
- new ContactTester(vec2(25,8), vec2(5), hsl(0,0,.1), GREEN);
170
+ new ContactTester(vec2(15,8), vec2(5), RED, RED);
171
+ new ContactTester(vec2(25,8), vec2(5), CYAN, CYAN, false, false);
217
172
  for (let i=200;i--;)
218
173
  spawnRandomObject(vec2(rand(1,39), rand(10,50)));
219
174
  }
@@ -221,192 +176,18 @@ function loadScene(_scene)
221
176
  {
222
177
  sceneName = 'Mobile';
223
178
  const pos = vec2(20, 16);
224
- const mobile = spawnMobile(pos, 12, 2, 5);
179
+ const mobile = new MobileObject(pos, 12, 2, 5);
225
180
  box2dCreateRevoluteJoint(groundObject, mobile, pos);
226
181
  }
227
-
228
- // helper functions
229
- function spawnRandomEdges()
230
- {
231
- // edge list along bottom
232
- const edgePoints = [];
233
- edgePoints.push(vec2(40,0));
234
- for (let i=40, y=0; i--;)
235
- edgePoints.push(vec2(i, y=clamp(y+rand(-2,2),0,5)));
236
- edgePoints.push(vec2(0,0));
237
- const o = new Box2dObject(vec2(), vec2(), 0, 0, BLACK, box2dBodyTypeStatic);
238
- o.addEdgeList(edgePoints);
239
- }
240
- function spawnRope(startPos, count, angle=PI, color=WHITE, size=vec2(.3,1))
241
- {
242
- let lastObject = groundObject;
243
- for (let i=0; i<count; ++i)
244
- {
245
- const pos = startPos.add(size.multiply(vec2(0,i+.5)).rotate(-angle));
246
- const o = spawnBox(pos, size, color, box2dBodyTypeDynamic, false, angle);
247
- o.setFilterData(2, 2);
248
- const anchorPos = pos.add(vec2(0,-size.y/2).rotate(-angle));
249
- box2dCreateRevoluteJoint(lastObject, o, anchorPos);
250
- lastObject = o;
251
- }
252
- const endPos = lastObject.localToWorld(vec2(0,-size.y/2));
253
- box2dCreateRopeJoint(groundObject, lastObject, startPos, endPos);
254
- return lastObject;
255
- }
256
- function spawnPyramid(pos, count)
257
- {
258
- for (let i=count+1;i--;)
259
- for (let j=i;j--;)
260
- spawnBox(pos.add(vec2(j-i/2+.5,count-i+.5)), 1, randColor());
261
- }
262
- function spawnDominoes(pos, count, size=vec2(.5,2))
263
- {
264
- for (let i=count; i--;)
265
- spawnBox(pos.add(vec2(i*size.y*.9,size.y/2)), size, randColor());
266
- }
267
- function spawnCar(pos)
268
- {
269
- // car object
270
- const car = new Box2dObject(pos, vec2(), 0, 0, randColor());
271
- const carPoints = [
272
- vec2(-1.5,-.5),
273
- vec2(1.5, -.5),
274
- vec2(1.5, 0),
275
- vec2(0, .9),
276
- vec2(-1.15,.9),
277
- vec2(-1.5, .2),
278
- ];
279
- car.addPoly(carPoints);
280
-
281
- // wheel settings
282
- const frequencyHz = 4;
283
- const dampingRatio = .7;
284
- const wheelDensity = 1;
285
- const wheelFriction = 2;
286
- const wheelRestitution = 0;
287
- const wheelSize = .8;
288
-
289
- // back wheel
290
- const wheel1Pos = pos.add(vec2(-1, -.65));
291
- const wheel1 = new Box2dObject(wheel1Pos, vec2(wheelSize), tile(4));
292
- wheel1.addCircle(wheelSize,vec2(),wheelDensity,wheelFriction,wheelRestitution);
293
- const joint1 = box2dCreateWheelJoint(car,wheel1);
294
- joint1.SetSpringDampingRatio(dampingRatio);
295
- joint1.SetSpringFrequencyHz(frequencyHz);
296
- joint1.SetMaxMotorTorque(50);
297
- joint1.EnableMotor(true);
298
- carWheelJoint = joint1;
299
-
300
- // front wheel
301
- const wheel2Pos = pos.add(vec2(1, -.6));
302
- const wheel2 = new Box2dObject(wheel2Pos, vec2(wheelSize), tile(4));
303
- wheel2.addCircle(wheelSize,vec2(),wheelDensity,wheelFriction,wheelRestitution);
304
- const joint2 = box2dCreateWheelJoint(car,wheel2);
305
- joint2.SetSpringDampingRatio(dampingRatio);
306
- joint2.SetSpringFrequencyHz(frequencyHz);
307
- }
308
- function spawnMobile(anchor, w, h, depth)
309
- {
310
- // tall box
311
- const pos = anchor.add(vec2(0,-h/2));
312
- const o = spawnBox(pos, vec2(h/4, h), randColor(), box2dBodyTypeDynamic, false);
313
- if (--depth)
314
- {
315
- // long box on bottom
316
- o.addBox(vec2(w, h/4), vec2(0, -h/2));
317
-
318
- // spawn smaller mobiles attached to each side
319
- for(let i=2; i--;)
320
- {
321
- const a = anchor.add(vec2( w/2*(i?1:-1), -h));
322
- const o2 = spawnMobile(a, w/2, h, depth);
323
- box2dCreateRevoluteJoint(o, o2, a);
324
- }
325
- }
326
- return o;
327
- }
328
- }
329
-
330
- ///////////////////////////////////////////////////////////////////////////////
331
- // Spawn object functions
332
-
333
- function spawnBox(pos, size=1, color=WHITE, type=box2dBodyTypeDynamic, applyTexture=true, angle=0)
334
- {
335
- size = typeof size == 'number' ? vec2(size) : size; // square
336
- const o = new Box2dObject(pos, size, applyTexture && tile(3), angle, color, type);
337
- o.addBox(size);
338
- return o;
339
- }
340
-
341
- function spawnCircle(pos, diameter=1, color=WHITE, type=box2dBodyTypeDynamic, applyTexture=true, angle=0)
342
- {
343
- const size = vec2(diameter);
344
- const o = new Box2dObject(pos, size, applyTexture && tile(2), angle, color, type);
345
- o.addCircle(diameter);
346
- return o;
347
- }
348
-
349
- function spawnRandomPoly(pos, diameter=1, color=WHITE, type=box2dBodyTypeDynamic, angle=0)
350
- {
351
- const o = new Box2dObject(pos, vec2(), 0, angle, color, type);
352
- o.addRandomPoly(diameter);
353
- return o;
354
- }
355
-
356
- function spawnRandomObject(pos, scale=1, type=box2dBodyTypeDynamic, angle=0)
357
- {
358
- if (randInt(2))
182
+ if (scene == 9)
359
183
  {
360
- const size = vec2(scale*rand(.5,1), scale*rand(.5,1));
361
- return spawnBox(pos, size, randColor(), type, true, angle);
184
+ sceneName = 'Cloth';
185
+ new ClothObject(vec2(20, 9), vec2(15), vec2(24), randColor());
362
186
  }
363
- else
364
- {
365
- const diameter = scale*rand(.5,1);
366
- return spawnCircle(pos, diameter, randColor(), type, true, angle);
367
- }
368
- }
369
-
370
- ///////////////////////////////////////////////////////////////////////////////
371
- // Effects
372
-
373
- const sound_click = new Sound([,.1]);
374
- const sound_explosion = new Sound([,.2,72,.01,.01,.2,4,,,,,,,1,,.5,.1,.5,.02]);
375
-
376
- function explosion(pos, radius=3, strength=300)
377
- {
378
- sound_explosion.play(pos);
379
- const objects = box2dCircleCastAll(pos, (radius*2));
380
- const newColor = randColor();
381
- for (const o of objects)
187
+ if (scene == 10)
382
188
  {
383
- const p = percent(o.pos.distance(pos), 2*radius, radius);
384
- const force = o.pos.subtract(pos).normalize(p*radius*strength);
385
- o.applyForce(force);
386
- o.color = newColor;
189
+ sceneName = 'Softbodies';
190
+ for(let i=3;i--;)
191
+ new SoftBodyObject(vec2(20, 3+i*7), vec2(6-i), vec2(9-i), randColor());
387
192
  }
388
-
389
- // smoke
390
- new ParticleEmitter(
391
- pos, 0, // pos, angle
392
- radius/2, .2, 50*radius, PI,// emitSize, emitTime, emitRate, emiteCone
393
- tile(1), // tileInfo
394
- hsl(0,0,0), hsl(0,0,0), // colorStartA, colorStartB
395
- hsl(0,0,0,0), hsl(0,0,0,0), // colorEndA, colorEndB
396
- 1, 1, 2, .1, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
397
- .9, 1, -.5, PI, .1, // damp, angleDamp, gravity, particleCone, fade
398
- 1, 0, 0, 0, 1e8 // randomness, collide, additive, colorLinear, renderOrder
399
- );
400
-
401
- // fire
402
- new ParticleEmitter(
403
- pos, 0, // pos, angle
404
- radius, .1, 100*radius, PI, // emitSize, emitTime, emitRate, emiteCone
405
- tile(1), // tileInfo
406
- hsl(0,1,.5), hsl(.15,1,.5), // colorStartA, colorStartB
407
- hsl(0,1,.5,0), hsl(.1, 1,.5,0), // colorEndA, colorEndB
408
- .7, 1, .5, .1, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
409
- .9, 1, 0, PI, .05, // damp, angleDamp, gravity, particleCone, fade
410
- 1, 0, 1, 0, 1e9 // randomness, collide, additive, colorLinear, renderOrder
411
- );
412
- }
193
+ }
Binary file
@@ -5,7 +5,7 @@
5
5
  <link rel=icon type=image/png href=../favicon.png>
6
6
  </head><body>
7
7
 
8
- <script src=../../dist/littlejs.js?197></script>
8
+ <script src=../../dist/littlejs.js?198></script>
9
9
  <script src=../../plugins/postProcess.js></script>
10
- <script src=gameObjects.js?197></script>
11
- <script src=game.js?197></script>
10
+ <script src=gameObjects.js?198></script>
11
+ <script src=game.js?198></script>
@@ -5,5 +5,5 @@
5
5
  <link rel=icon type=image/png href=../favicon.png>
6
6
  </head><body>
7
7
 
8
- <script src=../../dist/littlejs.js?197></script>
9
- <script src=game.js?197></script>
8
+ <script src=../../dist/littlejs.js?198></script>
9
+ <script src=game.js?198></script>
@@ -6,7 +6,7 @@
6
6
  </head><body>
7
7
 
8
8
  <!-- LittleJS Engine -->
9
- <script src=../../dist/littlejs.js?197></script>
9
+ <script src=../../dist/littlejs.js?198></script>
10
10
 
11
11
  <!-- Add your game scripts here -->
12
- <script src=game.js?197></script>
12
+ <script src=game.js?198></script>
@@ -1,18 +1,18 @@
1
1
  <head><title>LittleJS JS13K Project</title></head><body>
2
2
 
3
3
  <!-- LittleJS Engine -->
4
- <script src=../../src/engineDebug.js?197></script>
5
- <script src=../../src/engineUtilities.js?197></script>
6
- <script src=../../src/engineSettings.js?197></script>
7
- <script src=../../src/engineObject.js?197></script>
8
- <script src=../../src/engineDraw.js?197></script>
9
- <script src=../../src/engineInput.js?197></script>
10
- <script src=../../src/engineAudio.js?197></script>
11
- <script src=../../src/engineTileLayer.js?197></script>
12
- <script src=../../src/engineParticles.js?197></script>
13
- <script src=../../src/engineMedals.js?197></script>
14
- <script src=../../src/engineWebGL.js?197></script>
15
- <script src=../../src/engine.js?197></script>
4
+ <script src=../../src/engineDebug.js?198></script>
5
+ <script src=../../src/engineUtilities.js?198></script>
6
+ <script src=../../src/engineSettings.js?198></script>
7
+ <script src=../../src/engineObject.js?198></script>
8
+ <script src=../../src/engineDraw.js?198></script>
9
+ <script src=../../src/engineInput.js?198></script>
10
+ <script src=../../src/engineAudio.js?198></script>
11
+ <script src=../../src/engineTileLayer.js?198></script>
12
+ <script src=../../src/engineParticles.js?198></script>
13
+ <script src=../../src/engineMedals.js?198></script>
14
+ <script src=../../src/engineWebGL.js?198></script>
15
+ <script src=../../src/engine.js?198></script>
16
16
 
17
17
  <!-- Add your game scripts here -->
18
- <script src=game.js?197></script>
18
+ <script src=game.js?198></script>
@@ -6,4 +6,4 @@
6
6
  </head><body>
7
7
 
8
8
  <!-- Add your game scripts here -->
9
- <script src=game.js?197 type=module></script>
9
+ <script src=game.js?198 type=module></script>
@@ -9,7 +9,7 @@ button { margin:1px; }
9
9
  </style>
10
10
  </head><body>
11
11
 
12
- <script src=../../dist/littlejs.js?197></script>
12
+ <script src=../../dist/littlejs.js?198></script>
13
13
  <script>
14
14
 
15
15
  'use strict';
@@ -41,6 +41,7 @@ class Character extends GameObject
41
41
  if (this.isDead())
42
42
  return super.update();
43
43
 
44
+ // get move input
44
45
  const moveInput = this.moveInput.copy();
45
46
 
46
47
  // jump
@@ -92,8 +92,6 @@ function explosion(pos, radius=3)
92
92
  const p = percent(d, 2*radius, radius);
93
93
  const force = o.pos.subtract(pos).normalize(p*radius*.2);
94
94
  o.applyForce(force);
95
- if (o.isDead && o.isDead())
96
- o.angleVelocity += randSign()*rand(p*radius/4,.3);
97
95
  });
98
96
 
99
97
  // smoke
@@ -66,7 +66,7 @@ class Crate extends GameObject
66
66
  {
67
67
  constructor(pos)
68
68
  {
69
- super(pos, vec2(1), spriteAtlas.crate, (randInt(4))*PI/2);
69
+ super(pos, vec2(.999), spriteAtlas.crate, (randInt(4))*PI/2);
70
70
 
71
71
  this.color = hsl(rand(),1,.8);
72
72
  this.health = 5;
@@ -6,11 +6,11 @@
6
6
  <link rel=icon type=image/png href=../favicon.png>
7
7
  </head><body>
8
8
 
9
- <script src=../../dist/littlejs.js?197></script>
10
- <script src=gameObjects.js?197></script>
11
- <script src=gameCharacter.js?197></script>
12
- <script src=gamePlayer.js?197></script>
13
- <script src=gameEffects.js?197></script>
14
- <script src=gameLevel.js?197></script>
15
- <script src=gameLevelData.js?197></script>
16
- <script src=game.js?197></script>
9
+ <script src=../../dist/littlejs.js?198></script>
10
+ <script src=gameObjects.js?198></script>
11
+ <script src=gameCharacter.js?198></script>
12
+ <script src=gamePlayer.js?198></script>
13
+ <script src=gameEffects.js?198></script>
14
+ <script src=gameLevel.js?198></script>
15
+ <script src=gameLevelData.js?198></script>
16
+ <script src=game.js?198></script>
@@ -5,5 +5,5 @@
5
5
  <link rel=icon type=image/png href=../favicon.png>
6
6
  </head><body>
7
7
 
8
- <script src=../../dist/littlejs.js?197></script>
9
- <script src=game.js?197></script>
8
+ <script src=../../dist/littlejs.js?198></script>
9
+ <script src=game.js?198></script>
@@ -63,7 +63,7 @@ function gameInit()
63
63
  // create particle emitter
64
64
  particleEmitter = new ParticleEmitter(
65
65
  vec2(16,9), 0, // emitPos, emitAngle
66
- 1, 0, 500, PI, // emitSize, emitTime, emitRate, emiteCone
66
+ 0, 0, 500, PI, // emitSize, emitTime, emitRate, emiteCone
67
67
  tile(0, 16), // tileIndex, tileSize
68
68
  hsl(1,1,1), hsl(0,0,0), // colorStartA, colorStartB
69
69
  hsl(0,0,0,0), hsl(0,0,0,0), // colorEndA, colorEndB
@@ -6,18 +6,18 @@
6
6
  </head><body>
7
7
 
8
8
  <!-- LittleJS Engine -->
9
- <script src=../../src/engineDebug.js?197></script>
10
- <script src=../../src/engineUtilities.js?197></script>
11
- <script src=../../src/engineSettings.js?197></script>
12
- <script src=../../src/engineObject.js?197></script>
13
- <script src=../../src/engineDraw.js?197></script>
14
- <script src=../../src/engineInput.js?197></script>
15
- <script src=../../src/engineAudio.js?197></script>
16
- <script src=../../src/engineTileLayer.js?197></script>
17
- <script src=../../src/engineParticles.js?197></script>
18
- <script src=../../src/engineMedals.js?197></script>
19
- <script src=../../src/engineWebGL.js?197></script>
20
- <script src=../../src/engine.js?197></script>
9
+ <script src=../../src/engineDebug.js?198></script>
10
+ <script src=../../src/engineUtilities.js?198></script>
11
+ <script src=../../src/engineSettings.js?198></script>
12
+ <script src=../../src/engineObject.js?198></script>
13
+ <script src=../../src/engineDraw.js?198></script>
14
+ <script src=../../src/engineInput.js?198></script>
15
+ <script src=../../src/engineAudio.js?198></script>
16
+ <script src=../../src/engineTileLayer.js?198></script>
17
+ <script src=../../src/engineParticles.js?198></script>
18
+ <script src=../../src/engineMedals.js?198></script>
19
+ <script src=../../src/engineWebGL.js?198></script>
20
+ <script src=../../src/engine.js?198></script>
21
21
 
22
22
  <!-- Add your game scripts here -->
23
- <script src=game.js?197></script>
23
+ <script src=game.js?198></script>
@@ -6,7 +6,7 @@
6
6
  </head><body>
7
7
 
8
8
  <!-- LittleJS Engine -->
9
- <script src=../../dist/littlejs.min.js?197></script>
9
+ <script src=../../dist/littlejs.min.js?198></script>
10
10
  <script>
11
11
 
12
12
  /*
@@ -6,4 +6,4 @@
6
6
  </head><body>
7
7
 
8
8
  <!-- Add your game scripts here -->
9
- <script src=game.js?197 type=module></script>
9
+ <script src=game.js?198 type=module></script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "littlejsengine",
3
- "version": "1.9.9",
3
+ "version": "1.9.11",
4
4
  "description": "LittleJS - Tiny and Fast HTML5 Game Engine",
5
5
  "main": "dist/littlejs.esm.js",
6
6
  "types": "dist/littlejs.d.ts",