1
0
Fork 0
lasertank-js/scripts/game.js

119 lines
3.2 KiB
JavaScript

/**
* Game initialization script
*
* Copyright (C) 2012 Mike Gerwitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
( function()
{
var load_ltg = ltjs.FileLoader( document.getElementById( 'ltg' ) ),
load_lvl = ltjs.FileLoader( document.getElementById( 'lvl' ) ),
game = document.getElementById( 'game' ),
ctx = document.getElementById( 'render' ).getContext( '2d' ),
ltg_loader = ltjs.LtgLoader(),
masker = ltjs.TileMasker( ltjs.ClassicTileDfn() ),
tile_set = null,
map_set = null,
render = null;
load_ltg.onLoad( function( e, data )
{
if ( e ) throw e;
// get tile metadata
var meta = ltg_loader.fromString( data );
masker.getMaskedTiles( meta.tiles, meta.mask, function( tdata )
{
tile_set = tdata;
gamechk();
} );
} );
load_lvl.onLoad( function( e, data )
{
if ( e ) throw e;
map_set = ltjs.MapSet( data, ltjs.ClassicMap );
gamechk();
} );
function gamechk()
{
if ( !( tile_set && map_set ) ) return;
// temporary
if ( game.className.search( 'opening' ) > -1 ) return;
// clear any existing locks before rendering (allowing the user to change
// tile sets and map sets)
render && render.freeCanvas();
render = ltjs.MapRender( ctx, tile_set );
render.render( map_set.getMapByNumber( 1 ) );
}
// temporary
document.getElementById( 'new' ).onclick = function()
{
game.className = game.className.replace( ' opening', '' );
gamechk();
};
( function processMenu()
{
var menus = document.querySelectorAll( '.menu > li > a' ),
i = menus.length,
click = function( event )
{
event.target.parentNode.parentNode.className += ' focus';
return false;
};
var menu = document.querySelector( '.menu' )
menu.onmouseout = function( event )
{
// do not close the menu if our new target is a menu child element
if ( isNodeOrChildOf( menu, event.relatedTarget ) ) return;
var node = document.querySelector( '.menu' );
node.className = node.className.replace( ' focus', '' );
};
while ( i-- )
{
// ultimately we'll want to use onmousedown, but we'll leave it as
// onclick for now since we don't offer mouseup
menus[ i ].onclick = click;
}
function isNodeOrChildOf( parent, node )
{
if ( !node || !node.parentNode ) return false;
return ( ( node === parent ) || ( node.parentNode === parent ) )
? true
: isNodeOrChildOf( parent, node.parentNode );
}
} )();
} )();