phaser/src/tilemaps/ObjectHelper.js

243 lines
6.6 KiB
JavaScript
Raw Normal View History

/**
* @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.
*
* @class ObjectHelper
* @memberof Phaser.Tilemaps
* @constructor
2021-09-23 15:02:52 +00:00
* @since 3.60.0
*
* @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-10-06 20:39:44 +00:00
/**
* The Tile GIDs array.
*
* @name Phaser.Tilemaps.ObjectHelper#gids
* @type {array}
* @since 3.60.0
*/
this.gids = [];
2021-09-23 15:02:52 +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
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
*/
this._gids = this.gids;
},
/**
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
*
* @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
*/
enabled: {
2021-09-23 15:02:52 +00:00
get: function ()
{
return !!this.gids;
},
2021-09-23 15:02:52 +00:00
set: function (v)
{
this.gids = v ? this._gids : undefined;
}
2021-10-06 20:39:44 +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`.
*/
getTypeIncludingTile: function (obj)
{
2021-09-23 15:02:52 +00:00
if (obj.type !== undefined && obj.type !== '')
{
return obj.type;
}
2021-09-23 15:02:52 +00:00
if (!this.gids || obj.gid === undefined)
{
return undefined;
}
2021-09-23 15:02:52 +00:00
var tileset = this.gids[obj.gid];
2021-09-23 15:02:52 +00:00
if (!tileset)
{
return undefined;
}
2021-09-23 15:02:52 +00:00
var tileData = tileset.getTileData(obj.gid);
2021-09-23 15:02:52 +00:00
if (!tileData)
{
return undefined;
}
2021-09-23 15:02:52 +00:00
return tileData.type;
},
/**
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.
*/
setTextureAndFrame: function (sprite, key, frame, obj)
{
if ((key === null) && this.gids && obj.gid !== undefined)
{
var tileset = this.gids[obj.gid];
2021-09-23 15:02:52 +00:00
if (tileset)
{
if (key === null && tileset.image !== undefined)
{
key = tileset.image.key;
}
2021-09-23 15:02:52 +00:00
if (frame === null)
{
// This relies on the tileset texture *also* having been loaded as a spritesheet. This isn't guaranteed!
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
sprite.setTexture(key, frame);
},
/**
* 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
*/
setPropertiesFromTiledObject: function (sprite, obj)
{
if (this.gids !== undefined && obj.gid !== undefined)
{
var tileset = this.gids[obj.gid];
2021-09-23 15:02:52 +00:00
if (tileset !== undefined)
{
2021-09-23 15:02:52 +00:00
this.setFromJSON(sprite, tileset.getTileProperties(obj.gid));
}
}
2021-09-23 15:02:52 +00:00
this.setFromJSON(sprite, obj.properties);
},
/**
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
*
* @param {Phaser.GameObjects.GameObject} sprite - The object for which to populate `data`.
* @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-09-23 15:02:52 +00:00
setFromJSON: function (sprite, properties)
{
if (!properties)
{
return;
}
2021-09-23 15:02:52 +00:00
if (Array.isArray(properties))
{
for (var i = 0; i < properties.length; i++)
{
var prop = properties[i];
if (sprite[prop.name] !== undefined)
{
sprite[prop.name] = prop.value;
}
else
{
sprite.setData(prop.name, prop.value);
}
}
2021-09-23 15:02:52 +00:00
return;
}
2021-09-23 15:02:52 +00:00
for (var key in properties)
{
if (sprite[key] !== undefined)
{
sprite[key] = properties[key];
}
else
{
sprite.setData(key, properties[key]);
}
}
}
});
module.exports = ObjectHelper;