littlejsengine 1.11.17 → 1.12.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/box2d.wasm.js +630 -0
- package/dist/box2d.wasm.wasm +0 -0
- package/dist/littlejs.d.ts +158 -81
- package/dist/littlejs.esm.js +882 -793
- package/dist/littlejs.esm.min.js +1 -1
- package/dist/littlejs.js +3048 -175
- package/dist/littlejs.min.js +1 -1
- package/dist/littlejs.release.js +3047 -172
- package/examples/box2d/game.js +82 -56
- package/examples/box2d/gameObjects.js +114 -107
- package/examples/box2d/index.html +2 -7
- package/examples/box2d/scenes.js +72 -78
- package/examples/box2d/tiles.png +0 -0
- package/examples/breakout/game.js +63 -28
- package/examples/breakout/gameObjects.js +45 -47
- package/examples/breakout/index.html +1 -5
- package/examples/breakoutTutorial/game.js +36 -29
- package/examples/breakoutTutorial/index.html +1 -3
- package/examples/empty/game.js +5 -2
- package/examples/empty/index.html +1 -3
- package/examples/htmlMenu/game.js +24 -15
- package/examples/htmlMenu/index.html +5 -7
- package/examples/index.html +24 -17
- package/examples/module/game.js +32 -34
- package/examples/module/index.html +1 -1
- package/examples/particles/index.html +27 -22
- package/examples/platformer/game.js +62 -45
- package/examples/platformer/gameCharacter.js +42 -34
- package/examples/platformer/gameEffects.js +82 -75
- package/examples/platformer/gameLevel.js +67 -38
- package/examples/platformer/{gameLevelData.js → gameLevelData.json} +1 -11
- package/examples/platformer/gameObjects.js +70 -64
- package/examples/platformer/gamePlayer.js +12 -7
- package/examples/platformer/index.html +1 -9
- package/examples/puzzle/game.js +59 -40
- package/examples/puzzle/index.html +1 -3
- package/examples/shorts/base.html +3 -2
- package/examples/shorts/box2d.js +46 -0
- package/examples/shorts/box2dCar.js +49 -0
- package/examples/shorts/particles.js +1 -1
- package/examples/shorts/platformer.js +1 -1
- package/examples/shorts/postProcess.js +45 -0
- package/examples/shorts/tileLayer.js +5 -6
- package/examples/shorts/tiles.png +0 -0
- package/examples/shorts/uiSystem.js +39 -0
- package/examples/starter/game.js +9 -10
- package/examples/starter/index.html +13 -13
- package/examples/stress/index.html +44 -44
- package/examples/typescript/build.js +17 -18
- package/examples/typescript/game.js +32 -34
- package/examples/typescript/game.ts +32 -34
- package/examples/typescript/index.html +1 -1
- package/examples/uiSystem/game.js +34 -31
- package/examples/uiSystem/index.html +1 -5
- package/package.json +1 -1
- package/plugins/box2d.js +28 -30
- package/plugins/pluginExport.js +2 -3
- package/plugins/uiSystem.js +9 -3
- package/reference.md +29 -27
- package/src/engine.js +10 -11
- package/src/engineAudio.js +24 -15
- package/src/engineBuild.js +25 -8
- package/src/engineDebug.js +1 -3
- package/src/engineExport.js +3 -6
- package/src/engineInput.js +6 -4
- package/src/engineObject.js +18 -14
- package/src/engineSettings.js +6 -6
- package/src/engineTileLayer.js +179 -96
- package/src/engineUtilities.js +16 -0
- package/src/engineWebGL.js +13 -1
- package/examples/typescript/build/dist/littlejs.esm.js +0 -4834
- package/examples/typescript/build/examples/typescript/build.js +0 -24
- package/examples/typescript/build/examples/typescript/game.js +0 -102
package/examples/box2d/game.js
CHANGED
|
@@ -11,23 +11,54 @@
|
|
|
11
11
|
|
|
12
12
|
'use strict';
|
|
13
13
|
|
|
14
|
-
//
|
|
14
|
+
// import LittleJS module
|
|
15
|
+
import * as LJS from '../../dist/littlejs.esm.js';
|
|
16
|
+
import * as GameObjects from './gameObjects.js';
|
|
17
|
+
import * as Scenes from './scenes.js';
|
|
18
|
+
const {vec2, hsl} = LJS;
|
|
15
19
|
|
|
20
|
+
// use HD textures
|
|
21
|
+
LJS.setCanvasPixelated(false);
|
|
22
|
+
LJS.setTilesPixelated(false);
|
|
23
|
+
|
|
24
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
16
25
|
// game variables
|
|
26
|
+
|
|
17
27
|
const maxScenes = 11;
|
|
18
|
-
|
|
19
|
-
let
|
|
20
|
-
|
|
21
|
-
let groundObject;
|
|
22
|
-
let mouseJoint;
|
|
23
|
-
let car;
|
|
24
|
-
let repeatSpawnTimer = new Timer;
|
|
28
|
+
const startScene = 0;
|
|
29
|
+
export let spriteAtlas, groundObject, mouseJoint, repeatSpawnTimer = new LJS.Timer;
|
|
30
|
+
const sound_click = new LJS.Sound([.2,.1,,,,.01,,,,,,,,,,,,,,,-500]);
|
|
25
31
|
|
|
26
32
|
///////////////////////////////////////////////////////////////////////////////
|
|
27
|
-
function
|
|
33
|
+
function setScene(scene)
|
|
28
34
|
{
|
|
35
|
+
// setup
|
|
36
|
+
LJS.setCameraPos(vec2(20,10));
|
|
37
|
+
LJS.setGravity(vec2(0,-20));
|
|
38
|
+
|
|
39
|
+
// destroy old scene
|
|
40
|
+
LJS.engineObjectsDestroy();
|
|
41
|
+
mouseJoint = 0;
|
|
42
|
+
|
|
43
|
+
// create walls
|
|
44
|
+
groundObject = GameObjects.spawnBox(vec2(0,-4), vec2(1e3,8), hsl(0,0,.2), LJS.box2d.bodyTypeStatic);
|
|
45
|
+
GameObjects.spawnBox(vec2(-4, 0), vec2(8,1e3), LJS.BLACK, LJS.box2d.bodyTypeStatic);
|
|
46
|
+
GameObjects.spawnBox(vec2(44, 0), vec2(8,1e3), LJS.BLACK, LJS.box2d.bodyTypeStatic);
|
|
47
|
+
GameObjects.spawnBox(vec2(0,100), vec2(1e3,8), LJS.BLACK, LJS.box2d.bodyTypeStatic);
|
|
48
|
+
|
|
49
|
+
// load the scene
|
|
50
|
+
Scenes.loadScene(scene);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
54
|
+
async function gameInit()
|
|
55
|
+
{
|
|
56
|
+
// start up LittleJS Box2D plugin
|
|
57
|
+
await LJS.box2dInit();
|
|
58
|
+
//LJS.box2dSetDebug(true); // enable box2d debug draw
|
|
59
|
+
|
|
29
60
|
// create a table of all sprites
|
|
30
|
-
const gameTile = (i)=>
|
|
61
|
+
const gameTile = (i)=> LJS.tile(i, 496, 0, 8);
|
|
31
62
|
spriteAtlas =
|
|
32
63
|
{
|
|
33
64
|
circle: gameTile(0),
|
|
@@ -39,65 +70,60 @@ function gameInit()
|
|
|
39
70
|
squareOutline2: gameTile(6),
|
|
40
71
|
};
|
|
41
72
|
|
|
42
|
-
|
|
73
|
+
setScene(startScene);
|
|
43
74
|
}
|
|
44
75
|
|
|
45
76
|
///////////////////////////////////////////////////////////////////////////////
|
|
46
77
|
function gameUpdate()
|
|
47
78
|
{
|
|
48
79
|
// scale canvas to fit based on 1080p
|
|
49
|
-
|
|
80
|
+
LJS.setCameraScale(LJS.mainCanvasSize.y * 48 / 1080);
|
|
50
81
|
|
|
51
82
|
// mouse controls
|
|
52
|
-
if (
|
|
53
|
-
{
|
|
54
|
-
// grab object
|
|
55
|
-
sound_click.play(mousePos);
|
|
56
|
-
const object = box2d.pointCast(mousePos);
|
|
57
|
-
if (object)
|
|
58
|
-
mouseJoint = new Box2dTargetJoint(object, groundObject, mousePos);
|
|
59
|
-
}
|
|
60
|
-
if (mouseWasReleased(0))
|
|
83
|
+
if (mouseJoint)
|
|
61
84
|
{
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
if (
|
|
85
|
+
// update mouse joint
|
|
86
|
+
mouseJoint.setTarget(LJS.mousePos);
|
|
87
|
+
if (LJS.mouseWasReleased(0))
|
|
65
88
|
{
|
|
89
|
+
// release object
|
|
90
|
+
sound_click.play(LJS.mousePos, 1, .5);
|
|
66
91
|
mouseJoint.destroy();
|
|
67
92
|
mouseJoint = 0;
|
|
68
93
|
}
|
|
69
94
|
}
|
|
70
|
-
if (
|
|
95
|
+
else if (LJS.mouseWasPressed(0))
|
|
96
|
+
{
|
|
97
|
+
// grab object
|
|
98
|
+
sound_click.play(LJS.mousePos);
|
|
99
|
+
const object = LJS.box2d.pointCast(LJS.mousePos);
|
|
100
|
+
if (object)
|
|
101
|
+
mouseJoint = new LJS.Box2dTargetJoint(object, groundObject, LJS.mousePos);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// controls
|
|
105
|
+
if (LJS.mouseIsDown(1) || LJS.mouseIsDown('KeyZ'))
|
|
71
106
|
{
|
|
72
107
|
const isSet = repeatSpawnTimer.isSet();
|
|
73
108
|
if (!isSet || repeatSpawnTimer.elapsed())
|
|
74
109
|
{
|
|
75
110
|
// spawn continuously after a delay
|
|
76
111
|
isSet || repeatSpawnTimer.set(.5);
|
|
77
|
-
spawnRandomObject(mousePos);
|
|
112
|
+
GameObjects.spawnRandomObject(LJS.mousePos);
|
|
78
113
|
}
|
|
79
114
|
}
|
|
80
115
|
else
|
|
81
116
|
repeatSpawnTimer.unset();
|
|
82
|
-
if (mouseWasPressed(2) || keyWasPressed('KeyX'))
|
|
83
|
-
explosion(mousePos);
|
|
84
|
-
if (mouseJoint)
|
|
85
|
-
mouseJoint.setTarget(mousePos);
|
|
117
|
+
if (LJS.mouseWasPressed(2) || LJS.keyWasPressed('KeyX'))
|
|
118
|
+
GameObjects.explosion(LJS.mousePos);
|
|
86
119
|
|
|
87
|
-
if (keyWasPressed('KeyR'))
|
|
88
|
-
|
|
89
|
-
if (keyWasPressed('ArrowUp') || keyWasPressed('ArrowDown'))
|
|
120
|
+
if (LJS.keyWasPressed('KeyR'))
|
|
121
|
+
setScene(scene); // reset scene
|
|
122
|
+
if (LJS.keyWasPressed('ArrowUp') || LJS.keyWasPressed('ArrowDown'))
|
|
90
123
|
{
|
|
91
124
|
// change scene
|
|
92
|
-
|
|
93
|
-
scene
|
|
94
|
-
loadScene(scene);
|
|
95
|
-
}
|
|
96
|
-
if (car)
|
|
97
|
-
{
|
|
98
|
-
// update car controls
|
|
99
|
-
const input = keyDirection();
|
|
100
|
-
car.applyMotorInput(-input.x);
|
|
125
|
+
const upPressed = LJS.keyWasPressed('ArrowUp');
|
|
126
|
+
setScene(LJS.mod(Scenes.scene + (upPressed?1:-1), maxScenes));
|
|
101
127
|
}
|
|
102
128
|
}
|
|
103
129
|
|
|
@@ -111,20 +137,20 @@ function gameUpdatePost()
|
|
|
111
137
|
function gameRender()
|
|
112
138
|
{
|
|
113
139
|
// draw a grey square in the background without using webgl
|
|
114
|
-
drawRect(vec2(20,8), vec2(100), hsl(0,0,.8), 0, 0);
|
|
140
|
+
LJS.drawRect(vec2(20,8), vec2(100), hsl(0,0,.8), 0, 0);
|
|
115
141
|
|
|
116
|
-
if (scene == 5)
|
|
142
|
+
if (Scenes.scene == 5)
|
|
117
143
|
{
|
|
118
144
|
// raycast test
|
|
119
145
|
const count = 100;
|
|
120
146
|
const distance = 10;
|
|
121
147
|
for (let i=count;i--;)
|
|
122
148
|
{
|
|
123
|
-
const start = mousePos;
|
|
124
|
-
const end =
|
|
125
|
-
const result = box2d.raycast(start, end);
|
|
149
|
+
const start = LJS.mousePos;
|
|
150
|
+
const end = start.add(vec2(distance,0).rotate(i/count*LJS.PI*2));
|
|
151
|
+
const result = LJS.box2d.raycast(start, end);
|
|
126
152
|
const color = result ? hsl(0,1,.5,.5) : hsl(.5,1,.5,.5);
|
|
127
|
-
drawLine(start, result ? result.point : end, .1, color);
|
|
153
|
+
LJS.drawLine(start, result ? result.point : end, .1, color);
|
|
128
154
|
}
|
|
129
155
|
}
|
|
130
156
|
}
|
|
@@ -136,32 +162,32 @@ function gameRenderPost()
|
|
|
136
162
|
{
|
|
137
163
|
// draw mouse joint
|
|
138
164
|
const ab = mouseJoint.getAnchorB();
|
|
139
|
-
drawTile(ab, vec2(.3), spriteAtlas.circle, BLACK);
|
|
140
|
-
drawLine(mousePos, ab, .1, BLACK);
|
|
165
|
+
LJS.drawTile(ab, vec2(.3), spriteAtlas.circle, LJS.BLACK);
|
|
166
|
+
LJS.drawLine(LJS.mousePos, ab, .1, LJS.BLACK);
|
|
141
167
|
}
|
|
142
168
|
|
|
143
169
|
// draw to overlay canvas for hud rendering
|
|
144
|
-
const pos = vec2(mainCanvasSize.x/2, 50);
|
|
170
|
+
const pos = vec2(LJS.mainCanvasSize.x/2, 50);
|
|
145
171
|
drawText('LittleJS Box2D Demo', 80, 80);
|
|
146
|
-
drawText(sceneName, 60, 100);
|
|
147
|
-
if (scene == 0)
|
|
172
|
+
drawText(Scenes.sceneName, 60, 100);
|
|
173
|
+
if (Scenes.scene == 0)
|
|
148
174
|
{
|
|
149
175
|
drawText('Mouse Left = Grab');
|
|
150
176
|
drawText('Mouse Middle or Z = Spawn');
|
|
151
177
|
drawText('Mouse Right or X = Explode');
|
|
152
178
|
drawText('Arrows Up/Down = Change Scene');
|
|
153
179
|
}
|
|
154
|
-
if (scene == 3)
|
|
180
|
+
if (Scenes.scene == 3)
|
|
155
181
|
{
|
|
156
182
|
drawText('Right = Accelerate');
|
|
157
183
|
drawText('Left = Reverse');
|
|
158
184
|
}
|
|
159
185
|
|
|
160
186
|
function drawText(text, size=40, gap=50)
|
|
161
|
-
{ drawTextScreen(text, pos, size, WHITE, size*.1); pos.y += gap; }
|
|
187
|
+
{ LJS.drawTextScreen(text, pos, size, LJS.WHITE, size*.1); pos.y += gap; }
|
|
162
188
|
}
|
|
163
189
|
|
|
164
190
|
///////////////////////////////////////////////////////////////////////////////
|
|
165
191
|
// Startup LittleJS Engine with Box2D
|
|
166
192
|
|
|
167
|
-
|
|
193
|
+
LJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
|