2021-05-13 20:23:16 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2021 Photon Storm Ltd.
|
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Class = require('../utils/Class');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @classdesc
|
2021-10-06 20:39:44 +00:00
|
|
|
* The ObjectHelper helps tie objects with `gids` into the tileset
|
|
|
|
* that sits behind them.
|
2021-05-13 20:23:16 +00:00
|
|
|
*
|
|
|
|
* @class ObjectHelper
|
|
|
|
* @memberof Phaser.Tilemaps
|
|
|
|
* @constructor
|
2021-09-23 15:02:52 +00:00
|
|
|
* @since 3.60.0
|
2021-05-13 20:23:16 +00:00
|
|
|
*
|
|
|
|
* @param {Phaser.Tilemaps.Tileset[]} tilesets - The backing tileset data.
|
|
|
|
*/
|
|
|
|
var ObjectHelper = new Class({
|
|
|
|
|
2021-10-06 20:39:44 +00:00
|
|
|
initialize:
|
|
|
|
|
|
|
|
function ObjectHelper (tilesets)
|
2021-05-13 20:23:16 +00:00
|
|
|
{
|
2021-10-06 20:39:44 +00:00
|
|
|
/**
|
|
|
|
* The Tile GIDs array.
|
|
|
|
*
|
|
|
|
* @name Phaser.Tilemaps.ObjectHelper#gids
|
|
|
|
* @type {array}
|
|
|
|
* @since 3.60.0
|
|
|
|
*/
|
2021-05-13 20:23:16 +00:00
|
|
|
this.gids = [];
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
if (tilesets !== undefined)
|
|
|
|
{
|
|
|
|
for (var t = 0; t < tilesets.length; ++t)
|
|
|
|
{
|
|
|
|
var tileset = tilesets[t];
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
for (var i = 0; i < tileset.total; ++i)
|
|
|
|
{
|
|
|
|
this.gids[tileset.firstgid + i] = tileset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-10-06 20:39:44 +00:00
|
|
|
/**
|
|
|
|
* The Tile GIDs array.
|
|
|
|
*
|
|
|
|
* @name Phaser.Tilemaps.ObjectHelper#_gids
|
|
|
|
* @type {array}
|
|
|
|
* @private
|
|
|
|
* @since 3.60.0
|
|
|
|
*/
|
2021-05-13 20:23:16 +00:00
|
|
|
this._gids = this.gids;
|
|
|
|
},
|
|
|
|
|
2021-05-20 20:00:51 +00:00
|
|
|
/**
|
2021-10-06 20:39:44 +00:00
|
|
|
* Enabled if the object helper reaches in to tilesets for data.
|
|
|
|
* Disabled if it only uses data directly on a gid object.
|
2021-09-23 15:02:52 +00:00
|
|
|
*
|
2021-05-20 20:00:51 +00:00
|
|
|
* @name Phaser.Tilemaps.ObjectHelper#enabled
|
2021-10-06 20:39:44 +00:00
|
|
|
* @type {boolean}
|
2021-09-23 15:02:52 +00:00
|
|
|
* @since 3.60.0
|
2021-05-20 20:00:51 +00:00
|
|
|
*/
|
2021-05-13 20:23:16 +00:00
|
|
|
enabled: {
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return !!this.gids;
|
|
|
|
},
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
set: function (v)
|
|
|
|
{
|
|
|
|
this.gids = v ? this._gids : undefined;
|
|
|
|
}
|
2021-10-06 20:39:44 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
},
|
|
|
|
|
2021-05-20 20:00:51 +00:00
|
|
|
/**
|
|
|
|
* Gets the Tiled `type` field value from the object or the `gid` behind it.
|
2021-09-23 15:02:52 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tilemaps.ObjectHelper#getTypeIncludingTile
|
|
|
|
* @since 3.60.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Types.Tilemaps.TiledObject} obj - The Tiled object to investigate.
|
|
|
|
*
|
|
|
|
* @return {?string} The `type` of the object, the tile behind the `gid` of the object, or `undefined`.
|
2021-05-20 20:00:51 +00:00
|
|
|
*/
|
2021-05-13 20:23:16 +00:00
|
|
|
getTypeIncludingTile: function (obj)
|
|
|
|
{
|
2021-09-23 15:02:52 +00:00
|
|
|
if (obj.type !== undefined && obj.type !== '')
|
2021-05-13 20:23:16 +00:00
|
|
|
{
|
|
|
|
return obj.type;
|
|
|
|
}
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
if (!this.gids || obj.gid === undefined)
|
|
|
|
{
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
var tileset = this.gids[obj.gid];
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
if (!tileset)
|
|
|
|
{
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
var tileData = tileset.getTileData(obj.gid);
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
if (!tileData)
|
|
|
|
{
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
return tileData.type;
|
|
|
|
},
|
|
|
|
|
2021-05-20 20:00:51 +00:00
|
|
|
/**
|
2021-09-23 15:02:52 +00:00
|
|
|
* Sets the sprite texture data as specified (usually in a config) or, failing that,
|
|
|
|
* as specified in the `gid` of the object being loaded (if any).
|
|
|
|
*
|
|
|
|
* This fallback will only work if the tileset was loaded as a spritesheet matching
|
|
|
|
* the geometry of sprites fed into tiled, so that, for example: "tile id #`3`"" within
|
|
|
|
* the tileset is the same as texture frame `3` from the image of the tileset.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tilemaps.ObjectHelper#setTextureAndFrame
|
|
|
|
* @since 3.60.0
|
2021-10-06 20:39:44 +00:00
|
|
|
*
|
2021-09-23 15:02:52 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} sprite - The Game Object to modify.
|
|
|
|
* @param {string|Phaser.Textures.Texture} [key] - The texture key to set (or else the `obj.gid`'s tile is used if available).
|
|
|
|
* @param {string|number|Phaser.Textures.Frame} [frame] - The frames key to set (or else the `obj.gid`'s tile is used if available).
|
|
|
|
* @param {Phaser.Types.Tilemaps.TiledObject} [obj] - The Tiled object for fallback.
|
2021-05-20 20:00:51 +00:00
|
|
|
*/
|
2021-05-13 20:23:16 +00:00
|
|
|
setTextureAndFrame: function (sprite, key, frame, obj)
|
|
|
|
{
|
2021-05-20 20:00:51 +00:00
|
|
|
if ((key === null) && this.gids && obj.gid !== undefined)
|
2021-05-13 20:23:16 +00:00
|
|
|
{
|
|
|
|
var tileset = this.gids[obj.gid];
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
if (tileset)
|
|
|
|
{
|
|
|
|
if (key === null && tileset.image !== undefined)
|
|
|
|
{
|
|
|
|
key = tileset.image.key;
|
|
|
|
}
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
if (frame === null)
|
|
|
|
{
|
2021-05-20 20:00:51 +00:00
|
|
|
// This relies on the tileset texture *also* having been loaded as a spritesheet. This isn't guaranteed!
|
2021-05-13 20:23:16 +00:00
|
|
|
frame = obj.gid - tileset.firstgid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we can't satisfy the request, probably best to null it out rather than set a whole spritesheet or something.
|
|
|
|
if (!sprite.scene.textures.getFrame(key, frame))
|
|
|
|
{
|
|
|
|
key = null;
|
|
|
|
frame = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
sprite.setTexture(key, frame);
|
|
|
|
},
|
|
|
|
|
2021-05-20 20:00:51 +00:00
|
|
|
/**
|
|
|
|
* Sets the `sprite.data` field from the tiled properties on the object and its tile (if any).
|
2021-09-23 15:02:52 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tilemaps.ObjectHelper#setPropertiesFromTiledObject
|
|
|
|
* @since 3.60.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.GameObject} sprite
|
|
|
|
* @param {Phaser.Types.Tilemaps.TiledObject} obj
|
2021-05-20 20:00:51 +00:00
|
|
|
*/
|
|
|
|
setPropertiesFromTiledObject: function (sprite, obj)
|
2021-05-13 20:23:16 +00:00
|
|
|
{
|
|
|
|
if (this.gids !== undefined && obj.gid !== undefined)
|
|
|
|
{
|
|
|
|
var tileset = this.gids[obj.gid];
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-13 20:23:16 +00:00
|
|
|
if (tileset !== undefined)
|
|
|
|
{
|
2021-09-23 15:02:52 +00:00
|
|
|
this.setFromJSON(sprite, tileset.getTileProperties(obj.gid));
|
2021-05-13 20:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-23 15:02:52 +00:00
|
|
|
|
|
|
|
this.setFromJSON(sprite, obj.properties);
|
2021-05-20 20:00:51 +00:00
|
|
|
},
|
2021-05-13 20:23:16 +00:00
|
|
|
|
2021-05-20 20:00:51 +00:00
|
|
|
/**
|
2021-10-06 20:39:44 +00:00
|
|
|
* Sets the sprite data from the JSON object.
|
2021-09-23 15:02:52 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tilemaps.ObjectHelper#setFromJSON
|
|
|
|
* @since 3.60.0
|
|
|
|
* @private
|
|
|
|
*
|
2021-05-20 20:00:51 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} sprite - The object for which to populate `data`.
|
2021-10-13 22:58:32 +00:00
|
|
|
* @param {(Object.<string, *>|Object[])} properties - The properties to set in either JSON object format or else a list of objects with `name` and `value` fields.
|
2021-05-20 20:00:51 +00:00
|
|
|
*/
|
2021-09-23 15:02:52 +00:00
|
|
|
setFromJSON: function (sprite, properties)
|
2021-05-20 20:00:51 +00:00
|
|
|
{
|
|
|
|
if (!properties)
|
2021-05-13 20:23:16 +00:00
|
|
|
{
|
2021-05-20 20:00:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-20 20:00:51 +00:00
|
|
|
if (Array.isArray(properties))
|
|
|
|
{
|
|
|
|
// Tiled objects custom properties format
|
|
|
|
properties.forEach(function (propData)
|
2021-05-13 20:23:16 +00:00
|
|
|
{
|
2021-05-20 20:00:51 +00:00
|
|
|
sprite.setData(propData.name, propData.value);
|
|
|
|
});
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-20 20:00:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-09-23 15:02:52 +00:00
|
|
|
|
2021-05-20 20:00:51 +00:00
|
|
|
for (var key in properties)
|
|
|
|
{
|
2023-03-22 16:41:13 +00:00
|
|
|
if (sprite[key] !== undefined)
|
|
|
|
{
|
|
|
|
sprite[key] = properties[key];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprite.setData(key, properties[key]);
|
|
|
|
}
|
2021-05-13 20:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = ObjectHelper;
|