phaser/src/tilemaps/parsers/tiled/ParseObject.js

75 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2020-01-15 12:07:09 +00:00
* @copyright 2020 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
var Pick = require('../../../utils/object/Pick');
var ParseGID = require('./ParseGID');
var copyPoints = function (p) { return { x: p.x, y: p.y }; };
2018-02-10 01:50:48 +00:00
var commonObjectProps = [ 'id', 'name', 'type', 'rotation', 'properties', 'visible', 'x', 'y', 'width', 'height' ];
2018-02-10 01:50:48 +00:00
/**
2018-12-03 15:16:23 +00:00
* Convert a Tiled object to an internal parsed object normalising and copying properties over, while applying optional x and y offsets. The parsed object will always have the properties `id`, `name`, `type`, `rotation`, `properties`, `visible`, `x`, `y`, `width` and `height`. Other properties will be added according to the object type (such as text, polyline, gid etc.)
2018-02-10 01:50:48 +00:00
*
* @function Phaser.Tilemaps.Parsers.Tiled.ParseObject
* @since 3.0.0
*
2018-12-03 15:16:23 +00:00
* @param {object} tiledObject - Tiled object to convert to an internal parsed object normalising and copying properties over.
* @param {number} [offsetX=0] - Optional additional offset to apply to the object's x property. Defaults to 0.
* @param {number} [offsetY=0] - Optional additional offset to apply to the object's y property. Defaults to 0.
2018-02-10 01:50:48 +00:00
*
2018-12-03 15:16:23 +00:00
* @return {object} The parsed object containing properties read from the Tiled object according to it's type with x and y values updated according to the given offsets.
2018-02-10 01:50:48 +00:00
*/
var ParseObject = function (tiledObject, offsetX, offsetY)
{
if (offsetX === undefined) { offsetX = 0; }
if (offsetY === undefined) { offsetY = 0; }
var parsedObject = Pick(tiledObject, commonObjectProps);
parsedObject.x += offsetX;
parsedObject.y += offsetY;
if (tiledObject.gid)
{
// Object tiles
var gidInfo = ParseGID(tiledObject.gid);
parsedObject.gid = gidInfo.gid;
parsedObject.flippedHorizontal = gidInfo.flippedHorizontal;
parsedObject.flippedVertical = gidInfo.flippedVertical;
parsedObject.flippedAntiDiagonal = gidInfo.flippedAntiDiagonal;
}
else if (tiledObject.polyline)
{
parsedObject.polyline = tiledObject.polyline.map(copyPoints);
}
else if (tiledObject.polygon)
{
parsedObject.polygon = tiledObject.polygon.map(copyPoints);
}
else if (tiledObject.ellipse)
{
parsedObject.ellipse = tiledObject.ellipse;
}
else if (tiledObject.text)
{
parsedObject.text = tiledObject.text;
}
else if (tiledObject.point)
{
parsedObject.point = true;
}
else
{
// Otherwise, assume it is a rectangle
parsedObject.rectangle = true;
}
return parsedObject;
};
module.exports = ParseObject;