Skip to content

Instantly share code, notes, and snippets.

@hunterloftis
Created November 29, 2014 21:47
Show Gist options
  • Save hunterloftis/a6ea261badbfd7e368bf to your computer and use it in GitHub Desktop.
Save hunterloftis/a6ea261badbfd7e368bf to your computer and use it in GitHub Desktop.
function BlitTerrain(surface) {
this.surface = surface;
this.sprite = new Blit.Sprite(this.surface, 128, 128, 'images/dirt.jpg');
}
BlitTerrain.prototype.render = function(seconds, map, rect) {
var width = this.sprite.width;
var height = this.sprite.height;
var ox = Math.floor(rect.left / width) * width;
var oy = Math.floor(rect.top / height) * height;
for (var y = oy; y < rect.bottom; y += width) {
for (var x = ox; x < rect.right; x += height) {
this.sprite.blit(x, y, 0);
}
}
};
PixiTerrain.prototype.createTile = function(x, y, type, timestamp) {
var sprite = new PIXI.Sprite(this.availableBaseTextures[type]);
sprite.timestamp = timestamp;
sprite.position.set(x, y);
sprite.width = this.tileSize + 1;
sprite.height = this.tileSize + 1;
this.groundContainer.addChild(sprite);
return sprite;
};
PixiTerrain.prototype.destroyTile = function(tile) {
tile.parent.removeChild(tile);
};
PixiTerrain.prototype.render = function(seconds, state, rect) {
this.dustSprites = this.dustSprites.filter(renderDust);
var tileSize = this.tileSize;
var left = Math.floor(rect.left / tileSize);
var top = Math.floor(rect.top / tileSize);
var right = Math.floor(rect.right / tileSize);
var bottom = Math.floor(rect.bottom / tileSize);
var now = Date.now();
for (var row = top; row <= bottom; row++) {
for (var col = left; col <= right; col++) {
var index = col + row * state.mapWidth;
var tileType = Math.floor(Math.random() * this.availableBaseTextures.length); //state.map[index];
var exists = this.tiles[index];
if (exists) {
this.tiles[index].timestamp = now;
}
else {
this.tiles[index] = this.createTile(col * tileSize, row * tileSize, tileType, now);
}
}
}
Object.keys(this.tiles).forEach(cullOffscreenTiles.bind(this));
function cullOffscreenTiles(index) {
if (this.tiles[index].timestamp < now) {
this.destroyTile(this.tiles[index]);
delete this.tiles[index];
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment