littlejsengine 1.18.2 → 1.18.7
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/COPYRIGHT.txt +38 -0
- package/FAQ.md +633 -0
- package/LICENSE +5 -27
- package/README.md +19 -1
- package/dist/littlejs.d.ts +550 -57
- package/dist/littlejs.esm.js +2208 -492
- package/dist/littlejs.esm.min.js +1 -1
- package/dist/littlejs.js +2165 -480
- package/dist/littlejs.min.js +1 -1
- package/dist/littlejs.release.js +2166 -481
- package/package.json +8 -1
- package/plugins/box2d.js +36 -9
- package/{src/engineMedals.js → plugins/medalSystem.js} +248 -196
- package/plugins/pathFinder.js +758 -0
- package/plugins/pluginExport.js +26 -0
- package/plugins/tween.js +509 -0
- package/plugins/tweenSystem.js +509 -0
- package/plugins/uiSystem.js +151 -12
- package/src/engine.js +9 -145
- package/src/engineAudio.js +8 -5
- package/src/engineBuild.mjs +4 -1
- package/src/engineDebug.js +1 -1
- package/src/engineDraw.js +154 -5
- package/src/engineExport.js +17 -12
- package/src/engineInput.js +27 -1
- package/src/engineLogo.js +146 -0
- package/src/engineMath.js +10 -3
- package/src/engineObject.js +11 -5
- package/src/engineParticles.js +6 -3
- package/src/engineRelease.js +1 -1
- package/src/engineSettings.js +17 -47
- package/src/engineUtilities.js +7 -2
- package/src/engineWebGL.js +31 -8
- package/src/jsconfig.json +3 -1
- package/.claude/settings.local.json +0 -7
- package/.github/workflows/test.yml +0 -17
- package/AI.md +0 -172
- package/CLAUDE.md +0 -1
- package/examples/box2d/game.js +0 -192
- package/examples/box2d/gameObjects.js +0 -564
- package/examples/box2d/index.html +0 -9
- package/examples/box2d/scenes.js +0 -194
- package/examples/box2d/tiles.png +0 -0
- package/examples/breakout/game.js +0 -175
- package/examples/breakout/gameObjects.js +0 -147
- package/examples/breakout/index.html +0 -8
- package/examples/breakout/tiles.png +0 -0
- package/examples/breakoutTutorial/README.md +0 -521
- package/examples/breakoutTutorial/game.js +0 -191
- package/examples/breakoutTutorial/images/1.png +0 -0
- package/examples/breakoutTutorial/images/10.png +0 -0
- package/examples/breakoutTutorial/images/11.png +0 -0
- package/examples/breakoutTutorial/images/2.png +0 -0
- package/examples/breakoutTutorial/images/3.png +0 -0
- package/examples/breakoutTutorial/images/4.png +0 -0
- package/examples/breakoutTutorial/images/5.png +0 -0
- package/examples/breakoutTutorial/images/6.png +0 -0
- package/examples/breakoutTutorial/images/7.png +0 -0
- package/examples/breakoutTutorial/images/8.png +0 -0
- package/examples/breakoutTutorial/images/9.png +0 -0
- package/examples/breakoutTutorial/index.html +0 -8
- package/examples/electron/build.mjs +0 -127
- package/examples/electron/electron.js +0 -35
- package/examples/electron/game.js +0 -54
- package/examples/electron/index.html +0 -13
- package/examples/electron/package.json +0 -5
- package/examples/electron/tiles.png +0 -0
- package/examples/empty/game.js +0 -51
- package/examples/empty/index.html +0 -5
- package/examples/empty/tiles.png +0 -0
- package/examples/favicon.png +0 -0
- package/examples/games.jpg +0 -0
- package/examples/htmlMenu/game.js +0 -82
- package/examples/htmlMenu/index.html +0 -45
- package/examples/htmlMenu/tiles.png +0 -0
- package/examples/index.html +0 -800
- package/examples/logo.png +0 -0
- package/examples/logo2.png +0 -0
- package/examples/module/build.mjs +0 -124
- package/examples/module/game.js +0 -132
- package/examples/module/index.html +0 -10
- package/examples/module/tiles.png +0 -0
- package/examples/particles/index.html +0 -426
- package/examples/particles/tiles.png +0 -0
- package/examples/platformer/data/gameLevelData.tmx +0 -143
- package/examples/platformer/data/gameLevelData.tsx +0 -4
- package/examples/platformer/game.js +0 -147
- package/examples/platformer/gameCharacter.js +0 -309
- package/examples/platformer/gameEffects.js +0 -278
- package/examples/platformer/gameLevel.js +0 -205
- package/examples/platformer/gameLevelData.json +0 -171
- package/examples/platformer/gameObjects.js +0 -378
- package/examples/platformer/gamePlayer.js +0 -35
- package/examples/platformer/index.html +0 -9
- package/examples/platformer/tiles.png +0 -0
- package/examples/platformer/tilesLevel.png +0 -0
- package/examples/puzzle/game.js +0 -331
- package/examples/puzzle/index.html +0 -8
- package/examples/puzzle/tiles.png +0 -0
- package/examples/screenshot.jpg +0 -0
- package/examples/shorts/animation.js +0 -18
- package/examples/shorts/base.html +0 -54
- package/examples/shorts/blending.js +0 -14
- package/examples/shorts/box2d.js +0 -51
- package/examples/shorts/box2dCar.js +0 -59
- package/examples/shorts/box2dPool.js +0 -107
- package/examples/shorts/box2dTileLayer.js +0 -48
- package/examples/shorts/cameraDrag.js +0 -22
- package/examples/shorts/clock.js +0 -21
- package/examples/shorts/colors.js +0 -25
- package/examples/shorts/debugDraw.js +0 -37
- package/examples/shorts/empty.js +0 -30
- package/examples/shorts/flappyGame.js +0 -55
- package/examples/shorts/fontImage.js +0 -17
- package/examples/shorts/fps.js +0 -90
- package/examples/shorts/helloWorld.js +0 -11
- package/examples/shorts/hillGlideGame.js +0 -63
- package/examples/shorts/input.js +0 -64
- package/examples/shorts/landerGame.js +0 -57
- package/examples/shorts/maze.js +0 -48
- package/examples/shorts/medals.js +0 -51
- package/examples/shorts/music.js +0 -78
- package/examples/shorts/musicPlayer.js +0 -137
- package/examples/shorts/nineSlice.js +0 -40
- package/examples/shorts/parallax.js +0 -72
- package/examples/shorts/particles.js +0 -28
- package/examples/shorts/piano.js +0 -44
- package/examples/shorts/platformer.js +0 -45
- package/examples/shorts/pongGame.js +0 -41
- package/examples/shorts/postProcess.js +0 -62
- package/examples/shorts/sequencer.js +0 -124
- package/examples/shorts/shader.js +0 -29
- package/examples/shorts/shapes.js +0 -21
- package/examples/shorts/slidingPuzzle.js +0 -52
- package/examples/shorts/song.mp3 +0 -0
- package/examples/shorts/sound.js +0 -36
- package/examples/shorts/spaceGame.js +0 -58
- package/examples/shorts/spriteAtlas.js +0 -31
- package/examples/shorts/starfield.js +0 -15
- package/examples/shorts/texture.js +0 -16
- package/examples/shorts/tileLayer.js +0 -48
- package/examples/shorts/tileRaycast.js +0 -39
- package/examples/shorts/tiles.png +0 -0
- package/examples/shorts/tiltedView.js +0 -63
- package/examples/shorts/timers.js +0 -53
- package/examples/shorts/topDown.js +0 -38
- package/examples/shorts/uiSystem.js +0 -61
- package/examples/shorts/video.webm +0 -0
- package/examples/shorts/videoPlayer.js +0 -34
- package/examples/starter/build.bat +0 -7
- package/examples/starter/build.mjs +0 -126
- package/examples/starter/game.js +0 -137
- package/examples/starter/index.html +0 -35
- package/examples/starter/tiles.png +0 -0
- package/examples/stress/index.html +0 -173
- package/examples/style.css +0 -150
- package/examples/typescript/build.mjs +0 -60
- package/examples/typescript/game.js +0 -100
- package/examples/typescript/game.ts +0 -132
- package/examples/typescript/index.html +0 -10
- package/examples/typescript/tiles.png +0 -0
- package/examples/typescript/tsconfig.json +0 -17
- package/examples/uiSystem/game.js +0 -139
- package/examples/uiSystem/index.html +0 -10
- package/examples/uiSystem/tiles.png +0 -0
- package/jsconfig.json +0 -12
- package/plugins/desktop.ini +0 -2
- package/reference.md +0 -447
- package/test/math.test.mjs +0 -774
- package/test/setup.mjs +0 -22
- package/test/smoke.test.mjs +0 -258
- package/test/util.test.mjs +0 -80
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
let box2DTileLayer;
|
|
2
|
-
|
|
3
|
-
async function gameInit()
|
|
4
|
-
{
|
|
5
|
-
// setup box2d
|
|
6
|
-
await box2dInit();
|
|
7
|
-
cameraPos = vec2(16); // setup camera
|
|
8
|
-
gravity.y = -30; // enable gravity
|
|
9
|
-
canvasClearColor = hsl(0,0,.2); // background color
|
|
10
|
-
|
|
11
|
-
// create tile layer
|
|
12
|
-
const pos = vec2();
|
|
13
|
-
const tileLayer = new TileCollisionLayer(pos, vec2(32));
|
|
14
|
-
for (pos.x = tileLayer.size.x; pos.x--;)
|
|
15
|
-
for (pos.y = tileLayer.size.y; pos.y--;)
|
|
16
|
-
{
|
|
17
|
-
// check if tile should be solid
|
|
18
|
-
if (randBool(.7))
|
|
19
|
-
continue;
|
|
20
|
-
|
|
21
|
-
// set tile data
|
|
22
|
-
const tileIndex = 11;
|
|
23
|
-
const direction = randInt(4)
|
|
24
|
-
const mirror = randBool();
|
|
25
|
-
const color = randColor(WHITE, hsl(0,0,.2));
|
|
26
|
-
const data = new TileLayerData(tileIndex, direction, mirror, color);
|
|
27
|
-
tileLayer.setData(pos, data);
|
|
28
|
-
tileLayer.setCollisionData(pos);
|
|
29
|
-
}
|
|
30
|
-
tileLayer.redraw(); // redraw tile layer with new data
|
|
31
|
-
box2DTileLayer = new Box2dTileLayer(tileLayer);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function gameUpdate()
|
|
35
|
-
{
|
|
36
|
-
if (mouseWasPressed(0))
|
|
37
|
-
{
|
|
38
|
-
// clear tile that was clicked
|
|
39
|
-
box2DTileLayer.tileLayer.clearData(mousePos, true);
|
|
40
|
-
box2DTileLayer.tileLayer.clearCollisionData(mousePos);
|
|
41
|
-
box2DTileLayer.buildCollision();
|
|
42
|
-
|
|
43
|
-
// spawn box2d object at mouse position
|
|
44
|
-
const o = new Box2dObject(mousePos, vec2(), 0, 0, randColor());
|
|
45
|
-
const friction = .2, restitution = .5;
|
|
46
|
-
o.addCircle(rand(.5,1), vec2(), 1, friction, restitution);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
function gameInit()
|
|
2
|
-
{
|
|
3
|
-
// create some objects
|
|
4
|
-
for (let i=500; i--;)
|
|
5
|
-
{
|
|
6
|
-
const pos = randInCircle(100);
|
|
7
|
-
const size = vec2(rand(2,9),rand(2,9));
|
|
8
|
-
const color = randColor();
|
|
9
|
-
const angle = rand(PI);
|
|
10
|
-
new EngineObject(pos, size, 0, angle, color);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function gameUpdate()
|
|
15
|
-
{
|
|
16
|
-
// drag camera with mouse
|
|
17
|
-
if (mouseIsDown(0))
|
|
18
|
-
cameraPos = cameraPos.subtract(mouseDelta);
|
|
19
|
-
|
|
20
|
-
// zoom camera with mouse wheel
|
|
21
|
-
cameraScale = clamp(cameraScale*(1-mouseWheel/5), 1, 1e3);
|
|
22
|
-
}
|
package/examples/shorts/clock.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
function gameRender()
|
|
2
|
-
{
|
|
3
|
-
// draw background
|
|
4
|
-
for (let i=12; i--;)
|
|
5
|
-
{
|
|
6
|
-
const a = i/6*PI;
|
|
7
|
-
const pos = vec2(0,7).rotate(a);
|
|
8
|
-
drawRect(pos, vec2(.5,1), hsl(i/12,1,.5), a);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// get current time
|
|
12
|
-
const d = Date().slice(16,24);
|
|
13
|
-
const s = d.slice(6,8)|0;
|
|
14
|
-
const m = d.slice(3,5)|0;
|
|
15
|
-
const h = (d.slice(0,2)|0) + m/60;
|
|
16
|
-
|
|
17
|
-
// draw clock hands
|
|
18
|
-
drawLine(vec2(), vec2(0,4).rotate(h/12*2*PI), 1);
|
|
19
|
-
drawLine(vec2(), vec2(0,6).rotate(m/60*2*PI), .4);
|
|
20
|
-
drawLine(vec2(), vec2(0,8).rotate(s/60*2*PI), .1);
|
|
21
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
function gameRender()
|
|
2
|
-
{
|
|
3
|
-
// color constants
|
|
4
|
-
drawRect(vec2(-8, 5), vec2(3), RED);
|
|
5
|
-
drawRect(vec2(-4, 5), vec2(3), YELLOW);
|
|
6
|
-
drawRect(vec2(-0, 5), vec2(3), GREEN);
|
|
7
|
-
drawRect(vec2( 4, 5), vec2(3), BLUE);
|
|
8
|
-
drawRect(vec2( 8, 5), vec2(3), WHITE);
|
|
9
|
-
|
|
10
|
-
// rgb and hsl color
|
|
11
|
-
drawRect(vec2(-8, 0), vec2(3), new Color(1,0,0));
|
|
12
|
-
drawRect(vec2(-4, 0), vec2(3), rgb(0,1,1));
|
|
13
|
-
drawRect(vec2(-0, 0), vec2(3), hsl(0,1,.5, .5));
|
|
14
|
-
drawRect(vec2( 4, 0), vec2(3), hsl(.6,.5,.5));
|
|
15
|
-
drawRect(vec2( 8, 0), vec2(3), hsl(0,0,1));
|
|
16
|
-
|
|
17
|
-
// color lerping
|
|
18
|
-
for (let i=5; i--;)
|
|
19
|
-
{
|
|
20
|
-
const color1 = RED;
|
|
21
|
-
const color2 = YELLOW;
|
|
22
|
-
const color = color1.lerp(color2, i/4);
|
|
23
|
-
drawRect(vec2(-8+i*4, -5), vec2(3), color);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
class BounceObject extends EngineObject
|
|
2
|
-
{
|
|
3
|
-
constructor(pos, size, tile)
|
|
4
|
-
{
|
|
5
|
-
super(pos, size, tile);
|
|
6
|
-
this.velocity = vec2(.1);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
update()
|
|
10
|
-
{
|
|
11
|
-
// show debug info
|
|
12
|
-
debugText('Debug Text', this.pos.add(vec2(0,3)), 1, WHITE);
|
|
13
|
-
debugRect(this.pos, this.size, RED);
|
|
14
|
-
const trianglePoints = [vec2(-2,-1), vec2(0,2), vec2(2,-1)];
|
|
15
|
-
debugPoly(mousePos, trianglePoints, YELLOW);
|
|
16
|
-
debugLine(this.pos, mousePos, BLUE);
|
|
17
|
-
|
|
18
|
-
// bounce off screen edges
|
|
19
|
-
const cameraSize = getCameraSize();
|
|
20
|
-
if (abs(this.pos.x) > cameraSize.x/2)
|
|
21
|
-
{
|
|
22
|
-
debugCircle(this.pos, 3, GREEN, 1);
|
|
23
|
-
this.velocity.x = -this.velocity.x;
|
|
24
|
-
}
|
|
25
|
-
if (abs(this.pos.y) > cameraSize.y/2)
|
|
26
|
-
{
|
|
27
|
-
debugCircle(this.pos, 3, GREEN, 1);
|
|
28
|
-
this.velocity.y = -this.velocity.y;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function gameInit()
|
|
34
|
-
{
|
|
35
|
-
canvasClearColor = GRAY;
|
|
36
|
-
new BounceObject(vec2(), vec2(5), tile(3,128));
|
|
37
|
-
}
|
package/examples/shorts/empty.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
function gameInit()
|
|
2
|
-
{
|
|
3
|
-
// called once after the engine starts up
|
|
4
|
-
// setup the game
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function gameUpdate()
|
|
8
|
-
{
|
|
9
|
-
// called every frame at 60 frames per second
|
|
10
|
-
// handle input and update the game state
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function gameUpdatePost()
|
|
14
|
-
{
|
|
15
|
-
// called after physics and objects are updated
|
|
16
|
-
// setup camera and prepare for render
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function gameRender()
|
|
20
|
-
{
|
|
21
|
-
// called before objects are rendered
|
|
22
|
-
// draw any background effects that appear behind objects
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function gameRenderPost()
|
|
26
|
-
{
|
|
27
|
-
// called after objects are rendered
|
|
28
|
-
// draw effects or hud that appear above all objects
|
|
29
|
-
drawText('LittleJS Engine', vec2(0,6), 3);
|
|
30
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
class Wall extends EngineObject
|
|
2
|
-
{
|
|
3
|
-
constructor(pos, size)
|
|
4
|
-
{
|
|
5
|
-
super(pos, size, 0, 0, hsl(.3,.5,.3));
|
|
6
|
-
this.setCollision(); // make object collide
|
|
7
|
-
this.mass = 0;
|
|
8
|
-
}
|
|
9
|
-
update()
|
|
10
|
-
{
|
|
11
|
-
this.pos.x -= .1; // move walls to the left
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
class Player extends EngineObject
|
|
16
|
-
{
|
|
17
|
-
constructor(pos)
|
|
18
|
-
{
|
|
19
|
-
super(pos, vec2(1), tile(9), 0, YELLOW);
|
|
20
|
-
this.drawSize = vec2(2);
|
|
21
|
-
this.setCollision(); // make object collide
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
update()
|
|
25
|
-
{
|
|
26
|
-
super.update();
|
|
27
|
-
|
|
28
|
-
// flappy movement controls
|
|
29
|
-
if (mouseWasPressed(0) || keyWasPressed('Space'))
|
|
30
|
-
this.velocity = vec2(0,.2);
|
|
31
|
-
this.angle = -this.velocity.y*2;
|
|
32
|
-
this.pos.y = max(-50, this.pos.y);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
collideWithObject(object)
|
|
36
|
-
{
|
|
37
|
-
// reset game
|
|
38
|
-
engineObjectsDestroy();
|
|
39
|
-
gameInit();
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function gameInit()
|
|
44
|
-
{
|
|
45
|
-
// setup level
|
|
46
|
-
gravity.y = -.01;
|
|
47
|
-
for (let i=100; i--;)
|
|
48
|
-
{
|
|
49
|
-
const h=100, y=-h/2-6+rand(9), spacing=15, gap=5;
|
|
50
|
-
new Wall(vec2(14 + i*spacing,y+h + gap), vec2(3,h));
|
|
51
|
-
new Wall(vec2(14 + i*spacing,y), vec2(3,h));
|
|
52
|
-
}
|
|
53
|
-
new Player(vec2(-7,6));
|
|
54
|
-
canvasClearColor = hsl(.55,1,.8);
|
|
55
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
function gameRender()
|
|
2
|
-
{
|
|
3
|
-
// draw text with built in engine font image
|
|
4
|
-
const font = engineFontImage;
|
|
5
|
-
font.drawText('Engine Font', vec2(0,3), 2);
|
|
6
|
-
|
|
7
|
-
// show every character in the font
|
|
8
|
-
let s = '';
|
|
9
|
-
for (let i=32; i<128; ++i)
|
|
10
|
-
{
|
|
11
|
-
if (i%32 == 0)
|
|
12
|
-
s += '\n';
|
|
13
|
-
s += String.fromCharCode(i);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
font.drawText(s, vec2());
|
|
17
|
-
}
|
package/examples/shorts/fps.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
let playerPos = vec2(), playerAngle = 0;
|
|
2
|
-
setTileDefaultBleed(0);
|
|
3
|
-
|
|
4
|
-
// a simple function to create the level
|
|
5
|
-
const levelTest = (p)=> (floor(p.x)**3&floor(p.y)**2)%30>5;
|
|
6
|
-
|
|
7
|
-
function gameUpdate()
|
|
8
|
-
{
|
|
9
|
-
// update camera angle with mouse pointer lock
|
|
10
|
-
if (mouseWasPressed(0))
|
|
11
|
-
pointerLockRequest();
|
|
12
|
-
if (keyWasPressed('Escape'))
|
|
13
|
-
pointerLockExit()
|
|
14
|
-
if (pointerLockIsActive() || isTouchDevice)
|
|
15
|
-
playerAngle += mouseDelta.x * .03;
|
|
16
|
-
|
|
17
|
-
// update player movement, prevent walking through walls
|
|
18
|
-
const velocity = keyDirection().rotate(playerAngle).scale(.05);
|
|
19
|
-
const normal = vec2();
|
|
20
|
-
let newPos = playerPos.add(velocity);
|
|
21
|
-
if (lineTest(playerPos, newPos, levelTest, normal))
|
|
22
|
-
{
|
|
23
|
-
// adjust velocity to slide along wall
|
|
24
|
-
const d = velocity.dot(normal);
|
|
25
|
-
newPos = newPos.subtract(vec2(d*normal.x, d*normal.y));
|
|
26
|
-
if (!levelTest(newPos))
|
|
27
|
-
playerPos = newPos;
|
|
28
|
-
}
|
|
29
|
-
else
|
|
30
|
-
playerPos = newPos;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function gameRender()
|
|
34
|
-
{
|
|
35
|
-
{
|
|
36
|
-
// draw horizontal slices to create floor and ceiling
|
|
37
|
-
const h = 9;
|
|
38
|
-
let pos = vec2(), size = vec2(39, .15), color = rgb();
|
|
39
|
-
for (let y=-h; y<h; y+=.1)
|
|
40
|
-
{
|
|
41
|
-
const p = 1.01 - abs(y/h)
|
|
42
|
-
color.setHSLA(.1, y>0?0:.5, .7-p*.7);
|
|
43
|
-
pos.y = y;
|
|
44
|
-
drawRect(pos, size, color);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
{
|
|
48
|
-
// draw vertical slices to create the walls
|
|
49
|
-
// create objects in advance for optimal performance
|
|
50
|
-
const w = 15;
|
|
51
|
-
const maxDistance = 50;
|
|
52
|
-
const pos = vec2(), endPos = vec2(), size = vec2(.15);
|
|
53
|
-
const tileInfo = new TileInfo(vec2(), vec2(0,16));
|
|
54
|
-
const normal = vec2(), light = vec2().setAngle(2);
|
|
55
|
-
const color = rgb();
|
|
56
|
-
for (pos.x=-w; pos.x<w; pos.x+=.1)
|
|
57
|
-
{
|
|
58
|
-
// cast ray for this slice
|
|
59
|
-
const angle = playerAngle + pos.x/w/2;
|
|
60
|
-
endPos.setAngle(angle, maxDistance);
|
|
61
|
-
endPos.x += playerPos.x;
|
|
62
|
-
endPos.y += playerPos.y;
|
|
63
|
-
const p = lineTest(playerPos, endPos, levelTest, normal);
|
|
64
|
-
if (!p) continue;
|
|
65
|
-
|
|
66
|
-
// get texture coordinate
|
|
67
|
-
const t = mod(p.x+p.y, 1);
|
|
68
|
-
tileInfo.pos.x = 161 + 14*t;
|
|
69
|
-
|
|
70
|
-
// apply fog and lighting
|
|
71
|
-
const d = p.distance(playerPos)/maxDistance;
|
|
72
|
-
const l = max(0, normal.dot(light));
|
|
73
|
-
size.y = .5/d;
|
|
74
|
-
color.setHSLA(.6, 1-d, .7-d+l*.3);
|
|
75
|
-
|
|
76
|
-
// draw the section the wall
|
|
77
|
-
drawTile(pos, size, tileInfo, color);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// draw instructions and pointer lock status
|
|
82
|
-
const instructions = pointerLockIsActive() ?
|
|
83
|
-
'Mouse Control Active - ESC To Exit' :
|
|
84
|
-
'Click To Enable Mouse Control';
|
|
85
|
-
const textPos = vec2(mainCanvasSize.x/2, 50);
|
|
86
|
-
const textSize = 30;
|
|
87
|
-
const textColor = pointerLockIsActive() ? GREEN : WHITE;
|
|
88
|
-
if (!isTouchDevice)
|
|
89
|
-
drawTextScreen(instructions, textPos, textSize, textColor);
|
|
90
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
function gameRender()
|
|
2
|
-
{
|
|
3
|
-
// draw background gradient
|
|
4
|
-
drawRectGradient(cameraPos, getCameraSize(), BLACK, WHITE);
|
|
5
|
-
|
|
6
|
-
// draw text
|
|
7
|
-
drawText('LittleJS Engine', vec2(0,3), 3);
|
|
8
|
-
|
|
9
|
-
// draw a tile
|
|
10
|
-
drawTile(vec2(sin(time)*3,-2), vec2(7), tile(3,128));
|
|
11
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
class Player extends EngineObject
|
|
2
|
-
{
|
|
3
|
-
constructor(pos)
|
|
4
|
-
{
|
|
5
|
-
super(pos, vec2(2), tile(9), 0, RED);
|
|
6
|
-
this.damping = 1; // disable damping
|
|
7
|
-
this.clampSpeed = false; // disable speed clamping
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
update()
|
|
11
|
-
{
|
|
12
|
-
// check ground height
|
|
13
|
-
const h = getGroundHeight(this.pos.x);
|
|
14
|
-
if (this.pos.y < h + this.size.y/2)
|
|
15
|
-
{
|
|
16
|
-
// clamp to ground and reflect velocity
|
|
17
|
-
this.pos.y = h + this.size.y/2;
|
|
18
|
-
const h2 = getGroundHeight(this.pos.x+.1);
|
|
19
|
-
const n = vec2((h-h2)/.1, 1).normalize();
|
|
20
|
-
this.velocity = this.velocity.reflect(n,0);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// apply movement controls
|
|
24
|
-
if (mouseIsDown(0) || keyIsDown('Space'))
|
|
25
|
-
this.applyAcceleration(vec2(0,-.05));
|
|
26
|
-
this.velocity.x = max(this.velocity.x,.4);
|
|
27
|
-
this.angle = this.velocity.angle() - PI/2;
|
|
28
|
-
|
|
29
|
-
// move camera with player
|
|
30
|
-
cameraPos = vec2(this.pos.x+9,5);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function getGroundHeight(x)
|
|
35
|
-
{
|
|
36
|
-
return sin(x/4)*2 + sin(x/17);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function gameInit()
|
|
40
|
-
{
|
|
41
|
-
gravity.y = -.01;
|
|
42
|
-
new Player(vec2(0,5));
|
|
43
|
-
canvasClearColor = hsl(.6,1,.8);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function gameRender()
|
|
47
|
-
{
|
|
48
|
-
// draw ground as a series of thin rectangles
|
|
49
|
-
const h = 100, w = 20;
|
|
50
|
-
const pos = vec2();
|
|
51
|
-
const sizeTop = vec2(.4);
|
|
52
|
-
const size = vec2(.2,h);
|
|
53
|
-
const color = rgb();
|
|
54
|
-
for (let x=cameraPos.x-w; x<cameraPos.x+w; x+=.1)
|
|
55
|
-
{
|
|
56
|
-
pos.x = x;
|
|
57
|
-
pos.y = getGroundHeight(x);
|
|
58
|
-
drawRect(pos, sizeTop, BLACK);
|
|
59
|
-
pos.y -= h/2;
|
|
60
|
-
color.setHSLA(.2+.2*oscillate(.2,1,x), .7, .5);
|
|
61
|
-
drawRect(pos, size, color);
|
|
62
|
-
}
|
|
63
|
-
}
|
package/examples/shorts/input.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
function gameUpdate()
|
|
2
|
-
{
|
|
3
|
-
touchGamepadEnable = 1;
|
|
4
|
-
if (isTouchDevice || isUsingGamepad)
|
|
5
|
-
{
|
|
6
|
-
if (isTouchDevice)
|
|
7
|
-
{
|
|
8
|
-
debugText('Touch Gamepad Mode', vec2(0,5));
|
|
9
|
-
|
|
10
|
-
// touch input is routed to mouse
|
|
11
|
-
debugPoint(mousePos, mouseIsDown(0) ? RED : YELLOW, 1);
|
|
12
|
-
}
|
|
13
|
-
else
|
|
14
|
-
{
|
|
15
|
-
debugText('Gamepad Mode', vec2(0,5));
|
|
16
|
-
debugText('Primary Gamepad: ' + gamepadPrimary, vec2(0,4));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// analog sticks
|
|
20
|
-
for (let i=2; i--;)
|
|
21
|
-
{
|
|
22
|
-
const stick = gamepadStick(i);
|
|
23
|
-
const pos = vec2(i?3:-3, 1);
|
|
24
|
-
debugCircle(pos, 4, WHITE);
|
|
25
|
-
debugLine(pos, pos.add(stick.scale(2)), GREEN);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// buttons
|
|
29
|
-
for (let i=16; i--;)
|
|
30
|
-
{
|
|
31
|
-
const pos = vec2(-7 + i%8*2, -3 - (i/8|0)*2);
|
|
32
|
-
if (gamepadIsDown(i))
|
|
33
|
-
debugCircle(pos, 2, RED, 0, 1);
|
|
34
|
-
debugCircle(pos, 2, WHITE);
|
|
35
|
-
debugText(i, pos);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
else
|
|
39
|
-
{
|
|
40
|
-
debugText('Mouse and Keyboard Mode', vec2(0,5));
|
|
41
|
-
|
|
42
|
-
// keyboard key (space bar)
|
|
43
|
-
debugRect(vec2(), vec2(4), WHITE);
|
|
44
|
-
if (keyIsDown('Space'))
|
|
45
|
-
debugRect(vec2(), vec2(4), RED, 0, 0, 1);
|
|
46
|
-
|
|
47
|
-
// keyboard direction (arrow keys or WASD)
|
|
48
|
-
const inputDirection = keyDirection();
|
|
49
|
-
debugLine(vec2(), inputDirection.scale(2), GREEN);
|
|
50
|
-
|
|
51
|
-
// mouse pos
|
|
52
|
-
debugPoint(mousePos, mouseIsDown(0) ? RED : YELLOW, 1);
|
|
53
|
-
|
|
54
|
-
// mouse buttons
|
|
55
|
-
for (let i=3; i--;)
|
|
56
|
-
{
|
|
57
|
-
const pos = vec2(-2 + i*2, -5);
|
|
58
|
-
if (mouseIsDown(i))
|
|
59
|
-
debugCircle(pos, 2, RED, 0, 1);
|
|
60
|
-
debugCircle(pos, 2, WHITE);
|
|
61
|
-
debugText(i, pos);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
class Player extends EngineObject
|
|
2
|
-
{
|
|
3
|
-
constructor(pos)
|
|
4
|
-
{
|
|
5
|
-
super(pos, vec2(1,2), tile(8), 0, RED);
|
|
6
|
-
this.setCollision(); // make object collide
|
|
7
|
-
this.angleDamping = .95;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
update()
|
|
11
|
-
{
|
|
12
|
-
// space ship controls
|
|
13
|
-
const moveInput = keyDirection();
|
|
14
|
-
this.applyAngularAcceleration(moveInput.x * .002);
|
|
15
|
-
const accel = this.getUp(moveInput.y*.002);
|
|
16
|
-
this.applyAcceleration(accel);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// explode when it collides
|
|
20
|
-
collideWithObject()
|
|
21
|
-
{
|
|
22
|
-
// don't explode if player is almost still and not tilted
|
|
23
|
-
if (abs(this.angle) < .2)
|
|
24
|
-
if (this.velocity.length() < .05)
|
|
25
|
-
return true;
|
|
26
|
-
|
|
27
|
-
// explode!
|
|
28
|
-
this.destroy();
|
|
29
|
-
new ParticleEmitter(
|
|
30
|
-
this.pos, 0, // pos, angle
|
|
31
|
-
1, .1, 300, 3.14, 0, // emitSize, emitTime, rate, cone, tile
|
|
32
|
-
rgb(1,.5,.1), rgb(1,.1,.1), // colorStartA, colorStartB
|
|
33
|
-
rgb(1,.5,.1,0), rgb(1,.1,.1,0), // colorEndA, colorEndB
|
|
34
|
-
.7, .8, .2, .2, .05, // time, start, end, speed, angleSpeed
|
|
35
|
-
.9, 1, -.2, 3.14, .05, // damp, angleDamp, gravity, cone, fade
|
|
36
|
-
1, 0, 1 // randomness, collide, additive
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function gameInit()
|
|
42
|
-
{
|
|
43
|
-
// setup level
|
|
44
|
-
gravity.y = -.001;
|
|
45
|
-
new Player(vec2(0,4));
|
|
46
|
-
|
|
47
|
-
// create ground
|
|
48
|
-
for (let i=0, y=0; i<20; ++i)
|
|
49
|
-
{
|
|
50
|
-
y = (y + rand(1,-1)*4) * .5;
|
|
51
|
-
const pos = vec2(i*2-20, y-54);
|
|
52
|
-
const color = hsl(0, 0, .5+i%.29);
|
|
53
|
-
const o = new EngineObject(pos, vec2(2, 99), 0, 0, color);
|
|
54
|
-
o.setCollision(); // make object collide
|
|
55
|
-
o.mass = 0; // make object have static physics
|
|
56
|
-
}
|
|
57
|
-
}
|
package/examples/shorts/maze.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
function generateMaze(size)
|
|
2
|
-
{
|
|
3
|
-
const maze = [], w = size.x, h = size.y;
|
|
4
|
-
maze[1 + w] = 1; // start point
|
|
5
|
-
for (let k=w*h*9|0; k--;)
|
|
6
|
-
{
|
|
7
|
-
// get a random position on odd coordinates
|
|
8
|
-
const jx = randInt(w/2-(w%2?0:2)|0)*2 + 1;
|
|
9
|
-
const jy = randInt(h/2-(h%2?1:2)|0)*2 + 1;
|
|
10
|
-
const j = jx + jy*w;
|
|
11
|
-
|
|
12
|
-
// get a random direction
|
|
13
|
-
const d = randSign() * (randBool() ? 1 : w);
|
|
14
|
-
|
|
15
|
-
// check if we are not the same line or column
|
|
16
|
-
if (j%w != (j+d*2)%w && (j/w|0) != ((j+d*2)/w|0))
|
|
17
|
-
continue;
|
|
18
|
-
|
|
19
|
-
// check if pos is open and the next 2 cells are closed
|
|
20
|
-
if (maze[j] && !maze[j+d] && !maze[j+d*2])
|
|
21
|
-
maze[j+d] = maze[j+d*2] = 1;
|
|
22
|
-
}
|
|
23
|
-
return maze;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function gameInit()
|
|
27
|
-
{
|
|
28
|
-
// generate maze data
|
|
29
|
-
const mazeSize = vec2(27,15);
|
|
30
|
-
const maze = generateMaze(mazeSize);
|
|
31
|
-
|
|
32
|
-
// create tile layer
|
|
33
|
-
const pos = vec2();
|
|
34
|
-
const tileLayer = new TileCollisionLayer(pos, mazeSize);
|
|
35
|
-
for (pos.x = tileLayer.size.x; pos.x--;)
|
|
36
|
-
for (pos.y = tileLayer.size.y; pos.y--;)
|
|
37
|
-
{
|
|
38
|
-
// check if tile should be solid
|
|
39
|
-
if (maze[pos.x + pos.y*mazeSize.x])
|
|
40
|
-
continue;
|
|
41
|
-
|
|
42
|
-
// set tile data
|
|
43
|
-
tileLayer.setData(pos, new TileLayerData(1));
|
|
44
|
-
}
|
|
45
|
-
tileLayer.redraw(); // redraw tile layer with new data
|
|
46
|
-
cameraPos = mazeSize.scale(.5); // center camera
|
|
47
|
-
canvasClearColor = hsl(rand(),.3,.2); // random background color
|
|
48
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
// create example medals
|
|
2
|
-
const medal_openedExample = new Medal(0, 'Open', 'Opened this example!');
|
|
3
|
-
const medal_leftClick = new Medal(1, 'Lefty', 'Left clicked!', '🐁');
|
|
4
|
-
const medal_rightClick = new Medal(2, 'Righty', 'Right clicked!', '🐭');
|
|
5
|
-
const medal_spacePressed = new Medal(3, 'Space', 'Pressed spacebar!', '🚀');
|
|
6
|
-
|
|
7
|
-
function gameInit()
|
|
8
|
-
{
|
|
9
|
-
// setup medals
|
|
10
|
-
const saveName = 'Medals Example';
|
|
11
|
-
medalsInit(saveName);
|
|
12
|
-
|
|
13
|
-
// clear unlocked medals for testing
|
|
14
|
-
medalsForEach(medal=> medal.unlocked = false);
|
|
15
|
-
|
|
16
|
-
// unlock the example medal
|
|
17
|
-
medal_openedExample.unlock();
|
|
18
|
-
|
|
19
|
-
// set background color
|
|
20
|
-
canvasClearColor = hsl(.5,.3,.2);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function gameUpdate()
|
|
24
|
-
{
|
|
25
|
-
// unlock example medals based on input
|
|
26
|
-
if (mouseWasPressed(0))
|
|
27
|
-
medal_leftClick.unlock();
|
|
28
|
-
if (mouseWasPressed(2))
|
|
29
|
-
medal_rightClick.unlock();
|
|
30
|
-
if (keyWasPressed('Space'))
|
|
31
|
-
medal_spacePressed.unlock();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function gameRenderPost()
|
|
35
|
-
{
|
|
36
|
-
const size = 80;
|
|
37
|
-
let pos = mainCanvasSize.scale(.5).subtract(vec2(0,40));
|
|
38
|
-
drawTextScreen('Unlocked Medals', pos, size);
|
|
39
|
-
|
|
40
|
-
// show unlocked medals
|
|
41
|
-
let medalsCount = 0;
|
|
42
|
-
medalsForEach(medal=> medal.unlocked && medalsCount++);
|
|
43
|
-
pos = pos.add(vec2((1-medalsCount)*size/2, 100));
|
|
44
|
-
medalsForEach(medal=>
|
|
45
|
-
{
|
|
46
|
-
if (!medal.unlocked)
|
|
47
|
-
return;
|
|
48
|
-
medal.renderIcon(pos, size);
|
|
49
|
-
pos.x += size + 8;
|
|
50
|
-
});
|
|
51
|
-
}
|