diff --git a/src/tilemaps/components/CreateFromTiles.js b/src/tilemaps/components/CreateFromTiles.js index 493cf1fc3..94aff37d9 100644 --- a/src/tilemaps/components/CreateFromTiles.js +++ b/src/tilemaps/components/CreateFromTiles.js @@ -4,6 +4,7 @@ * @license {@link https://opensource.org/licenses/MIT|MIT License} */ +var DeepCopy = require('../../utils/object/DeepCopy'); var GetTilesWithin = require('./GetTilesWithin'); var ReplaceByIndex = require('./ReplaceByIndex'); @@ -39,7 +40,10 @@ var CreateFromTiles = function (indexes, replacements, spriteConfig, scene, came if (!scene) { scene = tilemapLayer.scene; } if (!camera) { camera = scene.cameras.main; } - var tiles = GetTilesWithin(0, 0, layer.width, layer.height, null, layer); + var layerWidth = layer.width; + var layerHeight = layer.height; + + var tiles = GetTilesWithin(0, 0, layerWidth, layerHeight, null, layer); var sprites = []; var i; @@ -59,34 +63,47 @@ var CreateFromTiles = function (indexes, replacements, spriteConfig, scene, came for (i = 0; i < tiles.length; i++) { var tile = tiles[i]; + var config = DeepCopy(spriteConfig); if (indexes.indexOf(tile.index) !== -1) { var point = tilemapLayer.tileToWorldXY(tile.x, tile.y, undefined, camera,layer); - spriteConfig.x = point.x; - spriteConfig.y = point.y; + config.x = point.x; + config.y = point.y; - mergeExtras(spriteConfig, tile, [ 'rotation', 'flipX', 'flipY', 'alpha', 'visible', 'tint' ]); + mergeExtras(config, tile, [ 'rotation', 'flipX', 'flipY', 'alpha', 'visible', 'tint' ]); - sprites.push(scene.make.sprite(spriteConfig)); + if (!config.hasOwnProperty('origin')) + { + config.x += tile.width * 0.5; + config.y += tile.height * 0.5; + } + + if (config.hasOwnProperty('useSpriteSheet')) + { + config.key = tile.tileset.image; + config.frame = tile.index - 1; + } + + sprites.push(scene.make.sprite(config)); } } - if (typeof replacements === 'number') - { - // Assume 1 replacement for all types of tile given - for (i = 0; i < indexes.length; i++) - { - ReplaceByIndex(indexes[i], replacements, 0, 0, layer.width, layer.height, layer); - } - } - else if (Array.isArray(replacements)) + if (Array.isArray(replacements)) { // Assume 1 to 1 mapping with indexes array for (i = 0; i < indexes.length; i++) { - ReplaceByIndex(indexes[i], replacements[i], 0, 0, layer.width, layer.height, layer); + ReplaceByIndex(indexes[i], replacements[i], 0, 0, layerWidth, layerHeight, layer); + } + } + else if (replacements !== null) + { + // Assume 1 replacement for all types of tile given + for (i = 0; i < indexes.length; i++) + { + ReplaceByIndex(indexes[i], replacements, 0, 0, layerWidth, layerHeight, layer); } }