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
package/index.html
CHANGED
|
@@ -4,21 +4,21 @@
|
|
|
4
4
|
</head><body>
|
|
5
5
|
|
|
6
6
|
<!-- LittleJS engine scripts -->
|
|
7
|
-
<script src=engine/engineDebug.js?
|
|
8
|
-
<script src=engine/
|
|
9
|
-
<script src=engine/
|
|
10
|
-
<script src=engine/
|
|
11
|
-
<script src=engine/
|
|
12
|
-
<script src=engine/
|
|
13
|
-
<script src=engine/
|
|
14
|
-
<script src=engine/
|
|
15
|
-
<script src=engine/
|
|
16
|
-
<script src=engine/
|
|
17
|
-
<script src=engine/
|
|
18
|
-
<script src=engine/
|
|
7
|
+
<script src=engine/engineDebug.js?60></script>
|
|
8
|
+
<script src=engine/engineUtilities.js?60></script>
|
|
9
|
+
<script src=engine/engineSettings.js?60></script>
|
|
10
|
+
<script src=engine/engineObject.js?60></script>
|
|
11
|
+
<script src=engine/engineDraw.js?60></script>
|
|
12
|
+
<script src=engine/engineInput.js?60></script>
|
|
13
|
+
<script src=engine/engineAudio.js?60></script>
|
|
14
|
+
<script src=engine/engineTileLayer.js?60></script>
|
|
15
|
+
<script src=engine/engineParticles.js?60></script>
|
|
16
|
+
<script src=engine/engineMedals.js?60></script>
|
|
17
|
+
<script src=engine/engineWebGL.js?60></script>
|
|
18
|
+
<script src=engine/engine.js?60></script>
|
|
19
19
|
|
|
20
20
|
<!-- You can also include all engine scripts like this... -->
|
|
21
21
|
<!-- <script src=engine/engine.all.js></script> -->
|
|
22
22
|
|
|
23
23
|
<!-- Add your game scripts here -->
|
|
24
|
-
<script src=game.js?
|
|
24
|
+
<script src=game.js?60></script>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "littlejsengine",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.8",
|
|
4
4
|
"description": "LittleJS - Tiny and Fast HTML5 Game Engine",
|
|
5
5
|
"main": "engine/engine.all.js",
|
|
6
6
|
"repository": {
|
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
"HTML5",
|
|
12
12
|
"JavaScript",
|
|
13
13
|
"game",
|
|
14
|
+
"engine",
|
|
14
15
|
"library",
|
|
15
16
|
"JS13K",
|
|
16
|
-
"webgl"
|
|
17
|
-
"engine"
|
|
17
|
+
"webgl"
|
|
18
18
|
],
|
|
19
19
|
"author": "Frank Force",
|
|
20
20
|
"license": "MIT",
|
|
@@ -22,4 +22,4 @@
|
|
|
22
22
|
"url": "https://github.com/KilledByAPixel/LittleJS/issues"
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://github.com/KilledByAPixel/LittleJS"
|
|
25
|
-
}
|
|
25
|
+
}
|
package/tiles.png
CHANGED
|
Binary file
|
package/engine/engineConfig.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
LittleJS Engine Configuration
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
6
|
-
// display settings
|
|
7
|
-
|
|
8
|
-
const maxWidth = 1920, maxHeight = 1200; // up to 1080p and 16:10
|
|
9
|
-
let defaultFont = 'arial'; // font used for text rendering
|
|
10
|
-
let fixedWidth = 0, fixedHeight = 0; // use native resolution
|
|
11
|
-
let fixedFitToWindow = 1; // stretch canvas to fit window
|
|
12
|
-
//const fixedWidth = 1280, fixedHeight = 720; // 720p
|
|
13
|
-
//const fixedWidth = 1920, fixedHeight = 1080; // 1080p
|
|
14
|
-
//const fixedWidth = 128, fixedHeight = 128; // PICO-8
|
|
15
|
-
//const fixedWidth = 240, fixedHeight = 136; // TIC-80
|
|
16
|
-
|
|
17
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
18
|
-
// tile sheet settings
|
|
19
|
-
|
|
20
|
-
const defaultTileSize = vec2(16); // default size of tiles in pixels
|
|
21
|
-
const tileBleedShrinkFix = .3; // prevent tile bleeding from neighbors
|
|
22
|
-
let pixelated = 1; // use crisp pixels for pixel art
|
|
23
|
-
|
|
24
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
25
|
-
// webgl config
|
|
26
|
-
|
|
27
|
-
const glEnable = 1; // can run without gl (texured coloring will be disabled)
|
|
28
|
-
let glOverlay = 0; // fix slow rendering in some browsers by not compositing the WebGL canvas
|
|
29
|
-
|
|
30
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
31
|
-
// object config
|
|
32
|
-
|
|
33
|
-
const defaultObjectSize = vec2(.999); // size of objecs, tiny bit less then 1 to fit in holes
|
|
34
|
-
const defaultObjectMass = 1; // how heavy are objects for collison calcuations
|
|
35
|
-
const defaultObjectDamping = .99; // how much to slow velocity by each frame 0-1
|
|
36
|
-
const defaultObjectAngleDamping = .99; // how much to slow angular velocity each frame 0-1
|
|
37
|
-
const defaultObjectElasticity = 0; // how much to bounce 0-1
|
|
38
|
-
const defaultObjectFriction = .8; // how much to slow when touching 0-1
|
|
39
|
-
const maxObjectSpeed = 1; // camp max speed to avoid fast objects missing collisions
|
|
40
|
-
|
|
41
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
42
|
-
// input config
|
|
43
|
-
|
|
44
|
-
const gamepadsEnable = 1; // should gamepads be allowed
|
|
45
|
-
const touchInputEnable = 1; // touch input is routed to mouse
|
|
46
|
-
const copyGamepadDirectionToStick = 1; // allow players to use dpad as analog stick
|
|
47
|
-
const copyWASDToDpad = 1; // allow players to use WASD as direction keys
|
|
48
|
-
|
|
49
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
50
|
-
// audio config
|
|
51
|
-
|
|
52
|
-
const soundEnable = 1; // all audio can be disabled
|
|
53
|
-
let audioVolume = .4; // volume for sound, music and speech
|
|
54
|
-
const defaultSoundRange = 30; // range where sound no longer plays
|
|
55
|
-
const defaultSoundTaper = .7; // what range percent to start tapering off sound 0-1
|
|
56
|
-
|
|
57
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
58
|
-
// medals config
|
|
59
|
-
|
|
60
|
-
const medalDisplayTime = 5; // how long to show medals
|
|
61
|
-
const medalDisplaySlideTime = .5; // how quick to slide on/off medals
|
|
62
|
-
const medalDisplayWidth = 640; // width of medal display
|
|
63
|
-
const medalDisplayHeight = 99; // height of medal display
|
|
64
|
-
const medalDisplayIconSize = 80; // size of icon in medal display
|
package/engine/engineParticle.js
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
LittleJS Particle System
|
|
3
|
-
- Spawns particles with randomness from parameters
|
|
4
|
-
- Updates particle physics
|
|
5
|
-
- Fast particle rendering
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
'use strict';
|
|
9
|
-
|
|
10
|
-
class ParticleEmitter extends EngineObject
|
|
11
|
-
{
|
|
12
|
-
constructor
|
|
13
|
-
(
|
|
14
|
-
pos, // world space position of emitter
|
|
15
|
-
emitSize = 0, // size of emitter (float for circle diameter, vec2 for rect)
|
|
16
|
-
emitTime = 0, // how long to stay alive (0 is forever)
|
|
17
|
-
emitRate = 100, // how many particles per second to spawn
|
|
18
|
-
emitConeAngle = PI, // local angle to apply velocity to particles from emitter
|
|
19
|
-
tileIndex = -1, // index into tile sheet, if <0 no texture is applied
|
|
20
|
-
tileSize = defaultTileSize, // tile size for particles
|
|
21
|
-
colorStartA = new Color, // color at start of life
|
|
22
|
-
colorStartB = new Color, // randomized between start colors
|
|
23
|
-
colorEndA = new Color(1,1,1,0), // color at end of life
|
|
24
|
-
colorEndB = new Color(1,1,1,0), // randomized between end colors
|
|
25
|
-
particleTime = .5, // how long particles live
|
|
26
|
-
sizeStart = .1, // how big are particles at start
|
|
27
|
-
sizeEnd = 1, // how big are particles at end
|
|
28
|
-
speed = .1, // how fast are particles when spawned
|
|
29
|
-
angleSpeed = .05, // how fast are particles rotating
|
|
30
|
-
damping = 1, // how much to dampen particle speed
|
|
31
|
-
angleDamping = 1, // how much to dampen particle angular speed
|
|
32
|
-
gravityScale = 0, // how much does gravity effect particles
|
|
33
|
-
particleConeAngle = PI, // cone for start particle angle
|
|
34
|
-
fadeRate = .1, // how quick to fade in particles at start/end in percent of life
|
|
35
|
-
randomness = .2, // apply extra randomness percent
|
|
36
|
-
collideTiles, // do particles collide against tiles
|
|
37
|
-
additive, // should particles use addtive blend
|
|
38
|
-
randomColorLinear = 1, // should color be randomized linearly or across each component
|
|
39
|
-
renderOrder = additive ? 1e9 : 0// render order for particles (additive is above other stuff by default)
|
|
40
|
-
)
|
|
41
|
-
{
|
|
42
|
-
super(pos, new Vector2, tileIndex, tileSize);
|
|
43
|
-
|
|
44
|
-
// emitter settings
|
|
45
|
-
this.emitSize = emitSize
|
|
46
|
-
this.emitTime = emitTime;
|
|
47
|
-
this.emitRate = emitRate;
|
|
48
|
-
this.emitConeAngle = emitConeAngle;
|
|
49
|
-
|
|
50
|
-
// color settings
|
|
51
|
-
this.colorStartA = colorStartA;
|
|
52
|
-
this.colorStartB = colorStartB;
|
|
53
|
-
this.colorEndA = colorEndA;
|
|
54
|
-
this.colorEndB = colorEndB;
|
|
55
|
-
this.randomColorLinear = randomColorLinear;
|
|
56
|
-
|
|
57
|
-
// particle settings
|
|
58
|
-
this.particleTime = particleTime;
|
|
59
|
-
this.sizeStart = sizeStart;
|
|
60
|
-
this.sizeEnd = sizeEnd;
|
|
61
|
-
this.speed = speed;
|
|
62
|
-
this.angleSpeed = angleSpeed;
|
|
63
|
-
this.damping = damping;
|
|
64
|
-
this.angleDamping = angleDamping;
|
|
65
|
-
this.gravityScale = gravityScale;
|
|
66
|
-
this.particleConeAngle = particleConeAngle;
|
|
67
|
-
this.fadeRate = fadeRate;
|
|
68
|
-
this.randomness = randomness;
|
|
69
|
-
this.collideTiles = collideTiles;
|
|
70
|
-
this.additive = additive;
|
|
71
|
-
this.renderOrder = renderOrder;
|
|
72
|
-
this.trailScale =
|
|
73
|
-
this.emitTimeBuffer = 0;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
update()
|
|
77
|
-
{
|
|
78
|
-
// only do default update to apply parent transforms
|
|
79
|
-
this.parent && super.update();
|
|
80
|
-
|
|
81
|
-
// update emitter
|
|
82
|
-
if (!this.emitTime || this.getAliveTime() <= this.emitTime)
|
|
83
|
-
{
|
|
84
|
-
// emit particles
|
|
85
|
-
if (this.emitRate)
|
|
86
|
-
{
|
|
87
|
-
const rate = 1/this.emitRate;
|
|
88
|
-
for (this.emitTimeBuffer += timeDelta; this.emitTimeBuffer > 0; this.emitTimeBuffer -= rate)
|
|
89
|
-
this.emitParticle();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
else
|
|
93
|
-
this.destroy();
|
|
94
|
-
|
|
95
|
-
debugParticles && debugRect(this.pos, vec2(this.emitSize), '#0f0', 0, this.angle);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
emitParticle()
|
|
99
|
-
{
|
|
100
|
-
// spawn a particle
|
|
101
|
-
const pos = this.emitSize.x != undefined ? // check if vec2 was used for size
|
|
102
|
-
(new Vector2(rand(-.5,.5), rand(-.5,.5))).multiply(this.emitSize).rotate(this.angle) // box emitter
|
|
103
|
-
: randInCircle(this.emitSize * .5); // circle emitter
|
|
104
|
-
const particle = new Particle(this.pos.add(pos), this.tileIndex, this.tileSize,
|
|
105
|
-
this.angle + rand(this.particleConeAngle, -this.particleConeAngle));
|
|
106
|
-
|
|
107
|
-
// randomness scales each paremeter by a percentage
|
|
108
|
-
const randomness = this.randomness;
|
|
109
|
-
const randomizeScale = (v)=> v + v*rand(randomness, -randomness);
|
|
110
|
-
|
|
111
|
-
// randomize particle settings
|
|
112
|
-
const particleTime = randomizeScale(this.particleTime);
|
|
113
|
-
const sizeStart = randomizeScale(this.sizeStart);
|
|
114
|
-
const sizeEnd = randomizeScale(this.sizeEnd);
|
|
115
|
-
const speed = randomizeScale(this.speed);
|
|
116
|
-
const angleSpeed = randomizeScale(this.angleSpeed) * randSign();
|
|
117
|
-
const coneAngle = rand(this.emitConeAngle, -this.emitConeAngle);
|
|
118
|
-
const colorStart = randColor(this.colorStartA, this.colorStartB, this.randomColorLinear);
|
|
119
|
-
const colorEnd = randColor(this.colorEndA, this.colorEndB, this.randomColorLinear);
|
|
120
|
-
|
|
121
|
-
// build particle settings
|
|
122
|
-
particle.colorStart = colorStart;
|
|
123
|
-
particle.colorEndDelta = colorEnd.subtract(colorStart);
|
|
124
|
-
particle.velocity = (new Vector2).setAngle(this.angle + coneAngle, speed);
|
|
125
|
-
particle.angleVelocity = angleSpeed;
|
|
126
|
-
particle.lifeTime = particleTime;
|
|
127
|
-
particle.sizeStart = sizeStart;
|
|
128
|
-
particle.sizeEndDelta = sizeEnd - sizeStart;
|
|
129
|
-
particle.fadeRate = this.fadeRate;
|
|
130
|
-
particle.damping = this.damping;
|
|
131
|
-
particle.angleDamping = this.angleDamping;
|
|
132
|
-
particle.elasticity = this.elasticity;
|
|
133
|
-
particle.friction = this.friction;
|
|
134
|
-
particle.gravityScale = this.gravityScale;
|
|
135
|
-
particle.collideTiles = this.collideTiles;
|
|
136
|
-
particle.additive = this.additive;
|
|
137
|
-
particle.renderOrder = this.renderOrder;
|
|
138
|
-
particle.trailScale = this.trailScale;
|
|
139
|
-
particle.mirror = rand()<.5;
|
|
140
|
-
|
|
141
|
-
// setup callbacks for particles
|
|
142
|
-
particle.destroyCallback = this.particleDestroyCallback;
|
|
143
|
-
this.particleCreateCallback && this.particleCreateCallback(particle);
|
|
144
|
-
|
|
145
|
-
// return the newly created particle
|
|
146
|
-
return particle;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
render() {} // emitters are not rendered
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
153
|
-
// particle object
|
|
154
|
-
|
|
155
|
-
class Particle extends EngineObject
|
|
156
|
-
{
|
|
157
|
-
constructor(pos, tileIndex, tileSize, angle) { super(pos, new Vector2, tileIndex, tileSize, angle); }
|
|
158
|
-
|
|
159
|
-
render()
|
|
160
|
-
{
|
|
161
|
-
// modulate size and color
|
|
162
|
-
const p = min((time - this.spawnTime) / this.lifeTime, 1);
|
|
163
|
-
const radius = this.sizeStart + p * this.sizeEndDelta;
|
|
164
|
-
const size = new Vector2(radius, radius);
|
|
165
|
-
const fadeRate = this.fadeRate/2;
|
|
166
|
-
const color = new Color(
|
|
167
|
-
this.colorStart.r + p * this.colorEndDelta.r,
|
|
168
|
-
this.colorStart.g + p * this.colorEndDelta.g,
|
|
169
|
-
this.colorStart.b + p * this.colorEndDelta.b,
|
|
170
|
-
(this.colorStart.a + p * this.colorEndDelta.a) *
|
|
171
|
-
(p < fadeRate ? p/fadeRate : p > 1-fadeRate ? (1-p)/fadeRate : 1)); // fade alpha
|
|
172
|
-
|
|
173
|
-
// draw the particle
|
|
174
|
-
this.additive && setBlendMode(1);
|
|
175
|
-
if (this.trailScale)
|
|
176
|
-
{
|
|
177
|
-
// trail style particles
|
|
178
|
-
const speed = this.velocity.length();
|
|
179
|
-
const direction = this.velocity.scale(1/speed);
|
|
180
|
-
const trailLength = speed * this.trailScale;
|
|
181
|
-
size.y = max(size.x, trailLength);
|
|
182
|
-
this.angle = direction.angle();
|
|
183
|
-
drawTile(this.pos.add(direction.multiply(vec2(0,-trailLength/2))), size, this.tileIndex, this.tileSize, color, this.angle, this.mirror);
|
|
184
|
-
}
|
|
185
|
-
else
|
|
186
|
-
drawTile(this.pos, size, this.tileIndex, this.tileSize, color, this.angle, this.mirror);
|
|
187
|
-
this.additive && setBlendMode();
|
|
188
|
-
debugParticles && debugRect(this.pos, size, '#f005', 0, this.angle);
|
|
189
|
-
|
|
190
|
-
if (p == 1)
|
|
191
|
-
{
|
|
192
|
-
// destroy particle when it's time runs out
|
|
193
|
-
this.color = color;
|
|
194
|
-
this.size = size;
|
|
195
|
-
this.destroyCallback && this.destroyCallback(this);
|
|
196
|
-
this.destroyed = 1;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
package/engine/engineUtil.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/*
|
|
3
|
-
LittleJS Utility Classes and Functions
|
|
4
|
-
- Vector2 - fast, simple, easy vector class
|
|
5
|
-
- Color - holds a rgba color with math functions
|
|
6
|
-
- Timer - tracks time automatically
|
|
7
|
-
- Small math lib
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
'use strict';
|
|
11
|
-
|
|
12
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
13
|
-
// helper functions
|
|
14
|
-
|
|
15
|
-
const PI = Math.PI;
|
|
16
|
-
const isChrome = window['chrome'];
|
|
17
|
-
const abs = (a)=> a < 0 ? -a : a;
|
|
18
|
-
const sign = (a)=> a < 0 ? -1 : 1;
|
|
19
|
-
const min = (a, b)=> a < b ? a : b;
|
|
20
|
-
const max = (a, b)=> a > b ? a : b;
|
|
21
|
-
const mod = (a, b)=> ((a % b) + b) % b;
|
|
22
|
-
const clamp = (v, max=1, min=0)=> (ASSERT(max > min), v < min ? min : v > max ? max : v);
|
|
23
|
-
const percent = (v, max=1, min=0)=> max-min ? clamp((v-min) / (max-min)) : 0;
|
|
24
|
-
const lerp = (p, max=1, min=0)=> min + clamp(p) * (max-min);
|
|
25
|
-
const formatTime = (t)=> (t/60|0)+':'+(t%60<10?'0':'')+(t%60|0);
|
|
26
|
-
const isOverlapping = (pA, sA, pB, sB)=> abs(pA.x - pB.x)*2 < sA.x + sB.x & abs(pA.y - pB.y)*2 < sA.y + sB.y;
|
|
27
|
-
const nearestPowerOfTwo = (v)=> 2**Math.ceil(Math.log2(v));
|
|
28
|
-
const wave = (f=1,a=1,t=time)=> a/2 * (1 - Math.cos(t*f*2*PI));
|
|
29
|
-
const smoothStep = (p)=> p * p * (3 - 2 * p);
|
|
30
|
-
|
|
31
|
-
// random functions
|
|
32
|
-
const rand = (a=1, b=0)=> b + (a-b)*Math.random();
|
|
33
|
-
const randInt = (a=1, b=0)=> rand(a,b)|0;
|
|
34
|
-
const randSign = ()=> (rand(2)|0)*2-1;
|
|
35
|
-
const randInCircle = (radius=1, minRadius=0)=> radius > 0 ? randVector(radius * rand(minRadius / radius, 1)**.5) : new Vector2;
|
|
36
|
-
const randVector = (length=1)=> new Vector2().setAngle(rand(2*PI), length);
|
|
37
|
-
const randColor = (cA = new Color, cB = new Color(0,0,0,1), linear)=>
|
|
38
|
-
linear ? cA.lerp(cB, rand()) : new Color(rand(cA.r,cB.r),rand(cA.g,cB.g),rand(cA.b,cB.b),rand(cA.a,cB.a));
|
|
39
|
-
|
|
40
|
-
// seeded random numbers using xorshift
|
|
41
|
-
let randSeed = 1;
|
|
42
|
-
const randSeeded = (a=1, b=0)=>
|
|
43
|
-
{
|
|
44
|
-
randSeed ^= randSeed << 13; randSeed ^= randSeed >>> 17; randSeed ^= randSeed << 5;
|
|
45
|
-
return b + (a-b)*abs(randSeed % 1e9)/1e9;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// create a 2d vector, can take another Vector2 to copy, 2 scalars, or 1 scalar
|
|
49
|
-
const vec2 = (x=0, y)=> x.x == undefined? new Vector2(x, y == undefined? x : y) : new Vector2(x.x, x.y);
|
|
50
|
-
|
|
51
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
52
|
-
class Vector2
|
|
53
|
-
{
|
|
54
|
-
constructor(x=0, y=0) { this.x = x; this.y = y; }
|
|
55
|
-
|
|
56
|
-
// basic math operators, a vector or scaler can be passed in
|
|
57
|
-
copy() { return new Vector2(this.x, this.y); }
|
|
58
|
-
scale(s) { ASSERT(s.x==undefined); return new Vector2(this.x * s, this.y * s); }
|
|
59
|
-
add(v) { ASSERT(v.x!=undefined); return new Vector2(this.x + v.x, this.y + v.y); }
|
|
60
|
-
subtract(v) { ASSERT(v.x!=undefined); return new Vector2(this.x - v.x, this.y - v.y); }
|
|
61
|
-
multiply(v) { ASSERT(v.x!=undefined); return new Vector2(this.x * v.x, this.y * v.y); }
|
|
62
|
-
divide(v) { ASSERT(v.x!=undefined); return new Vector2(this.x / v.x, this.y / v.y); }
|
|
63
|
-
|
|
64
|
-
// vector math operators
|
|
65
|
-
length() { return this.lengthSquared()**.5; }
|
|
66
|
-
lengthSquared() { return this.x**2 + this.y**2; }
|
|
67
|
-
distance(p) { return this.distanceSquared(p)**.5; }
|
|
68
|
-
distanceSquared(p) { return (this.x - p.x)**2 + (this.y - p.y)**2; }
|
|
69
|
-
normalize(length=1) { const l = this.length(); return l ? this.scale(length/l) : new Vector2(length); }
|
|
70
|
-
clampLength(length=1) { const l = this.length(); return l > length ? this.scale(length/l) : this; }
|
|
71
|
-
dot(v) { ASSERT(v.x!=undefined); return this.x*v.x + this.y*v.y; }
|
|
72
|
-
cross(v) { ASSERT(v.x!=undefined); return this.x*v.y - this.y*v.x; }
|
|
73
|
-
angle() { return Math.atan2(this.x, this.y); }
|
|
74
|
-
setAngle(a, length=1) { this.x = length*Math.sin(a); this.y = length*Math.cos(a); return this; }
|
|
75
|
-
rotate(a) { const c = Math.cos(a), s = Math.sin(a); return new Vector2(this.x*c-this.y*s, this.x*s+this.y*c); }
|
|
76
|
-
direction() { return abs(this.x) > abs(this.y) ? this.x < 0 ? 3 : 1 : this.y < 0 ? 2 : 0; }
|
|
77
|
-
flip() { return new Vector2(this.y, this.x); }
|
|
78
|
-
invert() { return new Vector2(this.y, -this.x); }
|
|
79
|
-
round() { return new Vector2(Math.round(this.x), Math.round(this.y)); }
|
|
80
|
-
floor() { return new Vector2(Math.floor(this.x), Math.floor(this.y)); }
|
|
81
|
-
int() { return new Vector2(this.x|0, this.y|0); }
|
|
82
|
-
lerp(v, p) { ASSERT(v.x!=undefined); return this.add(v.subtract(this).scale(clamp(p))); }
|
|
83
|
-
area() { return this.x * this.y; }
|
|
84
|
-
arrayCheck(arraySize) { return this.x >= 0 && this.y >= 0 && this.x < arraySize.x && this.y < arraySize.y; }
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
88
|
-
class Color
|
|
89
|
-
{
|
|
90
|
-
constructor(r=1, g=1, b=1, a=1) { this.r=r; this.g=g; this.b=b; this.a=a; }
|
|
91
|
-
|
|
92
|
-
copy(c) { return new Color(this.r, this.g, this.b, this.a); }
|
|
93
|
-
add(c) { return new Color(this.r+c.r, this.g+c.g, this.b+c.b, this.a+c.a); }
|
|
94
|
-
subtract(c) { return new Color(this.r-c.r, this.g-c.g, this.b-c.b, this.a-c.a); }
|
|
95
|
-
multiply(c) { return new Color(this.r*c.r, this.g*c.g, this.b*c.b, this.a*c.a); }
|
|
96
|
-
scale(s,a=s){ return new Color(this.r*s, this.g*s, this.b*s, this.a*a); }
|
|
97
|
-
clamp() { return new Color(clamp(this.r), clamp(this.g), clamp(this.b), clamp(this.a)); }
|
|
98
|
-
lerp(c, p) { return this.add(c.subtract(this).scale(clamp(p))); }
|
|
99
|
-
mutate(amount=.05, alphaAmount=0)
|
|
100
|
-
{
|
|
101
|
-
return new Color
|
|
102
|
-
(
|
|
103
|
-
this.r + rand(amount, -amount),
|
|
104
|
-
this.g + rand(amount, -amount),
|
|
105
|
-
this.b + rand(amount, -amount),
|
|
106
|
-
this.a + rand(alphaAmount, -alphaAmount)
|
|
107
|
-
).clamp();
|
|
108
|
-
}
|
|
109
|
-
rgba()
|
|
110
|
-
{
|
|
111
|
-
ASSERT(this.r>=0 && this.r<=1 && this.g>=0 && this.g<=1 && this.b>=0 && this.b<=1 && this.a>=0 && this.a<=1);
|
|
112
|
-
return `rgb(${this.r*255|0},${this.g*255|0},${this.b*255|0},${this.a})`;
|
|
113
|
-
}
|
|
114
|
-
rgbaInt()
|
|
115
|
-
{
|
|
116
|
-
ASSERT(this.r>=0 && this.r<=1 && this.g>=0 && this.g<=1 && this.b>=0 && this.b<=1 && this.a>=0 && this.a<=1);
|
|
117
|
-
return (this.r*255|0) + (this.g*255<<8) + (this.b*255<<16) + (this.a*255<<24);
|
|
118
|
-
}
|
|
119
|
-
setHSLA(h=0, s=0, l=1, a=1)
|
|
120
|
-
{
|
|
121
|
-
const q = l < .5 ? l*(1+s) : l+s-l*s, p = 2*l-q,
|
|
122
|
-
f = (p, q, t)=>
|
|
123
|
-
(t = ((t%1)+1)%1) < 1/6 ? p+(q-p)*6*t :
|
|
124
|
-
t < 1/2 ? q :
|
|
125
|
-
t < 2/3 ? p+(q-p)*(2/3-t)*6 : p;
|
|
126
|
-
|
|
127
|
-
this.r = f(p, q, h + 1/3);
|
|
128
|
-
this.g = f(p, q, h);
|
|
129
|
-
this.b = f(p, q, h - 1/3);
|
|
130
|
-
this.a = a;
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
136
|
-
class Timer
|
|
137
|
-
{
|
|
138
|
-
constructor(timeLeft) { this.time = timeLeft == undefined ? undefined : time + timeLeft; this.setTime = timeLeft; }
|
|
139
|
-
|
|
140
|
-
set(timeLeft=0) { this.time = time + timeLeft; this.setTime = timeLeft; }
|
|
141
|
-
unset() { this.time = undefined; }
|
|
142
|
-
isSet() { return this.time != undefined; }
|
|
143
|
-
active() { return time <= this.time; }
|
|
144
|
-
elapsed() { return time > this.time; }
|
|
145
|
-
get() { return this.isSet()? time - this.time : 0; }
|
|
146
|
-
getPercent() { return this.isSet()? percent(this.time - time, 0, this.setTime) : 0; }
|
|
147
|
-
}
|