mirror of
https://github.com/photonstorm/phaser
synced 2024-11-27 23:20:59 +00:00
Tilemap & Dynamic: new method for putting a 2D array of tiles at a location
This commit is contained in:
parent
e548b73e80
commit
1be6d7a681
4 changed files with 78 additions and 0 deletions
|
@ -836,6 +836,23 @@ var Tilemap = new Class({
|
|||
return TilemapComponents.PutTileAtWorldXY(tile, worldX, worldY, recalculateFaces, camera, layer);
|
||||
},
|
||||
|
||||
/**
|
||||
* See component documentation. If no layer specified, the map's current layer is used. This
|
||||
* cannot be applied to StaticTilemapLayers.
|
||||
*
|
||||
* @return {this|null} Returns this, or null if the layer given was invalid.
|
||||
*/
|
||||
putTilesAt: function (tilesArray, tileX, tileY, recalculateFaces, layer)
|
||||
{
|
||||
layer = this.getLayer(layer);
|
||||
if (this._isStaticCall(layer, 'putTilesAt')) { return this; }
|
||||
if (layer !== null)
|
||||
{
|
||||
TilemapComponents.PutTilesAt(tilesArray, tileX, tileY, recalculateFaces, layer);
|
||||
}
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* See component documentation. If no layer specified, the map's current layer is used. This
|
||||
* cannot be applied to StaticTilemapLayers.
|
||||
|
|
49
v3/src/gameobjects/tilemap/components/PutTilesAt.js
Normal file
49
v3/src/gameobjects/tilemap/components/PutTilesAt.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
var CalculateFacesWithin = require('./CalculateFacesWithin');
|
||||
var PutTileAt = require('./PutTileAt');
|
||||
|
||||
/**
|
||||
* Puts an array of tiles or a 2D array of tiles at the given tile coordinates in the specified
|
||||
* layer. The array can be composed of either tile indexes or Tile objects. If you pass in a Tile,
|
||||
* all attributes will be copied over to the specified location. If you pass in an index, only the
|
||||
* index at the specified location will be changed. Collision information will be recalculated
|
||||
* within the region tiles were changed.
|
||||
*
|
||||
* @param {integer[]|integer[][]|Tile[]|Tile[][]} tile - A row (array) or grid (2D array) of Tiles
|
||||
* or tile indexes to place.
|
||||
* @param {integer} tileX - [description]
|
||||
* @param {integer} tileY - [description]
|
||||
* @param {boolean} [recalculateFaces=true] - [description]
|
||||
* @param {LayerData} layer - [description]
|
||||
*/
|
||||
var PutTilesAt = function (tilesArray, tileX, tileY, recalculateFaces, layer)
|
||||
{
|
||||
if (!Array.isArray(tilesArray)) { return null; }
|
||||
if (recalculateFaces === undefined) { recalculateFaces = true; }
|
||||
|
||||
// Force the input array to be a 2D array
|
||||
if (!Array.isArray(tilesArray[0]))
|
||||
{
|
||||
tilesArray = [ tilesArray ];
|
||||
}
|
||||
|
||||
var height = tilesArray.length;
|
||||
var width = tilesArray[0].length;
|
||||
|
||||
for (var ty = 0; ty < height; ty++)
|
||||
{
|
||||
for (var tx = 0; tx < width; tx++)
|
||||
{
|
||||
var tile = tilesArray[ty][tx];
|
||||
PutTileAt(tile, tileX + tx, tileY + ty, false, layer);
|
||||
}
|
||||
}
|
||||
|
||||
if (recalculateFaces)
|
||||
{
|
||||
// Recalculate the faces within the destination area and neighboring tiles
|
||||
CalculateFacesWithin(tileX - 1, tileY - 1, width + 2, height + 2, layer);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = PutTilesAt;
|
||||
|
|
@ -17,6 +17,7 @@ module.exports = {
|
|||
IsInLayerBounds: require('./IsInLayerBounds'),
|
||||
PutTileAt: require('./PutTileAt'),
|
||||
PutTileAtWorldXY: require('./PutTileAtWorldXY'),
|
||||
PutTilesAt: require('./PutTilesAt'),
|
||||
Randomize: require('./Randomize'),
|
||||
CalculateFacesWithin: require('./CalculateFacesWithin'),
|
||||
RemoveTileAt: require('./RemoveTileAt'),
|
||||
|
|
|
@ -284,6 +284,17 @@ var DynamicTilemapLayer = new Class({
|
|||
return TilemapComponents.PutTileAtWorldXY(tile, worldX, worldY, recalculateFaces, camera, this.layer);
|
||||
},
|
||||
|
||||
/**
|
||||
* See component documentation.
|
||||
*
|
||||
* @return {this}
|
||||
*/
|
||||
putTilesAt: function (tilesArray, tileX, tileY, recalculateFaces)
|
||||
{
|
||||
TilemapComponents.PutTilesAt(tilesArray, tileX, tileY, recalculateFaces, this.layer);
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* See component documentation.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue