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
|
@@ -4,56 +4,54 @@
|
|
|
4
4
|
- Shows how to use LittleJS with modules
|
|
5
5
|
*/
|
|
6
6
|
'use strict';
|
|
7
|
-
// import module
|
|
8
|
-
import * as
|
|
9
|
-
const { tile, vec2, hsl } =
|
|
7
|
+
// import LittleJS module
|
|
8
|
+
import * as LJS from '../../dist/littlejs.esm.js';
|
|
9
|
+
const { tile, vec2, hsl } = LJS;
|
|
10
10
|
// show the LittleJS splash screen
|
|
11
|
-
|
|
11
|
+
LJS.setShowSplashScreen(true);
|
|
12
12
|
// fix texture bleeding by shrinking tile slightly
|
|
13
|
-
|
|
13
|
+
LJS.setTileFixBleedScale(.5);
|
|
14
14
|
// sound effects
|
|
15
|
-
const sound_click = new
|
|
15
|
+
const sound_click = new LJS.Sound([1, .5]);
|
|
16
16
|
// medals
|
|
17
|
-
const medal_example = new
|
|
18
|
-
|
|
17
|
+
const medal_example = new LJS.Medal(0, 'Example Medal', 'Welcome to LittleJS!');
|
|
18
|
+
LJS.medalsInit('Hello World');
|
|
19
19
|
// game variables
|
|
20
20
|
let particleEmitter;
|
|
21
21
|
///////////////////////////////////////////////////////////////////////////////
|
|
22
22
|
function gameInit() {
|
|
23
23
|
// create tile collision and visible tile layer
|
|
24
|
-
const tileCollisionSize = vec2(32, 16);
|
|
25
|
-
LittleJS.initTileCollision(tileCollisionSize);
|
|
26
24
|
const pos = vec2();
|
|
27
|
-
const tileLayer = new
|
|
25
|
+
const tileLayer = new LJS.TileCollisionLayer(pos, vec2(32, 16));
|
|
28
26
|
// get level data from the tiles image
|
|
29
|
-
const mainContext =
|
|
30
|
-
const tileImage =
|
|
27
|
+
const mainContext = LJS.mainContext;
|
|
28
|
+
const tileImage = LJS.textureInfos[0].image;
|
|
31
29
|
mainContext.drawImage(tileImage, 0, 0);
|
|
32
30
|
const imageData = mainContext.getImageData(0, 0, tileImage.width, tileImage.height).data;
|
|
33
|
-
for (pos.x =
|
|
34
|
-
for (pos.y =
|
|
31
|
+
for (pos.x = tileLayer.size.x; pos.x--;)
|
|
32
|
+
for (pos.y = tileLayer.size.y; pos.y--;) {
|
|
35
33
|
// check if this pixel is set
|
|
36
|
-
const i = pos.x + tileImage.width * (15 +
|
|
34
|
+
const i = pos.x + tileImage.width * (15 + tileLayer.size.y - pos.y);
|
|
37
35
|
if (!imageData[4 * i])
|
|
38
36
|
continue;
|
|
39
37
|
// set tile data
|
|
40
38
|
const tileIndex = 1;
|
|
41
|
-
const direction =
|
|
42
|
-
const mirror = !
|
|
43
|
-
const color =
|
|
44
|
-
const data = new
|
|
39
|
+
const direction = LJS.randInt(4);
|
|
40
|
+
const mirror = !LJS.randInt(2);
|
|
41
|
+
const color = LJS.randColor();
|
|
42
|
+
const data = new LJS.TileLayerData(tileIndex, direction, mirror, color);
|
|
45
43
|
tileLayer.setData(pos, data);
|
|
46
|
-
|
|
44
|
+
tileLayer.setCollisionData(pos);
|
|
47
45
|
}
|
|
48
46
|
// draw tile layer with new data
|
|
49
47
|
tileLayer.redraw();
|
|
50
48
|
// move camera to center of collision
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
LJS.setCameraPos(tileLayer.size.scale(.5));
|
|
50
|
+
LJS.setCameraScale(32);
|
|
53
51
|
// enable gravity
|
|
54
|
-
|
|
52
|
+
LJS.setGravity(vec2(0, -.01));
|
|
55
53
|
// create particle emitter
|
|
56
|
-
particleEmitter = new
|
|
54
|
+
particleEmitter = new LJS.ParticleEmitter(vec2(16, 9), 0, // emitPos, emitAngle
|
|
57
55
|
1, 0, 500, Math.PI, // emitSize, emitTime, emitRate, emitCone
|
|
58
56
|
tile(0, 16), // tileIndex, tileSize
|
|
59
57
|
hsl(1, 1, 1), hsl(0, 0, 0), // colorStartA, colorStartB
|
|
@@ -67,20 +65,20 @@ function gameInit() {
|
|
|
67
65
|
}
|
|
68
66
|
///////////////////////////////////////////////////////////////////////////////
|
|
69
67
|
function gameUpdate() {
|
|
70
|
-
if (
|
|
68
|
+
if (LJS.mouseWasPressed(0)) {
|
|
71
69
|
// play sound when mouse is pressed
|
|
72
|
-
sound_click.play(
|
|
70
|
+
sound_click.play(LJS.mousePos);
|
|
73
71
|
// change particle color and set to fade out
|
|
74
72
|
particleEmitter.colorStartA = hsl();
|
|
75
|
-
particleEmitter.colorStartB =
|
|
73
|
+
particleEmitter.colorStartB = LJS.randColor();
|
|
76
74
|
particleEmitter.colorEndA = particleEmitter.colorStartA.scale(1, 0);
|
|
77
75
|
particleEmitter.colorEndB = particleEmitter.colorStartB.scale(1, 0);
|
|
78
76
|
// unlock medals
|
|
79
77
|
medal_example.unlock();
|
|
80
78
|
}
|
|
81
79
|
// move particles to mouse location if on screen
|
|
82
|
-
if (
|
|
83
|
-
particleEmitter.pos =
|
|
80
|
+
if (LJS.mousePosScreen.x)
|
|
81
|
+
particleEmitter.pos = LJS.mousePos;
|
|
84
82
|
}
|
|
85
83
|
///////////////////////////////////////////////////////////////////////////////
|
|
86
84
|
function gameUpdatePost() {
|
|
@@ -88,15 +86,15 @@ function gameUpdatePost() {
|
|
|
88
86
|
///////////////////////////////////////////////////////////////////////////////
|
|
89
87
|
function gameRender() {
|
|
90
88
|
// draw a grey square in the background without using webgl
|
|
91
|
-
|
|
89
|
+
LJS.drawRect(vec2(16, 8), vec2(20, 14), hsl(0, 0, .6), 0, false);
|
|
92
90
|
// draw the logo as a tile
|
|
93
|
-
|
|
91
|
+
LJS.drawTile(vec2(21, 5), vec2(4.5), tile(3, 128));
|
|
94
92
|
}
|
|
95
93
|
///////////////////////////////////////////////////////////////////////////////
|
|
96
94
|
function gameRenderPost() {
|
|
97
95
|
// draw to overlay canvas for hud rendering
|
|
98
|
-
|
|
96
|
+
LJS.drawTextScreen('LittleJS with TypeScript', vec2(LJS.mainCanvasSize.x / 2, 80), 80);
|
|
99
97
|
}
|
|
100
98
|
///////////////////////////////////////////////////////////////////////////////
|
|
101
99
|
// Startup LittleJS Engine
|
|
102
|
-
|
|
100
|
+
LJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
|
|
@@ -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 TypeScript', 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']);
|
|
@@ -6,8 +6,12 @@
|
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
+
// import LittleJS module
|
|
10
|
+
import * as LJS from '../../dist/littlejs.esm.js';
|
|
11
|
+
const {vec2, hsl, tile} = LJS;
|
|
12
|
+
|
|
9
13
|
// sound effects
|
|
10
|
-
const sound_ui = new Sound([1,0]);
|
|
14
|
+
const sound_ui = new LJS.Sound([1,0]);
|
|
11
15
|
|
|
12
16
|
// UI system
|
|
13
17
|
let uiRoot, uiMenu;
|
|
@@ -15,43 +19,43 @@ const getMenuVisible =()=> uiMenu.visible;
|
|
|
15
19
|
const setMenuVisible =(visible)=> uiMenu.visible = visible;
|
|
16
20
|
|
|
17
21
|
// use a fixed size canvas
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
LJS.setCanvasFixedSize(vec2(1920, 1080)); // 1080p
|
|
23
|
+
LJS.setCanvasPixelated(false);
|
|
20
24
|
|
|
21
25
|
function createUI()
|
|
22
26
|
{
|
|
23
27
|
// setup root to attach all ui elements to
|
|
24
|
-
uiRoot = new UIObject
|
|
25
|
-
const uiInfo = new UIText(vec2(0,90), vec2(1e3, 70),
|
|
28
|
+
uiRoot = new LJS.UIObject;
|
|
29
|
+
const uiInfo = new LJS.UIText(vec2(0,90), vec2(1e3, 70),
|
|
26
30
|
'LittleJS UI System Example\nM = Toggle menu');
|
|
27
|
-
uiInfo.textColor = WHITE;
|
|
31
|
+
uiInfo.textColor = LJS.WHITE;
|
|
28
32
|
uiInfo.lineWidth = 8;
|
|
29
33
|
uiRoot.addChild(uiInfo);
|
|
30
34
|
|
|
31
35
|
// setup example menu
|
|
32
|
-
uiMenu = new UIObject(vec2(0,450));
|
|
36
|
+
uiMenu = new LJS.UIObject(vec2(0,450));
|
|
33
37
|
uiRoot.addChild(uiMenu);
|
|
34
|
-
const uiBackground = new UIObject(vec2(0,0), vec2(450,580));
|
|
38
|
+
const uiBackground = new LJS.UIObject(vec2(0,0), vec2(450,580));
|
|
35
39
|
uiBackground.lineWidth = 8;
|
|
36
40
|
uiMenu.addChild(uiBackground);
|
|
37
41
|
|
|
38
42
|
// example large text
|
|
39
|
-
const textTitle = new UIText(vec2(0,-220), vec2(500, 90), 'Test Title');
|
|
43
|
+
const textTitle = new LJS.UIText(vec2(0,-220), vec2(500, 90), 'Test Title');
|
|
40
44
|
uiMenu.addChild(textTitle);
|
|
41
|
-
textTitle.textColor = RED;
|
|
45
|
+
textTitle.textColor = LJS.RED;
|
|
42
46
|
textTitle.lineWidth = 4;
|
|
43
|
-
textTitle.lineColor = BLUE;
|
|
47
|
+
textTitle.lineColor = LJS.BLUE;
|
|
44
48
|
|
|
45
49
|
// example multiline text
|
|
46
|
-
const textTest = new UIText(vec2(-60,-140), vec2(300, 50), 'Test Text\nSecond text line.')
|
|
50
|
+
const textTest = new LJS.UIText(vec2(-60,-140), vec2(300, 50), 'Test Text\nSecond text line.')
|
|
47
51
|
uiMenu.addChild(textTest);
|
|
48
52
|
|
|
49
53
|
// example tile image
|
|
50
|
-
const tileTest = new UITile(vec2(150,-130), vec2(110), tile(3,128))
|
|
54
|
+
const tileTest = new LJS.UITile(vec2(150,-130), vec2(110), tile(3,128))
|
|
51
55
|
uiMenu.addChild(tileTest);
|
|
52
56
|
|
|
53
57
|
// example checkbox
|
|
54
|
-
const checkbox = new UICheckbox(vec2(-140,-20), vec2(40));
|
|
58
|
+
const checkbox = new LJS.UICheckbox(vec2(-140,-20), vec2(40));
|
|
55
59
|
uiMenu.addChild(checkbox);
|
|
56
60
|
checkbox.onPress = ()=>
|
|
57
61
|
{
|
|
@@ -61,18 +65,18 @@ function createUI()
|
|
|
61
65
|
}
|
|
62
66
|
|
|
63
67
|
// text attached to checkbox
|
|
64
|
-
const checkboxText = new UIText(vec2(170,0), vec2(300, 40), 'Test Checkbox');
|
|
68
|
+
const checkboxText = new LJS.UIText(vec2(170,0), vec2(300, 40), 'Test Checkbox');
|
|
65
69
|
checkbox.addChild(checkboxText);
|
|
66
70
|
|
|
67
71
|
// example scrollbar
|
|
68
|
-
const scrollbar = new UIScrollbar(vec2(0,60), vec2(350, 50));
|
|
72
|
+
const scrollbar = new LJS.UIScrollbar(vec2(0,60), vec2(350, 50));
|
|
69
73
|
uiMenu.addChild(scrollbar);
|
|
70
74
|
scrollbar.onChange = ()=> console.log('New scrollbar value:', scrollbar.value);
|
|
71
75
|
scrollbar.onPress = ()=> sound_ui.play(0,.3,2);
|
|
72
76
|
scrollbar.onRelease = ()=> sound_ui.play(0,.3,4);
|
|
73
77
|
|
|
74
78
|
// example button
|
|
75
|
-
const button1 = new UIButton(vec2(0,140), vec2(350, 50), 'Test Button');
|
|
79
|
+
const button1 = new LJS.UIButton(vec2(0,140), vec2(350, 50), 'Test Button');
|
|
76
80
|
uiMenu.addChild(button1);
|
|
77
81
|
button1.onPress = ()=>
|
|
78
82
|
{
|
|
@@ -81,7 +85,7 @@ function createUI()
|
|
|
81
85
|
}
|
|
82
86
|
|
|
83
87
|
// exit button
|
|
84
|
-
const button2 = new UIButton(vec2(0,220), vec2(350, 50), 'Exit Menu');
|
|
88
|
+
const button2 = new LJS.UIButton(vec2(0,220), vec2(350, 50), 'Exit Menu');
|
|
85
89
|
uiMenu.addChild(button2);
|
|
86
90
|
button2.onPress = ()=>
|
|
87
91
|
{
|
|
@@ -94,7 +98,7 @@ function createUI()
|
|
|
94
98
|
///////////////////////////////////////////////////////////////////////////////
|
|
95
99
|
function gameInit()
|
|
96
100
|
{
|
|
97
|
-
new UISystemPlugin;
|
|
101
|
+
new LJS.UISystemPlugin;
|
|
98
102
|
createUI();
|
|
99
103
|
}
|
|
100
104
|
|
|
@@ -106,27 +110,26 @@ function gameUpdate()
|
|
|
106
110
|
///////////////////////////////////////////////////////////////////////////////
|
|
107
111
|
function gameUpdatePost()
|
|
108
112
|
{
|
|
109
|
-
//
|
|
110
|
-
|
|
113
|
+
// toggle menu visibility
|
|
114
|
+
if (LJS.keyWasPressed('KeyM'))
|
|
115
|
+
setMenuVisible(!getMenuVisible());
|
|
111
116
|
|
|
112
|
-
//
|
|
113
|
-
|
|
114
|
-
paused = menuVisible;
|
|
117
|
+
// center ui
|
|
118
|
+
uiRoot.pos.x = LJS.mainCanvasSize.x/2;
|
|
115
119
|
|
|
116
|
-
//
|
|
117
|
-
|
|
118
|
-
setMenuVisible(!menuVisible);
|
|
120
|
+
// pause when menu is visible
|
|
121
|
+
LJS.setPaused(getMenuVisible())
|
|
119
122
|
}
|
|
120
123
|
|
|
121
124
|
///////////////////////////////////////////////////////////////////////////////
|
|
122
125
|
function gameRender()
|
|
123
126
|
{
|
|
124
127
|
// test game rendering
|
|
125
|
-
drawRect(vec2(), vec2(1e3), hsl(0,0,.2));
|
|
128
|
+
LJS.drawRect(vec2(), vec2(1e3), hsl(0,0,.2));
|
|
126
129
|
for(let i=0; i<1e3; ++i)
|
|
127
130
|
{
|
|
128
|
-
const pos = vec2(30*Math.sin(i+time/9),20*Math.sin(i*i+time/9));
|
|
129
|
-
drawTile(pos, vec2(2), tile(3,128), hsl(i/9,1,.4), time+i, !(i%2), hsl(i/9,1,.1,0));
|
|
131
|
+
const pos = vec2(30*Math.sin(i+LJS.time/9),20*Math.sin(i*i+LJS.time/9));
|
|
132
|
+
LJS.drawTile(pos, vec2(2), tile(3,128), hsl(i/9,1,.4), LJS.time+i, !(i%2), hsl(i/9,1,.1,0));
|
|
130
133
|
}
|
|
131
134
|
}
|
|
132
135
|
|
|
@@ -137,4 +140,4 @@ function gameRenderPost()
|
|
|
137
140
|
|
|
138
141
|
///////////////////////////////////////////////////////////////////////////////
|
|
139
142
|
// Startup LittleJS Engine
|
|
140
|
-
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
|
|
143
|
+
LJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, ['tiles.png']);
|
|
@@ -6,9 +6,5 @@
|
|
|
6
6
|
<link rel=icon type=image/png href=../favicon.png>
|
|
7
7
|
</head><body>
|
|
8
8
|
|
|
9
|
-
<!-- LittleJS Engine -->
|
|
10
|
-
<script src=../../dist/littlejs.js?11115></script>
|
|
11
|
-
|
|
12
9
|
<!-- Add your game scripts here -->
|
|
13
|
-
<script src
|
|
14
|
-
<script src=game.js?11115></script>
|
|
10
|
+
<script src=game.js type=module></script>
|
package/package.json
CHANGED
package/plugins/box2d.js
CHANGED
|
@@ -1,29 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* LittleJS Box2D Physics Plugin
|
|
3
3
|
* - Box2dObject extends EngineObject with Box2D physics
|
|
4
|
-
* - Call
|
|
4
|
+
* - Call box2dInit() before engineInit() to enable
|
|
5
5
|
* - You will also need to include box2d.wasm.js
|
|
6
|
-
* - Uses
|
|
6
|
+
* - Uses a super fast web assembly port of Box2D
|
|
7
7
|
* - More info: https://github.com/kripken/box2d.js
|
|
8
|
-
* - Fully wraps everything in Box2d
|
|
9
8
|
* - Functions to create polygon, circle, and edge shapes
|
|
10
|
-
* - Raycasting and querying
|
|
11
|
-
* - Joint creation
|
|
12
9
|
* - Contact begin and end callbacks
|
|
10
|
+
* - Wraps b2Vec2 type to/from Vector2
|
|
11
|
+
* - Raycasting and querying
|
|
12
|
+
* - Every type of joint
|
|
13
13
|
* - Debug physics drawing
|
|
14
|
+
* @namespace Box2D
|
|
14
15
|
*/
|
|
15
16
|
|
|
16
17
|
'use strict';
|
|
17
18
|
|
|
18
19
|
/** Global Box2d Plugin object
|
|
19
|
-
* @type {Box2dPlugin}
|
|
20
|
+
* @type {Box2dPlugin}
|
|
21
|
+
* @memberof Box2D */
|
|
20
22
|
let box2d;
|
|
21
23
|
|
|
22
24
|
/** Enable Box2D debug drawing
|
|
23
25
|
* @type {boolean}
|
|
24
|
-
* @default
|
|
26
|
+
* @default
|
|
27
|
+
* @memberof Box2D */
|
|
25
28
|
let box2dDebug = false;
|
|
26
29
|
|
|
30
|
+
/** Enable Box2D debug drawing
|
|
31
|
+
* @param {boolean} enable
|
|
32
|
+
* @memberof Box2D */
|
|
33
|
+
function box2dSetDebug(enable) { box2dDebug = enable; }
|
|
34
|
+
|
|
27
35
|
///////////////////////////////////////////////////////////////////////////////
|
|
28
36
|
/**
|
|
29
37
|
* Box2D Object - extend with your own custom physics objects
|
|
@@ -123,7 +131,7 @@ class Box2dObject extends EngineObject
|
|
|
123
131
|
* @param {number} [friction]
|
|
124
132
|
* @param {number} [restitution]
|
|
125
133
|
* @param {boolean} [isSensor] */
|
|
126
|
-
addShape(shape, density=1, friction
|
|
134
|
+
addShape(shape, density=1, friction=1, restitution=0, isSensor=false)
|
|
127
135
|
{
|
|
128
136
|
const fd = new box2d.instance.b2FixtureDef();
|
|
129
137
|
fd.set_shape(shape);
|
|
@@ -1427,7 +1435,7 @@ class Box2dPlugin
|
|
|
1427
1435
|
* @param {number} [frames] */
|
|
1428
1436
|
step(frames=1)
|
|
1429
1437
|
{
|
|
1430
|
-
box2d.world.SetGravity(box2d.vec2dTo(
|
|
1438
|
+
box2d.world.SetGravity(box2d.vec2dTo(gravity));
|
|
1431
1439
|
for (let i=frames; i--;)
|
|
1432
1440
|
box2d.world.Step(timeDelta, this.velocityIterations, this.positionIterations);
|
|
1433
1441
|
}
|
|
@@ -1568,8 +1576,8 @@ class Box2dPlugin
|
|
|
1568
1576
|
aabb.set_upperBound(box2d.vec2dTo(pos));
|
|
1569
1577
|
|
|
1570
1578
|
let queryObject;
|
|
1571
|
-
debugRaycast && debugRect(pos, vec2(), queryObject ? '#f00' : '#00f', .02);
|
|
1572
1579
|
box2d.world.QueryAABB(queryCallback, aabb);
|
|
1580
|
+
debugRaycast && debugRect(pos, vec2(), queryObject ? '#f00' : '#00f', .02);
|
|
1573
1581
|
return queryObject;
|
|
1574
1582
|
}
|
|
1575
1583
|
|
|
@@ -1762,28 +1770,18 @@ class Box2dPlugin
|
|
|
1762
1770
|
}
|
|
1763
1771
|
|
|
1764
1772
|
///////////////////////////////////////////////////////////////////////////////
|
|
1765
|
-
/** Box2d Init -
|
|
1766
|
-
* @
|
|
1767
|
-
* @
|
|
1768
|
-
|
|
1769
|
-
* @param {Function} gameRender - Called before objects are rendered, for drawing the background
|
|
1770
|
-
* @param {Function} gameRenderPost - Called after objects are rendered, useful for drawing UI
|
|
1771
|
-
* @param {Array<string>} [imageSources=[]] - List of images to load
|
|
1772
|
-
* @param {HTMLElement} [rootElement] - Root element to attach to, the document body by default */
|
|
1773
|
-
function box2dEngineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources, rootElement)
|
|
1773
|
+
/** Box2d Init - Call with await before starting LittleJS to init box2d
|
|
1774
|
+
* @return {Promise<Box2dPlugin>}
|
|
1775
|
+
* @memberof Box2D */
|
|
1776
|
+
async function box2dInit()
|
|
1774
1777
|
{
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
// start littlejs
|
|
1782
|
-
engineAddPlugin(box2dUpdate, box2dRender);
|
|
1783
|
-
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources, rootElement);
|
|
1784
|
-
});
|
|
1778
|
+
// load box2d
|
|
1779
|
+
new Box2dPlugin(await Box2D());
|
|
1780
|
+
setupDebugDraw();
|
|
1781
|
+
engineAddPlugin(box2dUpdate, box2dRender);
|
|
1782
|
+
return box2d;
|
|
1785
1783
|
|
|
1786
|
-
//
|
|
1784
|
+
// add the box2d plugin to the engine
|
|
1787
1785
|
function box2dUpdate()
|
|
1788
1786
|
{
|
|
1789
1787
|
if (!paused)
|
package/plugins/pluginExport.js
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
* LittleJS Module Plugins Export
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
'use strict';
|
|
6
|
-
|
|
7
5
|
export
|
|
8
6
|
{
|
|
9
7
|
// Newgrounds
|
|
@@ -31,7 +29,8 @@ export
|
|
|
31
29
|
// Box2D Physics
|
|
32
30
|
box2d,
|
|
33
31
|
box2dDebug,
|
|
34
|
-
|
|
32
|
+
box2dSetDebug,
|
|
33
|
+
box2dInit,
|
|
35
34
|
Box2dPlugin,
|
|
36
35
|
Box2dObject,
|
|
37
36
|
Box2dRaycastResult,
|
package/plugins/uiSystem.js
CHANGED
|
@@ -177,6 +177,8 @@ class UIObject
|
|
|
177
177
|
this.lineWidth = uiSystem.defaultLineWidth;
|
|
178
178
|
/** @property {string} */
|
|
179
179
|
this.font = uiSystem.defaultFont;
|
|
180
|
+
/** @property {number} - override for text height */
|
|
181
|
+
this.textHeight = undefined;
|
|
180
182
|
/** @property {boolean} */
|
|
181
183
|
this.visible = true;
|
|
182
184
|
/** @property {Array<UIObject>} */
|
|
@@ -287,7 +289,8 @@ class UIText extends UIObject
|
|
|
287
289
|
}
|
|
288
290
|
render()
|
|
289
291
|
{
|
|
290
|
-
|
|
292
|
+
const textSize = vec2(this.size.x, this.textHeight || this.size.y);
|
|
293
|
+
uiSystem.drawText(this.text, this.pos, textSize, this.textColor, this.lineWidth, this.lineColor, this.align, this.font);
|
|
291
294
|
}
|
|
292
295
|
}
|
|
293
296
|
|
|
@@ -350,7 +353,9 @@ class UIButton extends UIObject
|
|
|
350
353
|
const lineColor = this.mouseIsHeld ? this.color : this.lineColor;
|
|
351
354
|
const color = this.mouseIsOver? this.hoverColor : this.color;
|
|
352
355
|
uiSystem.drawRect(this.pos, this.size, color, this.lineWidth, lineColor);
|
|
353
|
-
|
|
356
|
+
|
|
357
|
+
const textScale = .8; // scale text to fit in button
|
|
358
|
+
const textSize = vec2(this.size.x, this.textHeight || this.size.y*textScale);
|
|
354
359
|
uiSystem.drawText(this.text, this.pos, textSize,
|
|
355
360
|
this.textColor, 0, undefined, this.align, this.font);
|
|
356
361
|
}
|
|
@@ -447,7 +452,8 @@ class UIScrollbar extends UIObject
|
|
|
447
452
|
const barColor = this.mouseIsHeld ? this.color : this.handleColor;
|
|
448
453
|
uiSystem.drawRect(handlePos, handleSize, barColor, this.lineWidth, this.lineColor);
|
|
449
454
|
|
|
450
|
-
const
|
|
455
|
+
const textScale = .8; // scale text to fit in scrollbar
|
|
456
|
+
const textSize = vec2(this.size.x, this.textHeight || this.size.y*textScale);
|
|
451
457
|
uiSystem.drawText(this.text, this.pos, textSize,
|
|
452
458
|
this.textColor, 0, undefined, this.align, this.font);
|
|
453
459
|
}
|