1
0
Fork 0
lasertank-js/test/ltgloader-demo.html

210 lines
5.7 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>LtgLoader &amp; TileMasker Demonstration</title>
<style type="text/css">
#bitmaps img {
border: 1px dotted black;
margin: 0.5em;
}
canvas {
float: left;
}
dt {
font-weight: bold;
text-align: right;
padding-right: 1em;
width: 7em;
height: 1.5em;
float: left;
}
dd { height: 1.5em; }
</style>
</head>
<body>
<h1>LtgLoader &amp; TileMasker Demonstration</h1>
<noscript>Please enable JavaScript.</noscript>
<div id="bitmaps">
<img id="bmp_game" />
<img id="bmp_mas" />
</div>
<input type="file" id="ltg" />
<input type="file" id="lvl" />
<dl>
<dt>Name</dt>
<dd id="ltg_name">(No LTG Loaded)</dd>
<dt>Author</dt>
<dd id="ltg_author">&nbsp;</dd>
<dt>Description</dt>
<dd id="ltg_desc">&nbsp;</dd>
<dt>Id</dt>
<dd id="ltg_id">&nbsp;</dd>
</dl>
<canvas id="canvas" width="250" height="400">
Your browser does not support the canvas element.
</canvas>
<canvas id="canvas_map" width="512" height="512"></canvas>
<script src="../scripts/ease.min.js"></script>
<script>
window.ltjs = {};
window.Class = easejs.Class;
window.Interface = easejs.Interface;
var ctx = document.getElementById( 'canvas' ).getContext( '2d' ),
ctxmap = document.getElementById( 'canvas_map' ).getContext( '2d' ),
ltg = document.getElementById( 'ltg' ),
lvl = document.getElementById( 'lvl' ),
// animation timer
ianim,
tile_set;
ctx.font = '7pt Arial';
ltg.addEventListener( 'change', ltgChange, false );
lvl.addEventListener( 'change', lvlChange, false );
function ltgChange( event )
{
if ( ltg.files.length === 0 ) return;
clearInterval( ianim );
var file = ltg.files[ 0 ],
reader = new FileReader();
reader.onload = function( event )
{
var loader = ltjs.LtgLoader(),
masker = ltjs.TileMasker( ltjs.ClassicTileDfn() ),
meta = loader.fromString( event.target.result ),
bmp_game = document.getElementById( 'bmp_game' ),
bmp_mask = document.getElementById( 'bmp_mas' );
bmp_game.src = meta.tiles;
bmp_mask.src = meta.mask;
// display metadata
for ( var name in meta )
{
if ( ( name === 'tiles' ) || ( name === 'mask' ) )
{
continue;
}
document.getElementById( 'ltg_' + name ).innerHTML = meta[ name ];
}
masker.getMaskedTiles( meta.tiles, meta.mask, function( tiles )
{
// clear canvas to erase any previous LTG contents
ctx.clearRect( 0, 0, ctx.canvas.width, ctx.canvas.height );
var i = 0,
anim = [];
tile_set = tiles;
console.log( tile_set );
for ( var tile in tiles )
{
var x = ( ( i % 5 ) * 50 ),
y = ( Math.floor( i / 5 ) * 50 ),
cur = tiles[ tile ];
ctx.putImageData( cur.data, x, y );
ctx.fillText( tile, x, ( y + 43 ), 50 );
// is this tile animated?
if ( cur !== cur.next )
{
// add to animation list
anim.push( [ cur.next, x, y ] );
}
i++;
}
// animate tiles with multiple frames
ianim = setInterval( function()
{
var i = anim.length;
while ( i-- )
{
var data = anim[ i ],
img = data[ 0 ].data,
x = data[ 1 ],
y = data[ 2 ];
// clear tile area (in case masks differ) and render the
// next frame
ctx.clearRect( x, y, img.width, img.height );
ctx.putImageData( img, x, y );
// advance the frame
anim[ i ][ 0 ] = data[ 0 ].next;
}
}, 200 );
// trigger map change (in case of page refresh or tile set change
// after map is already loaded)
lvlChange();
} );
reader.onerror = function( e )
{
alert(
'Error reading file: ' + e.message + '\n\n' +
'Are you viewing this page locally? Try using a webserver.'
);
};
};
reader.readAsBinaryString( file );
}
function lvlChange()
{
if ( lvl.files.length === 0 ) return;
var file = lvl.files[ 0 ],
reader = new FileReader();
reader.onload = function( event )
{
var map_set = ltjs.MapSet( event.target.result, ltjs.ClassicMap ),
render = ltjs.MapRender( ctxmap, tile_set );
render.render( map_set.getMapByNumber( 1 ) );
};
reader.readAsBinaryString( file );
}
// invoke immediately, as we may have a filename prefilled from a refresh
ltgChange();
</script>
<script src="../lib/TileDfn.js"></script>
<script src="../lib/ClassicTileDfn.js"></script>
<script src="../lib/LtgLoader.js"></script>
<script src="../lib/TileMasker.js"></script>
<script src="../lib/MapSet.js"></script>
<script src="../lib/Map.js"></script>
<script src="../lib/ClassicMap.js"></script>
<script src="../lib/MapRender.js"></script>
</body>
</html>