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.Sprite.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._cw = tilemap.tileWidth; this._ch = tilemap.tileHeight; this._ga = 1; this._dx = 0; this._dy = 0; this._dw = 0; this._dh = 0; this._tx = 0; this._ty = 0; this._tw = 0; this._th = 0; this._tl = 0; this._maxX = 0; this._maxY = 0; this._startX = 0; this._startY = 0; this._results = [] ; this._x = 0; this._y = 0; this._prevX = 0; this._prevY = 0; this.updateMax(); } ; Phaser.TilemapLayer.prototype = Object.create(Phaser.Sprite.prototype); Phaser.TilemapLayer.prototype = Phaser.Utils.extend(true , Phaser.TilemapLayer.prototype, Phaser.Sprite.prototype, PIXI.Sprite.prototype); Phaser.TilemapLayer.prototype.constructor = Phaser.TilemapLayer; Phaser.TilemapLayer.prototype.postUpdate = function (){ Phaser.Sprite.prototype.postUpdate.call(this); this.scrollX = this.game.camera.x * this.scrollFactorX; this.scrollY = this.game.camera.y * this.scrollFactorY; this.render(); } ; 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._x + (x - (this._x / this.scrollFactorX)); } ; Phaser.TilemapLayer.prototype._unfixX = function (x){ if (this.scrollFactorX === 1) { return x; } return (this._x / this.scrollFactorX) + (x - this._x); } ; Phaser.TilemapLayer.prototype._fixY = function (y){ if (y < 0) { y = 0; } if (this.scrollFactorY === 1) { return y; } return this._y + (y - (this._y / this.scrollFactorY)); } ; Phaser.TilemapLayer.prototype._unfixY = function (y){ if (this.scrollFactorY === 1) { return y; } return (this._y / this.scrollFactorY) + (y - this._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.getTiles = function (x, y, width, height, collides){ if (typeof collides === 'undefined') { collides = 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._tx = this.game.math.snapToFloor(x, this._cw) / this._cw; this._ty = this.game.math.snapToFloor(y, this._ch) / this._ch; this._tw = (this.game.math.snapToCeil(width, this._cw) + this._cw) / this._cw; this._th = (this.game.math.snapToCeil(height, this._ch) + this._ch) / this._ch; this._results.length = 0; for (var wy = this._ty; wy < this._ty + this._th; wy++ ){ for (var wx = this._tx; wx < this._tx + this._tw; wx++ ){ if (this.layer.data[wy] && this.layer.data[wy][wx]) { if (collides === false || (collides && this.layer.data[wy][wx].canCollide)) { var _wx = this._unfixX(wx * this._cw) / this._cw; var _wy = this._unfixY(wy * this._ch) / this._ch; this._results.push({ x: _wx * this._cw, y: _wy * this._ch, right: (_wx * this._cw) + this._cw, bottom: (_wy * this._ch) + this._ch, tile: this.layer.data[wy][wx], layer: this.layer.data[wy][wx].layer} ); } } } } return this._results; } ; Phaser.TilemapLayer.prototype.updateMax = function (){ this._maxX = this.game.math.ceil(this.canvas.width / this.map.tileWidth) + 1; this._maxY = this.game.math.ceil(this.canvas.height / this.map.tileHeight) + 1; if (this.layer) { if (this._maxX > this.layer.width) { this._maxX = this.layer.width; } if (this._maxY > this.layer.height) { this._maxY = this.layer.height; } } this.dirty = true ; } ; Phaser.TilemapLayer.prototype.render = function (){ if (this.layer.dirty) { this.dirty = true ; } if (!this.dirty || !this.visible) { return ; } this._prevX = this._dx; this._prevY = this._dy; this._dx = - (this._x - (this._startX * this.map.tileWidth)); this._dy = - (this._y - (this._startY * this.map.tileHeight)); this._tx = this._dx; this._ty = this._dy; this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); this.context.fillStyle = this.tileColor; var tile; var set; var ox = 0; var oy = 0; if (this.debug) { this.context.globalAlpha = this.debugAlpha; } for (var y = this._startY, lenY = this._startY + this._maxY; y < lenY; y++ ){ this._column = this.layer.data[y]; for (var x = this._startX, lenX = this._startX + this._maxX; x < lenX; x++ ){ if (this._column[x]) { tile = this._column[x]; if (this.map.tiles[tile.index]) { set = this.map.tilesets[this.map.tiles[tile.index][2]]; if (set.image) { if (this.debug === false && tile.alpha !== this.context.globalAlpha) { this.context.globalAlpha = tile.alpha; } if (set.tileWidth !== this.map.tileWidth || set.tileHeight !== this.map.tileHeight) { this.context.drawImage(this.map.tilesets[this.map.tiles[tile.index][2]].image, this.map.tiles[tile.index][0], this.map.tiles[tile.index][1], set.tileWidth, set.tileHeight, Math.floor(this._tx), Math.floor(this._ty) - (set.tileHeight - this.map.tileHeight), set.tileWidth, set.tileHeight); } else { this.context.drawImage(this.map.tilesets[this.map.tiles[tile.index][2]].image, this.map.tiles[tile.index][0], this.map.tiles[tile.index][1], this.map.tileWidth, this.map.tileHeight, Math.floor(this._tx), Math.floor(this._ty), this.map.tileWidth, this.map.tileHeight); } if (tile.debug) { this.context.fillStyle = 'rgba(0, 255, 0, 0.4)'; this.context.fillRect(Math.floor(this._tx), Math.floor(this._ty), this.map.tileWidth, this.map.tileHeight); } } else { this.context.fillRect(Math.floor(this._tx), Math.floor(this._ty), this.map.tileWidth, this.map.tileHeight); } } } this._tx += this.map.tileWidth; } this._tx = this._dx; this._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._tx = this._dx; this._ty = this._dy; this.context.strokeStyle = this.debugColor; this.context.fillStyle = this.debugFillColor; for (var y = this._startY, lenY = this._startY + this._maxY; y < lenY; y++ ){ this._column = this.layer.data[y]; for (var x = this._startX, lenX = this._startX + this._maxX; x < lenX; x++ ){ var tile = this._column[x]; if (tile && (tile.faceTop || tile.faceBottom || tile.faceLeft || tile.faceRight)) { this._tx = Math.floor(this._tx); if (this.debugFill) { this.context.fillRect(this._tx, this._ty, this._cw, this._ch); } this.context.beginPath(); if (tile.faceTop) { this.context.moveTo(this._tx, this._ty); this.context.lineTo(this._tx + this._cw, this._ty); } if (tile.faceBottom) { this.context.moveTo(this._tx, this._ty + this._ch); this.context.lineTo(this._tx + this._cw, this._ty + this._ch); } if (tile.faceLeft) { this.context.moveTo(this._tx, this._ty); this.context.lineTo(this._tx, this._ty + this._ch); } if (tile.faceRight) { this.context.moveTo(this._tx + this._cw, this._ty); this.context.lineTo(this._tx + this._cw, this._ty + this._ch); } this.context.stroke(); } if (tile && (tile.collisionCallback || tile.layer.callbacks[tile.index])) { this.context.fillStyle = this.debugCallbackColor; this.context.fillRect(this._tx, this._ty, this._cw, this._ch); this.context.fillStyle = this.debugFillColor; } this._tx += this.map.tileWidth; } this._tx = this._dx; this._ty += this.map.tileHeight; } } ; Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollX", { get: function (){ return this._x; } , set: function (value){ if (value !== this._x && value >= 0 && this.layer.widthInPixels > this.width) { this._x = value; if (this._x > (this.layer.widthInPixels - this.width)) { this._x = this.layer.widthInPixels - this.width; } this._startX = this.game.math.floor(this._x / this.map.tileWidth); if (this._startX < 0) { this._startX = 0; } if (this._startX + this._maxX > this.layer.width) { this._startX = this.layer.width - this._maxX; } this.dirty = true ; } } } ); Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollY", { get: function (){ return this._y; } , set: function (value){ if (value !== this._y && value >= 0 && this.layer.heightInPixels > this.height) { this._y = value; if (this._y > (this.layer.heightInPixels - this.height)) { this._y = this.layer.heightInPixels - this.height; } this._startY = this.game.math.floor(this._y / this.map.tileHeight); if (this._startY < 0) { this._startY = 0; } if (this._startY + this._maxY > this.layer.height) { this._startY = this.layer.height - this._maxY; } this.dirty = true ; } } } ); Object.defineProperty(Phaser.TilemapLayer.prototype, "collisionWidth", { get: function (){ return this._cw; } , set: function (value){ this._cw = value; this.dirty = true ; } } ); Object.defineProperty(Phaser.TilemapLayer.prototype, "collisionHeight", { get: function (){ return this._ch; } , set: function (value){ this._ch = value; this.dirty = true ; } } );