littlejsengine 1.8.9 → 1.9.1
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.
- package/README.md +17 -17
- package/build/littlejs.d.ts +352 -288
- package/build/littlejs.esm.js +695 -658
- package/build/littlejs.esm.min.js +1 -1
- package/build/littlejs.js +694 -658
- package/build/littlejs.min.js +1 -1
- package/build/littlejs.release.js +629 -594
- package/examples/breakout/game.js +4 -4
- package/examples/breakout/index.html +3 -3
- package/examples/breakoutTutorial/index.html +2 -2
- package/examples/electron/game.js +1 -1
- package/examples/electron/index.html +2 -2
- package/examples/favicon.png +0 -0
- package/examples/js13k/build.js +1 -1
- package/examples/js13k/index.html +13 -13
- package/examples/module/game.js +1 -1
- package/examples/module/index.html +1 -1
- package/examples/particles/index.html +1 -1
- package/examples/platformer/game.js +6 -6
- package/examples/platformer/gameCharacter.js +293 -0
- package/examples/platformer/gameEffects.js +8 -5
- package/examples/platformer/gameObjects.js +13 -13
- package/examples/platformer/gamePlayer.js +9 -293
- package/examples/platformer/index.html +7 -6
- package/examples/puzzle/game.js +3 -2
- package/examples/puzzle/index.html +2 -2
- package/examples/starter/build.js +1 -1
- package/examples/starter/game.js +2 -2
- package/examples/starter/index.html +13 -13
- package/examples/stress/index.html +35 -27
- package/examples/typescript/index.html +1 -1
- package/index.d.ts +2094 -0
- package/package.json +1 -1
- package/src/engine.js +28 -28
- package/src/engineAudio.js +57 -57
- package/src/engineDebug.js +66 -65
- package/src/engineDraw.js +64 -73
- package/src/engineExport.js +1 -0
- package/src/engineInput.js +57 -40
- package/src/engineMedals.js +32 -29
- package/src/engineObject.js +42 -27
- package/src/engineParticles.js +98 -72
- package/src/engineRelease.js +1 -1
- package/src/engineSettings.js +22 -22
- package/src/engineTileLayer.js +66 -60
- package/src/engineUtilities.js +49 -49
- package/src/engineWebGL.js +112 -135
- package/src/jsconfig.json +10 -0
|
@@ -1,311 +1,27 @@
|
|
|
1
1
|
/*
|
|
2
2
|
LittleJS Platformer Example - Player
|
|
3
|
-
-
|
|
3
|
+
- Extends character class
|
|
4
|
+
- Uses character physics system
|
|
4
5
|
- Player can jump, shoot, roll, and throw grenades
|
|
5
|
-
-
|
|
6
|
-
-
|
|
7
|
-
- Plays animation and sound effects
|
|
6
|
+
- Supports keyboard, mouse, and gamepad controls
|
|
7
|
+
- Keeps track of player deaths
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
|
-
class Character extends GameObject
|
|
13
|
-
{
|
|
14
|
-
constructor(pos)
|
|
15
|
-
{
|
|
16
|
-
super(pos, vec2(.6,.95), tile());
|
|
17
|
-
|
|
18
|
-
this.weapon = new Weapon(pos, this);
|
|
19
|
-
this.lastPos = pos;
|
|
20
|
-
this.groundTimer = new Timer;
|
|
21
|
-
this.jumpTimer = new Timer;
|
|
22
|
-
this.pressedJumpTimer = new Timer;
|
|
23
|
-
this.dodgeTimer = new Timer;
|
|
24
|
-
this.dodgeRechargeTimer = new Timer;
|
|
25
|
-
this.deadTimer = new Timer;
|
|
26
|
-
this.grendeThrowTimer = new Timer;
|
|
27
|
-
this.drawSize = vec2(1);
|
|
28
|
-
this.color = (new Color).setHSLA(rand(),1,.7);
|
|
29
|
-
this.renderOrder = 10;
|
|
30
|
-
this.walkCyclePercent = 0;
|
|
31
|
-
this.health = 1;
|
|
32
|
-
this.moveInput = 0;
|
|
33
|
-
this.setCollision(1,0);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
update()
|
|
37
|
-
{
|
|
38
|
-
this.gravityScale = 1; // reset to default gravity
|
|
39
|
-
|
|
40
|
-
if (this.isDead())
|
|
41
|
-
return super.update();
|
|
42
|
-
|
|
43
|
-
const moveInput = this.moveInput.copy();
|
|
44
|
-
|
|
45
|
-
// jump
|
|
46
|
-
if (!this.holdingJump)
|
|
47
|
-
this.pressedJumpTimer.unset();
|
|
48
|
-
else if (!this.wasHoldingJump || this.climbingWall)
|
|
49
|
-
this.pressedJumpTimer.set(.3);
|
|
50
|
-
this.wasHoldingJump = this.holdingJump;
|
|
51
|
-
|
|
52
|
-
// wall climb
|
|
53
|
-
this.climbingWall = 0;
|
|
54
|
-
if (moveInput.x && !this.velocity.x && this.velocity.y < 0)
|
|
55
|
-
{
|
|
56
|
-
this.velocity.y *=.8;
|
|
57
|
-
this.climbingWall = 1;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (this.dodgeTimer.active())
|
|
61
|
-
{
|
|
62
|
-
// update roll
|
|
63
|
-
this.angle = this.getMirrorSign()*2*PI*this.dodgeTimer.getPercent();
|
|
64
|
-
if (this.groundObject)
|
|
65
|
-
this.velocity.x += this.getMirrorSign()*.1;
|
|
66
|
-
}
|
|
67
|
-
else
|
|
68
|
-
{
|
|
69
|
-
// not rolling
|
|
70
|
-
this.angle = 0;
|
|
71
|
-
if (this.pressedDodge && !this.dodgeRechargeTimer.active())
|
|
72
|
-
{
|
|
73
|
-
// start dodge
|
|
74
|
-
this.dodgeTimer.set(.4);
|
|
75
|
-
this.dodgeRechargeTimer.set(2);
|
|
76
|
-
this.jumpTimer.unset();
|
|
77
|
-
sound_dodge.play(this.pos);
|
|
78
|
-
|
|
79
|
-
if (!this.groundObject && this.getAliveTime() > .2)
|
|
80
|
-
this.velocity.y += .2;
|
|
81
|
-
}
|
|
82
|
-
if (this.pressingThrow && !this.wasPressingThrow && !this.grendeThrowTimer.active())
|
|
83
|
-
{
|
|
84
|
-
// throw greande
|
|
85
|
-
const grenade = new Grenade(this.pos);
|
|
86
|
-
grenade.velocity = this.velocity.add(vec2(this.getMirrorSign(),rand(.8,.7)).normalize(.2+rand(.02)));
|
|
87
|
-
grenade.angleVelocity = this.getMirrorSign() * rand(.8,.5);
|
|
88
|
-
sound_jump.play(this.pos);
|
|
89
|
-
this.grendeThrowTimer.set(1);
|
|
90
|
-
}
|
|
91
|
-
this.wasPressingThrow = this.pressingThrow;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// allow grabbing ladder at head or feet
|
|
95
|
-
let touchingLadder = 0;
|
|
96
|
-
for (let y=2;y--;)
|
|
97
|
-
{
|
|
98
|
-
const testPos = this.pos.add(vec2(0, y + .1*moveInput.y - this.size.y/2));
|
|
99
|
-
const collisionData = getTileCollisionData(testPos);
|
|
100
|
-
touchingLadder ||= collisionData == tileType_ladder;
|
|
101
|
-
}
|
|
102
|
-
if (!touchingLadder)
|
|
103
|
-
this.climbingLadder = 0;
|
|
104
|
-
else if (moveInput.y)
|
|
105
|
-
this.climbingLadder = 1;
|
|
106
|
-
|
|
107
|
-
if (this.weapon) // update weapon trigger
|
|
108
|
-
this.weapon.triggerIsDown = this.holdingShoot && !this.dodgeTimer.active();
|
|
109
|
-
|
|
110
|
-
if (this.climbingLadder)
|
|
111
|
-
{
|
|
112
|
-
// update ladder
|
|
113
|
-
this.gravityScale = this.climbingWall = this.groundObject = 0;
|
|
114
|
-
this.jumpTimer.unset();
|
|
115
|
-
this.groundTimer.unset();
|
|
116
|
-
this.velocity = this.velocity.multiply(vec2(.85)).add(vec2(0,.02*moveInput.y));
|
|
117
|
-
|
|
118
|
-
// pull towards ladder
|
|
119
|
-
const delta = (this.pos.x|0)+.5 - this.pos.x;
|
|
120
|
-
this.velocity.x += .02*delta*abs(moveInput.x ? 0:moveInput.y);
|
|
121
|
-
moveInput.x *= .2;
|
|
122
|
-
|
|
123
|
-
// exit ladder if ground is below
|
|
124
|
-
this.climbingLadder = moveInput.y >= 0 || getTileCollisionData(this.pos.subtract(vec2(0,1))) <= 0;
|
|
125
|
-
}
|
|
126
|
-
else
|
|
127
|
-
{
|
|
128
|
-
// update jumping and ground check
|
|
129
|
-
if (this.groundObject || this.climbingWall)
|
|
130
|
-
{
|
|
131
|
-
if (!this.groundTimer.isSet())
|
|
132
|
-
sound_walk.play(this.pos); // land sound
|
|
133
|
-
|
|
134
|
-
this.groundTimer.set(.1);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (this.groundTimer.active() && !this.dodgeTimer.active())
|
|
138
|
-
{
|
|
139
|
-
// is on ground
|
|
140
|
-
if (this.pressedJumpTimer.active() && !this.jumpTimer.active())
|
|
141
|
-
{
|
|
142
|
-
// start jump
|
|
143
|
-
if (this.climbingWall)
|
|
144
|
-
this.velocity.y = .25;
|
|
145
|
-
else
|
|
146
|
-
{
|
|
147
|
-
this.velocity.y = .15;
|
|
148
|
-
this.jumpTimer.set(.2);
|
|
149
|
-
}
|
|
150
|
-
sound_jump.play(this.pos);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (this.jumpTimer.active() && !this.climbingWall)
|
|
155
|
-
{
|
|
156
|
-
// update variable height jump
|
|
157
|
-
this.groundTimer.unset();
|
|
158
|
-
if (this.holdingJump && this.velocity.y > 0 && this.jumpTimer.active())
|
|
159
|
-
this.velocity.y += .017;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (!this.groundObject)
|
|
163
|
-
{
|
|
164
|
-
// air control
|
|
165
|
-
if (sign(moveInput.x) == sign(this.velocity.x))
|
|
166
|
-
moveInput.x *= .1; // moving with velocity
|
|
167
|
-
else
|
|
168
|
-
moveInput.x *= .2; // moving against velocity (stopping)
|
|
169
|
-
|
|
170
|
-
// slight extra gravity when moving down
|
|
171
|
-
if (this.velocity.y < 0)
|
|
172
|
-
this.velocity.y += gravity*.2;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// apply movement acceleration and clamp
|
|
177
|
-
const maxCharacterSpeed = .2;
|
|
178
|
-
this.velocity.x = clamp(this.velocity.x + moveInput.x * .042, -maxCharacterSpeed, maxCharacterSpeed);
|
|
179
|
-
|
|
180
|
-
// track last pos for ladder collision code
|
|
181
|
-
this.lastPos = this.pos.copy();
|
|
182
|
-
|
|
183
|
-
// call parent and update physics
|
|
184
|
-
super.update();
|
|
185
|
-
|
|
186
|
-
// update walk cycle
|
|
187
|
-
const speed = this.velocity.length();
|
|
188
|
-
if (this.climbingLadder || this.groundTimer.active() && !this.dodgeTimer.active())
|
|
189
|
-
{
|
|
190
|
-
this.walkCyclePercent += speed * .5;
|
|
191
|
-
this.walkCyclePercent = speed > .01 ? mod(this.walkCyclePercent) : 0;
|
|
192
|
-
}
|
|
193
|
-
else
|
|
194
|
-
this.walkCyclePercent = 0;
|
|
195
|
-
|
|
196
|
-
// update walk sound
|
|
197
|
-
this.walkSoundTime += speed;
|
|
198
|
-
if (speed > .001 && ((this.climbingLadder || this.groundTimer.active()) && !this.dodgeTimer.active()))
|
|
199
|
-
{
|
|
200
|
-
if (this.walkSoundTime > 1)
|
|
201
|
-
{
|
|
202
|
-
this.walkSoundTime = this.walkSoundTime%1;
|
|
203
|
-
sound_walk.play(this.pos);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
else
|
|
207
|
-
this.walkSoundTime = 0;
|
|
208
|
-
|
|
209
|
-
// update mirror
|
|
210
|
-
if (moveInput.x && !this.dodgeTimer.active())
|
|
211
|
-
this.mirror = moveInput.x < 0;
|
|
212
|
-
|
|
213
|
-
// set tile to use
|
|
214
|
-
const tile = this.isDead() ? 3 :
|
|
215
|
-
this.climbingLadder || this.groundTimer.active() ? 3 + 2*this.walkCyclePercent|0 : 4;
|
|
216
|
-
this.tileInfo.pos.x = tile * 16;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
render()
|
|
220
|
-
{
|
|
221
|
-
let bodyPos = this.pos;
|
|
222
|
-
if (!this.isDead())
|
|
223
|
-
{
|
|
224
|
-
// bounce pos with walk cycle
|
|
225
|
-
bodyPos = bodyPos.add(vec2(0,.05*Math.sin(this.walkCyclePercent*PI)));
|
|
226
|
-
|
|
227
|
-
// make bottom flush
|
|
228
|
-
bodyPos = bodyPos.add(vec2(0,(this.drawSize.y-this.size.y)/2));
|
|
229
|
-
}
|
|
230
|
-
drawTile(bodyPos, this.drawSize, this.tileInfo, this.color, this.angle, this.mirror);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
damage(damage, damagingObject)
|
|
234
|
-
{
|
|
235
|
-
if (this.isDead() || this.getAliveTime() < 1 || this.dodgeTimer.active())
|
|
236
|
-
return;
|
|
237
|
-
|
|
238
|
-
makeBlood(damagingObject ? damagingObject.pos : this.pos);
|
|
239
|
-
super.damage(damage, damagingObject);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
kill(damagingObject)
|
|
243
|
-
{
|
|
244
|
-
if (this.isDead())
|
|
245
|
-
return;
|
|
246
|
-
|
|
247
|
-
makeBlood(this.pos, 300);
|
|
248
|
-
sound_die.play(this.pos);
|
|
249
|
-
|
|
250
|
-
this.health = 0;
|
|
251
|
-
if (this.weapon)
|
|
252
|
-
this.weapon.destroy();
|
|
253
|
-
this.deadTimer.set();
|
|
254
|
-
this.size = this.size.scale(.5);
|
|
255
|
-
const fallDirection = damagingObject ? sign(damagingObject.velocity.x) : randSign();
|
|
256
|
-
this.angleVelocity = fallDirection*rand(.22,.14);
|
|
257
|
-
this.angleDamping = .9;
|
|
258
|
-
this.renderOrder = -1; // move to back layer
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
collideWithTile(data, pos)
|
|
262
|
-
{
|
|
263
|
-
if (!data)
|
|
264
|
-
return;
|
|
265
|
-
|
|
266
|
-
if (data == tileType_ladder)
|
|
267
|
-
{
|
|
268
|
-
// handle ladder collisions
|
|
269
|
-
if (pos.y + 1 > this.lastPos.y - this.size.y/2)
|
|
270
|
-
return;
|
|
271
|
-
|
|
272
|
-
if (getTileCollisionData(pos.add(vec2(0,1))) // above
|
|
273
|
-
&& !getTileCollisionData(pos.add(vec2(1,0))) // left
|
|
274
|
-
&& !getTileCollisionData(pos.add(vec2(1,0)))) // right
|
|
275
|
-
return; // dont collide if something above it and nothing to left or right
|
|
276
|
-
|
|
277
|
-
// allow standing on top of ladders
|
|
278
|
-
return !this.climbingLadder;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const d = pos.y - this.pos.y;
|
|
282
|
-
if (!this.climbingLadder && this.velocity.y > .1 && d > 0 && d < this.size.y/2)
|
|
283
|
-
if (destroyTile(pos))
|
|
284
|
-
{
|
|
285
|
-
// break blocks above
|
|
286
|
-
this.velocity.y = 0;
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
return 1;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
295
|
-
|
|
296
12
|
class Player extends Character
|
|
297
13
|
{
|
|
298
14
|
update()
|
|
299
15
|
{
|
|
300
16
|
// player controls
|
|
301
|
-
this.holdingJump = keyIsDown(
|
|
302
|
-
this.holdingShoot = !isUsingGamepad && mouseIsDown(0) || keyIsDown(
|
|
303
|
-
this.pressingThrow =
|
|
304
|
-
this.pressedDodge =
|
|
17
|
+
this.holdingJump = keyIsDown('ArrowUp') || gamepadIsDown(0);
|
|
18
|
+
this.holdingShoot = !isUsingGamepad && mouseIsDown(0) || keyIsDown('KeyZ') || gamepadIsDown(2);
|
|
19
|
+
this.pressingThrow = keyIsDown('KeyC') || mouseIsDown(1) || gamepadIsDown(1);
|
|
20
|
+
this.pressedDodge = keyIsDown('KeyX') || mouseIsDown(2) || gamepadIsDown(3);
|
|
305
21
|
|
|
306
22
|
// movement control
|
|
307
23
|
this.moveInput = isUsingGamepad ? gamepadStick(0) :
|
|
308
|
-
vec2(keyIsDown(
|
|
24
|
+
vec2(keyIsDown('ArrowRight') - keyIsDown('ArrowLeft'), keyIsDown('ArrowUp') - keyIsDown('ArrowDown'));
|
|
309
25
|
|
|
310
26
|
super.update();
|
|
311
27
|
}
|
|
@@ -6,9 +6,10 @@
|
|
|
6
6
|
<link rel=icon type=image/png href=../favicon.png>
|
|
7
7
|
</head><body>
|
|
8
8
|
|
|
9
|
-
<script src=../../build/littlejs.js?
|
|
10
|
-
<script src=gameObjects.js?
|
|
11
|
-
<script src=
|
|
12
|
-
<script src=
|
|
13
|
-
<script src=
|
|
14
|
-
<script src=
|
|
9
|
+
<script src=../../build/littlejs.js?1812></script>
|
|
10
|
+
<script src=gameObjects.js?1812></script>
|
|
11
|
+
<script src=gameCharacter.js?1812></script>
|
|
12
|
+
<script src=gamePlayer.js?1812></script>
|
|
13
|
+
<script src=gameEffects.js?1812></script>
|
|
14
|
+
<script src=gameLevel.js?1812></script>
|
|
15
|
+
<script src=game.js?1812></script>
|
package/examples/puzzle/game.js
CHANGED
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
'use strict';
|
|
10
10
|
|
|
11
11
|
// show the LittleJS splash screen
|
|
12
|
-
|
|
12
|
+
setShowSplashScreen(true);
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
// do not use pixelated rendering
|
|
15
|
+
setCanvasPixelated(false);
|
|
15
16
|
|
|
16
17
|
const fallTime = .2;
|
|
17
18
|
const cameraOffset = vec2(0,-.5);
|
|
@@ -27,7 +27,7 @@ const fs = require('node:fs');
|
|
|
27
27
|
const child_process = require('node:child_process');
|
|
28
28
|
|
|
29
29
|
// rebuild engine
|
|
30
|
-
child_process.execSync(`npm run build`, { stdio: 'inherit' });
|
|
30
|
+
//child_process.execSync(`npm run build`, { stdio: 'inherit' });
|
|
31
31
|
|
|
32
32
|
// remove old files and setup build folder
|
|
33
33
|
fs.rmSync(BUILD_FOLDER, { recursive: true, force: true });
|
package/examples/starter/game.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
'use strict';
|
|
9
9
|
|
|
10
10
|
// show the LittleJS splash screen
|
|
11
|
-
|
|
11
|
+
setShowSplashScreen(true);
|
|
12
12
|
|
|
13
13
|
// sound effects
|
|
14
14
|
const sound_click = new Sound([1,.5]);
|
|
@@ -62,7 +62,7 @@ function gameInit()
|
|
|
62
62
|
|
|
63
63
|
// create particle emitter
|
|
64
64
|
particleEmitter = new ParticleEmitter(
|
|
65
|
-
vec2(16,
|
|
65
|
+
vec2(16,9), 0, // emitPos, emitAngle
|
|
66
66
|
1, 0, 500, PI, // emitSize, emitTime, emitRate, emiteCone
|
|
67
67
|
tile(0, 16), // tileIndex, tileSize
|
|
68
68
|
new Color(1,1,1), new Color(0,0,0), // colorStartA, colorStartB
|
|
@@ -6,18 +6,18 @@
|
|
|
6
6
|
</head><body>
|
|
7
7
|
|
|
8
8
|
<!-- LittleJS Engine -->
|
|
9
|
-
<script src=../../src/engineDebug.js?
|
|
10
|
-
<script src=../../src/engineUtilities.js?
|
|
11
|
-
<script src=../../src/engineSettings.js?
|
|
12
|
-
<script src=../../src/engineObject.js?
|
|
13
|
-
<script src=../../src/engineDraw.js?
|
|
14
|
-
<script src=../../src/engineInput.js?
|
|
15
|
-
<script src=../../src/engineAudio.js?
|
|
16
|
-
<script src=../../src/engineTileLayer.js?
|
|
17
|
-
<script src=../../src/engineParticles.js?
|
|
18
|
-
<script src=../../src/engineMedals.js?
|
|
19
|
-
<script src=../../src/engineWebGL.js?
|
|
20
|
-
<script src=../../src/engine.js?
|
|
9
|
+
<script src=../../src/engineDebug.js?1812></script>
|
|
10
|
+
<script src=../../src/engineUtilities.js?1812></script>
|
|
11
|
+
<script src=../../src/engineSettings.js?1812></script>
|
|
12
|
+
<script src=../../src/engineObject.js?1812></script>
|
|
13
|
+
<script src=../../src/engineDraw.js?1812></script>
|
|
14
|
+
<script src=../../src/engineInput.js?1812></script>
|
|
15
|
+
<script src=../../src/engineAudio.js?1812></script>
|
|
16
|
+
<script src=../../src/engineTileLayer.js?1812></script>
|
|
17
|
+
<script src=../../src/engineParticles.js?1812></script>
|
|
18
|
+
<script src=../../src/engineMedals.js?1812></script>
|
|
19
|
+
<script src=../../src/engineWebGL.js?1812></script>
|
|
20
|
+
<script src=../../src/engine.js?1812></script>
|
|
21
21
|
|
|
22
22
|
<!-- Add your game scripts here -->
|
|
23
|
-
<script src=game.js?
|
|
23
|
+
<script src=game.js?1812></script>
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
</head><body>
|
|
7
7
|
|
|
8
8
|
<!-- LittleJS Engine -->
|
|
9
|
-
<script src=../../build/littlejs.min.js?
|
|
9
|
+
<script src=../../build/littlejs.min.js?1812></script>
|
|
10
10
|
<script>
|
|
11
11
|
|
|
12
12
|
/*
|
|
@@ -23,7 +23,7 @@ let sprites, timeMS, fpsDisplay, statsDisplay, spriteColor, spriteAdditiveColor;
|
|
|
23
23
|
|
|
24
24
|
///////////////////////////////////////////////////////////////////////////////
|
|
25
25
|
|
|
26
|
-
class TestObject extends EngineObject
|
|
26
|
+
class TestObject extends EngineObject
|
|
27
27
|
{
|
|
28
28
|
constructor(pos)
|
|
29
29
|
{
|
|
@@ -36,31 +36,31 @@ class TestObject extends EngineObject
|
|
|
36
36
|
///////////////////////////////////////////////////////////////////////////////
|
|
37
37
|
function gameInit()
|
|
38
38
|
{
|
|
39
|
-
canvasFixedSize = vec2(800, 600); // 720p
|
|
40
|
-
|
|
41
39
|
// create tile collision
|
|
42
|
-
|
|
40
|
+
const w = 94;
|
|
41
|
+
initTileCollision(vec2(w,w*.5625|0));
|
|
43
42
|
for (let x = tileCollisionSize.x; x--;)
|
|
44
43
|
setTileCollisionData(vec2(x,0), 1), setTileCollisionData(vec2(x,tileCollisionSize.y-1), 1);
|
|
45
44
|
for (let y = tileCollisionSize.y; y--;)
|
|
46
45
|
setTileCollisionData(vec2(0,y), 1), setTileCollisionData(vec2(tileCollisionSize.x-1,y), 1);
|
|
47
46
|
|
|
48
47
|
// set things up
|
|
49
|
-
|
|
48
|
+
canvasFixedSize = vec2(1920, 1080); // 1080p
|
|
49
|
+
cameraScale = 20;
|
|
50
50
|
cameraPos = tileCollisionSize.scale(.5);
|
|
51
51
|
mainCanvas.style.background = '#555';
|
|
52
52
|
sprites = [];
|
|
53
|
-
gravity = -.
|
|
53
|
+
gravity = -.005;
|
|
54
54
|
|
|
55
55
|
// display stats using a div so when using glOverlay it still appears on top
|
|
56
56
|
document.body.appendChild(statsDisplay = document.createElement('div'));
|
|
57
|
-
statsDisplay.style = 'position:absolute;width:100%;
|
|
57
|
+
statsDisplay.style = 'position:absolute;width:100%;font-size:3em;text-align:center;font-family:arial;user-select:none;';
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
///////////////////////////////////////////////////////////////////////////////
|
|
61
61
|
function gameUpdate()
|
|
62
62
|
{
|
|
63
|
-
if (
|
|
63
|
+
if (mouseWasPressed(0) && !music.getSource())
|
|
64
64
|
music.play();
|
|
65
65
|
|
|
66
66
|
// mouse click = change color
|
|
@@ -73,15 +73,19 @@ function gameUpdate()
|
|
|
73
73
|
|
|
74
74
|
// mouse hold = add objects
|
|
75
75
|
if (mouseIsDown(0))
|
|
76
|
+
{
|
|
77
|
+
spriteColor = spriteColor.mutate();
|
|
78
|
+
spriteAdditiveColor = spriteAdditiveColor.mutate();
|
|
76
79
|
for (let i=100;i--;)
|
|
77
80
|
sprites.push({
|
|
78
81
|
pos:mousePos.add(randInCircle(2)),
|
|
79
|
-
angle:rand(),
|
|
82
|
+
angle:rand(2*PI),
|
|
80
83
|
velocity:randInCircle(.2),
|
|
81
|
-
color:spriteColor
|
|
82
|
-
additiveColor:spriteAdditiveColor.
|
|
84
|
+
color:spriteColor.rgbaInt(),
|
|
85
|
+
additiveColor:spriteAdditiveColor.rgbaInt()
|
|
83
86
|
});
|
|
84
|
-
|
|
87
|
+
}
|
|
88
|
+
|
|
85
89
|
// mouse wheel = zoom
|
|
86
90
|
cameraScale = clamp(cameraScale*(1-mouseWheel/10), 1, 1e3);
|
|
87
91
|
}
|
|
@@ -99,41 +103,45 @@ function gameRender()
|
|
|
99
103
|
fpsDisplay = lerp(.05, fpsDisplay||0, 1e3/(frameTimeMS - timeMS||1));
|
|
100
104
|
timeMS = frameTimeMS;
|
|
101
105
|
|
|
106
|
+
// show stats
|
|
102
107
|
const spriteCount = sprites.length
|
|
103
108
|
const objectCount = engineObjects.length;
|
|
104
|
-
statsDisplay.innerText =
|
|
109
|
+
statsDisplay.innerText =
|
|
105
110
|
spriteCount + objectCount ?
|
|
106
|
-
spriteCount + ' Sprites
|
|
107
|
-
+ objectCount + ' Objects
|
|
111
|
+
spriteCount + ' Sprites / '
|
|
112
|
+
+ (objectCount ? objectCount + ' Objects / ' : '')
|
|
108
113
|
+ fpsDisplay.toFixed(1) + ' FPS'
|
|
109
|
-
: 'LittleJS Stress Test\nLeft Click = Add
|
|
114
|
+
: 'LittleJS Stress Test\nLeft Click = Add Sprites\nRight Click = Add Objects';
|
|
115
|
+
|
|
116
|
+
// position stats
|
|
117
|
+
const canvasRect = mainCanvas.getBoundingClientRect();
|
|
118
|
+
statsDisplay.style.top = canvasRect.top+'px';
|
|
119
|
+
statsDisplay.style.fontSize = canvasRect.width*.04+'px';
|
|
110
120
|
|
|
111
|
-
|
|
121
|
+
// update sprites
|
|
112
122
|
for (const sprite of sprites)
|
|
113
123
|
{
|
|
114
124
|
// keep sprites above 0
|
|
115
125
|
sprite.pos.y = max(sprite.pos.y, 0);
|
|
116
126
|
|
|
117
|
-
// apply gravity
|
|
118
|
-
sprite.
|
|
119
|
-
|
|
120
|
-
// apply velocity
|
|
121
|
-
sprite.pos = sprite.pos.add(sprite.velocity);
|
|
127
|
+
// apply velocity and gravity
|
|
128
|
+
sprite.pos.x += sprite.velocity.x;
|
|
129
|
+
sprite.pos.y += sprite.velocity.y += gravity;
|
|
122
130
|
|
|
123
131
|
// bounce
|
|
124
132
|
if (sprite.pos.y < 0)
|
|
125
|
-
sprite.velocity.y = rand(
|
|
133
|
+
sprite.velocity.y = rand(.3,.8);
|
|
126
134
|
if (sprite.pos.x < 0)
|
|
127
135
|
sprite.pos.x = 0, sprite.velocity.x *= -1;
|
|
128
136
|
if (sprite.pos.x > tileCollisionSize.x)
|
|
129
137
|
sprite.pos.x = tileCollisionSize.x, sprite.velocity.x *= -1;
|
|
130
138
|
|
|
131
139
|
// rotate sprite
|
|
132
|
-
sprite.angle += .
|
|
140
|
+
sprite.angle += .02*sign(sprite.velocity.x);
|
|
133
141
|
|
|
134
142
|
// draw the sprite
|
|
135
|
-
glDraw(sprite.pos.x, sprite.pos.y, 1, 1, sprite.angle, 0, 0, 1, 1,
|
|
136
|
-
sprite.color
|
|
143
|
+
glDraw(sprite.pos.x, sprite.pos.y, 1, 1, sprite.angle, 0, 0, 1, 1,
|
|
144
|
+
sprite.color, sprite.additiveColor);
|
|
137
145
|
}
|
|
138
146
|
}
|
|
139
147
|
|