Skip to content

Commit

Permalink
typescript support
Browse files Browse the repository at this point in the history
remove <meta charset=utf-8>
add Music.stop() and Music.isPlaying()
add typescript example folder
some fixes for type issues
  • Loading branch information
KilledByAPixel committed Jun 29, 2023
1 parent 6603aa4 commit 00a9ba0
Show file tree
Hide file tree
Showing 31 changed files with 1,953 additions and 1,749 deletions.
10 changes: 4 additions & 6 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ if %ERRORLEVEL% NEQ 0 (
pause
exit /b %ERRORLEVEL%
)
del temp_%BUILD_FILENAME%

rem more minification with uglify or terser (they both do about the same)
call npx uglifyjs -o %BUILD_FILENAME% --compress --mangle -- %BUILD_FILENAME%
Expand All @@ -49,23 +50,20 @@ if %ERRORLEVEL% NEQ 0 (
)

rem roadroller compresses the code better then zip
copy %BUILD_FILENAME% roadroller_%BUILD_FILENAME%
call npx roadroller roadroller_%BUILD_FILENAME% -o roadroller_%BUILD_FILENAME%
call npx roadroller %BUILD_FILENAME% -o %BUILD_FILENAME%
if %ERRORLEVEL% NEQ 0 (
pause
exit /b %ERRORLEVEL%
)

rem build the html, you can add html header and footers here
rem type ..\header.html >> index.html
echo ^<body^>^<meta charset=utf-8^>^<script^> >> index.html
type roadroller_%BUILD_FILENAME% >> index.html
echo ^<body^>^<script^> >> index.html
type %BUILD_FILENAME% >> index.html
echo ^</script^> >> index.html

rem delete intermediate files
del %BUILD_FILENAME%
del temp_%BUILD_FILENAME%
del roadroller_%BUILD_FILENAME%

