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

264 lines
7.1 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;
}
dl {
float: left;
width: 30em;
margin-bottom: 4em;
}
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" />
<select id="lvl_id"></select>
<br clear="both" />
<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>
<dl>
<dt>Name</dt>
<dd id="lvl_name">(No LVL loaded)</dd>
<dt>Author</dt>
<dd id="lvl_author"></dd>
<dt>Difficulty</dt>
<dd id="lvl_diff"></dd>
<dt>Hint</dt>
<dd id="lvl_hint"></dd>
</dl>
<br clear="both" />
<canvas id="canvas" width="250" height="400">
Your browser does not support the canvas element.
</canvas>
<canvas id="canvas_level" width="512" height="512"></canvas>
<script src="../lasertank.js"></script>
<script>
var ctx = document.getElementById( 'canvas' ).getContext( '2d' ),
ctxlevel = document.getElementById( 'canvas_level' ).getContext( '2d' ),
ltg = document.getElementById( 'ltg' ),
lvl = document.getElementById( 'lvl' ),
render = null,
// 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 = lasertank.LtgLoader(),
meta = loader.fromString( event.target.result ),
masker = lasertank.TileMasker(
lasertank.ClassicTileDfn(),
document
),
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;
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 level change (in case of page refresh or tile set change
// after level 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 level_set = lasertank.level.LevelSet(
event.target.result,
lasertank.level.ClassicLevel
);
// clean up any previous render and create a new one to render the
// chosen level with our chosen tile set
render && render.freeCanvas();
render = lasertank.level.LevelRender( ctxlevel, tile_set );
var lvlsel = document.getElementById( 'lvl_id' ),
count = level_set.getLevelCount();
lvlsel.innerHTML = '';
for ( var i = 1; i <= count; i++ )
{
var option = document.createElement( 'option' );
option.value = i;
option.innerHTML = i;
lvlsel.appendChild( option );
}
lvlsel.onchange = lvlchange;
function lvlchange()
{
var level = level_set.getLevelByNumber( lvlsel.selectedIndex + 1 );
render.render(
level,
lasertank.level.LevelState(
level,
lasertank.ClassicGameObjectFactory()
)
);
document.getElementById( 'lvl_name' ).innerHTML =
level.getLevelName();
document.getElementById( 'lvl_author' ).innerHTML =
level.getLevelAuthor();
document.getElementById( 'lvl_diff' ).innerHTML =
level.getLevelDifficulty();
document.getElementById( 'lvl_hint' ).innerHTML =
level.getLevelHint();
};
lvlchange();
};
reader.readAsBinaryString( file );
}
// invoke immediately, as we may have a filename prefilled from a refresh
ltgChange();
</script>
</body>
</html>