diff --git a/lib/ClassicMap.js b/lib/ClassicMap.js index 9fd811b..cbb64c3 100644 --- a/lib/ClassicMap.js +++ b/lib/ClassicMap.js @@ -62,6 +62,29 @@ ltjs.ClassicMap = Class( 'ClassicMap' ) */ 'private const _GOSIZE': 256, + /** + * Tunnel bitmask + * @type {number} + */ + 'private const _TMASK': 0x40, + + /** + * Colors used to identify tunnels + * + * These colors will be rendered in the background and will bleed through + * the transparent portions of the tile. We use explicit HEX codes rather + * than CSS color names because environments may vary the colors used. + * + * Taken from ColorList in LTANK2.C in the original sources. Note that there + * is an endianness difference. + * + * @type {Array.} + */ + 'private const _TCOLORS': [ + '#ff0000', '#00ff00', '#0000ff', '#00ffff', // r g b c + '#ffff00', '#ff00ff', '#ffffff', '#808080' // y m w b + ], + /** * Map set data (binary string) @@ -165,19 +188,33 @@ ltjs.ClassicMap = Class( 'ClassicMap' ) /** - * Retrieve tunnel colors + * Retrieve tunnel color * - * These colors will be rendered in the background and will bleed through - * the transparent portions of the tile. We use explicit HEX codes rather - * than CSS color names because environments may vary the colors used. + * The color will be rendered in the background and will bleed through the + * transparent portions of the tile. We use explicit HEX codes rather than + * CSS color names because environments may vary the colors used. * - * @return {Array.} + * @param {number} oid tunnel object id + * + * @return {string} tunnel color */ - 'public getTunnelColors': function() + 'public getTunnelColor': function( oid ) { - return [ - '#ff0000', '#00ff00', '#0000ff', '#00ffff', // r g b c - '#ffff00', '#ff00ff', '#ffffff', '#808080' // y m w b - ]; + // get the tunnel id by stripping off the tunnel bitmask and then grab + // the associated color + var tunnel_id = ( ( +oid ^ this.__self.$( '_TMASK' ) ) / 2 ); + + return this.__self.$( '_TCOLORS' )[ tunnel_id ] || 'black'; + }, + + + /** + * Determines if the given object is a tunnel + * + * @return {boolean} true if tunnel, otherwise false + */ + 'public isObjectTunnel': function( oid ) + { + return ( oid & this.__self.$( '_TMASK' ) ); } } ); diff --git a/lib/Map.js b/lib/Map.js index 2e419fb..789ecfd 100644 --- a/lib/Map.js +++ b/lib/Map.js @@ -81,15 +81,24 @@ ltjs.Map = Interface( 'Map', /** - * Retrieve tunnel colors + * Retrieve tunnel color * - * These colors will be rendered in the background and will bleed through - * the transparent portions of the tile. A total of eight colors should be - * returned. They may be specified in any CSS format, but explicit values - * are recommended since color names (e.g. 'green') can vary between - * environments. + * The color will be rendered in the background and will bleed through the + * transparent portions of the tile. The color may be specified in any CSS + * format, but explicit values are recommended since color names (e.g. + * 'green') can vary between environments. * - * @return {Array.} + * @param {number} object_id tunnel object id + * + * @return {string} tunnel color or reasonable default if invalid obj id */ - 'public getTunnelColors': [] + 'public getTunnelColor': [ 'object_id' ], + + + /** + * Determines if the given object is a tunnel + * + * @return {boolean} true if tunnel, otherwise false + */ + 'public isObjectTunnel': [ 'object_id' ] } ); diff --git a/lib/MapRender.js b/lib/MapRender.js index 6eef1fc..dc51667 100644 --- a/lib/MapRender.js +++ b/lib/MapRender.js @@ -23,12 +23,6 @@ */ ltjs.MapRender = Class( 'MapRender', { - /** - * Tunnel bitmask - * @type {number} - */ - 'private const _TMASK': 0x40, - /** * Property to hold lock bit on canvas element * @type {string} @@ -169,10 +163,10 @@ ltjs.MapRender = Class( 'MapRender', var objs = map.getObjects(), size = map.getDimensions(), - i = objs.length, omap = map.getObjectTileMap(), - - tunnel_colors = map.getTunnelColors(), + sizex = size[ 0 ], + sizey = size[ 1 ], + i = objs.length, // get the width and height from one of the tiles t = this._tiles.dirt.data, @@ -185,13 +179,13 @@ ltjs.MapRender = Class( 'MapRender', { var oid = objs[ i ], tid = omap[ oid ], - x = ( Math.floor( i / size[ 0 ] ) * w ), - y = ( ( i % size[ 1 ] ) * h ); + x = ( Math.floor( i / sizex ) * w ), + y = ( ( i % sizey ) * h ); // tunnels are handled a bit differently than other objects - if ( this._isTunnel( oid ) ) + if ( map.isObjectTunnel( oid ) ) { - this._renderTunnel( oid, x, y, tunnel_colors ); + this._renderTunnel( x, y, map.getTunnelColor( oid ) ); continue; } @@ -229,19 +223,6 @@ ltjs.MapRender = Class( 'MapRender', }, - /** - * Returns whether the given object id is a tunnel - * - * @param {number} oid object id - * - * @return {boolean} TRUE if object is a tunnel, otherwise false - */ - 'private _isTunnel': function( oid ) - { - return ( oid & this.__self.$( '_TMASK' ) ); - }, - - /** * Render the given tunnel * @@ -249,21 +230,15 @@ ltjs.MapRender = Class( 'MapRender', * rendered to the base canvas, whereas the tunnel tile itself is rendered * on the overlaying canvas. * - * @param {number} oid tunnel object id - * @param {number} x left position - * @param {number} y top position - * - * @param {Array.} colors tunnel colors + * @param {number} x left position + * @param {number} y top position + * @param {string} color tunnel color (CSS value) * * @return {undefined} */ - 'private _renderTunnel': function( oid, x, y, colors ) + 'private _renderTunnel': function( x, y, color ) { - // get the tunnel id by stripping off the tunnel bitmask and grab the - // associated color - var tunnel_id = ( ( +oid ^ this.__self.$( '_TMASK' ) ) / 2 ), - color = colors[ tunnel_id ] || 'black', - tdata = this._tiles.tunnel.data; + var tdata = this._tiles.tunnel.data; // fill tile with the appropriate background color for this tile this._ctx.fillStyle = color;