Skip to content

Instantly share code, notes, and snippets.

@taddeimania
Last active December 14, 2015 16:19
Show Gist options
  • Save taddeimania/5114012 to your computer and use it in GitHub Desktop.
Save taddeimania/5114012 to your computer and use it in GitHub Desktop.
More efficient way to handle map generation
BaseBackground = ig.Class.extend({
init: function () {
this.map = new MapTiles();
},
draw: function () {
if (this.map.tilesLoaded()){
this.drawMap();
}
},
drawMap: function () {
for (var j = 0 ; j < this.MAP.length ; j++){
for (var i = 0 ; i < this.MAP[j].length ; i++){
var tile = parseInt(this.MAP[j][i]);
var currentTile = this.getCurrentCoords(j, i)
this.map.tileMap[this.MAP[j][i]].draw(currentTile.x, currentTile.y);
var shadowTile = this.SHADOW[j][i];
if (shadowTile > 0){
var shadowList = SHADOW_DEF[shadowTile](this);
for (var k = 0 ; k < shadowList.length ; k++){
shadowList[k].draw(currentTile.x, currentTile.y);
}
}
}
}
},
drawExtra: function () {
for (var i = 0 ; i < this.miscTiles.length ; i++){
var tileObj = this.miscTiles[i][0];
var coords = this.miscTiles[i][1];
var tileCoords = this.getCurrentCoords(coords[0], coords[1]);
tileObj.draw(tileCoords.x, tileCoords.y - 65);
}
},
getCharCoords: function (j, i){
var currentTile = this.getCurrentCoords(j, i);
currentTile.y -= 50;
return currentTile;
},
getCurrentCoords: function (j, i){
return {x: (i*100)-25, y: (j*82)+27};
},
checkBelowTile: function (j, i){
if (j > 4){
return true;
}
return parseInt(this.COLLISION[j+1][i]) > 0;
},
checkAboveTile: function (j, i){
if (j === 0){
return true;
}
return parseInt(this.COLLISION[j-1][i]) > 0;
},
checkRightTileForShadow: function (j, i) {
if (this.checkRightTile(j, i)){
return parseInt(this.MAP[j][i+1]) > 7;
}
return parseInt(this.COLLISION[j][i+1]) > 0;
},
checkLeftTileForShadow: function (j, i) {
if (this.checkLeftTile(j, i)){
return parseInt(this.MAP[j][i-1]) > 7;
}
return parseInt(this.COLLISION[j][i-1]) > 0;
},
checkRightTile: function (j, i) {
if (i > 9){
return true;
}
return parseInt(this.COLLISION[j][i+1]) > 0;
},
checkLeftTile: function (j, i) {
if (i === 0){
return true;
}
return parseInt(this.COLLISION[j][i-1]) > 0;
},
tilesLoaded: function () {
if (!this.map.loaded){
for (var i ; i < this.tileMap.length ; i++){
if (!this.map.tileMap.loaded){
return false;
}
}
}
this.map.loaded = true;
return true;
}
});
GrassBackground = BaseBackground.extend({
MAP: [
"7777777777",
"7777777777",
"7777777777",
"0000000004",
"0000000004",
"0000000004"
],
COLLISION: [
"1111111111",
"1111111111",
"1111111111",
"1000000001",
"1000000001",
"1000000001"
],
SHADOW: [
"0000000000",
"0000000000",
"0000000000",
"0000000000",
"0000000000",
"0000000000"
],
init: function () {
this.parent();
this.miscTiles = [];
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment