Fixed object creation, sprite origin and added useSpriteSheet property

This commit is contained in:
Richard Davey 2024-02-20 21:38:42 +00:00
parent 0002c009f7
commit ebe627836e

View file

@ -4,6 +4,7 @@
* @license {@link https://opensource.org/licenses/MIT|MIT License} * @license {@link https://opensource.org/licenses/MIT|MIT License}
*/ */
var DeepCopy = require('../../utils/object/DeepCopy');
var GetTilesWithin = require('./GetTilesWithin'); var GetTilesWithin = require('./GetTilesWithin');
var ReplaceByIndex = require('./ReplaceByIndex'); var ReplaceByIndex = require('./ReplaceByIndex');
@ -39,7 +40,10 @@ var CreateFromTiles = function (indexes, replacements, spriteConfig, scene, came
if (!scene) { scene = tilemapLayer.scene; } if (!scene) { scene = tilemapLayer.scene; }
if (!camera) { camera = scene.cameras.main; } 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 sprites = [];
var i; var i;
@ -59,34 +63,47 @@ var CreateFromTiles = function (indexes, replacements, spriteConfig, scene, came
for (i = 0; i < tiles.length; i++) for (i = 0; i < tiles.length; i++)
{ {
var tile = tiles[i]; var tile = tiles[i];
var config = DeepCopy(spriteConfig);
if (indexes.indexOf(tile.index) !== -1) if (indexes.indexOf(tile.index) !== -1)
{ {
var point = tilemapLayer.tileToWorldXY(tile.x, tile.y, undefined, camera,layer); var point = tilemapLayer.tileToWorldXY(tile.x, tile.y, undefined, camera,layer);
spriteConfig.x = point.x; config.x = point.x;
spriteConfig.y = point.y; 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') if (Array.isArray(replacements))
{
// 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))
{ {
// Assume 1 to 1 mapping with indexes array // Assume 1 to 1 mapping with indexes array
for (i = 0; i < indexes.length; i++) 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);
} }
} }