rem zip the result, ect is recommended
call ..\node_modules\ect-bin\vendor\win32\ect.exe -9 -strip -zip ..\%NAME%.zip index.html tiles.png
Expand Down
39 changes: 27 additions & 12 deletions engine/engine.all.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ const ASSERT = enableAsserts ? (...assert)=> console.assert(...assert) : ()=>{};
* @param {String} [color='#fff']
* @param {Number} [time=0]
* @param {Number} [angle=0]
* @param {Boolean} [fill=0]
* @param {Boolean} [fill=false]
* @memberof Debug */
const debugRect = (pos, size=vec2(), color='#fff', time=0, angle=0, fill=0)=>
const debugRect = (pos, size=vec2(), color='#fff', time=0, angle=0, fill=false)=>
{
ASSERT(typeof color == 'string'); // pass in regular html strings as colors
debugPrimitives.push({pos, size:vec2(size), color, time:new Timer(time), angle, fill});
Expand All @@ -72,9 +72,9 @@ const debugRect = (pos, size=vec2(), color='#fff', time=0, angle=0, fill=0)=>
* @param {Number} [radius=0]
* @param {String} [color='#fff']
* @param {Number} [time=0]
* @param {Boolean} [fill=0]
* @param {Boolean} [fill=false]
* @memberof Debug */
const debugCircle = (pos, radius=0, color='#fff', time=0, fill=0)=>
const debugCircle = (pos, radius=0, color='#fff', time=0, fill=false)=>
{
ASSERT(typeof color == 'string'); // pass in regular html strings as colors
debugPrimitives.push({pos, size:radius, color, time:new Timer(time), angle:0, fill});
Expand Down Expand Up @@ -1528,9 +1528,9 @@ class EngineObject
}

/** Set how this object collides
* @param {boolean} [collideSolidObjects=1] - Does it collide with solid objects
* @param {boolean} [isSolid=1] - Does it collide with and block other objects (expensive in large numbers)
* @param {boolean} [collideTiles=1] - Does it collide with the tile collision */
* @param {Boolean} [collideSolidObjects=1] - Does it collide with solid objects
* @param {Boolean} [isSolid=1] - Does it collide with and block other objects (expensive in large numbers)
* @param {Boolean} [collideTiles=1] - Does it collide with the tile collision */
setCollision(collideSolidObjects=1, isSolid=1, collideTiles=1)
{
ASSERT(collideSolidObjects || !isSolid); // solid objects must be set to collide
Expand All @@ -1540,6 +1540,8 @@ class EngineObject
this.collideTiles = collideTiles;
}

/** Returns string containg info about this object for debugging
* @return {String} */
toString()
{
if (debug)
Expand Down Expand Up @@ -1609,7 +1611,7 @@ let overlayContext;
let mainCanvasSize = vec2();

/** Tile sheet for batch rendering system
* @type {Image}
* @type {CanvasImageSource}
* @memberof Draw */
const tileImage = new Image;

Expand Down Expand Up @@ -2190,7 +2192,7 @@ const vibrateStop = ()=> vibrate(0);
// Touch input

/** True if a touch device has been detected
* @const {boolean}
* @const {Boolean}
* @memberof Input */
const isTouchDevice = window.ontouchstart !== undefined;

Expand Down Expand Up @@ -2520,8 +2522,21 @@ class Music
{
if (!soundEnable) return;

return playSamples(this.cachedSamples, volume, 1, 0, loop);
this.source = playSamples(this.cachedSamples, volume, 1, 0, loop);
}

/** Stop the music */
stop()
{
if (this.source)
this.source.stop();
this.source = 0;
}

/** Check if music is playing
* @return {Boolean}
*/
isPlaying() { return this.source; }
}

/** Play an mp3 or wav audio from a local file or url
Expand Down Expand Up @@ -3212,7 +3227,7 @@ class ParticleEmitter extends EngineObject
* @param {Number} [emitRate=100] - How many particles per second to spawn, does not emit if 0
* @param {Number} [emitConeAngle=PI] - Local angle to apply velocity to particles from emitter
* @param {Number} [tileIndex=-1] - Index into tile sheet, if <0 no texture is applied
* @param {Number} [tileSize=tileSizeDefault] - Tile size for particles
* @param {Vector2} [tileSize=tileSizeDefault] - Tile size for particles
* @param {Color} [colorStartA=new Color(1,1,1)] - Color at start of life 1, randomized between start colors
* @param {Color} [colorStartB=new Color(1,1,1)] - Color at start of life 2, randomized between start colors
* @param {Color} [colorEndA=new Color(1,1,1,0)] - Color at end of life 1, randomized between end colors
Expand Down Expand Up @@ -4190,7 +4205,7 @@ gl_VERTEX_BUFFER_SIZE = gl_MAX_BATCH * gl_VERTICES_PER_QUAD * gl_VERTEX_BYTE_STR
const engineName = 'LittleJS';

/** Version of engine */
const engineVersion = '1.4.9';
const engineVersion = '1.5.0';

/** Frames per second to update objects
* @default */
Expand Down
2 changes: 1 addition & 1 deletion engine/engine.all.min.js

Large diffs are not rendered by default.

39 changes: 27 additions & 12 deletions engine/engine.all.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ const ASSERT = enableAsserts ? (...assert)=> console.assert(...assert) : ()=>{};
* @param {String} [color='#fff']
* @param {Number} [time=0]
* @param {Number} [angle=0]
* @param {Boolean} [fill=0]
* @param {Boolean} [fill=false]
* @memberof Debug */
const debugRect = (pos, size=vec2(), color='#fff', time=0, angle=0, fill=0)=>
const debugRect = (pos, size=vec2(), color='#fff', time=0, angle=0, fill=false)=>
{
ASSERT(typeof color == 'string'); // pass in regular html strings as colors
debugPrimitives.push({pos, size:vec2(size), color, time:new Timer(time), angle, fill});
Expand All @@ -72,9 +72,9 @@ const debugRect = (pos, size=vec2(), color='#fff', time=0, angle=0, fill=0)=>
* @param {Number} [radius=0]
* @param {String} [color='#fff']
* @param {Number} [time=0]
* @param {Boolean} [fill=0]
* @param {Boolean} [fill=false]
* @memberof Debug */
const debugCircle = (pos, radius=0, color='#fff', time=0, fill=0)=>
const debugCircle = (pos, radius=0, color='#fff', time=0, fill=false)=>
{
ASSERT(typeof color == 'string'); // pass in regular html strings as colors
debugPrimitives.push({pos, size:radius, color, time:new Timer(time), angle:0, fill});
Expand Down Expand Up @@ -1528,9 +1528,9 @@ class EngineObject
}

/** Set how this object collides
* @param {boolean} [collideSolidObjects=1] - Does it collide with solid objects
* @param {boolean} [isSolid=1] - Does it collide with and block other objects (expensive in large numbers)
* @param {boolean} [collideTiles=1] - Does it collide with the tile collision */
* @param {Boolean} [collideSolidObjects=1] - Does it collide with solid objects
* @param {Boolean} [isSolid=1] - Does it collide with and block other objects (expensive in large numbers)
* @param {Boolean} [collideTiles=1] - Does it collide with the tile collision */
setCollision(collideSolidObjects=1, isSolid=1, collideTiles=1)
{
ASSERT(collideSolidObjects || !isSolid); // solid objects must be set to collide
Expand All @@ -1540,6 +1540,8 @@ class EngineObject
this.collideTiles = collideTiles;
}

/** Returns string containg info about this object for debugging
* @return {String} */
toString()
{
if (debug)
Expand Down Expand Up @@ -1609,7 +1611,7 @@ let overlayContext;
let mainCanvasSize = vec2();

/** Tile sheet for batch rendering system
* @type {Image}
* @type {CanvasImageSource}
* @memberof Draw */
const tileImage = new Image;

Expand Down Expand Up @@ -2190,7 +2192,7 @@ const vibrateStop = ()=> vibrate(0);
// Touch input

/** True if a touch device has been detected
* @const {boolean}
* @const {Boolean}
* @memberof Input */
const isTouchDevice = window.ontouchstart !== undefined;

Expand Down Expand Up @@ -2520,8 +2522,21 @@ class Music
{
if (!soundEnable) return;

return playSamples(this.cachedSamples, volume, 1, 0, loop);
this.source = playSamples(this.cachedSamples, volume, 1, 0, loop);
}

/** Stop the music */
stop()
{
if (this.source)
this.source.stop();
this.source = 0;
}

/** Check if music is playing
* @return {Boolean}
*/
isPlaying() { return this.source; }
}

/** Play an mp3 or wav audio from a local file or url
Expand Down Expand Up @@ -3212,7 +3227,7 @@ class ParticleEmitter extends EngineObject
* @param {Number} [emitRate=100] - How many particles per second to spawn, does not emit if 0
* @param {Number} [emitConeAngle=PI] - Local angle to apply velocity to particles from emitter
* @param {Number} [tileIndex=-1] - Index into tile sheet, if <0 no texture is applied
* @param {Number} [tileSize=tileSizeDefault] - Tile size for particles
* @param {Vector2} [tileSize=tileSizeDefault] - Tile size for particles
* @param {Color} [colorStartA=new Color(1,1,1)] - Color at start of life 1, randomized between start colors
* @param {Color} [colorStartB=new Color(1,1,1)] - Color at start of life 2, randomized between start colors
* @param {Color} [colorEndA=new Color(1,1,1,0)] - Color at end of life 1, randomized between end colors
Expand Down Expand Up @@ -4190,7 +4205,7 @@ gl_VERTEX_BUFFER_SIZE = gl_MAX_BATCH * gl_VERTICES_PER_QUAD * gl_VERTEX_BYTE_STR
const engineName = 'LittleJS';

/** Version of engine */
const engineVersion = '1.4.9';
const engineVersion = '1.5.0';

/** Frames per second to update objects
* @default */
Expand Down
2 changes: 1 addition & 1 deletion engine/engine.all.module.min.js

Large diffs are not rendered by default.

31 changes: 23 additions & 8 deletions engine/engine.all.release.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ let medalsPreventUnlock;
const engineName = 'LittleJS';

/** Version of engine */
const engineVersion = '1.4.9';
const engineVersion = '1.5.0';

/** Frames per second to update objects
* @default */
Expand Down Expand Up @@ -1462,9 +1462,9 @@ class EngineObject
}

/** Set how this object collides
* @param {boolean} [collideSolidObjects=1] - Does it collide with solid objects
* @param {boolean} [isSolid=1] - Does it collide with and block other objects (expensive in large numbers)
* @param {boolean} [collideTiles=1] - Does it collide with the tile collision */
* @param {Boolean} [collideSolidObjects=1] - Does it collide with solid objects
* @param {Boolean} [isSolid=1] - Does it collide with and block other objects (expensive in large numbers)
* @param {Boolean} [collideTiles=1] - Does it collide with the tile collision */
setCollision(collideSolidObjects=1, isSolid=1, collideTiles=1)
{
ASSERT(collideSolidObjects || !isSolid); // solid objects must be set to collide
Expand All @@ -1474,6 +1474,8 @@ class EngineObject
this.collideTiles = collideTiles;
}

/** Returns string containg info about this object for debugging
* @return {String} */
toString()
{
if (debug)
Expand Down Expand Up @@ -1543,7 +1545,7 @@ let overlayContext;
let mainCanvasSize = vec2();

/** Tile sheet for batch rendering system
* @type {Image}
* @type {CanvasImageSource}
* @memberof Draw */
const tileImage = new Image;

Expand Down Expand Up @@ -2124,7 +2126,7 @@ const vibrateStop = ()=> vibrate(0);
// Touch input

/** True if a touch device has been detected
* @const {boolean}
* @const {Boolean}
* @memberof Input */
const isTouchDevice = window.ontouchstart !== undefined;

Expand Down Expand Up @@ -2454,8 +2456,21 @@ class Music
{
if (!soundEnable) return;

return playSamples(this.cachedSamples, volume, 1, 0, loop);
this.source = playSamples(this.cachedSamples, volume, 1, 0, loop);
}

/** Stop the music */
stop()
{
if (this.source)
this.source.stop();
this.source = 0;
}

/** Check if music is playing
* @return {Boolean}
*/
isPlaying() { return this.source; }
}

/** Play an mp3 or wav audio from a local file or url
Expand Down Expand Up @@ -3146,7 +3161,7 @@ class ParticleEmitter extends EngineObject
* @param {Number} [emitRate=100] - How many particles per second to spawn, does not emit if 0
* @param {Number} [emitConeAngle=PI] - Local angle to apply velocity to particles from emitter
* @param {Number} [tileIndex=-1] - Index into tile sheet, if <0 no texture is applied
* @param {Number} [tileSize=tileSizeDefault] - Tile size for particles
* @param {Vector2} [tileSize=tileSizeDefault] - Tile size for particles
* @param {Color} [colorStartA=new Color(1,1,1)] - Color at start of life 1, randomized between start colors
* @param {Color} [colorStartB=new Color(1,1,1)] - Color at start of life 2, randomized between start colors
* @param {Color} [colorEndA=new Color(1,1,1,0)] - Color at end of life 1, randomized between end colors
Expand Down
2 changes: 1 addition & 1 deletion engine/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
const engineName = 'LittleJS';

/** Version of engine */
const engineVersion = '1.4.9';
const engineVersion = '1.5.0';

/** Frames per second to update objects
* @default */
Expand Down
15 changes: 14 additions & 1 deletion engine/engineAudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,21 @@ class Music
{
if (!soundEnable) return;

return playSamples(this.cachedSamples, volume, 1, 0, loop);
this.source = playSamples(this.cachedSamples, volume, 1, 0, loop);
}

/** Stop the music */
stop()
{
if (this.source)
this.source.stop();
this.source = 0;
}

/** Check if music is playing
* @return {Boolean}
*/
isPlaying() { return this.source; }
}

