phaser/src/tilemaps/Tile.js

904 lines
27 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
*/
2020-11-23 15:29:08 +00:00
var CONST = require('./const/ORIENTATION_CONST');
2018-02-07 17:10:01 +00:00
var Class = require('../utils/Class');
var Components = require('../gameobjects/components');
var Rectangle = require('../geom/rectangle');
2018-02-07 02:46:11 +00:00
/**
2018-02-07 15:27:21 +00:00
* @classdesc
2018-02-07 02:46:11 +00:00
* A Tile is a representation of a single tile within the Tilemap. This is a lightweight data
2018-04-12 13:25:52 +00:00
* representation, so its position information is stored without factoring in scroll, layer
2018-02-07 02:46:11 +00:00
* scale or layer position.
*
* @class Tile
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Tilemaps
2018-02-07 02:46:11 +00:00
* @constructor
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @extends Phaser.GameObjects.Components.Alpha
* @extends Phaser.GameObjects.Components.Flip
* @extends Phaser.GameObjects.Components.Visible
2018-02-07 02:46:11 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {Phaser.Tilemaps.LayerData} layer - The LayerData object in the Tilemap that this tile belongs to.
2020-11-23 10:22:13 +00:00
* @param {number} index - The unique index of this tile within the map.
* @param {number} x - The x coordinate of this tile in tile coordinates.
* @param {number} y - The y coordinate of this tile in tile coordinates.
* @param {number} width - Width of the tile in pixels.
* @param {number} height - Height of the tile in pixels.
* @param {number} baseWidth - The base width a tile in the map (in pixels). Tiled maps support
2018-02-07 02:46:11 +00:00
* multiple tileset sizes within one map, but they are still placed at intervals of the base
* tile width.
2020-11-23 10:22:13 +00:00
* @param {number} baseHeight - The base height of the tile in pixels (in pixels). Tiled maps
2018-02-07 02:46:11 +00:00
* support multiple tileset sizes within one map, but they are still placed at intervals of the
* base tile height.
*/
var Tile = new Class({
Mixins: [
Components.Alpha,
Components.Flip,
Components.Visible
],
initialize:
function Tile (layer, index, x, y, width, height, baseWidth, baseHeight)
{
2017-11-29 21:37:23 +00:00
/**
* The LayerData in the Tilemap data that this tile belongs to.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#layer
2018-02-07 22:46:07 +00:00
* @type {Phaser.Tilemaps.LayerData}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.layer = layer;
2017-11-29 21:37:23 +00:00
/**
* The index of this tile within the map data corresponding to the tileset, or -1 if this
* represents a blank tile.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#index
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.index = index;
2017-11-29 21:37:23 +00:00
/**
* The x map coordinate of this tile in tile units.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#x
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.x = x;
2017-11-29 21:37:23 +00:00
/**
* The y map coordinate of this tile in tile units.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#y
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.y = y;
2017-11-29 21:37:23 +00:00
/**
* The width of the tile in pixels.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#width
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.width = width;
2017-11-29 21:37:23 +00:00
/**
* The height of the tile in pixels.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#height
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.height = height;
2017-11-29 21:37:23 +00:00
/**
* The right of the tile in pixels.
*
2020-11-26 12:55:40 +00:00
* Set in the `updatePixelXY` method.
*
* @name Phaser.Tilemaps.Tile#right
2020-11-23 10:22:13 +00:00
* @type {number}
* @since 3.50.0
*/
2020-11-26 12:55:40 +00:00
this.right;
/**
* The bottom of the tile in pixels.
*
2020-11-26 12:55:40 +00:00
* Set in the `updatePixelXY` method.
*
* @name Phaser.Tilemaps.Tile#bottom
2020-11-23 10:22:13 +00:00
* @type {number}
* @since 3.50.0
*/
2020-11-26 12:55:40 +00:00
this.bottom;
/**
2020-10-12 10:33:33 +00:00
* The maps base width of a tile in pixels. Tiled maps support multiple tileset sizes
* within one map, but they are still placed at intervals of the base tile size.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#baseWidth
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
this.baseWidth = (baseWidth !== undefined) ? baseWidth : width;
/**
2020-10-12 10:33:33 +00:00
* The maps base height of a tile in pixels. Tiled maps support multiple tileset sizes
* within one map, but they are still placed at intervals of the base tile size.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#baseHeight
2020-11-23 10:22:13 +00:00
* @type {number}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
this.baseHeight = (baseHeight !== undefined) ? baseHeight : height;
/**
* The x coordinate of the top left of this tile in pixels. This is relative to the top left
* of the layer this tile is being rendered within. This property does NOT factor in camera
* scroll, layer scale or layer position.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#pixelX
2018-02-07 22:46:07 +00:00
* @type {number}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
this.pixelX = 0;
/**
* The y coordinate of the top left of this tile in pixels. This is relative to the top left
* of the layer this tile is being rendered within. This property does NOT factor in camera
* scroll, layer scale or layer position.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#pixelY
2018-02-07 22:46:07 +00:00
* @type {number}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
this.pixelY = 0;
this.updatePixelXY();
2017-11-29 21:37:23 +00:00
/**
* Tile specific properties. These usually come from Tiled.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#properties
* @type {any}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
2017-11-16 19:09:07 +00:00
this.properties = {};
2017-11-29 21:37:23 +00:00
/**
* The rotation angle of this tile.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#rotation
2018-02-07 22:46:07 +00:00
* @type {number}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.rotation = 0;
2017-11-29 21:37:23 +00:00
/**
* Whether the tile should collide with any object on the left side.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#collideLeft
2018-02-07 22:46:07 +00:00
* @type {boolean}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.collideLeft = false;
2017-11-29 21:37:23 +00:00
/**
* Whether the tile should collide with any object on the right side.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#collideRight
2018-02-07 22:46:07 +00:00
* @type {boolean}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.collideRight = false;
2017-11-29 21:37:23 +00:00
/**
* Whether the tile should collide with any object on the top side.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#collideUp
2018-02-07 22:46:07 +00:00
* @type {boolean}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.collideUp = false;
2017-11-29 21:37:23 +00:00
/**
* Whether the tile should collide with any object on the bottom side.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#collideDown
2018-02-07 22:46:07 +00:00
* @type {boolean}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.collideDown = false;
2017-11-29 21:37:23 +00:00
/**
2020-10-12 10:33:33 +00:00
* Whether the tiles left edge is interesting for collisions.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#faceLeft
2018-02-07 22:46:07 +00:00
* @type {boolean}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.faceLeft = false;
2017-11-29 21:37:23 +00:00
/**
2020-10-12 10:33:33 +00:00
* Whether the tiles right edge is interesting for collisions.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#faceRight
2018-02-07 22:46:07 +00:00
* @type {boolean}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.faceRight = false;
2017-11-29 21:37:23 +00:00
/**
2020-10-12 10:33:33 +00:00
* Whether the tiles top edge is interesting for collisions.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#faceTop
2018-02-07 22:46:07 +00:00
* @type {boolean}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.faceTop = false;
2017-11-29 21:37:23 +00:00
/**
2020-10-12 10:33:33 +00:00
* Whether the tiles bottom edge is interesting for collisions.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#faceBottom
2018-02-07 22:46:07 +00:00
* @type {boolean}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.faceBottom = false;
2017-11-29 21:37:23 +00:00
/**
* Tile collision callback.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#collisionCallback
2018-02-07 22:46:07 +00:00
* @type {function}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
2020-11-26 12:55:40 +00:00
this.collisionCallback = undefined;
2017-11-29 21:37:23 +00:00
/**
* The context in which the collision callback will be called.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#collisionCallbackContext
2018-02-07 22:46:07 +00:00
* @type {object}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.collisionCallbackContext = this;
2017-11-29 21:37:23 +00:00
/**
* The tint to apply to this tile. Note: tint is currently a single color value instead of
* the 4 corner tint component on other GameObjects.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#tint
2018-02-07 22:46:07 +00:00
* @type {number}
2017-11-29 21:37:23 +00:00
* @default
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
this.tint = 0xffffff;
/**
* An empty object where physics-engine specific information (e.g. bodies) may be stored.
2018-03-20 11:36:35 +00:00
*
2018-02-22 01:38:19 +00:00
* @name Phaser.Tilemaps.Tile#physics
2018-02-07 22:46:07 +00:00
* @type {object}
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
this.physics = {};
2017-11-16 19:09:07 +00:00
},
2017-11-29 21:37:23 +00:00
/**
* Check if the given x and y world coordinates are within this Tile. This does not factor in
* camera scroll, layer scale or layer position.
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#containsPoint
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2017-11-29 21:37:23 +00:00
* @param {number} x - The x coordinate to test.
* @param {number} y - The y coordinate to test.
2018-03-20 11:36:35 +00:00
*
2017-11-29 21:37:23 +00:00
* @return {boolean} True if the coordinates are within this Tile, otherwise false.
*/
containsPoint: function (x, y)
{
return !(x < this.pixelX || y < this.pixelY || x > this.right || y > this.bottom);
2017-11-29 21:37:23 +00:00
},
/**
* Copies the tile data & properties from the given tile to this tile. This copies everything
* except for position and interesting faces.
*
2018-04-16 13:43:24 +00:00
* @method Phaser.Tilemaps.Tile#copy
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {Phaser.Tilemaps.Tile} tile - The tile to copy from.
2018-03-20 11:36:35 +00:00
*
* @return {this} This Tile object instance.
2017-11-29 21:37:23 +00:00
*/
2017-11-16 19:09:07 +00:00
copy: function (tile)
{
this.index = tile.index;
this.alpha = tile.alpha;
this.properties = tile.properties;
this.visible = tile.visible;
this.setFlip(tile.flipX, tile.flipY);
this.tint = tile.tint;
this.rotation = tile.rotation;
this.collideUp = tile.collideUp;
this.collideDown = tile.collideDown;
this.collideLeft = tile.collideLeft;
this.collideRight = tile.collideRight;
this.collisionCallback = tile.collisionCallback;
this.collisionCallbackContext = tile.collisionCallbackContext;
2017-11-16 19:09:07 +00:00
return this;
},
/**
* The collision group for this Tile, defined within the Tileset. This returns a reference to
* the collision group stored within the Tileset, so any modification of the returned object
* will impact all tiles that have the same index as this tile.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#getCollisionGroup
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2020-10-12 10:33:33 +00:00
* @return {?object} The collision group for this Tile, as defined in the Tileset, or `null` if no group was defined.
*/
getCollisionGroup: function ()
{
return this.tileset ? this.tileset.getTileCollisionGroup(this.index) : null;
},
/**
* The tile data for this Tile, defined within the Tileset. This typically contains Tiled
* collision data, tile animations and terrain information. This returns a reference to the tile
* data stored within the Tileset, so any modification of the returned object will impact all
* tiles that have the same index as this tile.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#getTileData
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2020-10-12 10:33:33 +00:00
* @return {?object} The tile data for this Tile, as defined in the Tileset, or `null` if no data was defined.
*/
getTileData: function ()
{
return this.tileset ? this.tileset.getTileData(this.index) : null;
},
/**
2018-02-07 21:58:23 +00:00
* Gets the world X position of the left side of the tile, factoring in the layers position,
* scale and scroll.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#getLeft
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use to perform the check.
2018-03-20 11:36:35 +00:00
*
2020-10-12 10:33:33 +00:00
* @return {number} The left (x) value of this tile.
*/
getLeft: function (camera)
{
var tilemapLayer = this.tilemapLayer;
2018-02-07 21:58:23 +00:00
return (tilemapLayer) ? tilemapLayer.tileToWorldX(this.x, camera) : this.x * this.baseWidth;
},
/**
* Gets the world X position of the right side of the tile, factoring in the layer's position,
* scale and scroll.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#getRight
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use to perform the check.
2018-03-20 11:36:35 +00:00
*
2020-10-12 10:33:33 +00:00
* @return {number} The right (x) value of this tile.
*/
getRight: function (camera)
{
var tilemapLayer = this.tilemapLayer;
2018-02-07 21:58:23 +00:00
return (tilemapLayer) ? this.getLeft(camera) + this.width * tilemapLayer.scaleX : this.getLeft(camera) + this.width;
},
/**
* Gets the world Y position of the top side of the tile, factoring in the layer's position,
* scale and scroll.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#getTop
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use to perform the check.
2018-03-20 11:36:35 +00:00
*
2020-10-12 10:33:33 +00:00
* @return {number} The top (y) value of this tile.
*/
getTop: function (camera)
{
var tilemapLayer = this.tilemapLayer;
// Tiled places tiles on a grid of baseWidth x baseHeight. The origin for a tile in grid
// units is the bottom left, so the y coordinate needs to be adjusted by the difference
// between the base size and this tile's size.
return tilemapLayer
? tilemapLayer.tileToWorldY(this.y, camera) - (this.height - this.baseHeight) * tilemapLayer.scaleY
: this.y * this.baseHeight - (this.height - this.baseHeight);
},
/**
* Gets the world Y position of the bottom side of the tile, factoring in the layer's position,
* scale and scroll.
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#getBottom
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use to perform the check.
2018-03-20 11:36:35 +00:00
*
2020-10-12 10:33:33 +00:00
* @return {number} The bottom (y) value of this tile.
*/
getBottom: function (camera)
{
var tilemapLayer = this.tilemapLayer;
2020-10-12 10:33:33 +00:00
return tilemapLayer
? this.getTop(camera) + this.height * tilemapLayer.scaleY
: this.getTop(camera) + this.height;
},
2018-01-25 19:48:22 +00:00
/**
2018-02-07 21:58:23 +00:00
* Gets the world rectangle bounding box for the tile, factoring in the layers position,
2018-01-25 19:48:22 +00:00
* scale and scroll.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#getBounds
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use to perform the check.
2020-04-27 15:13:40 +00:00
* @param {Phaser.Geom.Rectangle} [output] - Optional Rectangle object to store the results in.
2018-03-20 11:36:35 +00:00
*
2020-10-12 10:33:33 +00:00
* @return {(Phaser.Geom.Rectangle|object)} The bounds of this Tile.
2018-01-25 19:48:22 +00:00
*/
getBounds: function (camera, output)
{
if (output === undefined) { output = new Rectangle(); }
output.x = this.getLeft();
output.y = this.getTop();
output.width = this.getRight() - output.x;
output.height = this.getBottom() - output.y;
2018-02-07 21:58:23 +00:00
2018-01-25 19:48:22 +00:00
return output;
},
/**
* Gets the world X position of the center of the tile, factoring in the layer's position,
* scale and scroll.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#getCenterX
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use to perform the check.
2018-03-20 11:36:35 +00:00
*
2020-10-12 10:33:33 +00:00
* @return {number} The center x position of this Tile.
*/
getCenterX: function (camera)
{
return (this.getLeft(camera) + this.getRight(camera)) / 2;
},
/**
* Gets the world Y position of the center of the tile, factoring in the layer's position,
* scale and scroll.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#getCenterY
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera to use to perform the check.
2018-03-20 11:36:35 +00:00
*
2020-10-12 10:33:33 +00:00
* @return {number} The center y position of this Tile.
*/
getCenterY: function (camera)
{
return (this.getTop(camera) + this.getBottom(camera)) / 2;
},
2017-11-29 21:37:23 +00:00
/**
* Check for intersection with this tile. This does not factor in camera scroll, layer scale or
* layer position.
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#intersects
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2017-11-29 21:37:23 +00:00
* @param {number} x - The x axis in pixels.
* @param {number} y - The y axis in pixels.
* @param {number} right - The right point.
* @param {number} bottom - The bottom point.
2018-03-20 11:36:35 +00:00
*
2020-10-12 10:33:33 +00:00
* @return {boolean} `true` if the Tile intersects with the given dimensions, otherwise `false`.
2017-11-29 21:37:23 +00:00
*/
intersects: function (x, y, right, bottom)
{
2017-11-29 21:37:23 +00:00
return !(
right <= this.pixelX || bottom <= this.pixelY ||
2017-11-29 21:37:23 +00:00
x >= this.right || y >= this.bottom
);
},
2017-11-29 21:37:23 +00:00
/**
* Checks if the tile is interesting.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#isInteresting
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {boolean} collides - If true, will consider the tile interesting if it collides on any side.
* @param {boolean} faces - If true, will consider the tile interesting if it has an interesting face.
2018-03-20 11:36:35 +00:00
*
2018-02-07 23:27:01 +00:00
* @return {boolean} True if the Tile is interesting, otherwise false.
2017-11-29 21:37:23 +00:00
*/
isInteresting: function (collides, faces)
{
2020-10-12 10:33:33 +00:00
if (collides && faces)
{
return (this.canCollide || this.hasInterestingFace);
}
else if (collides)
{
return this.collides;
}
else if (faces)
{
return this.hasInterestingFace;
}
return false;
},
2017-11-29 21:37:23 +00:00
/**
* Reset collision status flags.
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#resetCollision
* @since 3.0.0
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @param {boolean} [recalculateFaces=true] - Whether or not to recalculate interesting faces for this tile and its neighbors.
2018-03-20 11:36:35 +00:00
*
* @return {this} This Tile object instance.
2017-11-29 21:37:23 +00:00
*/
resetCollision: function (recalculateFaces)
{
if (recalculateFaces === undefined) { recalculateFaces = true; }
this.collideLeft = false;
this.collideRight = false;
this.collideUp = false;
this.collideDown = false;
this.faceTop = false;
this.faceBottom = false;
this.faceLeft = false;
this.faceRight = false;
2017-11-29 21:37:23 +00:00
if (recalculateFaces)
{
var tilemapLayer = this.tilemapLayer;
2018-02-07 21:58:23 +00:00
if (tilemapLayer)
{
this.tilemapLayer.calculateFacesAt(this.x, this.y);
}
}
2017-11-29 21:37:23 +00:00
return this;
},
/**
* Reset faces.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#resetFaces
* @since 3.0.0
*
* @return {this} This Tile object instance.
*/
resetFaces: function ()
{
this.faceTop = false;
this.faceBottom = false;
this.faceLeft = false;
this.faceRight = false;
return this;
},
2017-11-29 21:37:23 +00:00
/**
* Sets the collision flags for each side of this tile and updates the interesting faces list.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#setCollision
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*
* @param {boolean} left - Indicating collide with any object on the left.
2018-02-07 21:58:23 +00:00
* @param {boolean} [right] - Indicating collide with any object on the right.
* @param {boolean} [up] - Indicating collide with any object on the top.
* @param {boolean} [down] - Indicating collide with any object on the bottom.
2020-10-12 10:33:33 +00:00
* @param {boolean} [recalculateFaces=true] - Whether or not to recalculate interesting faces for this tile and its neighbors.
2018-03-20 11:36:35 +00:00
*
* @return {this} This Tile object instance.
2017-11-29 21:37:23 +00:00
*/
setCollision: function (left, right, up, down, recalculateFaces)
{
if (right === undefined) { right = left; }
if (up === undefined) { up = left; }
if (down === undefined) { down = left; }
if (recalculateFaces === undefined) { recalculateFaces = true; }
this.collideLeft = left;
this.collideRight = right;
this.collideUp = up;
this.collideDown = down;
this.faceLeft = left;
this.faceRight = right;
this.faceTop = up;
this.faceBottom = down;
2017-11-29 21:37:23 +00:00
if (recalculateFaces)
{
var tilemapLayer = this.tilemapLayer;
2018-02-07 21:58:23 +00:00
if (tilemapLayer)
{
this.tilemapLayer.calculateFacesAt(this.x, this.y);
}
}
2017-11-29 21:37:23 +00:00
return this;
},
2017-11-29 21:37:23 +00:00
/**
* Set a callback to be called when this tile is hit by an object. The callback must true for
* collision processing to take place.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#setCollisionCallback
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*
* @param {function} callback - Callback function.
* @param {object} context - Callback will be called within this context.
2018-03-20 11:36:35 +00:00
*
* @return {this} This Tile object instance.
2017-11-29 21:37:23 +00:00
*/
setCollisionCallback: function (callback, context)
{
2017-11-29 21:37:23 +00:00
if (callback === null)
{
2017-11-29 14:20:24 +00:00
this.collisionCallback = undefined;
this.collisionCallbackContext = undefined;
}
else
{
this.collisionCallback = callback;
this.collisionCallbackContext = context;
}
2017-11-29 21:37:23 +00:00
return this;
},
2017-11-29 21:37:23 +00:00
/**
* Sets the size of the tile and updates its pixelX and pixelY.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#setSize
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*
2020-11-23 10:22:13 +00:00
* @param {number} tileWidth - The width of the tile in pixels.
* @param {number} tileHeight - The height of the tile in pixels.
* @param {number} baseWidth - The base width a tile in the map (in pixels).
* @param {number} baseHeight - The base height of the tile in pixels (in pixels).
2018-03-20 11:36:35 +00:00
*
* @return {this} This Tile object instance.
*/
setSize: function (tileWidth, tileHeight, baseWidth, baseHeight)
{
if (tileWidth !== undefined) { this.width = tileWidth; }
if (tileHeight !== undefined) { this.height = tileHeight; }
if (baseWidth !== undefined) { this.baseWidth = baseWidth; }
if (baseHeight !== undefined) { this.baseHeight = baseHeight; }
this.updatePixelXY();
return this;
},
/**
* Used internally. Updates the tiles world XY position based on the current tile size.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @method Phaser.Tilemaps.Tile#updatePixelXY
* @since 3.0.0
*
* @return {this} This Tile object instance.
2017-11-29 21:37:23 +00:00
*/
updatePixelXY: function ()
{
var orientation = this.layer.orientation;
if (orientation === CONST.ORTHOGONAL)
2020-09-19 08:56:05 +00:00
{
// In orthogonal mode, Tiled places tiles on a grid of baseWidth x baseHeight. The origin for a tile is the
// bottom left, while the Phaser renderer assumes the origin is the top left. The y
// coordinate needs to be adjusted by the difference.
this.pixelX = this.x * this.baseWidth;
this.pixelY = this.y * this.baseHeight;
}
else if (orientation === CONST.ISOMETRIC)
{
// Reminder: For the tilemap to be centered we have to move the image to the right with the camera!
// This is crucial for wordtotile, tiletoworld to work.
2020-09-19 08:56:05 +00:00
this.pixelX = (this.x - this.y) * this.baseWidth * 0.5;
this.pixelY = (this.x + this.y) * this.baseHeight * 0.5;
}
else if (orientation === CONST.STAGGERED)
2020-09-19 08:56:05 +00:00
{
this.pixelX = this.x * this.baseWidth + this.y % 2 * (this.baseWidth / 2);
this.pixelY = this.y * (this.baseHeight / 2);
}
else if (orientation === CONST.HEXAGONAL)
2020-09-19 08:56:05 +00:00
{
2020-10-12 10:33:33 +00:00
var len = this.layer.hexSideLength;
var rowHeight = ((this.baseHeight - len) / 2 + len);
2020-09-19 08:56:05 +00:00
this.pixelX = this.x * this.baseWidth + this.y % 2 * (this.baseWidth / 2);
this.pixelY = this.y * rowHeight;
}
2020-11-26 12:55:40 +00:00
this.right = this.pixelX + this.baseWidth;
this.bottom = this.pixelY + this.baseHeight;
2017-11-29 21:37:23 +00:00
return this;
},
2020-10-12 10:33:33 +00:00
/**
* Clean up memory.
*
* @method Phaser.Tilemaps.Tile#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.collisionCallback = undefined;
this.collisionCallbackContext = undefined;
this.properties = undefined;
},
2017-11-29 21:37:23 +00:00
/**
* True if this tile can collide on any of its faces or has a collision callback set.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @name Phaser.Tilemaps.Tile#canCollide
* @type {boolean}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
canCollide: {
2020-10-12 10:33:33 +00:00
get: function ()
{
2020-11-26 12:55:40 +00:00
return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown || (this.collisionCallback !== undefined));
}
2020-10-12 10:33:33 +00:00
},
2017-11-29 21:37:23 +00:00
/**
* True if this tile can collide on any of its faces.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @name Phaser.Tilemaps.Tile#collides
* @type {boolean}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
collides: {
2020-10-12 10:33:33 +00:00
get: function ()
{
return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown);
}
2020-10-12 10:33:33 +00:00
},
2017-11-29 21:37:23 +00:00
/**
* True if this tile has any interesting faces.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @name Phaser.Tilemaps.Tile#hasInterestingFace
* @type {boolean}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2017-11-29 21:37:23 +00:00
*/
hasInterestingFace: {
2020-10-12 10:33:33 +00:00
get: function ()
{
return (this.faceTop || this.faceBottom || this.faceLeft || this.faceRight);
}
2020-10-12 10:33:33 +00:00
},
/**
* The tileset that contains this Tile. This is null if accessed from a LayerData instance
2020-10-12 10:33:33 +00:00
* before the tile is placed in a TilemapLayer, or if the tile has an index that doesn't correspond
* to any of the maps tilesets.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @name Phaser.Tilemaps.Tile#tileset
* @type {?Phaser.Tilemaps.Tileset}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
tileset: {
2018-11-20 12:33:08 +00:00
get: function ()
{
2018-11-20 12:33:08 +00:00
var tilemapLayer = this.layer.tilemapLayer;
2018-11-20 12:33:08 +00:00
if (tilemapLayer)
{
var tileset = tilemapLayer.gidMap[this.index];
2018-11-20 12:33:08 +00:00
if (tileset)
{
return tileset;
}
}
return null;
}
2018-11-20 12:33:08 +00:00
},
/**
* The tilemap layer that contains this Tile. This will only return null if accessed from a
2020-10-12 10:33:33 +00:00
* LayerData instance before the tile is placed within a TilemapLayer.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @name Phaser.Tilemaps.Tile#tilemapLayer
2020-10-12 10:33:33 +00:00
* @type {?Phaser.Tilemaps.TilemapLayer}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
tilemapLayer: {
2020-10-12 10:33:33 +00:00
get: function ()
{
return this.layer.tilemapLayer;
}
2020-10-12 10:33:33 +00:00
2018-01-25 13:25:14 +00:00
},
/**
* The tilemap that contains this Tile. This will only return null if accessed from a LayerData
2020-10-12 10:33:33 +00:00
* instance before the tile is placed within a TilemapLayer.
2018-03-20 11:36:35 +00:00
*
2018-02-07 21:58:23 +00:00
* @name Phaser.Tilemaps.Tile#tilemap
* @type {?Phaser.Tilemaps.Tilemap}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
2018-01-25 13:25:14 +00:00
*/
tilemap: {
2020-10-12 10:33:33 +00:00
2018-01-25 13:25:14 +00:00
get: function ()
{
var tilemapLayer = this.tilemapLayer;
2020-10-12 10:33:33 +00:00
return tilemapLayer ? tilemapLayer.tilemap : null;
2018-01-25 13:25:14 +00:00
}
2020-10-12 10:33:33 +00:00
}
2018-02-07 21:58:23 +00:00
});
module.exports = Tile;