Created
June 12, 2017 06:42
-
-
Save timetocode/ba2e71e42614da29ca3cec31ee4d1979 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
var Floor = require('../map/Floor') | |
var Wall = require('../map/Wall') | |
function createKey(x, y) { | |
return x + y * 10000000 | |
} | |
function keyInBounds(key, gx, gy, halfWidth, halfHeight) { | |
var keyX = key % 10000000 | |
var keyY = Math.floor(key / 10000000) | |
return keyX >= gx - halfWidth && keyX <= gx + halfWidth && | |
keyY >= gy - halfHeight && keyY <= gy + halfHeight | |
} | |
class MapRenderer extends PIXI.Container { | |
constructor(map) { | |
super() | |
// underlying map data | |
this.map = map | |
this.wallShadows = new PIXI.Container() | |
var blurFilter2 = new PIXI.filters.BlurFilter() | |
blurFilter2.blurX = 24//48 | |
blurFilter2.blurY = 24//48 | |
this.wallShadows.filters = [blurFilter2] | |
this.walls = new PIXI.Container() | |
this.floors = new PIXI.Container() | |
this.floors.x = 16 | |
this.floors.y = 16 | |
this.addChild(this.floors) | |
this.addChild(this.wallShadows) | |
this.addChild(this.walls) | |
this.floorsRef = {} | |
this.wallsRef = {} | |
this.wallShadowsRef = {} | |
this.keysObj = {} | |
this.keysArr = [] | |
this.prev = { | |
x: null, | |
y: null | |
} | |
// TODO: calculate visible tile dimensions based on resolution | |
this.gViewWidth = 17 | |
this.gViewHeight = 11 | |
} | |
refresh(gx, gy) { | |
///console.log(gx, gy) | |
var sx = gx - this.gViewWidth | |
if (sx < 0) { | |
sx = 0 | |
} | |
var sy = gy - this.gViewHeight | |
if (gy < 0) { | |
gy = 0 | |
} | |
var ex = gx + this.gViewWidth | |
if (ex > this.map.width) { | |
ex = this.map.width | |
} | |
var ey = gy + this.gViewHeight | |
if (ey > this.map.height) { | |
ey = this.map.height | |
} | |
// remove tiles that are no longer visible | |
if (gx !== this.prev.x || gy !== this.prev.y) { | |
for (var i = this.keysArr.length - 1; i > -1; i--) { | |
var key = this.keysArr[i] | |
if (!keyInBounds(key, gx, gy, this.gViewWidth, this.gViewHeight)) { | |
//console.log('removing', gx, gy) | |
this.floors.removeChild(this.floorsRef[key]) | |
delete this.floorsRef[key] | |
this.walls.removeChild(this.wallsRef[key]) | |
delete this.wallsRef[key] | |
this.wallShadows.removeChild(this.wallShadowsRef[key]) | |
delete this.wallShadowsRef[key] | |
delete this.keysObj[key] | |
this.keysArr.splice(i, 1) | |
} | |
} | |
} | |
this.prev.x = gx | |
this.prev.y = gy | |
for (var y = sy; y < ey; y++) { | |
for (var x = sx; x < ex; x++) { | |
var key = createKey(x, y) | |
if (this.keysObj[key]) { | |
continue | |
} | |
this.keysObj[key] = true | |
this.keysArr.push(key) | |
var floor = this.map.floors.get(x, y) | |
var wall = this.map.walls.get(x, y) | |
//console.log('x,y', x,y) | |
if (floor) { | |
var f = new PIXI.Sprite.fromFrame(floor.frame) | |
f.x = x * 32 | |
f.y = y * 32 | |
this.floors.addChild(f) | |
this.floorsRef[key] = f | |
} | |
if (wall) { | |
var w1 = new PIXI.Sprite.fromFrame(wall.frame) | |
w1.x = x * 32 | |
w1.y = y * 32 | |
this.walls.addChild(w1) | |
this.wallsRef[key] = w1 | |
var ws = new PIXI.Sprite.fromFrame(wall.frame) | |
ws.x = -16 + x * 32 | |
ws.y = -16 + y * 32 | |
ws.scale.x = ws.scale.y = 2 | |
this.wallShadows.addChild(ws) | |
this.wallShadowsRef[key] = ws | |
} | |
} | |
} | |
} | |
} | |
/* | |
var wallHitboxes = this.map.wallHitboxes.get(x, y) | |
if (wallHitboxes) { | |
//console.log(wallHitbox) | |
wallHitboxes.forEach(wallHitbox => { | |
if (wallHitbox !== 0) { | |
var points = [] | |
wallHitbox.points.forEach(point => { | |
//console.log(wallHitbox.pos.x) | |
var x = wallHitbox.pos.x*0.5 + (point.x*0.5)//wallHitbox.pos.x //* 0.5 | |
var y = wallHitbox.pos.y*0.5 + (point.y*0.5)//wallHitbox.pos.y //* 0.5 | |
points.push(x) | |
points.push(y) | |
}) | |
//points.push(points[0]) | |
var poly = new PIXI.Graphics() | |
poly.beginFill(0xff0000) | |
poly.drawPolygon(points) | |
poly.alpha = 0.25 | |
//poly.scale.x = poly.scale.y = 0.5 | |
this.walls.addChild(poly) | |
} | |
}) | |
} | |
*/ | |
module.exports = MapRenderer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment