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
|
@@ -7,22 +7,26 @@
|
|
|
7
7
|
|
|
8
8
|
'use strict';
|
|
9
9
|
|
|
10
|
+
// import LittleJS module
|
|
11
|
+
import * as LJS from '../../dist/littlejs.esm.js';
|
|
12
|
+
const {vec2, rgb} = LJS;
|
|
13
|
+
|
|
10
14
|
///////////////////////////////////////////////////////////////////////////////
|
|
11
15
|
|
|
12
16
|
// globals
|
|
13
17
|
const levelSize = vec2(38, 20); // size of play area
|
|
14
18
|
let score = 0; // start score at 0
|
|
15
19
|
let ball; // keep track of ball object
|
|
16
|
-
let paddle; // keep track of player
|
|
20
|
+
let paddle; // keep track of player paddle
|
|
17
21
|
|
|
18
22
|
// sound effects
|
|
19
|
-
const sound_bounce = new Sound([,,1e3,,.03,.02,1,2,,,940,.03,,,,,.2,.6,,.06], 0);
|
|
20
|
-
const sound_break
|
|
21
|
-
const sound_start
|
|
23
|
+
const sound_bounce = new LJS.Sound([,,1e3,,.03,.02,1,2,,,940,.03,,,,,.2,.6,,.06], 0);
|
|
24
|
+
const sound_break = new LJS.Sound([,,90,,.01,.03,4,,,,,,,9,50,.2,,.2,.01], 0);
|
|
25
|
+
const sound_start = new LJS.Sound([,0,500,,.04,.3,1,2,,,570,.02,.02,,,,.04]);
|
|
22
26
|
|
|
23
27
|
///////////////////////////////////////////////////////////////////////////////
|
|
24
28
|
|
|
25
|
-
class Paddle extends EngineObject
|
|
29
|
+
class Paddle extends LJS.EngineObject
|
|
26
30
|
{
|
|
27
31
|
constructor()
|
|
28
32
|
{
|
|
@@ -33,14 +37,14 @@ class Paddle extends EngineObject
|
|
|
33
37
|
|
|
34
38
|
update()
|
|
35
39
|
{
|
|
36
|
-
this.pos.x = mousePos.x; // move paddle to mouse
|
|
40
|
+
this.pos.x = LJS.mousePos.x; // move paddle to mouse
|
|
37
41
|
|
|
38
42
|
// clamp paddle to level size
|
|
39
|
-
this.pos.x = clamp(this.pos.x, this.size.x/2, levelSize.x - this.size.x/2);
|
|
43
|
+
this.pos.x = LJS.clamp(this.pos.x, this.size.x/2, levelSize.x - this.size.x/2);
|
|
40
44
|
}
|
|
41
45
|
}
|
|
42
46
|
|
|
43
|
-
class Ball extends EngineObject
|
|
47
|
+
class Ball extends LJS.EngineObject
|
|
44
48
|
{
|
|
45
49
|
constructor(pos)
|
|
46
50
|
{
|
|
@@ -57,7 +61,7 @@ class Ball extends EngineObject
|
|
|
57
61
|
return false;
|
|
58
62
|
|
|
59
63
|
// speed up
|
|
60
|
-
const speed = min(1.04*this.velocity.length(), .5);
|
|
64
|
+
const speed = LJS.min(1.04*this.velocity.length(), .5);
|
|
61
65
|
this.velocity = this.velocity.normalize(speed);
|
|
62
66
|
|
|
63
67
|
// play bounce sound with pitch scaled by speed
|
|
@@ -70,7 +74,7 @@ class Ball extends EngineObject
|
|
|
70
74
|
this.velocity = this.velocity.rotate(.3 * deltaX);
|
|
71
75
|
|
|
72
76
|
// make sure ball is moving upwards with a minimum speed
|
|
73
|
-
this.velocity.y = max(-this.velocity.y, .2);
|
|
77
|
+
this.velocity.y = LJS.max(-this.velocity.y, .2);
|
|
74
78
|
|
|
75
79
|
// prevent default collision code
|
|
76
80
|
return false;
|
|
@@ -80,7 +84,7 @@ class Ball extends EngineObject
|
|
|
80
84
|
}
|
|
81
85
|
}
|
|
82
86
|
|
|
83
|
-
class Wall extends EngineObject
|
|
87
|
+
class Wall extends LJS.EngineObject
|
|
84
88
|
{
|
|
85
89
|
constructor(pos, size)
|
|
86
90
|
{
|
|
@@ -88,11 +92,11 @@ class Wall extends EngineObject
|
|
|
88
92
|
|
|
89
93
|
this.setCollision(); // make object collide
|
|
90
94
|
this.mass = 0; // make object have static physics
|
|
91
|
-
this.color =
|
|
95
|
+
this.color = rgb(0,0,0,0); // make object invisible
|
|
92
96
|
}
|
|
93
97
|
}
|
|
94
98
|
|
|
95
|
-
class Brick extends EngineObject
|
|
99
|
+
class Brick extends LJS.EngineObject
|
|
96
100
|
{
|
|
97
101
|
constructor(pos, size)
|
|
98
102
|
{
|
|
@@ -100,7 +104,7 @@ class Brick extends EngineObject
|
|
|
100
104
|
|
|
101
105
|
this.setCollision(); // make object collide
|
|
102
106
|
this.mass = 0; // make object have static physics
|
|
103
|
-
this.color = randColor(); // give brick a random color
|
|
107
|
+
this.color = LJS.randColor(); // give brick a random color
|
|
104
108
|
}
|
|
105
109
|
|
|
106
110
|
collideWithObject(o)
|
|
@@ -111,14 +115,14 @@ class Brick extends EngineObject
|
|
|
111
115
|
|
|
112
116
|
// create explosion effect
|
|
113
117
|
const color = this.color;
|
|
114
|
-
new ParticleEmitter(
|
|
115
|
-
this.pos, 0,
|
|
116
|
-
this.size, .1, 200,
|
|
117
|
-
undefined,
|
|
118
|
+
new LJS.ParticleEmitter(
|
|
119
|
+
this.pos, 0, // pos, angle
|
|
120
|
+
this.size, .1, 200, 3.14,// emitSize, emitTime, emitRate, emitCone
|
|
121
|
+
undefined, // tileInfo
|
|
118
122
|
color, color, // colorStartA, colorStartB
|
|
119
123
|
color.scale(1,0), color.scale(1,0), // colorEndA, colorEndB
|
|
120
124
|
.2, .5, 1, .1, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
|
|
121
|
-
.99, .95, .4,
|
|
125
|
+
.99, .95, .4, 3.14, // damp, angleDamp, gravity, cone
|
|
122
126
|
.1, .5, false, true // fade, randomness, collide, additive
|
|
123
127
|
);
|
|
124
128
|
|
|
@@ -129,15 +133,17 @@ class Brick extends EngineObject
|
|
|
129
133
|
///////////////////////////////////////////////////////////////////////////////
|
|
130
134
|
function gameInit()
|
|
131
135
|
{
|
|
136
|
+
// setup camera and canvas
|
|
137
|
+
LJS.setCameraPos(levelSize.scale(.5)); // center camera in level
|
|
138
|
+
LJS.setCanvasFixedSize(vec2(1280, 720)); // use a 720p fixed size canvas
|
|
139
|
+
|
|
132
140
|
// create bricks
|
|
133
141
|
for(let x=2; x<=levelSize.x-2; x+=2)
|
|
134
142
|
for(let y=12; y<=levelSize.y-2; y+=1)
|
|
135
143
|
new Brick(vec2(x,y), vec2(2,1)); // create a brick
|
|
136
144
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
paddle = new Paddle; // create player's paddle
|
|
145
|
+
// create player paddle
|
|
146
|
+
paddle = new Paddle;
|
|
141
147
|
|
|
142
148
|
// create walls
|
|
143
149
|
new Wall(vec2(-.5,levelSize.y/2), vec2(1,100)) // top
|
|
@@ -154,9 +160,10 @@ function gameUpdate()
|
|
|
154
160
|
ball.destroy();
|
|
155
161
|
ball = 0;
|
|
156
162
|
}
|
|
157
|
-
if (!ball && mouseWasPressed(0))
|
|
163
|
+
if (!ball && LJS.mouseWasPressed(0))
|
|
158
164
|
{
|
|
159
|
-
|
|
165
|
+
// spawn new ball if there is no ball and left mouse pressed
|
|
166
|
+
ball = new Ball(LJS.cameraPos); // create a ball
|
|
160
167
|
sound_start.play(); // play start sound
|
|
161
168
|
}
|
|
162
169
|
}
|
|
@@ -169,16 +176,16 @@ function gameUpdatePost()
|
|
|
169
176
|
///////////////////////////////////////////////////////////////////////////////
|
|
170
177
|
function gameRender()
|
|
171
178
|
{
|
|
172
|
-
drawRect(cameraPos, vec2(100),
|
|
173
|
-
drawRect(cameraPos, levelSize,
|
|
179
|
+
LJS.drawRect(LJS.cameraPos, vec2(100), rgb(.5,.5,.5)); // draw background
|
|
180
|
+
LJS.drawRect(LJS.cameraPos, levelSize, rgb(.1,.1,.1)); // draw level boundary
|
|
174
181
|
}
|
|
175
182
|
|
|
176
183
|
///////////////////////////////////////////////////////////////////////////////
|
|
177
184
|
function gameRenderPost()
|
|
178
185
|
{
|
|
179
|
-
drawTextScreen("Score " + score, vec2(mainCanvasSize.x/2, 70), 50); // show score
|
|
186
|
+
LJS.drawTextScreen("Score " + score, vec2(LJS.mainCanvasSize.x/2, 70), 50); // show score
|
|
180
187
|
}
|
|
181
188
|
|
|
182
189
|
///////////////////////////////////////////////////////////////////////////////
|
|
183
190
|
// Startup LittleJS Engine
|
|
184
|
-
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);
|
|
191
|
+
LJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);
|
package/examples/empty/game.js
CHANGED
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
+
// import LittleJS module
|
|
10
|
+
import * as LJS from '../../dist/littlejs.esm.js';
|
|
11
|
+
|
|
9
12
|
///////////////////////////////////////////////////////////////////////////////
|
|
10
13
|
function gameInit()
|
|
11
14
|
{
|
|
@@ -39,9 +42,9 @@ function gameRenderPost()
|
|
|
39
42
|
{
|
|
40
43
|
// called after objects are rendered
|
|
41
44
|
// draw effects or hud that appear above all objects
|
|
42
|
-
drawTextScreen('Hello World!', mainCanvasSize.scale(.5), 80);
|
|
45
|
+
LJS.drawTextScreen('Hello World!', LJS.mainCanvasSize.scale(.5), 80);
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
///////////////////////////////////////////////////////////////////////////////
|
|
46
49
|
// Startup LittleJS Engine
|
|
47
|
-
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
|
|
50
|
+
LJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
|
|
@@ -8,8 +8,12 @@
|
|
|
8
8
|
|
|
9
9
|
'use strict';
|
|
10
10
|
|
|
11
|
+
// import LittleJS module
|
|
12
|
+
import * as LJS from '../../dist/littlejs.esm.js';
|
|
13
|
+
const {vec2, hsl, tile} = LJS;
|
|
14
|
+
|
|
11
15
|
// sound effects
|
|
12
|
-
const sound_click = new Sound([1,.5]);
|
|
16
|
+
const sound_click = new LJS.Sound([1,.5]);
|
|
13
17
|
|
|
14
18
|
///////////////////////////////////////////////////////////////////////////////
|
|
15
19
|
|
|
@@ -18,12 +22,17 @@ const getMenuVisible = ()=> menu.style.visibility != 'hidden';
|
|
|
18
22
|
function setMenuVisible(visible)
|
|
19
23
|
{
|
|
20
24
|
menu.style.visibility = visible ? 'visible' : 'hidden';
|
|
21
|
-
setInputPreventDefault(!visible); // don't prevent default when menu is visible
|
|
25
|
+
LJS.setInputPreventDefault(!visible); // don't prevent default when menu is visible
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
///////////////////////////////////////////////////////////////////////////////
|
|
25
29
|
function gameInit()
|
|
26
30
|
{
|
|
31
|
+
button_test.onclick = function() { alert('Button was clicked!'); }
|
|
32
|
+
button_exitMenu.onclick = function() { setMenuVisible(false); }
|
|
33
|
+
input_test.onchange = function() { alert('New text: ' + this.value); }
|
|
34
|
+
input_rangeTest.onchange = function() { alert('New value: ' + this.value); }
|
|
35
|
+
|
|
27
36
|
// show menu for demo
|
|
28
37
|
setMenuVisible(true);
|
|
29
38
|
}
|
|
@@ -32,31 +41,31 @@ function gameInit()
|
|
|
32
41
|
function gameUpdate()
|
|
33
42
|
{
|
|
34
43
|
// play sound when mouse is pressed
|
|
35
|
-
if (mouseWasPressed(0))
|
|
36
|
-
sound_click.play(mousePos);
|
|
44
|
+
if (LJS.mouseWasPressed(0))
|
|
45
|
+
sound_click.play(LJS.mousePos);
|
|
37
46
|
}
|
|
38
47
|
|
|
39
48
|
///////////////////////////////////////////////////////////////////////////////
|
|
40
49
|
function gameUpdatePost()
|
|
41
50
|
{
|
|
42
|
-
// pause game when menu is visible
|
|
43
|
-
const menuVisible = getMenuVisible();
|
|
44
|
-
paused = menuVisible;
|
|
45
|
-
|
|
46
51
|
// open menu visibility
|
|
47
|
-
if (keyWasPressed('KeyM'))
|
|
52
|
+
if (LJS.keyWasPressed('KeyM'))
|
|
48
53
|
setMenuVisible(true);
|
|
54
|
+
|
|
55
|
+
// pause game when menu is visible
|
|
56
|
+
LJS.setPaused(getMenuVisible());
|
|
49
57
|
}
|
|
50
58
|
|
|
51
59
|
///////////////////////////////////////////////////////////////////////////////
|
|
52
60
|
function gameRender()
|
|
53
61
|
{
|
|
54
62
|
// test game rendering
|
|
55
|
-
drawRect(vec2(), vec2(1e3), hsl(0,0,.2));
|
|
63
|
+
LJS.drawRect(vec2(), vec2(1e3), hsl(0,0,.2));
|
|
56
64
|
for(let i=0; i<1e3; ++i)
|
|
57
65
|
{
|
|
66
|
+
const time = LJS.time;
|
|
58
67
|
const pos = vec2(30*Math.sin(i+time/9),20*Math.sin(i*i+time/9));
|
|
59
|
-
drawTile(pos, vec2(2), tile(3,128), hsl(i/9,1,.4), time+i, !(i%2), hsl(i/9,1,.1,0));
|
|
68
|
+
LJS.drawTile(pos, vec2(2), tile(3,128), hsl(i/9,1,.4), time+i, !(i%2), hsl(i/9,1,.1,0));
|
|
60
69
|
}
|
|
61
70
|
}
|
|
62
71
|
|
|
@@ -64,11 +73,11 @@ function gameRender()
|
|
|
64
73
|
function gameRenderPost()
|
|
65
74
|
{
|
|
66
75
|
// draw to overlay canvas for hud rendering
|
|
67
|
-
drawTextScreen('LittleJS HTML Menu Example\nM = Open menu',
|
|
68
|
-
vec2(mainCanvasSize.x/2, 70), 60,
|
|
69
|
-
hsl(0,0,1), 6, hsl(0,0,0));
|
|
76
|
+
LJS.drawTextScreen('LittleJS HTML Menu Example\nM = Open menu',
|
|
77
|
+
vec2(LJS.mainCanvasSize.x/2, 70), 60, // position, size
|
|
78
|
+
hsl(0,0,1), 6, hsl(0,0,0)); // color, outline size and color
|
|
70
79
|
}
|
|
71
80
|
|
|
72
81
|
///////////////////////////////////////////////////////////////////////////////
|
|
73
82
|
// Startup LittleJS Engine
|
|
74
|
-
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
|
|
83
|
+
LJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
visibility: hidden;
|
|
28
28
|
}
|
|
29
29
|
</style>
|
|
30
|
-
|
|
31
30
|
</head><body>
|
|
32
31
|
|
|
33
32
|
<!-- setup html menu system -->
|
|
@@ -35,13 +34,12 @@
|
|
|
35
34
|
<div id="menu" class="menuDiv">
|
|
36
35
|
<b style="font-size:50px">Test Menu</b>
|
|
37
36
|
This is an example menu using html elements.
|
|
38
|
-
<input
|
|
39
|
-
<input
|
|
40
|
-
<button
|
|
41
|
-
<button
|
|
37
|
+
<input id="input_test" value="Test Input">
|
|
38
|
+
<input id="input_rangeTest" type="range">
|
|
39
|
+
<button id="button_test">Test Button</button>
|
|
40
|
+
<button id="button_exitMenu">Exit Menu</button>
|
|
42
41
|
</div>
|
|
43
42
|
</div>
|
|
44
43
|
|
|
45
44
|
<!-- Add your game scripts here -->
|
|
46
|
-
<script src
|
|
47
|
-
<script src=game.js?11115></script>
|
|
45
|
+
<script src=game.js type=module></script>
|
package/examples/index.html
CHANGED
|
@@ -122,7 +122,7 @@ const exampleList =
|
|
|
122
122
|
new ExampleInfo('Hello World', 'helloWorld.js', 'Simplest example'),
|
|
123
123
|
new ExampleInfo('Shapes', 'shapes.js', 'How to draw geometric shapes'),
|
|
124
124
|
new ExampleInfo('Colors', 'colors.js', 'How to use the color object'),
|
|
125
|
-
new ExampleInfo('Blending', 'blending.js', '
|
|
125
|
+
new ExampleInfo('Blending', 'blending.js', 'How to use blending modes'),
|
|
126
126
|
new ExampleInfo('Timers', 'timers.js', 'How to use the timer object'),
|
|
127
127
|
new ExampleInfo('Texture', 'texture.js', 'How to display a full texture'),
|
|
128
128
|
new ExampleInfo('Particles', 'particles.js', 'How to use the particle system'),
|
|
@@ -130,12 +130,16 @@ const exampleList =
|
|
|
130
130
|
new ExampleInfo('System Font', 'systemFont.js', 'Demo of the included system font'),
|
|
131
131
|
new ExampleInfo('Sprite Atlas', 'spriteAtlas.js', 'How to set up a simple sprite atlas'),
|
|
132
132
|
new ExampleInfo('Animation', 'animation.js', 'How to animate sprites'),
|
|
133
|
-
new ExampleInfo('Clock', 'clock.js', '
|
|
133
|
+
new ExampleInfo('Clock', 'clock.js', 'Simple clock showing the time'),
|
|
134
134
|
new ExampleInfo('Tile Layer', 'tileLayer.js', 'How to use the tile layer object'),
|
|
135
|
-
new ExampleInfo('Platformer Game', 'platformer.js', '
|
|
136
|
-
new ExampleInfo('Top Down Game', 'topDown.js', '
|
|
135
|
+
new ExampleInfo('Platformer Game', 'platformer.js', 'Platformer jumping game'),
|
|
136
|
+
new ExampleInfo('Top Down Game', 'topDown.js', 'Rop down adventure game'),
|
|
137
137
|
new ExampleInfo('Tilted View', 'tiltedView.js', 'Pseudo 3D view with tilted camera'),
|
|
138
|
-
new ExampleInfo('Pong Game', 'pong.js', '
|
|
138
|
+
new ExampleInfo('Pong Game', 'pong.js', 'Simple pong game'),
|
|
139
|
+
new ExampleInfo('Box2d Demo', 'box2d.js', 'Demo of the Box2D physics plugin'),
|
|
140
|
+
new ExampleInfo('Box2d Car', 'box2dCar.js', 'Controllable vehicle using the Box2D physics plugin'),
|
|
141
|
+
new ExampleInfo('Post Processing', 'postProcess.js', 'Example of the post processing plugin'),
|
|
142
|
+
new ExampleInfo('UI System Plugin', 'uiSystem.js', 'Simple example of LittleJS UI plugin'),
|
|
139
143
|
|
|
140
144
|
new ExampleInfo('--- FULL EXAMPLES ---'),
|
|
141
145
|
new ExampleInfo('Starter', 'starter', 'Clean starter project', true),
|
|
@@ -163,9 +167,8 @@ for (const example of exampleList)
|
|
|
163
167
|
selectExample.add(o);
|
|
164
168
|
}
|
|
165
169
|
|
|
166
|
-
//
|
|
170
|
+
// set the selected example from the URL hash
|
|
167
171
|
selectExample.selectedIndex = location.hash.split`#`[1] | 0
|
|
168
|
-
|
|
169
172
|
setExample();
|
|
170
173
|
|
|
171
174
|
onresize = () =>
|
|
@@ -206,7 +209,7 @@ function codeInput()
|
|
|
206
209
|
inputTimeout = setTimeout(() => setCode(textareaCode.value), 500);
|
|
207
210
|
}
|
|
208
211
|
|
|
209
|
-
function loadFile(filename, fullExample)
|
|
212
|
+
async function loadFile(filename, fullExample)
|
|
210
213
|
{
|
|
211
214
|
textareaCode.disabled = fullExample;
|
|
212
215
|
if (fullExample)
|
|
@@ -228,14 +231,18 @@ function loadFile(filename, fullExample)
|
|
|
228
231
|
return;
|
|
229
232
|
}
|
|
230
233
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
234
|
+
try
|
|
235
|
+
{
|
|
236
|
+
const response = await fetch(filename);
|
|
237
|
+
if (!response.ok)
|
|
238
|
+
throw new Error('Could not load file: ' + filename);
|
|
239
|
+
const text = await response.text();
|
|
240
|
+
setCode(textareaCode.value = text, fullExample);
|
|
241
|
+
}
|
|
242
|
+
catch (error)
|
|
243
|
+
{
|
|
244
|
+
setErrorMessage(error.message);
|
|
245
|
+
}
|
|
239
246
|
}
|
|
240
247
|
|
|
241
248
|
function setCode(code, largeExample)
|
|
@@ -305,4 +312,4 @@ function setErrorMessage(message='')
|
|
|
305
312
|
|
|
306
313
|
</script>
|
|
307
314
|
</body>
|
|
308
|
-
</html>
|
|
315
|
+
</html>
|
package/examples/module/game.js
CHANGED
|
@@ -6,22 +6,22 @@
|
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
-
// import module
|
|
10
|
-
import * as
|
|
11
|
-
const {tile, vec2, hsl} =
|
|
9
|
+
// import LittleJS module
|
|
10
|
+
import * as LJS from '../../dist/littlejs.esm.js';
|
|
11
|
+
const {tile, vec2, hsl} = LJS;
|
|
12
12
|
|
|
13
13
|
// show the LittleJS splash screen
|
|
14
|
-
|
|
14
|
+
LJS.setShowSplashScreen(true);
|
|
15
15
|
|
|
16
16
|
// fix texture bleeding by shrinking tile slightly
|
|
17
|
-
|
|
17
|
+
LJS.setTileFixBleedScale(.5);
|
|
18
18
|
|
|
19
19
|
// sound effects
|
|
20
|
-
const sound_click = new
|
|
20
|
+
const sound_click = new LJS.Sound([1,.5]);
|
|
21
21
|
|
|
22
22
|
// medals
|
|
23
|
-
const medal_example = new
|
|
24
|
-
|
|
23
|
+
const medal_example = new LJS.Medal(0, 'Example Medal', 'Welcome to LittleJS!');
|
|
24
|
+
LJS.medalsInit('Hello World');
|
|
25
25
|
|
|
26
26
|
// game variables
|
|
27
27
|
let particleEmitter;
|
|
@@ -30,46 +30,44 @@ let particleEmitter;
|
|
|
30
30
|
function gameInit()
|
|
31
31
|
{
|
|
32
32
|
// create tile collision and visible tile layer
|
|
33
|
-
const tileCollisionSize = vec2(32, 16);
|
|
34
|
-
LittleJS.initTileCollision(tileCollisionSize);
|
|
35
33
|
const pos = vec2();
|
|
36
|
-
const tileLayer = new
|
|
34
|
+
const tileLayer = new LJS.TileCollisionLayer(pos, vec2(32,16));
|
|
37
35
|
|
|
38
36
|
// get level data from the tiles image
|
|
39
|
-
const mainContext =
|
|
40
|
-
const tileImage =
|
|
37
|
+
const mainContext = LJS.mainContext;
|
|
38
|
+
const tileImage = LJS.textureInfos[0].image;
|
|
41
39
|
mainContext.drawImage(tileImage, 0, 0);
|
|
42
40
|
const imageData = mainContext.getImageData(0,0,tileImage.width,tileImage.height).data;
|
|
43
|
-
for (pos.x =
|
|
44
|
-
for (pos.y =
|
|
41
|
+
for (pos.x = tileLayer.size.x; pos.x--;)
|
|
42
|
+
for (pos.y = tileLayer.size.y; pos.y--;)
|
|
45
43
|
{
|
|
46
44
|
// check if this pixel is set
|
|
47
|
-
const i = pos.x + tileImage.width*(15 +
|
|
45
|
+
const i = pos.x + tileImage.width*(15 + tileLayer.size.y - pos.y);
|
|
48
46
|
if (!imageData[4*i])
|
|
49
47
|
continue;
|
|
50
48
|
|
|
51
49
|
// set tile data
|
|
52
50
|
const tileIndex = 1;
|
|
53
|
-
const direction =
|
|
54
|
-
const mirror = !
|
|
55
|
-
const color =
|
|
56
|
-
const data = new
|
|
51
|
+
const direction = LJS.randInt(4)
|
|
52
|
+
const mirror = !LJS.randInt(2);
|
|
53
|
+
const color = LJS.randColor();
|
|
54
|
+
const data = new LJS.TileLayerData(tileIndex, direction, mirror, color);
|
|
57
55
|
tileLayer.setData(pos, data);
|
|
58
|
-
|
|
56
|
+
tileLayer.setCollisionData(pos);
|
|
59
57
|
}
|
|
60
58
|
|
|
61
59
|
// draw tile layer with new data
|
|
62
60
|
tileLayer.redraw();
|
|
63
61
|
|
|
64
62
|
// move camera to center of collision
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
LJS.setCameraPos(tileLayer.size.scale(.5));
|
|
64
|
+
LJS.setCameraScale(32);
|
|
67
65
|
|
|
68
66
|
// enable gravity
|
|
69
|
-
|
|
67
|
+
LJS.setGravity(vec2(0,-.01));
|
|
70
68
|
|
|
71
69
|
// create particle emitter
|
|
72
|
-
particleEmitter = new
|
|
70
|
+
particleEmitter = new LJS.ParticleEmitter(
|
|
73
71
|
vec2(16,9), 0, // emitPos, emitAngle
|
|
74
72
|
1, 0, 500, Math.PI, // emitSize, emitTime, emitRate, emitCone
|
|
75
73
|
tile(0, 16), // tileIndex, tileSize
|
|
@@ -86,14 +84,14 @@ function gameInit()
|
|
|
86
84
|
///////////////////////////////////////////////////////////////////////////////
|
|
87
85
|
function gameUpdate()
|
|
88
86
|
{
|
|
89
|
-
if (
|
|
87
|
+
if (LJS.mouseWasPressed(0))
|
|
90
88
|
{
|
|
91
89
|
// play sound when mouse is pressed
|
|
92
|
-
sound_click.play(
|
|
90
|
+
sound_click.play(LJS.mousePos);
|
|
93
91
|
|
|
94
92
|
// change particle color and set to fade out
|
|
95
93
|
particleEmitter.colorStartA = hsl();
|
|
96
|
-
particleEmitter.colorStartB =
|
|
94
|
+
particleEmitter.colorStartB = LJS.randColor();
|
|
97
95
|
particleEmitter.colorEndA = particleEmitter.colorStartA.scale(1,0);
|
|
98
96
|
particleEmitter.colorEndB = particleEmitter.colorStartB.scale(1,0);
|
|
99
97
|
|
|
@@ -102,8 +100,8 @@ function gameUpdate()
|
|
|
102
100
|
}
|
|
103
101
|
|
|
104
102
|
// move particles to mouse location if on screen
|
|
105
|
-
if (
|
|
106
|
-
particleEmitter.pos =
|
|
103
|
+
if (LJS.mousePosScreen.x)
|
|
104
|
+
particleEmitter.pos = LJS.mousePos;
|
|
107
105
|
}
|
|
108
106
|
|
|
109
107
|
///////////////////////////////////////////////////////////////////////////////
|
|
@@ -116,19 +114,19 @@ function gameUpdatePost()
|
|
|
116
114
|
function gameRender()
|
|
117
115
|
{
|
|
118
116
|
// draw a grey square in the background without using webgl
|
|
119
|
-
|
|
117
|
+
LJS.drawRect(vec2(16,8), vec2(20,14), hsl(0,0,.6), 0, false);
|
|
120
118
|
|
|
121
119
|
// draw the logo as a tile
|
|
122
|
-
|
|
120
|
+
LJS.drawTile(vec2(21,5), vec2(4.5), tile(3,128));
|
|
123
121
|
}
|
|
124
122
|
|
|
125
123
|
///////////////////////////////////////////////////////////////////////////////
|
|
126
124
|
function gameRenderPost()
|
|
127
125
|
{
|
|
128
126
|
// draw to overlay canvas for hud rendering
|
|
129
|
-
|
|
127
|
+
LJS.drawTextScreen('LittleJS with Modules', vec2(LJS.mainCanvasSize.x/2, 80), 80);
|
|
130
128
|
}
|
|
131
129
|
|
|
132
130
|
///////////////////////////////////////////////////////////////////////////////
|
|
133
131
|
// Startup LittleJS Engine
|
|
134
|
-
|
|
132
|
+
LJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
|