Phaser.TilemapLayer = function (game, tilemap, index, width, height){ this.game = game; this.map = tilemap; this.index = index; this.layer = tilemap.layers[index]; this.canvas = Phaser.Canvas.create(width, height, '', true ); this.context = this.canvas.getContext('2d'); this.baseTexture = new PIXI.BaseTexture(this.canvas); this.texture = new PIXI.Texture(this.baseTexture); this.textureFrame = new Phaser.Frame(0, 0, 0, width, height, 'tilemapLayer', game.rnd.uuid()); Phaser.Image.call(this, this.game, 0, 0, this.texture, this.textureFrame); this.name = ''; this.type = Phaser.TILEMAPLAYER; this.fixedToCamera = true ; this.cameraOffset = new Phaser.Point(0, 0); this.tileColor = 'rgb(255, 255, 255)'; this.debug = false ; this.debugAlpha = 0.5; this.debugColor = 'rgba(0, 255, 0, 1)'; this.debugFill = false ; this.debugFillColor = 'rgba(0, 255, 0, 0.2)'; this.debugCallbackColor = 'rgba(255, 0, 0, 1)'; this.scrollFactorX = 1; this.scrollFactorY = 1; this.dirty = true ; this.rayStepRate = 4; this.wrap = false ; this._mc = { cw: tilemap.tileWidth, ch: tilemap.tileHeight, ga: 1, dx: 0, dy: 0, dw: 0, dh: 0, tx: 0, ty: 0, tw: 0, th: 0, tl: 0, maxX: 0, maxY: 0, startX: 0, startY: 0, x: 0, y: 0, prevX: 0, prevY: 0} ; this._results = [] ; this.updateMax(); } ; Phaser.TilemapLayer.prototype = Object.create(Phaser.Image.prototype); Phaser.TilemapLayer.prototype.constructor = Phaser.TilemapLayer; Phaser.TilemapLayer.prototype.postUpdate = function (){ Phaser.Image.prototype.postUpdate.call(this); this.scrollX = this.game.camera.x * this.scrollFactorX; this.scrollY = this.game.camera.y * this.scrollFactorY; this.render(); if (this._cache[7] === 1) { this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x; this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y; } } ; Phaser.TilemapLayer.prototype.resizeWorld = function (){ this.game.world.setBounds(0, 0, this.layer.widthInPixels, this.layer.heightInPixels); } ; Phaser.TilemapLayer.prototype._fixX = function (x){ if (x < 0) { x = 0; } if (this.scrollFactorX === 1) { return x; } return this._mc.x + (x - (this._mc.x / this.scrollFactorX)); } ; Phaser.TilemapLayer.prototype._unfixX = function (x){ if (this.scrollFactorX === 1) { return x; } return (this._mc.x / this.scrollFactorX) + (x - this._mc.x); } ; Phaser.TilemapLayer.prototype._fixY = function (y){ if (y < 0) { y = 0; } if (this.scrollFactorY === 1) { return y; } return this._mc.y + (y - (this._mc.y / this.scrollFactorY)); } ; Phaser.TilemapLayer.prototype._unfixY = function (y){ if (this.scrollFactorY === 1) { return y; } return (this._mc.y / this.scrollFactorY) + (y - this._mc.y); } ; Phaser.TilemapLayer.prototype.getTileX = function (x){ return this.game.math.snapToFloor(this._fixX(x), this.map.tileWidth) / this.map.tileWidth; } ; Phaser.TilemapLayer.prototype.getTileY = function (y){ return this.game.math.snapToFloor(this._fixY(y), this.map.tileHeight) / this.map.tileHeight; } ; Phaser.TilemapLayer.prototype.getTileXY = function (x, y, point){ point.x = this.getTileX(x); point.y = this.getTileY(y); return point; } ; Phaser.TilemapLayer.prototype.getRayCastTiles = function (line, stepRate, collides, interestingFace){ if (typeof stepRate === 'undefined' || stepRate === null ) { stepRate = this.rayStepRate; } if (typeof collides === 'undefined') { collides = false ; } if (typeof 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 total = _AN_Read_length('length', coords); var results = [] ; for (var i = 0; i < _AN_Read_length('length', tiles); i++ ){ for (var t = 0; t < total; t++ ){ if (tiles[i].containsPoint(coords[t][0], coords[t][1])) { results.push(tiles[i]); break ; } } } return results; } ; Phaser.TilemapLayer.prototype.getTiles = function (x, y, width, height, collides, interestingFace){ if (typeof collides === 'undefined') { collides = false ; } if (typeof interestingFace === 'undefined') { interestingFace = false ; } x = this._fixX(x); y = this._fixY(y); if (width > this.layer.widthInPixels) { width = this.layer.widthInPixels; } if (height > this.layer.heightInPixels) { height = this.layer.heightInPixels; } this._mc.tx = this.game.math.snapToFloor(x, this._mc.cw) / this._mc.cw; this._mc.ty = this.game.math.snapToFloor(y, this._mc.ch) / this._mc.ch; this._mc.tw = (this.game.math.snapToCeil(width, this._mc.cw) + this._mc.cw) / this._mc.cw; this._mc.th = (this.game.math.snapToCeil(height, this._mc.ch) + this._mc.ch) / this._mc.ch; this._results.length = 0; for (var wy = this._mc.ty; wy < this._mc.ty + this._mc.th; wy++ ){ for (var wx = this._mc.tx; wx < this._mc.tx + this._mc.tw; wx++ ){ if (this.layer.data[wy] && this.layer.data[wy][wx]) { if ((!collides && !interestingFace) || this.layer.data[wy][wx].isInteresting(collides, interestingFace)) { this._results.push(this.layer.data[wy][wx]); } } } } return this._results; } ; Phaser.TilemapLayer.prototype.updateMax = function (){ this._mc.maxX = this.game.math.ceil(this.canvas.width / this.map.tileWidth) + 1; this._mc.maxY = this.game.math.ceil(this.canvas.height / this.map.tileHeight) + 1; this.dirty = true ; } ; Phaser.TilemapLayer.prototype.render = function (){ if (this.layer.dirty) { this.dirty = true ; } if (!this.dirty || !this.visible) { return ; } this._mc.prevX = this._mc.dx; this._mc.prevY = this._mc.dy; this._mc.dx = - (this._mc.x - (this._mc.startX * this.map.tileWidth)); this._mc.dy = - (this._mc.y - (this._mc.startY * this.map.tileHeight)); this._mc.tx = this._mc.dx; this._mc.ty = this._mc.dy; this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); this.context.fillStyle = this.tileColor; var tile; var set; if (this.debug) { this.context.globalAlpha = this.debugAlpha; } for (var y = this._mc.startY, lenY = this._mc.startY + this._mc.maxY; y < lenY; y++ ){ this._column = null ; if (y < 0 && this.wrap) { this._column = this.layer.data[y + this.map.height]; } else if (y >= this.map.height && this.wrap) { this._column = this.layer.data[y - this.map.height]; } else if (this.layer.data[y]) { this._column = this.layer.data[y]; } if (this._column) { for (var x = this._mc.startX, lenX = this._mc.startX + this._mc.maxX; x < lenX; x++ ){ var tile = null ; if (x < 0 && this.wrap) { tile = this._column[x + this.map.width]; } else if (x >= this.map.width && this.wrap) { tile = this._column[x - this.map.width]; } else if (this._column[x]) { tile = this._column[x]; } if (tile && tile.index > -1) { set = this.map.tilesets[this.map.tiles[tile.index][2]]; if (this.debug === false && tile.alpha !== this.context.globalAlpha) { this.context.globalAlpha = tile.alpha; } set.draw(this.context, Math.floor(this._mc.tx), Math.floor(this._mc.ty), tile.index); if (tile.debug) { this.context.fillStyle = 'rgba(0, 255, 0, 0.4)'; this.context.fillRect(Math.floor(this._mc.tx), Math.floor(this._mc.ty), this.map.tileWidth, this.map.tileHeight); } } this._mc.tx += this.map.tileWidth; } } this._mc.tx = this._mc.dx; this._mc.ty += this.map.tileHeight; } if (this.debug) { this.context.globalAlpha = 1; this.renderDebug(); } if (this.game.renderType === Phaser.WEBGL) { PIXI.updateWebGLTexture(this.baseTexture, this.game.renderer.gl); } this.dirty = false ; this.layer.dirty = false ; return true ; } ; Phaser.TilemapLayer.prototype.renderDebug = function (){ this._mc.tx = this._mc.dx; this._mc.ty = this._mc.dy; this.context.strokeStyle = this.debugColor; this.context.fillStyle = this.debugFillColor; for (var y = this._mc.startY, lenY = this._mc.startY + this._mc.maxY; y < lenY; y++ ){ this._column = null ; if (y < 0 && this.wrap) { this._column = this.layer.data[y + this.map.height]; } else if (y >= this.map.height && this.wrap) { this._column = this.layer.data[y - this.map.height]; } else if (this.layer.data[y]) { this._column = this.layer.data[y]; } if (this._column) { for (var x = this._mc.startX, lenX = this._mc.startX + this._mc.maxX; x < lenX; x++ ){ var tile = null ; if (x < 0 && this.wrap) { tile = this._column[x + this.map.width]; } else if (x >= this.map.width && this.wrap) { tile = this._column[x - this.map.width]; } else if (this._column[x]) { tile = this._column[x]; } if (tile && (tile.faceTop || tile.faceBottom || tile.faceLeft || tile.faceRight)) { this._mc.tx = Math.floor(this._mc.tx); if (this.debugFill) { this.context.fillRect(this._mc.tx, this._mc.ty, this._mc.cw, this._mc.ch); } this.context.beginPath(); if (tile.faceTop) { this.context.moveTo(this._mc.tx, this._mc.ty); this.context.lineTo(this._mc.tx + this._mc.cw, this._mc.ty); } if (tile.faceBottom) { this.context.moveTo(this._mc.tx, this._mc.ty + this._mc.ch); this.context.lineTo(this._mc.tx + this._mc.cw, this._mc.ty + this._mc.ch); } if (tile.faceLeft) { this.context.moveTo(this._mc.tx, this._mc.ty); this.context.lineTo(this._mc.tx, this._mc.ty + this._mc.ch); } if (tile.faceRight) { this.context.moveTo(this._mc.tx + this._mc.cw, this._mc.ty); this.context.lineTo(this._mc.tx + this._mc.cw, this._mc.ty + this._mc.ch); } this.context.stroke(); } this._mc.tx += this.map.tileWidth; } } this._mc.tx = this._mc.dx; this._mc.ty += this.map.tileHeight; } } ; Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollX", { get: function (){ return this._mc.x; } , set: function (value){ if (value !== this._mc.x) { this._mc.x = value; this._mc.startX = this.game.math.floor(this._mc.x / this.map.tileWidth); this.dirty = true ; } } } ); Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollY", { get: function (){ return this._mc.y; } , set: function (value){ if (value !== this._mc.y) { this._mc.y = value; this._mc.startY = this.game.math.floor(this._mc.y / this.map.tileHeight); this.dirty = true ; } } } ); Object.defineProperty(Phaser.TilemapLayer.prototype, "collisionWidth", { get: function (){ return this._mc.cw; } , set: function (value){ this._mc.cw = value; this.dirty = true ; } } ); Object.defineProperty(Phaser.TilemapLayer.prototype, "collisionHeight", { get: function (){ return this._mc.ch; } , set: function (value){ this._mc.ch = value; this.dirty = true ; } } );