littlejsengine 1.6.0 → 1.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -33
- package/build/littlejs.d.ts +13 -5
- package/build/littlejs.esm.js +106 -95
- package/build/littlejs.esm.min.js +1 -1
- package/build/littlejs.js +98 -93
- package/build/littlejs.min.js +1 -1
- package/build/littlejs.release.js +91 -91
- package/examples/breakout/game.js +5 -0
- package/examples/breakout/gameObjects.js +41 -48
- package/examples/breakout/index.html +3 -3
- package/examples/breakoutTutorial/README.md +514 -0
- package/examples/breakoutTutorial/game.js +181 -0
- 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 +10 -0
- package/examples/empty/game.js +10 -0
- package/examples/module/index.html +1 -1
- package/examples/particles/index.html +1 -1
- package/examples/platformer/gameLevel.js +1 -1
- package/examples/platformer/gamePlayer.js +1 -1
- package/examples/platformer/index.html +6 -6
- package/examples/puzzle/index.html +2 -2
- package/examples/starter/build.bat +3 -2
- package/examples/starter/game.js +9 -9
- package/examples/starter/index.html +13 -13
- package/examples/stress/index.html +1 -1
- package/examples/typescript/game.js +89 -89
- package/examples/typescript/game.ts +10 -10
- package/examples/typescript/index.html +1 -1
- package/package.json +3 -3
- package/src/engine.js +1 -2
- package/src/engineAudio.js +11 -11
- package/src/engineBuild.bat +3 -1
- package/src/engineDebug.js +8 -2
- package/src/engineDraw.js +19 -15
- package/src/engineExport.js +8 -2
- package/src/engineInput.js +51 -55
- package/src/engineObject.js +4 -4
- package/src/engineRelease.js +1 -0
- package/src/engineUtilities.js +4 -4
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Little JS Breakout Tutorial
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
8
|
+
|
|
9
|
+
// globals
|
|
10
|
+
const levelSize = vec2(38, 20); // size of play area
|
|
11
|
+
let score = 0; // start score at 0
|
|
12
|
+
let ball; // keep track of ball object
|
|
13
|
+
let paddle; // keep track of player's paddle
|
|
14
|
+
|
|
15
|
+
// sound effects
|
|
16
|
+
const sound_bounce = new Sound([,,1e3,,.03,.02,1,2,,,940,.03,,,,,.2,.6,,.06], 0);
|
|
17
|
+
const sound_break = new Sound([,,90,,.01,.03,4,,,,,,,9,50,.2,,.2,.01], 0);
|
|
18
|
+
const sound_start = new Sound([,0,500,,.04,.3,1,2,,,570,.02,.02,,,,.04]);
|
|
19
|
+
|
|
20
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
21
|
+
|
|
22
|
+
class Paddle extends EngineObject
|
|
23
|
+
{
|
|
24
|
+
constructor()
|
|
25
|
+
{
|
|
26
|
+
super(vec2(0,1), vec2(6,.5)); // set object position and size
|
|
27
|
+
this.setCollision(); // make object collide
|
|
28
|
+
this.mass = 0; // make object have static physics
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
update()
|
|
32
|
+
{
|
|
33
|
+
this.pos.x = mousePos.x; // move paddle to mouse
|
|
34
|
+
|
|
35
|
+
// clamp paddle to level size
|
|
36
|
+
this.pos.x = clamp(this.pos.x, this.size.x/2, levelSize.x - this.size.x/2);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class Ball extends EngineObject
|
|
41
|
+
{
|
|
42
|
+
constructor(pos)
|
|
43
|
+
{
|
|
44
|
+
super(pos, vec2(.5)); // set object position and size
|
|
45
|
+
|
|
46
|
+
this.velocity = vec2(-.1, -.1); // give ball some movement
|
|
47
|
+
this.setCollision(); // make object collide
|
|
48
|
+
this.elasticity = 1; // make object bounce
|
|
49
|
+
}
|
|
50
|
+
collideWithObject(o)
|
|
51
|
+
{
|
|
52
|
+
// prevent colliding with paddle if moving upwards
|
|
53
|
+
if (o == paddle && this.velocity.y > 0)
|
|
54
|
+
return 0;
|
|
55
|
+
|
|
56
|
+
// speed up
|
|
57
|
+
const speed = min(1.04*this.velocity.length(), .5);
|
|
58
|
+
this.velocity = this.velocity.normalize(speed);
|
|
59
|
+
|
|
60
|
+
// play bounce sound with pitch scaled by speed
|
|
61
|
+
sound_bounce.play(this.pos, 1, speed);
|
|
62
|
+
|
|
63
|
+
if (o == paddle)
|
|
64
|
+
{
|
|
65
|
+
// control bounce angle when ball collides with paddle
|
|
66
|
+
const deltaX = this.pos.x - o.pos.x;
|
|
67
|
+
this.velocity = this.velocity.rotate(.3 * deltaX);
|
|
68
|
+
|
|
69
|
+
// make sure ball is moving upwards with a minimum speed
|
|
70
|
+
this.velocity.y = max(-this.velocity.y, .2);
|
|
71
|
+
|
|
72
|
+
// prevent default collision code
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return 1; // allow object to collide
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
class Wall extends EngineObject
|
|
81
|
+
{
|
|
82
|
+
constructor(pos, size)
|
|
83
|
+
{
|
|
84
|
+
super(pos, size); // set object position and size
|
|
85
|
+
|
|
86
|
+
this.setCollision(); // make object collide
|
|
87
|
+
this.mass = 0; // make object have static physics
|
|
88
|
+
this.color = new Color(0,0,0,0); // make object invisible
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
class Brick extends EngineObject
|
|
93
|
+
{
|
|
94
|
+
constructor(pos, size)
|
|
95
|
+
{
|
|
96
|
+
super(pos, size);
|
|
97
|
+
|
|
98
|
+
this.setCollision(); // make object collide
|
|
99
|
+
this.mass = 0; // make object have static physics
|
|
100
|
+
this.color = randColor(); // give brick a random color
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
collideWithObject(o)
|
|
104
|
+
{
|
|
105
|
+
this.destroy(); // destroy block when hit
|
|
106
|
+
sound_break.play(this.pos); // play brick break sound
|
|
107
|
+
++score; // award a point for each brick broke
|
|
108
|
+
|
|
109
|
+
// create explosion effect
|
|
110
|
+
const color = this.color;
|
|
111
|
+
new ParticleEmitter(
|
|
112
|
+
this.pos, 0, // pos, angle
|
|
113
|
+
this.size, .1, 200, PI, // emitSize, emitTime, emitRate, emiteCone
|
|
114
|
+
-1, vec2(16), // tileIndex, tileSize
|
|
115
|
+
color, color, // colorStartA, colorStartB
|
|
116
|
+
color.scale(1,0), color.scale(1,0), // colorEndA, colorEndB
|
|
117
|
+
.2, .5, 1, .1, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
|
|
118
|
+
.99, .95, .4, PI, // damping, angleDamping, gravityScale, cone
|
|
119
|
+
.1, .5, 0, 1 // fadeRate, randomness, collide, additive
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
return 1; // allow object to collide
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
127
|
+
function gameInit()
|
|
128
|
+
{
|
|
129
|
+
// create bricks
|
|
130
|
+
for(let x=2; x<=levelSize.x-2; x+=2)
|
|
131
|
+
for(let y=12; y<=levelSize.y-2; y+=1)
|
|
132
|
+
new Brick(vec2(x,y), vec2(2,1)); // create a brick
|
|
133
|
+
|
|
134
|
+
cameraPos = levelSize.scale(.5); // center camera in level
|
|
135
|
+
canvasFixedSize = vec2(1280, 720); // use a 720p fixed size canvas
|
|
136
|
+
|
|
137
|
+
paddle = new Paddle; // create player's paddle
|
|
138
|
+
|
|
139
|
+
// create walls
|
|
140
|
+
new Wall(vec2(-.5,levelSize.y/2), vec2(1,100)) // top
|
|
141
|
+
new Wall(vec2(levelSize.x+.5,levelSize.y/2), vec2(1,100)) // left
|
|
142
|
+
new Wall(vec2(levelSize.x/2,levelSize.y+.5), vec2(100,1)) // right
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
146
|
+
function gameUpdate()
|
|
147
|
+
{
|
|
148
|
+
if (ball && ball.pos.y < -1) // if ball is below level
|
|
149
|
+
{
|
|
150
|
+
// destroy old ball
|
|
151
|
+
ball.destroy();
|
|
152
|
+
ball = 0;
|
|
153
|
+
}
|
|
154
|
+
if (!ball && mouseWasPressed(0)) // if there is no ball and left mouse is pressed
|
|
155
|
+
{
|
|
156
|
+
ball = new Ball(cameraPos); // create a ball
|
|
157
|
+
sound_start.play(); // play start sound
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
162
|
+
function gameUpdatePost()
|
|
163
|
+
{
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
167
|
+
function gameRender()
|
|
168
|
+
{
|
|
169
|
+
drawRect(cameraPos, vec2(100), new Color(.5,.5,.5)); // draw background
|
|
170
|
+
drawRect(cameraPos, levelSize, new Color(.1,.1,.1)); // draw level boundary
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
174
|
+
function gameRenderPost()
|
|
175
|
+
{
|
|
176
|
+
drawTextScreen("Score " + score, vec2(mainCanvasSize.x/2, 70), 50); // show score
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
180
|
+
// Startup LittleJS Engine
|
|
181
|
+
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost);
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
<html><head>
|
|
2
|
+
<title>Little JS Breakout Tutorial</title>
|
|
3
|
+
<meta name=viewport content=width=device-width,height=device-height,initial-scale=1,maximum-scale=1>
|
|
4
|
+
<meta name=apple-mobile-web-app-capable content=yes>
|
|
5
|
+
<meta name=mobile-web-app-capable content=yes>
|
|
6
|
+
<link rel=icon type=image/png href=../favicon.png>
|
|
7
|
+
</head><body>
|
|
8
|
+
|
|
9
|
+
<script src=../../build/littlejs.js?126></script>
|
|
10
|
+
<script src=game.js?126></script>
|
package/examples/empty/game.js
CHANGED
|
@@ -7,26 +7,36 @@
|
|
|
7
7
|
///////////////////////////////////////////////////////////////////////////////
|
|
8
8
|
function gameInit()
|
|
9
9
|
{
|
|
10
|
+
// called once after the engine starts up
|
|
11
|
+
// setup the game
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
///////////////////////////////////////////////////////////////////////////////
|
|
13
15
|
function gameUpdate()
|
|
14
16
|
{
|
|
17
|
+
// called every frame at 60 frames per second
|
|
18
|
+
// handle input and update the game state
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
///////////////////////////////////////////////////////////////////////////////
|
|
18
22
|
function gameUpdatePost()
|
|
19
23
|
{
|
|
24
|
+
// called after physics and objects are updated
|
|
25
|
+
// setup camera and prepare for render
|
|
20
26
|
}
|
|
21
27
|
|
|
22
28
|
///////////////////////////////////////////////////////////////////////////////
|
|
23
29
|
function gameRender()
|
|
24
30
|
{
|
|
31
|
+
// called before objects are rendered
|
|
32
|
+
// draw any background effects that appear behind objects
|
|
25
33
|
}
|
|
26
34
|
|
|
27
35
|
///////////////////////////////////////////////////////////////////////////////
|
|
28
36
|
function gameRenderPost()
|
|
29
37
|
{
|
|
38
|
+
// called after objects are rendered
|
|
39
|
+
// draw effects or hud that appear above all objects
|
|
30
40
|
drawTextScreen('Hello World!', mainCanvasSize.scale(.5), 80);
|
|
31
41
|
}
|
|
32
42
|
|
|
@@ -192,7 +192,7 @@ function makeTileLayers()
|
|
|
192
192
|
|
|
193
193
|
function buildLevel()
|
|
194
194
|
{
|
|
195
|
-
levelSize = vec2(
|
|
195
|
+
levelSize = vec2(128);
|
|
196
196
|
levelColor = randColor(new Color(.2,.2,.2), new Color(.8,.8,.8));
|
|
197
197
|
levelGroundColor = levelColor.mutate().add(new Color(.4,.4,.4)).clamp();
|
|
198
198
|
|
|
@@ -298,7 +298,7 @@ class Player extends Character
|
|
|
298
298
|
{
|
|
299
299
|
// player controls
|
|
300
300
|
this.holdingJump = keyIsDown(38) || gamepadIsDown(0);
|
|
301
|
-
this.holdingShoot = mouseIsDown(0) || keyIsDown(90) || gamepadIsDown(2);
|
|
301
|
+
this.holdingShoot = !isUsingGamepad && mouseIsDown(0) || keyIsDown(90) || gamepadIsDown(2);
|
|
302
302
|
this.pressingThrow = mouseIsDown(1) || keyIsDown(67) || gamepadIsDown(1);
|
|
303
303
|
this.pressedDodge = mouseIsDown(2) || keyIsDown(88) || gamepadIsDown(3);
|
|
304
304
|
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
<link rel=icon type=image/png href=../favicon.png>
|
|
7
7
|
</head><body>
|
|
8
8
|
|
|
9
|
-
<script src=../../build/littlejs.js?
|
|
10
|
-
<script src=gameObjects.js?
|
|
11
|
-
<script src=gamePlayer.js?
|
|
12
|
-
<script src=gameEffects.js?
|
|
13
|
-
<script src=gameLevel.js?
|
|
14
|
-
<script src=game.js?
|
|
9
|
+
<script src=../../build/littlejs.js?126></script>
|
|
10
|
+
<script src=gameObjects.js?126></script>
|
|
11
|
+
<script src=gamePlayer.js?126></script>
|
|
12
|
+
<script src=gameEffects.js?126></script>
|
|
13
|
+
<script src=gameLevel.js?126></script>
|
|
14
|
+
<script src=game.js?126></script>
|
|
@@ -58,11 +58,12 @@ if %ERRORLEVEL% NEQ 0 (
|
|
|
58
58
|
exit /b %ERRORLEVEL%
|
|
59
59
|
)
|
|
60
60
|
|
|
61
|
-
rem build the html, you can add html header and
|
|
61
|
+
rem build the html, you can add an html header and footer here
|
|
62
62
|
rem type ..\header.html >> index.html
|
|
63
|
-
echo ^<
|
|
63
|
+
echo ^<script^> >> index.html
|
|
64
64
|
type %BUILD_FILENAME% >> index.html
|
|
65
65
|
echo ^</script^> >> index.html
|
|
66
|
+
rem type ..\footer.html >> index.html
|
|
66
67
|
|
|
67
68
|
rem delete intermediate files
|
|
68
69
|
del %BUILD_FILENAME%
|
package/examples/starter/game.js
CHANGED
|
@@ -12,7 +12,7 @@ const medal_example = new Medal(0, 'Example Medal', 'Welcome to LittleJS!');
|
|
|
12
12
|
medalsInit('Hello World');
|
|
13
13
|
|
|
14
14
|
// game variables
|
|
15
|
-
let
|
|
15
|
+
let particleEmitter;
|
|
16
16
|
|
|
17
17
|
///////////////////////////////////////////////////////////////////////////////
|
|
18
18
|
function gameInit()
|
|
@@ -49,7 +49,7 @@ function gameInit()
|
|
|
49
49
|
gravity = -.01;
|
|
50
50
|
|
|
51
51
|
// create particle emitter
|
|
52
|
-
|
|
52
|
+
particleEmitter = new ParticleEmitter(
|
|
53
53
|
vec2(16), 0, // emitPos, emitAngle
|
|
54
54
|
1, 0, 500, PI, // emitSize, emitTime, emitRate, emiteCone
|
|
55
55
|
0, vec2(16), // tileIndex, tileSize
|
|
@@ -59,8 +59,8 @@ function gameInit()
|
|
|
59
59
|
.99, 1, 1, PI, // damping, angleDamping, gravityScale, cone
|
|
60
60
|
.05, .5, 1, 1 // fadeRate, randomness, collide, additive
|
|
61
61
|
);
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
particleEmitter.elasticity = .3; // bounce when it collides
|
|
63
|
+
particleEmitter.trailScale = 2; // stretch in direction of motion
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
///////////////////////////////////////////////////////////////////////////////
|
|
@@ -72,10 +72,10 @@ function gameUpdate()
|
|
|
72
72
|
sound_click.play(mousePos);
|
|
73
73
|
|
|
74
74
|
// change particle color and set to fade out
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
particleEmitter.colorStartA = new Color;
|
|
76
|
+
particleEmitter.colorStartB = randColor();
|
|
77
|
+
particleEmitter.colorEndA = particleEmitter.colorStartA.scale(1,0);
|
|
78
|
+
particleEmitter.colorEndB = particleEmitter.colorStartB.scale(1,0);
|
|
79
79
|
|
|
80
80
|
// unlock medals
|
|
81
81
|
medal_example.unlock();
|
|
@@ -83,7 +83,7 @@ function gameUpdate()
|
|
|
83
83
|
|
|
84
84
|
// move particles to mouse location if on screen
|
|
85
85
|
if (mousePosScreen.x)
|
|
86
|
-
|
|
86
|
+
particleEmitter.pos = mousePos;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
///////////////////////////////////////////////////////////////////////////////
|
|
@@ -7,21 +7,21 @@
|
|
|
7
7
|
</head><body>
|
|
8
8
|
|
|
9
9
|
<!-- LittleJS engine scripts -->
|
|
10
|
-
<script src=../../src/engineDebug.js?
|
|
11
|
-
<script src=../../src/engineUtilities.js?
|
|
12
|
-
<script src=../../src/engineSettings.js?
|
|
13
|
-
<script src=../../src/engineObject.js?
|
|
14
|
-
<script src=../../src/engineDraw.js?
|
|
15
|
-
<script src=../../src/engineInput.js?
|
|
16
|
-
<script src=../../src/engineAudio.js?
|
|
17
|
-
<script src=../../src/engineTileLayer.js?
|
|
18
|
-
<script src=../../src/engineParticles.js?
|
|
19
|
-
<script src=../../src/engineMedals.js?
|
|
20
|
-
<script src=../../src/engineWebGL.js?
|
|
21
|
-
<script src=../../src/engine.js?
|
|
10
|
+
<script src=../../src/engineDebug.js?126></script>
|
|
11
|
+
<script src=../../src/engineUtilities.js?126></script>
|
|
12
|
+
<script src=../../src/engineSettings.js?126></script>
|
|
13
|
+
<script src=../../src/engineObject.js?126></script>
|
|
14
|
+
<script src=../../src/engineDraw.js?126></script>
|
|
15
|
+
<script src=../../src/engineInput.js?126></script>
|
|
16
|
+
<script src=../../src/engineAudio.js?126></script>
|
|
17
|
+
<script src=../../src/engineTileLayer.js?126></script>
|
|
18
|
+
<script src=../../src/engineParticles.js?126></script>
|
|
19
|
+
<script src=../../src/engineMedals.js?126></script>
|
|
20
|
+
<script src=../../src/engineWebGL.js?126></script>
|
|
21
|
+
<script src=../../src/engine.js?126></script>
|
|
22
22
|
|
|
23
23
|
<!-- You can also include all engine scripts like this... -->
|
|
24
24
|
<!-- <script src=engine/engine.all.js></script> -->
|
|
25
25
|
|
|
26
26
|
<!-- Add your game scripts here -->
|
|
27
|
-
<script src=game.js?
|
|
27
|
+
<script src=game.js?126></script>
|
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
/*
|
|
2
|
-
LittleJS Hello World Starter Game
|
|
3
|
-
*/
|
|
4
|
-
'use strict';
|
|
5
|
-
// import module
|
|
6
|
-
import * as LittleJS from '../../build/littlejs.esm.js';
|
|
7
|
-
const {
|
|
8
|
-
// sound effects
|
|
9
|
-
const sound_click = new LittleJS.Sound([1, .5]);
|
|
10
|
-
// medals
|
|
11
|
-
const medal_example = new LittleJS.Medal(0, 'Example Medal', 'Welcome to LittleJS!');
|
|
12
|
-
LittleJS.medalsInit('Hello World');
|
|
13
|
-
// game variables
|
|
14
|
-
let
|
|
15
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
16
|
-
function gameInit() {
|
|
17
|
-
// create tile collision and visible tile layer
|
|
18
|
-
LittleJS.initTileCollision(vec2(32, 16));
|
|
19
|
-
const pos = vec2();
|
|
20
|
-
const tileLayer = new LittleJS.TileLayer(pos, LittleJS.tileCollisionSize);
|
|
21
|
-
// get level data from the tiles image
|
|
22
|
-
const imageLevelDataRow = 1;
|
|
23
|
-
const mainContext = LittleJS.mainContext;
|
|
24
|
-
mainContext.drawImage(LittleJS.tileImage, 0, 0);
|
|
25
|
-
for (pos.x = LittleJS.tileCollisionSize.x; pos.x--;)
|
|
26
|
-
for (pos.y = LittleJS.tileCollisionSize.y; pos.y--;) {
|
|
27
|
-
const imageData = mainContext.getImageData(pos.x, 16 * (imageLevelDataRow + 1) - pos.y - 1, 1, 1).data;
|
|
28
|
-
if (imageData[0]) {
|
|
29
|
-
const tileIndex = 1;
|
|
30
|
-
const direction = LittleJS.randInt(4);
|
|
31
|
-
const mirror = LittleJS.randInt(2) ? true : false;
|
|
32
|
-
const color = LittleJS.randColor();
|
|
33
|
-
const data = new LittleJS.TileLayerData(tileIndex, direction, mirror, color);
|
|
34
|
-
tileLayer.setData(pos, data);
|
|
35
|
-
LittleJS.setTileCollisionData(pos, 1);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
tileLayer.redraw();
|
|
39
|
-
// move camera to center of collision
|
|
40
|
-
LittleJS.setCameraPos(LittleJS.tileCollisionSize.scale(.5));
|
|
41
|
-
// enable gravity
|
|
42
|
-
LittleJS.setGravity(-.01);
|
|
43
|
-
// create particle emitter
|
|
44
|
-
const center = LittleJS.tileCollisionSize.scale(.5).add(vec2(0, 9));
|
|
45
|
-
|
|
46
|
-
1, 0, 500, Math.PI, // emitSize, emitTime, emitRate, emiteCone
|
|
47
|
-
0, vec2(16), // tileIndex, tileSize
|
|
48
|
-
new Color(1, 1, 1), new Color(0, 0, 0), // colorStartA, colorStartB
|
|
49
|
-
new Color(1, 1, 1, 0), new Color(0, 0, 0, 0), // colorEndA, colorEndB
|
|
50
|
-
2, .2, .2, .1, .05, // time, sizeStart, sizeEnd, speed, angleSpeed
|
|
51
|
-
.99, 1, 1, Math.PI, // damping, angleDamping, gravityScale, cone
|
|
52
|
-
.05, .5, true, true // fadeRate, randomness, collide, additive
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
58
|
-
function gameUpdate() {
|
|
59
|
-
if (LittleJS.mouseWasPressed(0)) {
|
|
60
|
-
// play sound when mouse is pressed
|
|
61
|
-
sound_click.play(LittleJS.mousePos);
|
|
62
|
-
// change particle color and set to fade out
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
// unlock medals
|
|
68
|
-
medal_example.unlock();
|
|
69
|
-
}
|
|
70
|
-
// move particles to mouse location if on screen
|
|
71
|
-
if (LittleJS.mousePosScreen.x)
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
75
|
-
function gameUpdatePost() {
|
|
76
|
-
}
|
|
77
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
78
|
-
function gameRender() {
|
|
79
|
-
// draw a grey square in the background without using webgl
|
|
80
|
-
LittleJS.drawRect(LittleJS.cameraPos, LittleJS.tileCollisionSize.add(vec2(5)), new Color(.2, .2, .2), 0, false);
|
|
81
|
-
}
|
|
82
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
83
|
-
function gameRenderPost() {
|
|
84
|
-
// draw to overlay canvas for hud rendering
|
|
85
|
-
LittleJS.drawTextScreen('LittleJS with TypeScript', vec2(LittleJS.mainCanvasSize.x / 2, 80), 80);
|
|
86
|
-
}
|
|
87
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
88
|
-
// Startup LittleJS Engine
|
|
89
|
-
LittleJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, 'tiles.png');
|
|
1
|
+
/*
|
|
2
|
+
LittleJS Hello World Starter Game
|
|
3
|
+
*/
|
|
4
|
+
'use strict';
|
|
5
|
+
// import module
|
|
6
|
+
import * as LittleJS from '../../build/littlejs.esm.js';
|
|
7
|
+
const { Color, vec2 } = LittleJS;
|
|
8
|
+
// sound effects
|
|
9
|
+
const sound_click = new LittleJS.Sound([1, .5]);
|
|
10
|
+
// medals
|
|
11
|
+
const medal_example = new LittleJS.Medal(0, 'Example Medal', 'Welcome to LittleJS!');
|
|
12
|
+
LittleJS.medalsInit('Hello World');
|
|
13
|
+
// game variables
|
|
14
|
+
let particleEmitter;
|
|
15
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
16
|
+
function gameInit() {
|
|
17
|
+
// create tile collision and visible tile layer
|
|
18
|
+
LittleJS.initTileCollision(vec2(32, 16));
|
|
19
|
+
const pos = vec2();
|
|
20
|
+
const tileLayer = new LittleJS.TileLayer(pos, LittleJS.tileCollisionSize);
|
|
21
|
+
// get level data from the tiles image
|
|
22
|
+
const imageLevelDataRow = 1;
|
|
23
|
+
const mainContext = LittleJS.mainContext;
|
|
24
|
+
mainContext.drawImage(LittleJS.tileImage, 0, 0);
|
|
25
|
+
for (pos.x = LittleJS.tileCollisionSize.x; pos.x--;)
|
|
26
|
+
for (pos.y = LittleJS.tileCollisionSize.y; pos.y--;) {
|
|
27
|
+
const imageData = mainContext.getImageData(pos.x, 16 * (imageLevelDataRow + 1) - pos.y - 1, 1, 1).data;
|
|
28
|
+
if (imageData[0]) {
|
|
29
|
+
const tileIndex = 1;
|
|
30
|
+
const direction = LittleJS.randInt(4);
|
|
31
|
+
const mirror = LittleJS.randInt(2) ? true : false;
|
|
32
|
+
const color = LittleJS.randColor();
|
|
33
|
+
const data = new LittleJS.TileLayerData(tileIndex, direction, mirror, color);
|
|
34
|
+
tileLayer.setData(pos, data);
|
|
35
|
+
LittleJS.setTileCollisionData(pos, 1);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
tileLayer.redraw();
|
|
39
|
+
// move camera to center of collision
|
|
40
|
+
LittleJS.setCameraPos(LittleJS.tileCollisionSize.scale(.5));
|
|
41
|
+
// enable gravity
|
|
42
|
+
LittleJS.setGravity(-.01);
|
|
43
|
+
// create particle emitter
|
|
44
|
+
const center = LittleJS.tileCollisionSize.scale(.5).add(vec2(0, 9));
|
|
45
|
+
particleEmitter = new LittleJS.ParticleEmitter(center, 0, // emitPos, emitAngle
|
|
46
|
+
1, 0, 500, Math.PI, // emitSize, emitTime, emitRate, emiteCone
|
|
47
|
+
0, vec2(16), // tileIndex, tileSize
|
|
48
|
+
new Color(1, 1, 1), new Color(0, 0, 0), // colorStartA, colorStartB
|
|
49
|
+
new Color(1, 1, 1, 0), new Color(0, 0, 0, 0), // colorEndA, colorEndB
|
|
50
|
+
2, .2, .2, .1, .05, // time, sizeStart, sizeEnd, speed, angleSpeed
|
|
51
|
+
.99, 1, 1, Math.PI, // damping, angleDamping, gravityScale, cone
|
|
52
|
+
.05, .5, true, true // fadeRate, randomness, collide, additive
|
|
53
|
+
);
|
|
54
|
+
particleEmitter.elasticity = .3; // bounce when it collides
|
|
55
|
+
particleEmitter.trailScale = 2; // stretch in direction of motion
|
|
56
|
+
}
|
|
57
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
58
|
+
function gameUpdate() {
|
|
59
|
+
if (LittleJS.mouseWasPressed(0)) {
|
|
60
|
+
// play sound when mouse is pressed
|
|
61
|
+
sound_click.play(LittleJS.mousePos);
|
|
62
|
+
// change particle color and set to fade out
|
|
63
|
+
particleEmitter.colorStartA = new Color;
|
|
64
|
+
particleEmitter.colorStartB = LittleJS.randColor();
|
|
65
|
+
particleEmitter.colorEndA = particleEmitter.colorStartA.scale(1, 0);
|
|
66
|
+
particleEmitter.colorEndB = particleEmitter.colorStartB.scale(1, 0);
|
|
67
|
+
// unlock medals
|
|
68
|
+
medal_example.unlock();
|
|
69
|
+
}
|
|
70
|
+
// move particles to mouse location if on screen
|
|
71
|
+
if (LittleJS.mousePosScreen.x)
|
|
72
|
+
particleEmitter.pos = LittleJS.mousePos;
|
|
73
|
+
}
|
|
74
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
75
|
+
function gameUpdatePost() {
|
|
76
|
+
}
|
|
77
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
78
|
+
function gameRender() {
|
|
79
|
+
// draw a grey square in the background without using webgl
|
|
80
|
+
LittleJS.drawRect(LittleJS.cameraPos, LittleJS.tileCollisionSize.add(vec2(5)), new Color(.2, .2, .2), 0, false);
|
|
81
|
+
}
|
|
82
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
83
|
+
function gameRenderPost() {
|
|
84
|
+
// draw to overlay canvas for hud rendering
|
|
85
|
+
LittleJS.drawTextScreen('LittleJS with TypeScript', vec2(LittleJS.mainCanvasSize.x / 2, 80), 80);
|
|
86
|
+
}
|
|
87
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
88
|
+
// Startup LittleJS Engine
|
|
89
|
+
LittleJS.engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, 'tiles.png');
|