littlejsengine 1.0.14 → 1.1.8
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/build.bat +5 -1
- package/docs/Audio.html +2268 -0
- package/docs/Color.html +2989 -0
- package/docs/Debug.html +2933 -0
- package/docs/Draw.html +4092 -0
- package/docs/EngineObject.html +4458 -0
- package/docs/Input.html +2482 -0
- package/docs/Medal.html +1007 -0
- package/docs/Medals.html +628 -0
- package/docs/Music.html +584 -0
- package/docs/Newgrounds.html +1399 -0
- package/docs/Particle.html +4535 -0
- package/docs/ParticleEmitter.html +7770 -0
- package/docs/Random.html +1774 -0
- package/docs/Settings.html +2679 -0
- package/docs/Sound.html +1249 -0
- package/docs/TileCollision.html +1409 -0
- package/docs/TileLayer.html +6948 -0
- package/docs/TileLayerData.html +1052 -0
- package/docs/Timer.html +1183 -0
- package/docs/Utilities.html +3281 -0
- package/docs/Vector2.html +4207 -0
- package/docs/WebGL.html +2327 -0
- package/docs/engine.js.html +429 -0
- package/docs/engineAudio.js.html +609 -0
- package/docs/engineDebug.js.html +725 -0
- package/docs/engineDraw.js.html +419 -0
- package/docs/engineInput.js.html +401 -0
- package/docs/engineMedals.js.html +461 -0
- package/docs/engineObject.js.html +518 -0
- package/docs/engineParticles.js.html +441 -0
- package/docs/engineSettings.js.html +349 -0
- package/docs/engineTileLayer.js.html +513 -0
- package/docs/engineUtilities.js.html +647 -0
- package/docs/engineWebGL.js.html +522 -0
- package/docs/fonts/MavenPro-Regular.ttf +0 -0
- package/docs/fonts/Montserrat-Regular.ttf +0 -0
- package/docs/fonts/Muli-Black.ttf +0 -0
- package/docs/fonts/OFL-hind.txt +93 -0
- package/docs/fonts/OFL-montserrat.txt +93 -0
- package/docs/global.html +1691 -0
- package/docs/index.css +6 -0
- package/docs/index.html +247 -0
- package/docs/scripts/fix-code-block.js +53 -0
- package/docs/scripts/fix-navbar.js +25 -0
- package/docs/scripts/linenumber.js +25 -0
- package/docs/scripts/misc.js +217 -0
- package/docs/scripts/resize.js +85 -0
- package/docs/scripts/search.js +83 -0
- package/docs/scripts/third-party/Apache-License-2.0.txt +202 -0
- package/docs/scripts/third-party/fuse.js +9 -0
- package/docs/scripts/third-party/lang-css.js +2 -0
- package/docs/scripts/third-party/prettify.js +28 -0
- package/docs/static/favicon.png +0 -0
- package/docs/static/index.css +4 -0
- package/docs/styles/clean-jsdoc-theme-base.css +393 -0
- package/docs/styles/clean-jsdoc-theme-dark.css +324 -0
- package/docs/styles/clean-jsdoc-theme-light.css +319 -0
- package/docs/styles/reset.css +346 -0
- package/docs/styles/third-party/ionicons.min.css +11 -0
- package/docs/styles/third-party/prettify-jsdoc.css +111 -0
- package/docs/styles/third-party/prettify-tomorrow.css +5 -0
- package/engine/engine.all.js +1997 -744
- package/engine/engine.all.min.js +1 -1
- package/engine/engine.all.release.js +1743 -569
- package/engine/engine.js +107 -39
- package/engine/engineAudio.js +152 -34
- package/engine/{build.bat → engineBuild.bat} +10 -10
- package/engine/{buildSetup.bat → engineBuildSetup.bat} +0 -0
- package/engine/engineDebug.js +105 -32
- package/engine/engineDraw.js +137 -32
- package/engine/engineInput.js +111 -37
- package/engine/{engineMedal.js → engineMedals.js} +129 -56
- package/engine/engineObject.js +117 -67
- package/engine/engineParticles.js +282 -0
- package/engine/engineRelease.js +3 -9
- package/engine/engineSettings.js +190 -0
- package/engine/engineTileLayer.js +147 -44
- package/engine/engineUtilities.js +488 -0
- package/engine/engineWebGL.js +113 -74
- package/examples/breakout/game.js +1 -1
- package/examples/breakout/gameObjects.js +3 -3
- package/examples/breakout/index.html +3 -3
- package/examples/platformer/game.js +5 -7
- package/examples/platformer/gameEffects.js +7 -7
- package/examples/platformer/gameLevel.js +2 -2
- package/examples/platformer/gameObjects.js +4 -5
- package/examples/platformer/gamePlayer.js +3 -3
- package/examples/platformer/index.html +6 -6
- package/examples/puzzle/game.js +1 -2
- package/examples/puzzle/index.html +2 -2
- package/examples/stress/index.html +2 -8
- package/game.js +5 -11
- package/index.html +13 -13
- package/package.json +4 -4
- package/tiles.png +0 -0
- package/engine/engineConfig.js +0 -64
- package/engine/engineParticle.js +0 -199
- package/engine/engineUtil.js +0 -147
|
@@ -1,22 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
/**
|
|
2
|
+
* LittleJS Tile Layer System
|
|
3
|
+
* <br> - Caches arrays of tiles to offscreen canvas for fast rendering
|
|
4
|
+
* <br> - Unlimted numbers of layers, allocates canvases as needed
|
|
5
|
+
* <br> - Interfaces with EngineObject for collision
|
|
6
|
+
* <br> - Collision layer is separate from visible layers
|
|
7
|
+
* <br> - Tile layers can be drawn to using their context with canvas2d
|
|
8
|
+
* <br> - It is recommended to have a visible layer that matches the collision
|
|
9
|
+
* @namespace TileCollision
|
|
10
|
+
*/
|
|
10
11
|
|
|
11
12
|
'use strict';
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
/** The tile collision layer array, use setTileCollisionData and getTileCollisionData to access
|
|
15
|
+
* @memberof TileCollision */
|
|
16
16
|
let tileCollision = [];
|
|
17
|
+
|
|
18
|
+
/** Size of the tile collision layer
|
|
19
|
+
* @type {Vector2}
|
|
20
|
+
* @memberof TileCollision */
|
|
17
21
|
let tileCollisionSize = vec2();
|
|
18
|
-
const tileLayerCanvasCache = [];
|
|
19
22
|
|
|
23
|
+
/** Clear and initialize tile collision
|
|
24
|
+
* @param {Vector2} size
|
|
25
|
+
* @memberof TileCollision */
|
|
20
26
|
function initTileCollision(size)
|
|
21
27
|
{
|
|
22
28
|
tileCollisionSize = size;
|
|
@@ -25,21 +31,34 @@ function initTileCollision(size)
|
|
|
25
31
|
tileCollision[i] = 0;
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
/** Set tile collision data
|
|
35
|
+
* @param {Vector2} pos
|
|
36
|
+
* @param {Number} [data=0]
|
|
37
|
+
* @memberof TileCollision */
|
|
29
38
|
const setTileCollisionData = (pos, data=0)=>
|
|
30
39
|
pos.arrayCheck(tileCollisionSize) && (tileCollision[(pos.y|0)*tileCollisionSize.x+pos.x|0] = data);
|
|
40
|
+
|
|
41
|
+
/** Get tile collision data
|
|
42
|
+
* @param {Vector2} pos
|
|
43
|
+
* @return {Number}
|
|
44
|
+
* @memberof TileCollision */
|
|
31
45
|
const getTileCollisionData = (pos)=>
|
|
32
46
|
pos.arrayCheck(tileCollisionSize) ? tileCollision[(pos.y|0)*tileCollisionSize.x+pos.x|0] : 0;
|
|
33
47
|
|
|
34
|
-
|
|
48
|
+
/** Check if collision with another object should occur
|
|
49
|
+
* @param {Vector2} pos
|
|
50
|
+
* @param {Vector2} [size=new Vector2(1,1)]
|
|
51
|
+
* @param {EngineObject} [object]
|
|
52
|
+
* @return {Boolean}
|
|
53
|
+
* @memberof TileCollision */
|
|
35
54
|
function tileCollisionTest(pos, size=vec2(), object)
|
|
36
55
|
{
|
|
37
56
|
const minX = max(Math.floor(pos.x - size.x/2), 0);
|
|
38
57
|
const minY = max(Math.floor(pos.y - size.y/2), 0);
|
|
39
|
-
const maxX = min(
|
|
40
|
-
const maxY = min(
|
|
41
|
-
for (let y = minY; y
|
|
42
|
-
for (let x = minX; x
|
|
58
|
+
const maxX = min(pos.x + size.x/2, tileCollisionSize.x-1);
|
|
59
|
+
const maxY = min(pos.y + size.y/2, tileCollisionSize.y-1);
|
|
60
|
+
for (let y = minY; y < maxY; ++y)
|
|
61
|
+
for (let x = minX; x < maxX; ++x)
|
|
43
62
|
{
|
|
44
63
|
const tileData = tileCollision[y*tileCollisionSize.x+x];
|
|
45
64
|
if (tileData && (!object || object.collideWithTile(tileData, new Vector2(x, y))))
|
|
@@ -47,13 +66,18 @@ function tileCollisionTest(pos, size=vec2(), object)
|
|
|
47
66
|
}
|
|
48
67
|
}
|
|
49
68
|
|
|
50
|
-
|
|
51
|
-
|
|
69
|
+
/** Return the center of tile if any that is hit (this does not return the exact hit point)
|
|
70
|
+
* @param {Vector2} posStart
|
|
71
|
+
* @param {Vector2} posEnd
|
|
72
|
+
* @param {EngineObject} [object]
|
|
73
|
+
* @return {Vector2}
|
|
74
|
+
* @memberof TileCollision */
|
|
52
75
|
function tileCollisionRaycast(posStart, posEnd, object)
|
|
53
76
|
{
|
|
54
77
|
// test if a ray collides with tiles from start to end
|
|
55
|
-
|
|
56
|
-
|
|
78
|
+
// todo: a way to get the exact hit point, it must still register as inside the hit tile
|
|
79
|
+
posStart = posStart.floor();
|
|
80
|
+
posEnd = posEnd.floor();
|
|
57
81
|
const posDelta = posEnd.subtract(posStart);
|
|
58
82
|
const dx = abs(posDelta.x), dy = -abs(posDelta.y);
|
|
59
83
|
const sx = sign(posDelta.x), sy = sign(posDelta.y);
|
|
@@ -81,32 +105,74 @@ function tileCollisionRaycast(posStart, posEnd, object)
|
|
|
81
105
|
///////////////////////////////////////////////////////////////////////////////
|
|
82
106
|
// Tile Layer Rendering System
|
|
83
107
|
|
|
108
|
+
// Reuse canvas autmatically when destroyed
|
|
109
|
+
const tileLayerCanvasCache = [];
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Tile layer data object stores info about how to render a tile
|
|
113
|
+
* @example
|
|
114
|
+
* // create tile layer data with tile index 0 and random orientation and color
|
|
115
|
+
* const tileIndex = 0;
|
|
116
|
+
* const direction = randInt(4)
|
|
117
|
+
* const mirror = randInt(2);
|
|
118
|
+
* const color = randColor();
|
|
119
|
+
* const data = new TileLayerData(tileIndex, direction, mirror, color);
|
|
120
|
+
*/
|
|
84
121
|
class TileLayerData
|
|
85
122
|
{
|
|
86
|
-
|
|
123
|
+
/** Create a tile layer data object, one for each tile in a TileLayer
|
|
124
|
+
* @param {Number} [tile] - The tile to use, untextured if undefined
|
|
125
|
+
* @param {Number} [direction=0] - Integer direction of tile, in 90 degree increments
|
|
126
|
+
* @param {Boolean} [mirror=0] - If the tile should be mirrored along the x axis
|
|
127
|
+
* @param {Color} [color=new Color(1,1,1)] - Color of the tile */
|
|
128
|
+
constructor(tile, direction=0, mirror=0, color=new Color)
|
|
87
129
|
{
|
|
130
|
+
/** @property {Number} - The tile to use, untextured if undefined */
|
|
88
131
|
this.tile = tile;
|
|
132
|
+
/** @property {Number} - Integer direction of tile, in 90 degree increments */
|
|
89
133
|
this.direction = direction;
|
|
134
|
+
/** @property {Boolean} - If the tile should be mirrored along the x axis */
|
|
90
135
|
this.mirror = mirror;
|
|
136
|
+
/** @property {Color} - Color of the tile */
|
|
91
137
|
this.color = color;
|
|
92
138
|
}
|
|
139
|
+
|
|
140
|
+
/** Set this tile to clear, it will not be rendered */
|
|
93
141
|
clear() { this.tile = this.direction = this.mirror = 0; color = new Color; }
|
|
94
142
|
}
|
|
95
143
|
|
|
144
|
+
/**
|
|
145
|
+
* Tile layer object - cached rendering system for tile layers
|
|
146
|
+
* <br> - Each Tile layer is rendered to an off screen canvas
|
|
147
|
+
* <br> - Tile layers are not rendered using WebGL to allow modifications at run time
|
|
148
|
+
* <br> - Tile layers are sorted
|
|
149
|
+
* @extends EngineObject
|
|
150
|
+
* @example
|
|
151
|
+
* // create tile collision and visible tile layer
|
|
152
|
+
* initTileCollision(vec2(200,100));
|
|
153
|
+
* const tileLayer = new TileLayer();
|
|
154
|
+
*/
|
|
96
155
|
class TileLayer extends EngineObject
|
|
97
156
|
{
|
|
98
|
-
|
|
157
|
+
/** Create a tile layer object
|
|
158
|
+
* @param {Vector2} [position=new Vector2(0,0)] - World space position
|
|
159
|
+
* @param {Vector2} [size=objectDefaultSize] - World space size
|
|
160
|
+
* @param {Vector2} [tileSize=tileSizeDefault] - Size of tiles in source pixels
|
|
161
|
+
* @param {Vector2} [scale=new Vector2(1,1)] - How much to scale this layer when rendered
|
|
162
|
+
* @param {Number} [renderOrder=0] - Objects sorted by renderOrder before being rendered
|
|
163
|
+
*/
|
|
164
|
+
constructor(pos, size=tileCollisionSize, tileSize=tileSizeDefault, scale=vec2(1), renderOrder=0)
|
|
99
165
|
{
|
|
100
|
-
super(pos, size);
|
|
166
|
+
super(pos, size, -1, tileSize, 0, undefined, renderOrder);
|
|
101
167
|
|
|
102
|
-
|
|
168
|
+
/** @property {HTMLCanvasElement} - The canvas used by this tile layer */
|
|
103
169
|
this.canvas = tileLayerCanvasCache.length ? tileLayerCanvasCache.pop() : document.createElement('canvas');
|
|
170
|
+
/** @property {CanvasRenderingContext2D} - The 2D canvas context used by this tile layer */
|
|
104
171
|
this.context = this.canvas.getContext('2d');
|
|
172
|
+
/** @property {Vector2} - How much to scale this layer when rendered */
|
|
105
173
|
this.scale = scale;
|
|
106
|
-
|
|
107
|
-
this.
|
|
108
|
-
this.renderOrder = layer;
|
|
109
|
-
this.flushGLBeforeRender = 1;
|
|
174
|
+
/** @property {Boolean} [isOverlay=0] - If true this layer will render to overlay canvas and appear above all objects */
|
|
175
|
+
this.isOverlay;
|
|
110
176
|
|
|
111
177
|
// init tile data
|
|
112
178
|
this.data = [];
|
|
@@ -114,6 +180,7 @@ class TileLayer extends EngineObject
|
|
|
114
180
|
this.data.push(new TileLayerData());
|
|
115
181
|
}
|
|
116
182
|
|
|
183
|
+
/** Destroy this tile layer */
|
|
117
184
|
destroy()
|
|
118
185
|
{
|
|
119
186
|
// add canvas back to the cache
|
|
@@ -121,6 +188,10 @@ class TileLayer extends EngineObject
|
|
|
121
188
|
super.destroy();
|
|
122
189
|
}
|
|
123
190
|
|
|
191
|
+
/** Set data at a given position in the array
|
|
192
|
+
* @param {Vector2} position - Local position in array
|
|
193
|
+
* @param {TileLayerData} data - Data to set
|
|
194
|
+
* @param {Boolean} [redraw=0] - Force the tile to redraw if true */
|
|
124
195
|
setData(layerPos, data, redraw)
|
|
125
196
|
{
|
|
126
197
|
if (layerPos.arrayCheck(this.size))
|
|
@@ -130,38 +201,46 @@ class TileLayer extends EngineObject
|
|
|
130
201
|
}
|
|
131
202
|
}
|
|
132
203
|
|
|
204
|
+
/** Get data at a given position in the array
|
|
205
|
+
* @param {Vector2} layerPos - Local position in array
|
|
206
|
+
* @return {TileLayerData} */
|
|
133
207
|
getData(layerPos)
|
|
134
208
|
{ return layerPos.arrayCheck(this.size) && this.data[(layerPos.y|0)*this.size.x+layerPos.x|0]; }
|
|
135
209
|
|
|
136
|
-
|
|
210
|
+
// Tile layers are not updated
|
|
211
|
+
update() {}
|
|
212
|
+
|
|
213
|
+
// Render the tile layer, called automatically by the engine
|
|
137
214
|
render()
|
|
138
215
|
{
|
|
139
216
|
ASSERT(mainContext != this.context); // must call redrawEnd() after drawing tiles
|
|
140
217
|
|
|
141
|
-
// flush and copy gl canvas because tile canvas does not use
|
|
142
|
-
|
|
218
|
+
// flush and copy gl canvas because tile canvas does not use webgl
|
|
219
|
+
glEnable && !glOverlay && !this.isOverlay && glCopyToContext(mainContext);
|
|
143
220
|
|
|
144
221
|
// draw the entire cached level onto the main canvas
|
|
145
222
|
const pos = worldToScreen(this.pos.add(vec2(0,this.size.y*this.scale.y)));
|
|
146
|
-
mainContext.drawImage
|
|
223
|
+
(this.isOverlay ? overlayContext : mainContext).drawImage
|
|
147
224
|
(
|
|
148
225
|
this.canvas, pos.x, pos.y,
|
|
149
226
|
cameraScale*this.size.x*this.scale.x, cameraScale*this.size.y*this.scale.y
|
|
150
227
|
);
|
|
151
228
|
}
|
|
152
229
|
|
|
230
|
+
/** Draw all the tile data to an offscreen canvas using webgl if possible */
|
|
153
231
|
redraw()
|
|
154
232
|
{
|
|
155
|
-
// draw all the tile data to an offscreen canvas using webgl if possible
|
|
156
233
|
this.redrawStart();
|
|
157
234
|
this.drawAllTileData();
|
|
158
235
|
this.redrawEnd();
|
|
159
236
|
}
|
|
160
237
|
|
|
238
|
+
/** Call to start the redraw process
|
|
239
|
+
* @param {Boolean} [clear=1] - Should it clear the canvas before drawing */
|
|
161
240
|
redrawStart(clear = 1)
|
|
162
241
|
{
|
|
163
242
|
// clear and set size
|
|
164
|
-
const width
|
|
243
|
+
const width = this.size.x * this.tileSize.x;
|
|
165
244
|
const height = this.size.y * this.tileSize.y;
|
|
166
245
|
if (clear)
|
|
167
246
|
{
|
|
@@ -182,6 +261,7 @@ class TileLayer extends EngineObject
|
|
|
182
261
|
glPreRender(width, height);
|
|
183
262
|
}
|
|
184
263
|
|
|
264
|
+
/** Call to end the redraw process */
|
|
185
265
|
redrawEnd()
|
|
186
266
|
{
|
|
187
267
|
ASSERT(mainContext == this.context); // must call redrawStart() before drawing tiles
|
|
@@ -192,18 +272,24 @@ class TileLayer extends EngineObject
|
|
|
192
272
|
[mainCanvasSize, mainCanvas, mainContext, cameraScale, cameraPos] = this.savedRenderSettings;
|
|
193
273
|
}
|
|
194
274
|
|
|
275
|
+
/** Draw the tile at a given position
|
|
276
|
+
* @param {Vector2} layerPos */
|
|
195
277
|
drawTileData(layerPos)
|
|
196
278
|
{
|
|
197
279
|
// first clear out where the tile was
|
|
198
|
-
const pos = layerPos.
|
|
280
|
+
const pos = layerPos.floor().add(this.pos).add(vec2(.5));
|
|
199
281
|
this.drawCanvas2D(pos, vec2(1), 0, 0, (context)=>context.clearRect(-.5, -.5, 1, 1));
|
|
200
282
|
|
|
201
|
-
// draw the tile
|
|
283
|
+
// draw the tile if not undefined
|
|
202
284
|
const d = this.getData(layerPos);
|
|
203
|
-
|
|
204
|
-
|
|
285
|
+
if (d.tile != undefined)
|
|
286
|
+
{
|
|
287
|
+
ASSERT(mainContext == this.context); // must call redrawStart() before drawing tiles
|
|
288
|
+
drawTile(pos, vec2(1), d.tile, this.tileSize, d.color, d.direction*PI/2, d.mirror);
|
|
289
|
+
}
|
|
205
290
|
}
|
|
206
291
|
|
|
292
|
+
/** Draw all the tiles in this layer */
|
|
207
293
|
drawAllTileData()
|
|
208
294
|
{
|
|
209
295
|
for (let x = this.size.x; x--;)
|
|
@@ -211,7 +297,12 @@ class TileLayer extends EngineObject
|
|
|
211
297
|
this.drawTileData(vec2(x,y));
|
|
212
298
|
}
|
|
213
299
|
|
|
214
|
-
|
|
300
|
+
/** Draw directly to the 2d canvas in world space (bipass webgl)
|
|
301
|
+
* @param {Vector2} pos
|
|
302
|
+
* @param {Vector2} size
|
|
303
|
+
* @param {Number} angle
|
|
304
|
+
* @param {Boolean} mirror
|
|
305
|
+
* @param {Function} drawFunction */
|
|
215
306
|
drawCanvas2D(pos, size, angle, mirror, drawFunction)
|
|
216
307
|
{
|
|
217
308
|
const context = this.context;
|
|
@@ -225,8 +316,15 @@ class TileLayer extends EngineObject
|
|
|
225
316
|
context.restore();
|
|
226
317
|
}
|
|
227
318
|
|
|
228
|
-
|
|
229
|
-
|
|
319
|
+
/** Draw a tile directly onto the layer canvas
|
|
320
|
+
* @param {Vector2} pos
|
|
321
|
+
* @param {Vector2} [size=new Vector2(1,1)]
|
|
322
|
+
* @param {Number} [tileIndex=-1]
|
|
323
|
+
* @param {Vector2} [tileSize=tileSizeDefault]
|
|
324
|
+
* @param {Color} [color=new Color(1,1,1)]
|
|
325
|
+
* @param {Number} [angle=0]
|
|
326
|
+
* @param {Boolean} [mirror=0] */
|
|
327
|
+
drawTile(pos, size=vec2(1), tileIndex=-1, tileSize=tileSizeDefault, color=new Color, angle=0, mirror)
|
|
230
328
|
{
|
|
231
329
|
this.drawCanvas2D(pos, size, angle, mirror, (context)=>
|
|
232
330
|
{
|
|
@@ -247,5 +345,10 @@ class TileLayer extends EngineObject
|
|
|
247
345
|
});
|
|
248
346
|
}
|
|
249
347
|
|
|
348
|
+
/** Draw a rectangle directly onto the layer canvas
|
|
349
|
+
* @param {Vector2} pos
|
|
350
|
+
* @param {Vector2} [size=new Vector2(1,1)]
|
|
351
|
+
* @param {Color} [color=new Color(1,1,1)]
|
|
352
|
+
* @param {Number} [angle=0] */
|
|
250
353
|
drawRect(pos, size, color, angle) { this.drawTile(pos, size, -1, 0, color, angle, 0); }
|
|
251
354
|
}
|