/** Play an mp3 or wav audio from a local file or url
Expand Down
16 changes: 8 additions & 8 deletions engine/engineBuild.bat
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@ if %ERRORLEVEL% NEQ 0 (
exit /b %ERRORLEVEL%
)

rem --- BUILD TYPESCRIPT DEFINITIONS ---

call npx tsc engine.all.js --declaration --allowJs --emitDeclarationOnly --outFile index.d.ts
if %ERRORLEVEL% NEQ 0 (
pause
exit /b %ERRORLEVEL%
)

rem --- BUILD ENGINE MODULE ---

set OUTPUT_FILENAME=engine.all.module.js
Expand All @@ -121,6 +113,14 @@ echo.>> %OUTPUT_FILENAME%

rem lightly minify with uglify
call npx uglifyjs -o %OUTPUT_FILENAME% -- %OUTPUT_FILENAME%
if %ERRORLEVEL% NEQ 0 (
pause
exit /b %ERRORLEVEL%
)

rem --- BUILD TYPESCRIPT DEFINITIONS ---

call npx tsc engine.all.module.js --declaration --allowJs --emitDeclarationOnly --outFile index.d.ts
if %ERRORLEVEL% NEQ 0 (
pause
exit /b %ERRORLEVEL%
Expand Down
Loading

0 comments on commit 00a9ba0

Please sign in to comment.