2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2022-02-28 14:29:51 +00:00
|
|
|
* @copyright 2022 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
|
|
|
*/
|
|
|
|
|
2018-02-07 17:10:01 +00:00
|
|
|
var GameObjectFactory = require('../gameobjects/GameObjectFactory');
|
2017-11-17 21:55:12 +00:00
|
|
|
var ParseToTilemap = require('./ParseToTilemap');
|
2017-11-11 16:38:52 +00:00
|
|
|
|
2017-11-29 19:46:29 +00:00
|
|
|
/**
|
|
|
|
* Creates a Tilemap from the given key or data, or creates a blank Tilemap if no key/data provided.
|
2017-11-29 19:53:05 +00:00
|
|
|
* When loading from CSV or a 2D array, you should specify the tileWidth & tileHeight. When parsing
|
|
|
|
* from a map from Tiled, the tileWidth, tileHeight, width & height will be pulled from the map
|
|
|
|
* data. For an empty map, you should specify tileWidth, tileHeight, width & height.
|
2017-11-29 19:46:29 +00:00
|
|
|
*
|
2018-02-07 23:27:01 +00:00
|
|
|
* @method Phaser.GameObjects.GameObjectFactory#tilemap
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2017-11-29 19:46:29 +00:00
|
|
|
* @param {string} [key] - The key in the Phaser cache that corresponds to the loaded tilemap data.
|
2020-11-23 10:22:13 +00:00
|
|
|
* @param {number} [tileWidth=32] - The width of a tile in pixels. Pass in `null` to leave as the
|
2017-11-29 19:46:29 +00:00
|
|
|
* default.
|
2020-11-23 10:22:13 +00:00
|
|
|
* @param {number} [tileHeight=32] - The height of a tile in pixels. Pass in `null` to leave as the
|
2017-11-29 19:46:29 +00:00
|
|
|
* default.
|
2020-11-23 10:22:13 +00:00
|
|
|
* @param {number} [width=10] - The width of the map in tiles. Pass in `null` to leave as the
|
2017-11-29 19:46:29 +00:00
|
|
|
* default.
|
2020-11-23 10:22:13 +00:00
|
|
|
* @param {number} [height=10] - The height of the map in tiles. Pass in `null` to leave as the
|
2017-11-29 19:46:29 +00:00
|
|
|
* default.
|
2020-11-23 10:24:10 +00:00
|
|
|
* @param {number[][]} [data] - Instead of loading from the cache, you can also load directly from
|
2017-11-29 21:07:56 +00:00
|
|
|
* a 2D array of tile indexes. Pass in `null` for no data.
|
2017-11-29 19:46:29 +00:00
|
|
|
* @param {boolean} [insertNull=false] - Controls how empty tiles, tiles with an index of -1, in the
|
|
|
|
* map data are handled. If `true`, empty locations will get a value of `null`. If `false`, empty
|
|
|
|
* location will get a Tile object with an index of -1. If you've a large sparsely populated map and
|
|
|
|
* the tile data doesn't need to change then setting this value to `true` will help with memory
|
|
|
|
* consumption. However if your map is small or you need to update the tiles dynamically, then leave
|
|
|
|
* the default value set.
|
2022-02-28 14:29:51 +00:00
|
|
|
*
|
2018-02-07 23:27:01 +00:00
|
|
|
* @return {Phaser.Tilemaps.Tilemap}
|
2017-11-29 19:46:29 +00:00
|
|
|
*/
|
2017-11-17 21:55:12 +00:00
|
|
|
GameObjectFactory.register('tilemap', function (key, tileWidth, tileHeight, width, height, data, insertNull)
|
2017-11-11 16:38:52 +00:00
|
|
|
{
|
2017-11-29 19:46:29 +00:00
|
|
|
// Allow users to specify null to indicate that they want the default value, since null is
|
|
|
|
// shorter & more legible than undefined. Convert null to undefined to allow ParseToTilemap
|
|
|
|
// defaults to take effect.
|
2018-02-07 23:27:01 +00:00
|
|
|
|
2017-11-17 21:55:12 +00:00
|
|
|
if (key === null) { key = undefined; }
|
|
|
|
if (tileWidth === null) { tileWidth = undefined; }
|
|
|
|
if (tileHeight === null) { tileHeight = undefined; }
|
|
|
|
if (width === null) { width = undefined; }
|
|
|
|
if (height === null) { height = undefined; }
|
2017-11-11 16:38:52 +00:00
|
|
|
|
2017-11-17 21:55:12 +00:00
|
|
|
return ParseToTilemap(this.scene, key, tileWidth, tileHeight, width, height, data, insertNull);
|
2017-11-11 16:38:52 +00:00
|
|
|
});
|
2018-02-07 23:27:01 +00:00
|
|
|
|
|
|
|
// When registering a factory function 'this' refers to the GameObjectFactory context.
|
|
|
|
//
|
|
|
|
// There are several properties available to use:
|
|
|
|
//
|
|
|
|
// this.scene - a reference to the Scene that owns the GameObjectFactory
|
|
|
|
// this.displayList - a reference to the Display List the Scene owns
|
|
|
|
// this.updateList - a reference to the Update List the Scene owns
|