1
0
Fork 0
lasertank-js/lib/ClassicMap.js

146 lines
4.1 KiB
JavaScript
Raw Normal View History

Initial map rendering support The next major step for the clone is the loading and parsing of LVL files (maps). This is especially imporant for the purpose of ensuring that users can continue to use the maps the know and love (and possibly created themselves), rather than having to recreate them in the clone. The maps are concatenated into a single fixed-width file and loaded (in the original sources) into the TLEVEL struct (LTANK.H). The main thing to note about this data is the manner in which the object data (the playfield) is stored. The TPLAYFIELD type (defined as a 16x16 multidimensional signed char array) is formatted as [x][y], meaning that it is an array of *columns*, not rows. While at first confusing, this does not complicate matters any. This commit adds initial rendering support for the playfield via MapRender and is demonstrated in the same demo file as LtgLoader. After loading a tile set, the user can load an LVL file. The rendering is done using two canvas elements, the second of which is created by MapRender itself and overlayed atop of the original canvas. Unmasked tiles are rendered to the bottom canvas and masked tiles to the upper, allowing us to move game objects without having to redraw underlying tiles. This is also necessary as the context putImageData() method overwrites any existing data rather than leaving values corresponding to the transparent pixels in tact. This commit also added support for tunnel coloring - each set of tunnels has its own distinct color. The color values were taken from ColorList in LTANK2.C in the original sources rather than using CSS color names (e.g. 'red', 'blue', 'magenta') because different environments may assign slightly different colors to those values (as an example, FireFox does not use #00FF00 for 'green'). Tunnels themselves are identified by a bitmask (0x40), so we can get the tunnel id by XORing with that mask. The ids are incremented by two in the LVL data (e.g. 0x40 for index 1, 0x42 for index 2), so we can then divide by two and take that index in the color array. This color is used to fill the tile with a solid color in the lower canvas. We can then paint the tile on the upper canvas, which will allow the color of the lower canvas to peek through the transparent pixels. Animation support has not yet been added (but animations are still visible in the demo to the left of the map rendering). This is an exciting start, as we can clearly see what the game will look like in the browser. This commit also does not cover any additional level metadata (e.g. author, difficulty, hints); that will be addressed in future commits and added to the demo.
2012-03-15 23:30:20 -04:00
/**
* Represents a classic map (level)
*
* 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/>.
*
*
* Each map is concatenated in the file and consists of the following
* information:
*
* - Playfield data (game objects), 16x16 multidimensional char array
* - Level name, 31 characters
* - Hint, 256 characters
* - Author, 31 characters
* - Difficulty, 16-bit integer, little endian
*
* One can easily calculate the position of the level in a file, given its
* number, by multiplying by the size of the data structure (see TLEVEL, LTANK.H
* in the original sources).
*
* It is worth mentioning how the playfield data is stored. Since the
* multidimensional array is defined as [x][y], the array is created as an
* "array of columns", meaning that the data is organized in columns instead of
* rows. For example, when viewing the data in a HEX editor that displays 16
* bytes per line (e.g. xxd), the map would appear to be mirrored rotated 90
* degrees counter-clockwise. To make it easier to visualize, one can create a
* map with a number of tunnels in a pattern to take advantage of the ASCII
* display.
*/
/**
* Represents a classic map, as they exist in the original game.
*
* Classic maps are 16x16 tiles in size (for a total of 256 tiles).
*/
ltjs.ClassicMap = Class( 'ClassicMap' )
.implement( ltjs.Map )
.extend(
{
'private const _SIZE': 590,
/**
* Game object size
*
* The game objects are stores as a 16x16 multi-dimensional signed char
* array (in the C sources).
*
* @type {number}
*/
'private const _GOSIZE': 256,
/**
* Map set data (binary string)
* @type {string}
*/
'private _data': null,
/**
* Map id (1-indexed)
* @type {string}
*/
'private _id': 0,
/**
* Initialize map with map data and the given id
*
* @param {ltjs.MapSet} set map set data
* @param {number} id 1-indexed map id
*/
__construct: function( data, id )
{
this._data = ''+( data );
this._id = +id;
},
/**
* Retrieve game objects
*
* The game objects are returned in a manner consistent with the original
* sources - in columns, not rows. The reason for this is that the original
* game uses a multi-dimensional array [x][y], which creates an array of
* columns (TPLAYFIELD, LTANK.H).
*
* @return {Array.<number>} array of game objects
*/
'public getObjects': function()
{
// calculate offset in LVL file
var offset = ( this.__self.$( '_SIZE' ) * ( this._id - 1 ) );
return this._getMapData( offset );
},
/**
* Retrieve the map data (game objects only) at the requested offset
*
* The object data at the requested position will be loaded and converted to
* integers (from a binary string).
*
* @param {number} offset offset of the requested map data
*
* @return {Array.<number>} converted map data
*/
'private _getMapData': function( offset )
{
var tiles = this._data.substr( offset, this.__self.$( '_GOSIZE' ) )
.split( '' ),
i = tiles.length;
while ( i-- )
{
tiles[ i ] = tiles[ i ].charCodeAt( 0 );
}
return tiles;
},
/**
* Retrieve map dimensions
*
* @return {Array.<number>} width and height in tiles
*/
'public getDimensions': function()
{
return [ 16, 16 ];
}
} );