Skip to content

Instantly share code, notes, and snippets.

@vegeta897
Created February 9, 2018 20:52
Show Gist options
  • Save vegeta897/ef27396cf20ac4d857951db8bfdae4bc to your computer and use it in GitHub Desktop.
Save vegeta897/ef27396cf20ac4d857951db8bfdae4bc to your computer and use it in GitHub Desktop.
World.prototype.createTiles = function() {
// Tile types:
// Grass G
// Slab S
// Flowers F
// Empty E
// Tile code constructed as NW-NE-SE-SW (eg. "S-X-X-B")
this.tileMap = {};
var self = this;
function tileType(grid) { return self.map[grid].style[0].replace(/p/,'s').toUpperCase(); }
function getTileCode(oGrid, nGrid) {
if(oGrid == nGrid) return tileType(oGrid);
var neighbor = self.map[nGrid];
if(!neighbor) return 'E';
return tileType(nGrid);
}
function generateTile(oGrid, tile, grid, game) {
var nGrids = tile.grids;
var tileCode = getTileCode(oGrid,nGrids[0])+'-'+getTileCode(oGrid,nGrids[1])
+'-'+getTileCode(oGrid,nGrids[2])+'-'+getTileCode(oGrid,nGrids[3]);
var tileSprite = (new TileSheet('tile')).map[tileCode];
if(!tileSprite) console.error('unknown tile code',tileCode,nGrids);
return {
tileCode: tileCode, position: tile, grid: grid, game: game
};
}
for(var key in this.map) { if(!this.map.hasOwnProperty(key)) continue;
var x = +key.split(':')[0], y = +key.split(':')[1], z = this.map[key].position.z;
var neighbors = geometry.get8Neighbors(key);
var nw = { x: x-0.5, y: y-0.5, z: z, grids: [neighbors.nw, neighbors.n, key, neighbors.w] },
ne = { x: x+0.5, y: y-0.5, z: z, grids: [neighbors.n, neighbors.ne, neighbors.e, key] },
se = { x: x+0.5, y: y+0.5, z: z, grids: [key, neighbors.e, neighbors.se, neighbors.s] },
sw = { x: x-0.5, y: y+0.5, z: z, grids: [neighbors.w, key, neighbors.s, neighbors.sw] };
var tiles = [nw,ne,se,sw];
for(var i = 0; i < tiles.length; i++) {
var tileGrid = z+':'+tiles[i].x+':'+tiles[i].y;
if(this.tileMap[tileGrid]) continue;
this.tileMap[tileGrid] = new Tile(generateTile(key, tiles[i], tileGrid, this.game));
this.staticMap.push(this.tileMap[tileGrid]);
}
}
this.staticMap.sort(function(a,b) { return a.zDepth - b.zDepth; });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment