Phaser.TilemapLayerGL = function (game, tilemap, index, width, height){ this.game = game; width |= 0; height |= 0; this.map = tilemap; this.index = index; this.layer = tilemap.layers[index]; this.type = Phaser.TILEMAPLAYER; this.physicsType = Phaser.TILEMAPLAYER; this.renderSettings = { overdrawRatio: 0.2} ; this.exists = true ; this.debugSettings = { missingImageFill: 'rgb(255,255,255)', debuggedTileOverfill: 'rgba(0,255,0,0.4)', forceFullRedraw: true , debugAlpha: 0.5, facingEdgeStroke: 'rgba(0,255,0,1)', collidingTileOverfill: 'rgba(0,255,0,0.2)'} ; this.scrollFactorX = 1; this.scrollFactorY = 1; this.dirty = true ; this.rayStepRate = 4; this._wrap = false ; var tileset = this.layer.tileset || this.map.tilesets[0]; this._mc = { scrollX: 0, scrollY: 0, renderWidth: 0, renderHeight: 0, tileWidth: tilemap.tileWidth, tileHeight: tilemap.tileHeight, cw: tileset.tileWidth, ch: tileset.tileHeight, tileset: tileset, tilesets: [] } ; this._renderMode = 0; this._scrollX = 0; this._scrollY = 0; this._results = [] ; var baseTexture = new PIXI.BaseTexture(tileset.image); PIXI.Tilemap.call(this, new PIXI.Texture(baseTexture), this.map.width, this.map.height, this._mc.tileset.tileWidth, this._mc.tileset.tileHeight, this.layer); Phaser.Component.Core.init.call(this, game, 0, 0, null , null ); this.fixedToCamera = true ; } ; Phaser.TilemapLayerGL.prototype = Object.create(PIXI.Tilemap.prototype); Phaser.TilemapLayerGL.prototype.constructor = Phaser.TilemapLayerGL; Phaser.Component.Core.install.call(Phaser.TilemapLayerGL.prototype, ['FixedToCamera'] ); Phaser.TilemapLayerGL.prototype.preUpdateCore = Phaser.Component.Core.preUpdate; Phaser.TilemapLayerGL.prototype.preUpdate = function (){ return this.preUpdateCore(); } ; Phaser.TilemapLayerGL.prototype.postUpdate = function (){ Phaser.Component.FixedToCamera.postUpdate.call(this); var camera = this.game.camera; this.scrollX = camera.x * this.scrollFactorX / this.scale.x; this.scrollY = camera.y * this.scrollFactorY / this.scale.y; this.render(); } ; Phaser.TilemapLayerGL.prototype.destroy = function (){ Phaser.Component.Destroy.prototype.destroy.call(this); } ; Phaser.TilemapLayerGL.prototype.resize = function (width, height){ this.texture.frame.resize(width, height); this.texture.width = width; this.texture.height = height; this.texture.crop.width = width; this.texture.crop.height = height; this.texture.baseTexture.width = width; this.texture.baseTexture.height = height; this.texture.baseTexture.dirty(); this.texture.requiresUpdate = true ; this.texture._updateUvs(); this.dirty = true ; } ; Phaser.TilemapLayerGL.prototype.resizeWorld = function (){ this.game.world.setBounds(0, 0, this.layer.widthInPixels * this.scale.x, this.layer.heightInPixels * this.scale.y); } ; Phaser.TilemapLayerGL.prototype._fixX = function (x){ if (x < 0) { x = 0; } if (this.scrollFactorX === 1) { return x; } return this._scrollX + (x - (this._scrollX / this.scrollFactorX)); } ; Phaser.TilemapLayerGL.prototype._unfixX = function (x){ if (this.scrollFactorX === 1) { return x; } return (this._scrollX / this.scrollFactorX) + (x - this._scrollX); } ; Phaser.TilemapLayerGL.prototype._fixY = function (y){ if (y < 0) { y = 0; } if (this.scrollFactorY === 1) { return y; } return this._scrollY + (y - (this._scrollY / this.scrollFactorY)); } ; Phaser.TilemapLayerGL.prototype._unfixY = function (y){ if (this.scrollFactorY === 1) { return y; } return (this._scrollY / this.scrollFactorY) + (y - this._scrollY); } ; Phaser.TilemapLayerGL.prototype.getTileX = function (x){ return Math.floor(this._fixX(x) / this._mc.tileWidth); } ; Phaser.TilemapLayerGL.prototype.getTileY = function (y){ return Math.floor(this._fixY(y) / this._mc.tileHeight); } ; Phaser.TilemapLayerGL.prototype.getTileXY = function (x, y, point){ point.x = this.getTileX(x); point.y = this.getTileY(y); return point; } ; Phaser.TilemapLayerGL.prototype.getRayCastTiles = function (line, stepRate, collides, interestingFace){ if (!stepRate) { stepRate = this.rayStepRate; } if (collides === undefined) { collides = false ; } if (interestingFace === undefined) { interestingFace = false ; } var tiles = this.getTiles(line.x, line.y, line.width, line.height, collides, interestingFace); if (_AN_Read_length('length', tiles) === 0) { return [] ; } var coords = line.coordinatesOnLine(stepRate); var results = [] ; for (var i = 0; i < _AN_Read_length('length', tiles); i++ ){ for (var t = 0; t < _AN_Read_length('length', coords); t++ ){ var tile = tiles[i]; var coord = coords[t]; if (tile.containsPoint(coord[0], coord[1])) { results.push(tile); break ; } } } return results; } ; Phaser.TilemapLayerGL.prototype.getTiles = function (x, y, width, height, collides, interestingFace){ if (collides === undefined) { collides = false ; } if (interestingFace === undefined) { interestingFace = false ; } var fetchAll = !(collides || interestingFace); x = this._fixX(x); y = this._fixY(y); var tx = Math.floor(x / (this._mc.cw * this.scale.x)); var ty = Math.floor(y / (this._mc.ch * this.scale.y)); var tw = Math.ceil((x + width) / (this._mc.cw * this.scale.x)) - tx; var th = Math.ceil((y + height) / (this._mc.ch * this.scale.y)) - ty; this._results = [] ; for (var wy = ty; wy < ty + th; wy++ ){ for (var wx = tx; wx < tx + tw; wx++ ){ var row = this.layer.data[wy]; if (row && row[wx]) { if (fetchAll || row[wx].isInteresting(collides, interestingFace)) { this._results.push(row[wx]); } } } } return this._results.slice(); } ; Phaser.TilemapLayerGL.prototype.resetTilesetCache = function (){ this._mc.tilesets = [] ; } ; Phaser.TilemapLayerGL.prototype.setScale = function (xScale, yScale){ xScale = xScale || 1; yScale = yScale || xScale; for (var y = 0; y < _AN_Read_length('length', this.layer.data); y++ ){ var row = this.layer.data[y]; for (var x = 0; x < _AN_Read_length('length', row); x++ ){ var tile = row[x]; tile.width = this.map.tileWidth * xScale; tile.height = this.map.tileHeight * yScale; tile.worldX = tile.x * tile.width; tile.worldY = tile.y * tile.height; } } this.scale.setTo(xScale, yScale); } ; Phaser.TilemapLayerGL.prototype.renderRegion = function (scrollX, scrollY, left, top, right, bottom, offx, offy){ var width = this.layer.width; var height = this.layer.height; var tw = this._mc.tileWidth; var th = this._mc.tileHeight; offx = offx || 0; offy = offy || 0; if (!this._wrap) { if (left <= right) { left = Math.max(0, left); right = Math.min(width - 1, right); } if (top <= bottom) { top = Math.max(0, top); bottom = Math.min(height - 1, bottom); } } var baseX = (left * tw) - scrollX; var baseY = (top * th) - scrollY; var normStartX = (left + ((1 << 20) * width)) % width; var normStartY = (top + ((1 << 20) * height)) % height; var tx, ty, x, y, xmax, ymax; for (y = normStartY, ymax = bottom - top, ty = baseY; ymax >= 0; y++ , ymax-- , ty += th){ if (y >= height) { y -= height; } var row = this.layer.data[y]; for (x = normStartX, xmax = right - left, tx = baseX; xmax >= 0; x++ , xmax-- , tx += tw){ if (x >= width) { x -= width; } var tile = row[x]; if (!tile || tile.index < 0) { this._mc.tileset.addDegenerate(this.glBatch); continue ; } var index = tile.index; this._mc.tileset.drawGl(this.glBatch, tx + offx, ty + offy, index, tile.alpha, tile.flippedVal); } this._mc.tileset.addDegenerate(this.glBatch); } } ; Phaser.TilemapLayerGL.prototype.renderFull = function (){ var scrollX = this._mc.scrollX; var scrollY = this._mc.scrollY; var renderW = this.game._width; var renderH = this.game._height; var tw = this._mc.tileWidth; var th = this._mc.tileHeight; var cw = this._mc.cw; var ch = this._mc.ch; var left = Math.floor((scrollX - (cw - tw)) / tw); var right = Math.floor((renderW - 1 + scrollX) / tw); var top = Math.floor((scrollY - (ch - th)) / th); var bottom = Math.floor((renderH - 1 + scrollY) / th); this.glBatch = [] ; this.renderRegion(scrollX, scrollY, left, top, right, bottom, 0, - (ch - th)); } ; Phaser.TilemapLayerGL.prototype.render = function (){ var redrawAll = false ; if (!this.visible) { return ; } if (this.dirty || this.layer.dirty) { this.layer.dirty = false ; redrawAll = true ; } var scrollX = this._scrollX | 0; var scrollY = this._scrollY | 0; var mc = this._mc; var shiftX = mc.scrollX - scrollX; var shiftY = mc.scrollY - scrollY; if (!redrawAll && shiftX === 0 && shiftY === 0) { return ; } mc.scrollX = scrollX; mc.scrollY = scrollY; this.renderFull(); this.texture.baseTexture.dirty(); this.dirty = false ; return true ; } ; Object.defineProperty(Phaser.TilemapLayerGL.prototype, "wrap", { get: function (){ return this._wrap; } , set: function (value){ this._wrap = value; this.dirty = true ; } } ); Object.defineProperty(Phaser.TilemapLayerGL.prototype, "scrollX", { get: function (){ return this._scrollX; } , set: function (value){ this._scrollX = value; } } ); Object.defineProperty(Phaser.TilemapLayerGL.prototype, "scrollY", { get: function (){ return this._scrollY; } , set: function (value){ this._scrollY = value; } } ); Object.defineProperty(Phaser.TilemapLayerGL.prototype, "collisionWidth", { get: function (){ return this._mc.cw; } , set: function (value){ this._mc.cw = value | 0; this.dirty = true ; } } ); Object.defineProperty(Phaser.TilemapLayerGL.prototype, "collisionHeight", { get: function (){ return this._mc.ch; } , set: function (value){ this._mc.ch = value | 0; this.dirty = true ; } } );