Skip to content

Instantly share code, notes, and snippets.

@timetocode
Created July 26, 2016 03:48
Show Gist options
  • Save timetocode/294030e9b5914d31a384b50d23eb753f to your computer and use it in GitHub Desktop.
Save timetocode/294030e9b5914d31a384b50d23eb753f to your computer and use it in GitHub Desktop.
MapVisual.prototype.recenter = function(me) {
var gridCoord = this.map.worldCoordsToGridCoords(me.x, me.y)
if (this.previousX !== gridCoord.x || this.previousY !== gridCoord.y) {
var dx = gridCoord.x - this.previousX
var dy = gridCoord.y - this.previousY
// start
var sx = gridCoord.x - 14
var sy = gridCoord.y - 10
// end
var ex = gridCoord.x + 14
var ey = gridCoord.y + 10
// add floors and walls within the range of start to end (if they aren't in the cache)
for (var i = sx; i < ex; i++) {
for (var j = sy; j < ey; j++) {
var wall = this.map.getWall(i, j)
var floor = this.map.getFloor(i, j)
var cacheKey = createKey(i, j)
if (wall.type !== WallType.Void && wall.type !== WallType.None && typeof this.wallCache[cacheKey] === 'undefined') {
var index = indexify(i, j, this.map.walls)
var frameName = getWallImageFrameName(wall.type, index)
var sprite = new PIXI.Sprite.fromFrame(frameName)
sprite.x = i * 16
sprite.y = j * 16
//sprite.anchor.y = 1/3
this.wallLayer.addChild(sprite)
this.wallCache[cacheKey] = sprite
}
if (floor.type !== FloorType.Void && typeof this.floorCache[cacheKey] === 'undefined') {
var index = indexify(i, j, this.map.floors)
var frameName = getFloorImageFrameName(floor.type, index)
var sprite = new PIXI.Sprite.fromFrame(frameName)
sprite.x = i * 16
sprite.y = j * 16
this.groundLayer.addChild(sprite)
this.floorCache[cacheKey] = sprite
}
}
}
// remove floors and walls that are no longer on screen
if (dx < 0) {
var rx = ex - dx
for (var i = sy; i <= ey; i++) {
var cacheKey = createKey(rx, i)
if (typeof this.floorCache[cacheKey] !== 'undefined') {
this.groundLayer.removeChild(this.floorCache[cacheKey])
delete this.floorCache[cacheKey]
}
if (typeof this.wallCache[cacheKey] !== 'undefined') {
this.wallLayer.removeChild(this.wallCache[cacheKey])
delete this.wallCache[cacheKey]
}
}
}
if (dx > 0) {
var rx = sx - dx
for (var i = sy; i <= ey; i++) {
var cacheKey = createKey(rx, i)
if (typeof this.floorCache[cacheKey] !== 'undefined') {
this.groundLayer.removeChild(this.floorCache[cacheKey])
delete this.floorCache[cacheKey]
}
if (typeof this.wallCache[cacheKey] !== 'undefined') {
this.wallLayer.removeChild(this.wallCache[cacheKey])
delete this.wallCache[cacheKey]
}
}
}
if (dy < 0) {
var ry = ey - dy
for (var i = sx; i <= ex; i++) {
var cacheKey = createKey(i, ry)
if (typeof this.floorCache[cacheKey] !== 'undefined') {
this.groundLayer.removeChild(this.floorCache[cacheKey])
delete this.floorCache[cacheKey]
}
if (typeof this.wallCache[cacheKey] !== 'undefined') {
this.wallLayer.removeChild(this.wallCache[cacheKey])
delete this.wallCache[cacheKey]
}
}
}
if (dy > 0) {
var ry = sy - dy
for (var i = sx; i <= ex; i++) {
var cacheKey = createKey(i, ry)
if (typeof this.floorCache[cacheKey] !== 'undefined') {
this.groundLayer.removeChild(this.floorCache[cacheKey])
delete this.floorCache[cacheKey]
}
if (typeof this.wallCache[cacheKey] !== 'undefined') {
this.wallLayer.removeChild(this.wallCache[cacheKey])
delete this.wallCache[cacheKey]
}
}
}
}
this.previousX = gridCoord.x
this.previousY = gridCoord.y
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment