Created
July 27, 2013 16:49
-
-
Save unstoppablecarl/6095442 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// base tile class | |
var Tile = Base.extend({ | |
// the map level object this tile is attached to | |
mapLevel: null, | |
x: null, | |
y: null, | |
miniMapColor: "#0e541e", | |
// if passable != true, then actors cannot walk on the tile ("fish" and "birds" handled separately) | |
passable: false, | |
blocksLOS: false, | |
// entities on this tile | |
entities: [], | |
constructor: function(settings){ | |
// initialize instance variables with objects/arrays | |
this.entities = []; | |
settings = settings || {}; | |
_.extend(this, settings); | |
}, | |
getAdjacent: function(includeDiagonals){ | |
includeDiagonals = includeDiagonals || true; | |
var adjacentTiles = [], | |
count, | |
adjacentCoords; | |
// this is written out this way for performance | |
if(includeDiagonals){ | |
count = 8; | |
adjacentCoords = [ | |
{x: this.x + 1, y: this.y + 0}, | |
{x: this.x + 0, y: this.y + 1}, | |
{x: this.x + -1, y: this.y + 0}, | |
{x: this.x + 0, y: this.y + -1}, | |
{x: this.x + 1, y: this.y + 1}, | |
{x: this.x + -1, y: this.y + -1}, | |
{x: this.x + -1, y: this.y + 1}, | |
{x: this.x + 1, y: this.y + -1} | |
]; | |
} else { | |
count = 4, | |
adjacentCoords = [ | |
{x: this.x + 1, y: this.y + 0}, | |
{x: this.x + 0, y: this.y + 1}, | |
{x: this.x + -1, y: this.y + 0}, | |
{x: this.x + 0, y: this.y + -1} | |
]; | |
} | |
for(var i = 0; i < count; i++){ | |
var x = adjacentCoords[i].x, | |
y = adjacentCoords[i].y; | |
if(this.mapLevel.data[x] && this.mapLevel.data[x][y]){ | |
adjacentTiles.push(this.mapLevel.data[x][y]); | |
} | |
} | |
return adjacentTiles; | |
}, | |
getTilesWithinRadius:function(radius){ | |
var minX = this.x - Math.round(radius), | |
minY = this.y - Math.round(radius), | |
maxX = this.x + Math.round(radius), | |
maxY = this.y + Math.round(radius), | |
tilesWithinRadius = [], | |
x, y, xd, yd, distance; | |
for (x = minX; x <= maxX; x++){ | |
for (y = minY; y <= maxY; y++){ | |
xd = centerX - x; | |
yd = centerY - y; | |
distance = Math.round(Math.sqrt( xd*xd + yd*yd )); | |
if(distance <= radius){ | |
tilesWithinRadius.push({x: x, y: y}); | |
} | |
} | |
} | |
return tilesWithinRadius; | |
}, | |
getTilesAlongLine: function(endTile){ | |
var x0 = this.x, | |
y0 = this.y, | |
dx = Math.abs(x1 - x0), | |
dy = Math.abs(y1 - y0), | |
sx = (x0 < x1) ? 1 : -1, | |
sy = (y0 < y1) ? 1 : -1, | |
err = dx-dy, | |
output = []; | |
while(x0!=x1 || y0!=y1){ | |
output.push(this.getTile(x0, y0)); | |
var e2 = 2 * err; | |
if (e2 >-dy){ err -= dy; x0 += sx; } | |
if (e2 < dx){ err += dx; y0 += sy; } | |
} | |
return output; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MapLevel = Base.extend({ | |
// map object this map level is attached to | |
map: null, | |
// multi-dimensional array of tiles | |
data: null, | |
width: 100, | |
height: 100, | |
tileWidth: 32, | |
tileHeight: 32, | |
elevation: 0, | |
constructor: function(settings){ | |
settings = settings || {}; | |
_.extend(this, settings); | |
this.data = {}; | |
// init map tile coords | |
var x, y; | |
for(x = 0; x < this.width; x++){ | |
this.data[x] = this.data[x] || {}; | |
for(y = 0; y < this.height; y++){ | |
this.data[x][y] = this.data[x][y] || {}; | |
} | |
} | |
}, | |
getTile: function(x, y){ | |
if(this.data[x] && this.data[x][y]){ | |
return this.data[x][y]; | |
} | |
}, | |
eachTile: function(func, context, breakOnFalse){ | |
breakOnFalse = breakOnFalse || false; | |
var x, y, thisTile, ret; | |
for(x = 0; x < this.width; x++){ | |
for(y = 0; y < this.height; y++){ | |
thisTile = this.data[x][y]; | |
context = context || thisTile; | |
ret = func.apply(context, [thisTile, x, y]); | |
if(breakOnFalse && ret === false){ | |
break; | |
} | |
} | |
} | |
}, | |
/** | |
* | |
*/ | |
setTile: function(x, y, tileData){ | |
this.data[x][y] = new Tile({ | |
width: this.tileWidth, | |
height: this.tileHeight, | |
x: x, | |
y: y, | |
mapLevel: this | |
}); | |
}, | |
getTilesAlongLine: function(startTile, endTile){ | |
var x0 = startTile.x, | |
y0 = startTile.y, | |
dx = Math.abs(x1 - x0), | |
dy = Math.abs(y1 - y0), | |
sx = (x0 < x1) ? 1 : -1, | |
sy = (y0 < y1) ? 1 : -1, | |
err = dx-dy, | |
output = []; | |
while(x0!=x1 || y0!=y1){ | |
output.push(this.getTile(x0, y0)); | |
var e2 = 2 * err; | |
if (e2 >-dy){ err -= dy; x0 += sx; } | |
if (e2 < dx){ err += dx; y0 += sy; } | |
} | |
return output; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment