Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, tileset, tilemap, layer){ this.game = game; this.canvas = Phaser.Canvas.create(renderWidth, renderHeight); 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, renderWidth, renderHeight, 'tilemaplayer', game.rnd.uuid()); Phaser.Sprite.call(this, this.game, x, y, this.texture, this.textureFrame); this.type = Phaser.TILEMAPLAYER; this.fixedToCamera = true ; this.tileset = null ; this.tileWidth = 0; this.tileHeight = 0; this.tileMargin = 0; this.tileSpacing = 0; this.widthInPixels = 0; this.heightInPixels = 0; this.renderWidth = renderWidth; this.renderHeight = renderHeight; this._ga = 1; this._dx = 0; this._dy = 0; this._dw = 0; this._dh = 0; this._tx = 0; this._ty = 0; this._results = [] ; this._tw = 0; this._th = 0; this._tl = 0; this._maxX = 0; this._maxY = 0; this._startX = 0; this._startY = 0; this.tilemap = null ; this.layer = null ; this.index = 0; this._x = 0; this._y = 0; this._prevX = 0; this._prevY = 0; this.dirty = true ; if (tileset instanceof Phaser.Tileset || typeof tileset === 'string') { this.updateTileset(tileset); } if (tilemap instanceof Phaser.Tilemap) { this.updateMapData(tilemap, layer); } } ; 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.update = function (){ this.scrollX = this.game.camera.x; this.scrollY = this.game.camera.y; this.render(); } ; Phaser.TilemapLayer.prototype.resizeWorld = function (){ this.game.world.setBounds(0, 0, this.widthInPixels, this.heightInPixels); } ; Phaser.TilemapLayer.prototype.updateTileset = function (tileset){ if (tileset instanceof Phaser.Tileset) { this.tileset = tileset; } else if (typeof tileset === 'string') { this.tileset = this.game.cache.getTileset('tiles'); } else { return ; } this.tileWidth = this.tileset.tileWidth; this.tileHeight = this.tileset.tileHeight; this.tileMargin = this.tileset.tileMargin; this.tileSpacing = this.tileset.tileSpacing; this.updateMax(); } ; Phaser.TilemapLayer.prototype.updateMapData = function (tilemap, layer){ if (typeof layer === 'undefined') { layer = 0; } if (tilemap instanceof Phaser.Tilemap) { this.tilemap = tilemap; this.layer = this.tilemap.layers[layer]; this.index = layer; this.updateMax(); this.tilemap.dirty = true ; } } ; Phaser.TilemapLayer.prototype.getTileX = function (x){ var tileWidth = this.tileWidth * this.scale.x; return this.game.math.snapToFloor(x, tileWidth) / tileWidth; } ; Phaser.TilemapLayer.prototype.getTileY = function (y){ var tileHeight = this.tileHeight * this.scale.y; return this.game.math.snapToFloor(y, tileHeight) / 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 (this.tilemap === null ) { return ; } if (typeof collides === 'undefined') { collides = false ; } if (x < 0) { x = 0; } if (y < 0) { y = 0; } if (width > this.widthInPixels) { width = this.widthInPixels; } if (height > this.heightInPixels) { height = this.heightInPixels; } var tileWidth = this.tileWidth * this.scale.x; var tileHeight = this.tileHeight * this.scale.y; this._tx = this.game.math.snapToFloor(x, tileWidth) / tileWidth; this._ty = this.game.math.snapToFloor(y, tileHeight) / tileHeight; this._tw = (this.game.math.snapToCeil(width, tileWidth) + tileWidth) / tileWidth; this._th = (this.game.math.snapToCeil(height, tileHeight) + tileHeight) / tileHeight; this._results = [] ; var _index = 0; var _tile = null ; var sx = 0; var sy = 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]) { _index = this.layer.data[wy][wx] - 1; _tile = this.tileset.getTile(_index); sx = _tile.width * this.scale.x; sy = _tile.height * this.scale.y; if (collides == false || (collides && _tile.collideNone == false )) { this._results.push({ x: wx * sx, right: (wx * sx) + sx, y: wy * sy, bottom: (wy * sy) + sy, width: sx, height: sy, tx: wx, ty: wy, tile: _tile} ); } } } } return this._results; } ; Phaser.TilemapLayer.prototype.updateMax = function (){ this._maxX = this.game.math.ceil(this.canvas.width / this.tileWidth) + 1; this._maxY = this.game.math.ceil(this.canvas.height / this.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.widthInPixels = this.layer.width * this.tileWidth; this.heightInPixels = this.layer.height * this.tileHeight; } this.dirty = true ; } ; Phaser.TilemapLayer.prototype.render = function (){ if (this.tilemap && this.tilemap.dirty) { this.dirty = true ; } if (!this.dirty || !this.tileset || !this.tilemap || !this.visible) { return ; } this._prevX = this._dx; this._prevY = this._dy; this._dx = - (this._x - (this._startX * this.tileWidth)); this._dy = - (this._y - (this._startY * this.tileHeight)); this._tx = this._dx; this._ty = this._dy; this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); for (var y = this._startY; y < this._startY + this._maxY; y++ ){ this._column = this.layer.data[y]; for (var x = this._startX; x < this._startX + this._maxX; x++ ){ var tile = this.tileset.tiles[this._column[x] - 1]; if (tile) { this.context.drawImage(this.tileset.image, tile.x, tile.y, this.tileWidth, this.tileHeight, Math.floor(this._tx), Math.floor(this._ty), this.tileWidth, this.tileHeight); } this._tx += this.tileWidth; } this._tx = this._dx; this._ty += this.tileHeight; } if (this.game.renderType == Phaser.WEBGL) { PIXI.texturesToUpdate.push(this.baseTexture); } this.dirty = false ; if (this.tilemap.dirty) { this.tilemap.dirty = false ; } return true ; } ; Phaser.TilemapLayer.prototype.deltaAbsX = function (){ return (this.deltaX() > 0? this.deltaX(): - this.deltaX()); } ; Phaser.TilemapLayer.prototype.deltaAbsY = function (){ return (this.deltaY() > 0? this.deltaY(): - this.deltaY()); } ; Phaser.TilemapLayer.prototype.deltaX = function (){ return this._dx - this._prevX; } ; Phaser.TilemapLayer.prototype.deltaY = function (){ return this._dy - this._prevY; } ; Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollX", { get: function (){ return this._x; } , set: function (value){ if (value !== this._x && value >= 0 && this.layer) { this._x = value; if (this._x > (this.widthInPixels - this.renderWidth)) { this._x = this.widthInPixels - this.renderWidth; } this._startX = this.game.math.floor(this._x / this.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) { this._y = value; if (this._y > (this.heightInPixels - this.renderHeight)) { this._y = this.heightInPixels - this.renderHeight; } this._startY = this.game.math.floor(this._y / this.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 ; } } } );