From ef9f53e3504a76a1303853734370dfc3e45df54c Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 22 Jul 2016 03:03:50 +0100 Subject: [PATCH] TilemapLayer.getRayCastTiles has been moved to Tilemap.getRayCastTiles. TilemapLayer.rayStepRate has been moved to Tilemap.rayStepRate. TilemapLayer.getTiles has been moved to Tilemap.getTiles. --- README.md | 3 + src/tilemap/Tilemap.js | 131 ++++++++++++++++++++++++++++++++-- src/tilemap/TilemapLayer.js | 119 +----------------------------- src/tilemap/TilemapLayerGL.js | 120 +------------------------------ 4 files changed, 132 insertions(+), 241 deletions(-) diff --git a/README.md b/README.md index 830e7d53a..9e808eefc 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,9 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/ * The Loader.headers object has a new property `requestedWith`. By default this is set to `false`, but it can be used to set the `X-Requested-With` header to `XMLHttpRequest` (or any other value you need). To enable this do `this.load.headers.requestedWith = 'XMLHttpRequest'` before adding anything to the Loader. * ScaleManager.hasPhaserSetFullScreen is a new boolean that identifies if the browser is in full screen mode or not, and if Phaser was the one that requested it. As it's possible to enter full screen mode outside of Phaser, and it then gets confused about what bounding parent to use. * Phaser.Tileset has a new property `lastgid` which is populated automatically by the TilemapParser when importing Tiled map data, or can be set manually if building your own tileset. +* TilemapLayer.getRayCastTiles has been moved to Tilemap.getRayCastTiles. +* TilemapLayer.rayStepRate has been moved to Tilemap.rayStepRate. +* TilemapLayer.getTiles has been moved to Tilemap.getTiles. ### Bug Fixes diff --git a/src/tilemap/Tilemap.js b/src/tilemap/Tilemap.js index cb1c4e77d..294638ce7 100644 --- a/src/tilemap/Tilemap.js +++ b/src/tilemap/Tilemap.js @@ -151,6 +151,13 @@ Phaser.Tilemap = function (game, key, tileWidth, tileHeight, width, height) { */ this.debugMap = []; + /** + * When ray-casting against tiles this is the number of steps it will jump. For larger tile sizes you can increase this to improve performance. + * @property {integer} rayStepRate + * @default + */ + this.rayStepRate = 4; + /** * @property {array} _results - Internal var. * @private @@ -731,7 +738,6 @@ Phaser.Tilemap.prototype = { for (var x = 0; x < width; x++) { - // row.push(null); row.push(new Phaser.Tile(layer, -1, x, y, tileWidth, tileHeight)); } @@ -758,14 +764,16 @@ Phaser.Tilemap.prototype = { } var output; - if ( this.game.renderType === Phaser.WEBGL ) + + if (this.game.renderType === Phaser.WEBGL) { - output = new Phaser.TilemapLayerGL(this.game, this, this.layers.length - 1, w, h); + output = new Phaser.TilemapLayerGL(this.game, this, this.layers.length - 1, w, h, null); } else { output = new Phaser.TilemapLayer(this.game, this, this.layers.length - 1, w, h); } + output.name = name; return group.add(output); @@ -1311,10 +1319,14 @@ Phaser.Tilemap.prototype = { hasTile: function (x, y, layer) { layer = this.getLayer(layer); - if (this.layers[layer].data[y] === undefined || this.layers[layer].data[y][x] === undefined) { + + if (this.layers[layer].data[y] === undefined || this.layers[layer].data[y][x] === undefined) + { return false; } + return (this.layers[layer].data[y][x].index > -1); + }, /** @@ -1984,6 +1996,117 @@ Phaser.Tilemap.prototype = { }, + /** + * Gets all tiles from `Tilemap.layer` that intersect with the given Phaser.Line object. + * + * If you need to get the tiles from a different layer, then set the `layer` property first. + * + * @method Phaser.Tilemap#getRayCastTiles + * @param {Phaser.Line} line - The line used to determine which tiles to return. + * @param {integer} [stepRate=(rayStepRate)] - How many steps through the ray will we check? Defaults to `rayStepRate`. + * @param {boolean} [collides=false] - If true, _only_ return tiles that collide on one or more faces. + * @param {boolean} [interestingFace=false] - If true, _only_ return tiles that have interesting faces. + * @return {Phaser.Tile[]} An array of Phaser.Tiles. + */ + getRayCastTiles: function (line, stepRate, collides, interestingFace) { + + if (!stepRate) { stepRate = this.rayStepRate; } + if (collides === undefined) { collides = false; } + if (interestingFace === undefined) { interestingFace = false; } + + // First get all tiles that touch the bounds of the line + var tiles = this.getTiles(line.x, line.y, line.width, line.height, collides, interestingFace); + + if (tiles.length === 0) + { + return []; + } + + // Now we only want the tiles that intersect with the points on this line + var coords = line.coordinatesOnLine(stepRate); + var results = []; + + for (var i = 0; i < tiles.length; i++) + { + for (var t = 0; t < coords.length; t++) + { + var tile = tiles[i]; + var coord = coords[t]; + + if (tile.containsPoint(coord[0], coord[1])) + { + results.push(tile); + break; + } + } + } + + return results; + + }, + + /** + * Get all Tiles that exist on the current `Tilemap.layer`, within the given area, + * as defined by the top-left corner, width and height arguments. + * + * Values given are in pixels, not tiles. + * + * If you need to get the tiles from a different layer, then set the `layer` property first. + * + * @method Phaser.Tilemap#getTiles + * @param {number} x - X position of the top left corner (in pixels). + * @param {number} y - Y position of the top left corner (in pixels). + * @param {number} width - Width of the area to get (in pixels). + * @param {number} height - Height of the area to get (in pixels). + * @param {boolean} [collides=false] - If true, _only_ return tiles that collide on one or more faces. + * @param {boolean} [interestingFace=false] - If true, _only_ return tiles that have interesting faces. + * @return {array} An array of Tiles. + */ + getTiles: function (x, y, width, height, collides, interestingFace) { + + // Should we only get tiles that have at least one of their collision flags set? (true = yes, false = no just get them all) + + if (collides === undefined) { collides = false; } + if (interestingFace === undefined) { interestingFace = false; } + + var fetchAll = !(collides || interestingFace); + + var layer = this.layer; + + // Adjust the x,y coordinates for scrollFactor + x = layer._fixX(x); + y = layer._fixY(y); + + // Convert the pixel values into tile coordinates + var tx = Math.floor(x / (layer._mc.cw * layer.scale.x)); + var ty = Math.floor(y / (layer._mc.ch * layer.scale.y)); + + // Don't just use ceil(width/cw) to allow account for x/y diff within cell + var tw = Math.ceil((x + width) / (layer._mc.cw * layer.scale.x)) - tx; + var th = Math.ceil((y + height) / (layer._mc.ch * layer.scale.y)) - ty; + + this._results = []; + + for (var wy = ty; wy < ty + th; wy++) + { + for (var wx = tx; wx < tx + tw; wx++) + { + var row = layer.data[wy]; + + if (row && row[wx]) + { + if (fetchAll || row[wx].isInteresting(collides, interestingFace)) + { + this._results.push(row[wx]); + } + } + } + } + + return this._results.slice(); + + }, + /** * Removes all layer data from this tile map and nulls the game reference. * Note: You are responsible for destroying any TilemapLayer objects you generated yourself, as Tilemap doesn't keep a reference to them. diff --git a/src/tilemap/TilemapLayer.js b/src/tilemap/TilemapLayer.js index 8f0106517..4e09ffa2f 100644 --- a/src/tilemap/TilemapLayer.js +++ b/src/tilemap/TilemapLayer.js @@ -166,13 +166,6 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) { */ this.dirty = true; - /** - * When ray-casting against tiles this is the number of steps it will jump. For larger tile sizes you can increase this to improve performance. - * @property {integer} rayStepRate - * @default - */ - this.rayStepRate = 4; - /** * Flag controlling if the layer tiles wrap at the edges. * @property {boolean} _wrap @@ -221,13 +214,6 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) { */ this._scrollY = 0; - /** - * Used for caching the tiles / array of tiles. - * @property {Phaser.Tile[]} _results - * @private - */ - this._results = []; - if (!game.device.canvasBitBltShift) { this.renderSettings.copyCanvas = Phaser.TilemapLayer.ensureSharedCopyCanvas(); @@ -548,109 +534,6 @@ Phaser.TilemapLayer.prototype.getTileXY = function (x, y, point) { }; -/** -* Gets all tiles that intersect with the given line. -* -* @method Phaser.TilemapLayer#getRayCastTiles -* @public -* @param {Phaser.Line} line - The line used to determine which tiles to return. -* @param {integer} [stepRate=(rayStepRate)] - How many steps through the ray will we check? Defaults to `rayStepRate`. -* @param {boolean} [collides=false] - If true, _only_ return tiles that collide on one or more faces. -* @param {boolean} [interestingFace=false] - If true, _only_ return tiles that have interesting faces. -* @return {Phaser.Tile[]} An array of Phaser.Tiles. -*/ -Phaser.TilemapLayer.prototype.getRayCastTiles = function (line, stepRate, collides, interestingFace) { - - if (!stepRate) { stepRate = this.rayStepRate; } - if (collides === undefined) { collides = false; } - if (interestingFace === undefined) { interestingFace = false; } - - // First get all tiles that touch the bounds of the line - var tiles = this.getTiles(line.x, line.y, line.width, line.height, collides, interestingFace); - - if (tiles.length === 0) - { - return []; - } - - // Now we only want the tiles that intersect with the points on this line - var coords = line.coordinatesOnLine(stepRate); - var results = []; - - for (var i = 0; i < tiles.length; i++) - { - for (var t = 0; t < coords.length; t++) - { - var tile = tiles[i]; - var coord = coords[t]; - if (tile.containsPoint(coord[0], coord[1])) - { - results.push(tile); - break; - } - } - } - - return results; - -}; - -/** -* Get all tiles that exist within the given area, defined by the top-left corner, width and height. Values given are in pixels, not tiles. -* -* @method Phaser.TilemapLayer#getTiles -* @public -* @param {number} x - X position of the top left corner (in pixels). -* @param {number} y - Y position of the top left corner (in pixels). -* @param {number} width - Width of the area to get (in pixels). -* @param {number} height - Height of the area to get (in pixels). -* @param {boolean} [collides=false] - If true, _only_ return tiles that collide on one or more faces. -* @param {boolean} [interestingFace=false] - If true, _only_ return tiles that have interesting faces. -* @return {array} An array of Tiles. -*/ -Phaser.TilemapLayer.prototype.getTiles = function (x, y, width, height, collides, interestingFace) { - - // Should we only get tiles that have at least one of their collision flags set? (true = yes, false = no just get them all) - if (collides === undefined) { collides = false; } - if (interestingFace === undefined) { interestingFace = false; } - - var fetchAll = !(collides || interestingFace); - - // Adjust the x,y coordinates for scrollFactor - x = this._fixX(x); - y = this._fixY(y); - - // Convert the pixel values into tile coordinates - var tx = Math.floor(x / (this._mc.cw * this.scale.x)); - var ty = Math.floor(y / (this._mc.ch * this.scale.y)); - // Don't just use ceil(width/cw) to allow account for x/y diff within cell - 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; - - while (this._results.length) - { - this._results.pop(); - } - - 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(); - -}; /** * Returns the appropriate tileset for the index, updating the internal cache as required. @@ -676,7 +559,7 @@ Phaser.TilemapLayer.prototype.resolveTileset = function (tileIndex) { var setIndex = this.map.tiles[tileIndex] && this.map.tiles[tileIndex][2]; - if (setIndex != null) // number: not null or undefined + if (setIndex !== null) { var tileset = this.map.tilesets[setIndex]; diff --git a/src/tilemap/TilemapLayerGL.js b/src/tilemap/TilemapLayerGL.js index 323cd6a07..480f8d191 100644 --- a/src/tilemap/TilemapLayerGL.js +++ b/src/tilemap/TilemapLayerGL.js @@ -152,13 +152,6 @@ Phaser.TilemapLayerGL = function (game, tilemap, index, width, height, tileset) */ this.dirty = true; - /** - * When ray-casting against tiles this is the number of steps it will jump. For larger tile sizes you can increase this to improve performance. - * @property {integer} rayStepRate - * @default - */ - this.rayStepRate = 4; - /** * Flag controlling if the layer tiles wrap at the edges. * @property {boolean} _wrap @@ -223,13 +216,6 @@ Phaser.TilemapLayerGL = function (game, tilemap, index, width, height, tileset) */ this._scrollY = 0; - /** - * Used for caching the tiles / array of tiles. - * @property {Phaser.Tile[]} _results - * @private - */ - this._results = []; - var baseTexture = new PIXI.BaseTexture(tileset.image); PIXI.Tilemap.call(this, new PIXI.Texture(baseTexture), width | 0, height | 0, this.map.width, this.map.height, this._mc.tileset.tileWidth, this._mc.tileset.tileHeight, this.layer); @@ -456,114 +442,10 @@ Phaser.TilemapLayerGL.prototype.getTileXY = function (x, y, point) { }; -/** -* Gets all tiles that intersect with the given line. -* -* @method Phaser.TilemapLayerGL#getRayCastTiles -* @public -* @param {Phaser.Line} line - The line used to determine which tiles to return. -* @param {integer} [stepRate=(rayStepRate)] - How many steps through the ray will we check? Defaults to `rayStepRate`. -* @param {boolean} [collides=false] - If true, _only_ return tiles that collide on one or more faces. -* @param {boolean} [interestingFace=false] - If true, _only_ return tiles that have interesting faces. -* @return {Phaser.Tile[]} An array of Phaser.Tiles. -*/ -Phaser.TilemapLayerGL.prototype.getRayCastTiles = function (line, stepRate, collides, interestingFace) { - - if (!stepRate) { stepRate = this.rayStepRate; } - if (collides === undefined) { collides = false; } - if (interestingFace === undefined) { interestingFace = false; } - - // First get all tiles that touch the bounds of the line - var tiles = this.getTiles(line.x, line.y, line.width, line.height, collides, interestingFace); - - if (tiles.length === 0) - { - return []; - } - - // Now we only want the tiles that intersect with the points on this line - var coords = line.coordinatesOnLine(stepRate); - var results = []; - - for (var i = 0; i < tiles.length; i++) - { - for (var t = 0; t < coords.length; t++) - { - var tile = tiles[i]; - var coord = coords[t]; - - if (tile.containsPoint(coord[0], coord[1])) - { - results.push(tile); - break; - } - } - } - - return results; - -}; - -/** -* Get all tiles that exist within the given area, defined by the top-left corner, width and height. Values given are in pixels, not tiles. -* -* @method Phaser.TilemapLayerGL#getTiles -* @public -* @param {number} x - X position of the top left corner (in pixels). -* @param {number} y - Y position of the top left corner (in pixels). -* @param {number} width - Width of the area to get (in pixels). -* @param {number} height - Height of the area to get (in pixels). -* @param {boolean} [collides=false] - If true, _only_ return tiles that collide on one or more faces. -* @param {boolean} [interestingFace=false] - If true, _only_ return tiles that have interesting faces. -* @return {array} An array of Tiles. -*/ -Phaser.TilemapLayerGL.prototype.getTiles = function (x, y, width, height, collides, interestingFace) { - - // Should we only get tiles that have at least one of their collision flags set? (true = yes, false = no just get them all) - if (collides === undefined) { collides = false; } - if (interestingFace === undefined) { interestingFace = false; } - - var fetchAll = !(collides || interestingFace); - - // Adjust the x,y coordinates for scrollFactor - x = this._fixX(x); - y = this._fixY(y); - - // Convert the pixel values into tile coordinates - var tx = Math.floor(x / (this._mc.cw * this.scale.x)); - var ty = Math.floor(y / (this._mc.ch * this.scale.y)); - - // Don't just use ceil(width/cw) to allow account for x/y diff within cell - 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; - - // Discard old results before storing the new ones - 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(); - -}; - /** * The TilemapLayerGL caches tileset look-ups. * -* Call this method of clear the cache if tilesets have been added or updated after the layer has been rendered. +* Call this method to clear the cache if tilesets have been added or updated after the layer has been rendered. * * @method Phaser.TilemapLayerGL#resetTilesetCache * @public