From 6af5adddb85462052e35cb08821a02ec318ce507 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 16 Mar 2012 20:04:52 -0400 Subject: [PATCH] Game object tile mappings and tunnel colors are now part of the ClassicMap --- lib/ClassicMap.js | 38 ++++++++++++++++++++++++++++++++++++++ lib/Map.js | 24 +++++++++++++++++++++++- lib/MapRender.js | 46 +++++++++------------------------------------- 3 files changed, 70 insertions(+), 38 deletions(-) diff --git a/lib/ClassicMap.js b/lib/ClassicMap.js index 133f490..9fd811b 100644 --- a/lib/ClassicMap.js +++ b/lib/ClassicMap.js @@ -141,5 +141,43 @@ ltjs.ClassicMap = Class( 'ClassicMap' ) 'public getDimensions': function() { return [ 16, 16 ]; + }, + + + /** + * Retrieve map of object codes to their appropriate tiles + * + * @return {Array.} + */ + 'public getObjectTileMap': function() + { + // we return these values here instead of returning, say, a constant, + // because we would have to clone it to ensure that our inner state + // would not be altered + return [ + 'dirt', 'tup', 'base', 'water', 'block', 'mblock', 'brick', + 'atup', 'atright', 'atdown', 'atleft', 'mirrorul', 'mirrorur', + 'mirrordr', 'mirrordl', 'owup', 'owright', 'owdown', 'owleft', + 'cblock', 'rmirrorul', 'rmirrorur', 'rmirrordr', 'rmirrordl', + 'ice', 'thinice' + ]; + }, + + + /** + * Retrieve tunnel colors + * + * 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. + * + * @return {Array.} + */ + 'public getTunnelColors': function() + { + return [ + '#ff0000', '#00ff00', '#0000ff', '#00ffff', // r g b c + '#ffff00', '#ff00ff', '#ffffff', '#808080' // y m w b + ]; } } ); diff --git a/lib/Map.js b/lib/Map.js index 7b8a323..2e419fb 100644 --- a/lib/Map.js +++ b/lib/Map.js @@ -69,5 +69,27 @@ ltjs.Map = Interface( 'Map', * * @return {Array.} width and height in tiles */ - 'public getDimensions': [] + 'public getDimensions': [], + + + /** + * Retrieve map of object codes to their appropriate tiles + * + * @return {Array.} + */ + 'public getObjectTileMap': [], + + + /** + * Retrieve tunnel colors + * + * 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. + * + * @return {Array.} + */ + 'public getTunnelColors': [] } ); diff --git a/lib/MapRender.js b/lib/MapRender.js index 32cc925..6eef1fc 100644 --- a/lib/MapRender.js +++ b/lib/MapRender.js @@ -54,38 +54,6 @@ ltjs.MapRender = Class( 'MapRender', */ 'private _tiles': {}, - /** - * Maps each "game object" in the map to a tile id - * TODO: abstract - * @type {Array.} - */ - 'private _objMap': [ - 'dirt', 'tup', 'base', 'water', 'block', 'mblock', 'brick', - 'atup', 'atright', 'atdown', 'atleft', 'mirrorul', 'mirrorur', - 'mirrordr', 'mirrordl', 'owup', 'owright', 'owdown', 'owleft', - 'cblock', 'rmirrorul', 'rmirrorur', 'rmirrordr', 'rmirrordl', - 'ice', 'thinice' - ], - - /** - * 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. - * - * TODO: abstract - * - * @type {Array.} - */ - 'private _tunnelColors': [ - '#ff0000', '#00ff00', '#0000ff', '#00ffff', // r g b c - '#ffff00', '#ff00ff', '#ffffff', '#808080' // y m w b - ], - /** * Initialize renderer with a canvas context and a tile set @@ -202,7 +170,9 @@ ltjs.MapRender = Class( 'MapRender', var objs = map.getObjects(), size = map.getDimensions(), i = objs.length, - tiles = this._tiles, + omap = map.getObjectTileMap(), + + tunnel_colors = map.getTunnelColors(), // get the width and height from one of the tiles t = this._tiles.dirt.data, @@ -214,14 +184,14 @@ ltjs.MapRender = Class( 'MapRender', while ( i-- ) { var oid = objs[ i ], - tid = this._objMap[ oid ], + tid = omap[ oid ], x = ( Math.floor( i / size[ 0 ] ) * w ), y = ( ( i % size[ 1 ] ) * h ); // tunnels are handled a bit differently than other objects if ( this._isTunnel( oid ) ) { - this._renderTunnel( oid, x, y ); + this._renderTunnel( oid, x, y, tunnel_colors ); continue; } @@ -283,14 +253,16 @@ ltjs.MapRender = Class( 'MapRender', * @param {number} x left position * @param {number} y top position * + * @param {Array.} colors tunnel colors + * * @return {undefined} */ - 'private _renderTunnel': function( oid, x, y ) + 'private _renderTunnel': function( oid, x, y, colors ) { // get the tunnel id by stripping off the tunnel bitmask and grab the // associated color var tunnel_id = ( ( +oid ^ this.__self.$( '_TMASK' ) ) / 2 ), - color = this._tunnelColors[ tunnel_id ] || 'black', + color = colors[ tunnel_id ] || 'black', tdata = this._tiles.tunnel.data; // fill tile with the appropriate background color for this tile