1
0
Fork 0

Refactored tunnel logic from MapRender into ClassicMap

master
Mike Gerwitz 2012-03-16 20:34:54 -04:00
parent 6af5adddb8
commit 90b9208bca
3 changed files with 76 additions and 55 deletions

View File

@ -62,6 +62,29 @@ ltjs.ClassicMap = Class( 'ClassicMap' )
*/ */
'private const _GOSIZE': 256, '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.<string>}
*/
'private const _TCOLORS': [
'#ff0000', '#00ff00', '#0000ff', '#00ffff', // r g b c
'#ffff00', '#ff00ff', '#ffffff', '#808080' // y m w b
],
/** /**
* Map set data (binary string) * 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 color will be rendered in the background and will bleed through the
* the transparent portions of the tile. We use explicit HEX codes rather * transparent portions of the tile. We use explicit HEX codes rather than
* than CSS color names because environments may vary the colors used. * CSS color names because environments may vary the colors used.
* *
* @return {Array.<string>} * @param {number} oid tunnel object id
*
* @return {string} tunnel color
*/ */
'public getTunnelColors': function() 'public getTunnelColor': function( oid )
{ {
return [ // get the tunnel id by stripping off the tunnel bitmask and then grab
'#ff0000', '#00ff00', '#0000ff', '#00ffff', // r g b c // the associated color
'#ffff00', '#ff00ff', '#ffffff', '#808080' // y m w b 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' ) );
} }
} ); } );

View File

@ -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 color will be rendered in the background and will bleed through the
* the transparent portions of the tile. A total of eight colors should be * transparent portions of the tile. The color may be specified in any CSS
* returned. They may be specified in any CSS format, but explicit values * format, but explicit values are recommended since color names (e.g.
* are recommended since color names (e.g. 'green') can vary between * 'green') can vary between environments.
* environments.
* *
* @return {Array.<string>} * @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' ]
} ); } );

View File

@ -23,12 +23,6 @@
*/ */
ltjs.MapRender = Class( 'MapRender', ltjs.MapRender = Class( 'MapRender',
{ {
/**
* Tunnel bitmask
* @type {number}
*/
'private const _TMASK': 0x40,
/** /**
* Property to hold lock bit on canvas element * Property to hold lock bit on canvas element
* @type {string} * @type {string}
@ -169,10 +163,10 @@ ltjs.MapRender = Class( 'MapRender',
var objs = map.getObjects(), var objs = map.getObjects(),
size = map.getDimensions(), size = map.getDimensions(),
i = objs.length,
omap = map.getObjectTileMap(), omap = map.getObjectTileMap(),
sizex = size[ 0 ],
tunnel_colors = map.getTunnelColors(), sizey = size[ 1 ],
i = objs.length,
// get the width and height from one of the tiles // get the width and height from one of the tiles
t = this._tiles.dirt.data, t = this._tiles.dirt.data,
@ -185,13 +179,13 @@ ltjs.MapRender = Class( 'MapRender',
{ {
var oid = objs[ i ], var oid = objs[ i ],
tid = omap[ oid ], tid = omap[ oid ],
x = ( Math.floor( i / size[ 0 ] ) * w ), x = ( Math.floor( i / sizex ) * w ),
y = ( ( i % size[ 1 ] ) * h ); y = ( ( i % sizey ) * h );
// tunnels are handled a bit differently than other objects // 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; 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 * Render the given tunnel
* *
@ -249,21 +230,15 @@ ltjs.MapRender = Class( 'MapRender',
* rendered to the base canvas, whereas the tunnel tile itself is rendered * rendered to the base canvas, whereas the tunnel tile itself is rendered
* on the overlaying canvas. * on the overlaying canvas.
* *
* @param {number} oid tunnel object id * @param {number} x left position
* @param {number} x left position * @param {number} y top position
* @param {number} y top position * @param {string} color tunnel color (CSS value)
*
* @param {Array.<string>} colors tunnel colors
* *
* @return {undefined} * @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 var tdata = this._tiles.tunnel.data;
// associated color
var tunnel_id = ( ( +oid ^ this.__self.$( '_TMASK' ) ) / 2 ),
color = colors[ tunnel_id ] || 'black',
tdata = this._tiles.tunnel.data;
// fill tile with the appropriate background color for this tile // fill tile with the appropriate background color for this tile
this._ctx.fillStyle = color; this._ctx.fillStyle = color;