Skip to content

Instantly share code, notes, and snippets.

@frostney
Created February 8, 2014 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frostney/8885053 to your computer and use it in GitHub Desktop.
Save frostney/8885053 to your computer and use it in GitHub Desktop.
Simple TileMap
define('tilemap', function() {
var TileMap = (function() {
var TileMap = function(width, height) {
if (width == null) {
width = {
min: 0,
max: 4
};
}
if (height == null) {
height = {
min: 0,
max: 4
};
}
this.tile = [];
this.generateTiles(width, height);
};
TileMap.defaultType = 'empty';
TileMap.prototype.generateTiles = function(width, height) {
for (var x = width.min, xl = width.max - 1; x < xl; x++) {
for (var y = height.min, yl = height.max - 1; y < yl; y++) {
this.tile[x] = this.tile[x] || [];
this.tile[x].push(TileMap.defaultType);
}
}
};
TileMap.prototype.each = function(callback) {
this.map(callback);
return;
};
TileMap.prototype.map = function(callback) {
var result = [];
var tile = this.tile;
for (var x = 0, xl = tile.length; x < xl; x++) {
(function(tileY) {
for (var y = 0, yl = tileY.length; y < yl; y++) {
result.push(callback(x, y, tile[x][y]));
}
})(tile[x]);
}
return result;
};
TileMap.prototype.flatten = function() {
return this.map(function(x, y, content) {
return content;
});
};
TileMap.prototype.flattenWithModifier = function() {
return this.map(function(x, y, content) {
return {
x: x,
y: y,
type: content
}
});
};
return TileMap;
})();
return TileMap;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment