2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Tilemap Layer is a set of map data combined with a Tileset in order to render that data to the game.
|
|
|
|
*
|
|
|
|
* @class Phaser.TilemapLayer
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - Game reference to the currently running game.
|
|
|
|
* @param {number} x - The x coordinate of this layer.
|
|
|
|
* @param {number} y - The y coordinate of this layer.
|
2013-12-17 05:07:00 +00:00
|
|
|
* @param {number} renderWidth - Width of the renderable area of the layer.
|
|
|
|
* @param {number} renderHeight - Height of the renderable area of the layer.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @param {Phaser.Tileset|string} tileset - The tile set used for rendering.
|
|
|
|
* @param {Phaser.Tilemap} tilemap - The tilemap to which this layer belongs.
|
2013-12-10 12:23:42 +00:00
|
|
|
* @param {number|string} [layer=0] - The layer within the tilemap this TilemapLayer represents.
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2013-10-14 18:37:44 +00:00
|
|
|
Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, tileset, tilemap, layer) {
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* @property {Phaser.Game} game - A reference to the currently running Game.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.game = game;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2013-12-17 05:07:00 +00:00
|
|
|
* @property {HTMLCanvasElement} canvas - The canvas to which this TilemapLayer draws.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.canvas = Phaser.Canvas.create(renderWidth, renderHeight);
|
2013-10-01 12:54:29 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* @property {CanvasRenderingContext2D} context - The 2d context of the canvas.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.context = this.canvas.getContext('2d');
|
2013-10-01 12:54:29 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* @property {PIXI.BaseTexture} baseTexture - Required Pixi var.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.baseTexture = new PIXI.BaseTexture(this.canvas);
|
2013-10-01 12:54:29 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* @property {PIXI.Texture} texture - Required Pixi var.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.texture = new PIXI.Texture(this.baseTexture);
|
2013-10-01 12:54:29 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Frame} textureFrame - Dimensions of the renderable area.
|
|
|
|
*/
|
2013-12-10 12:23:42 +00:00
|
|
|
this.textureFrame = new Phaser.Frame(0, 0, 0, renderWidth, renderHeight, 'tilemapLayer', game.rnd.uuid());
|
2013-10-11 17:18:27 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
Phaser.Sprite.call(this, this.game, x, y, this.texture, this.textureFrame);
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @property {number} type - The const type of this object.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-10-16 05:33:39 +00:00
|
|
|
this.type = Phaser.TILEMAPLAYER;
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
2013-12-17 05:07:00 +00:00
|
|
|
* An object that is fixed to the camera ignores the position of any ancestors in the display list and uses its x/y coordinates as offsets from the top left of the camera.
|
|
|
|
* @property {boolean} fixedToCamera - Fixes this object to the Camera.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-10-16 05:33:39 +00:00
|
|
|
this.fixedToCamera = true;
|
2013-10-14 18:37:44 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Point} cameraOffset - If this object is fixed to the camera then use this Point to specify how far away from the Camera x/y it's rendered.
|
|
|
|
*/
|
|
|
|
this.cameraOffset = new Phaser.Point(x, y);
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* @property {Phaser.Tileset} tileset - The tile set used for rendering.
|
2013-10-11 03:42:11 +00:00
|
|
|
*/
|
2013-10-11 05:30:28 +00:00
|
|
|
this.tileset = null;
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @property {number} tileWidth - The width of a single tile in pixels.
|
|
|
|
*/
|
2013-10-11 05:30:28 +00:00
|
|
|
this.tileWidth = 0;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} tileHeight - The height of a single tile in pixels.
|
|
|
|
*/
|
2013-10-11 05:30:28 +00:00
|
|
|
this.tileHeight = 0;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} tileMargin - The margin around the tiles.
|
|
|
|
*/
|
2013-10-16 20:25:51 +00:00
|
|
|
this.tileMargin = 0;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} tileSpacing - The spacing around the tiles.
|
|
|
|
*/
|
2013-10-16 20:25:51 +00:00
|
|
|
this.tileSpacing = 0;
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} debug - If set to true the collideable tile edges path will be rendered.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.debug = false;
|
|
|
|
|
2013-12-18 00:44:04 +00:00
|
|
|
/**
|
|
|
|
* @property {number} debugAlpha - If debug is true then the tileset is rendered with this alpha level, to make the tile edges clearer.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.debugAlpha = 0.5;
|
|
|
|
|
2013-10-11 19:02:12 +00:00
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* @property {number} widthInPixels - Do NOT recommend changing after the map is loaded!
|
|
|
|
* @readonly
|
2013-10-11 19:02:12 +00:00
|
|
|
*/
|
|
|
|
this.widthInPixels = 0;
|
|
|
|
|
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* @property {number} heightInPixels - Do NOT recommend changing after the map is loaded!
|
|
|
|
* @readonly
|
2013-10-11 19:02:12 +00:00
|
|
|
*/
|
|
|
|
this.heightInPixels = 0;
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @property {number} renderWidth - The width of the area being rendered.
|
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.renderWidth = renderWidth;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} renderHeight - The height of the area being rendered.
|
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.renderHeight = renderHeight;
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 05:30:28 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _ga - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._ga = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _dx - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._dx = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _dy - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._dy = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _dw - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._dw = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _dh - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._dh = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _tx - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tx = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _ty - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._ty = 0;
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _tw - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-15 03:28:16 +00:00
|
|
|
this._tw = 0;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _th - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-15 03:28:16 +00:00
|
|
|
this._th = 0;
|
2013-10-11 05:30:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _tl - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tl = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _maxX - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._maxX = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _maxY - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._maxY = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _startX - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._startX = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _startY - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._startY = 0;
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @property {array} _results - Local render loop var to help avoid gc spikes.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._results = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _x - Private var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._x = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _y - Private var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._y = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _prevX - Private var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._prevX = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _prevY - Private var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._prevY = 0;
|
|
|
|
|
2013-11-07 18:44:04 +00:00
|
|
|
/**
|
|
|
|
* @property {number} scrollFactorX - speed at which this layer scrolls
|
2013-11-25 04:40:04 +00:00
|
|
|
* horizontally, relative to the camera (e.g. scrollFactorX of 0.5 scrolls
|
|
|
|
* half as quickly as the 'normal' camera-locked layers do)
|
|
|
|
* @default 1
|
2013-11-07 18:44:04 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.scrollFactorX = 1;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
2013-11-07 18:44:04 +00:00
|
|
|
/**
|
|
|
|
* @property {number} scrollFactorY - speed at which this layer scrolls
|
2013-11-25 04:40:04 +00:00
|
|
|
* vertically, relative to the camera (e.g. scrollFactorY of 0.5 scrolls
|
|
|
|
* half as quickly as the 'normal' camera-locked layers do)
|
|
|
|
* @default 1
|
2013-11-07 18:44:04 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.scrollFactorY = 1;
|
2013-11-07 18:44:04 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Tilemap} tilemap - The Tilemap to which this layer is bound.
|
|
|
|
*/
|
2013-10-14 18:37:44 +00:00
|
|
|
this.tilemap = null;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-10 12:23:42 +00:00
|
|
|
* @property {object} layer - The layer object within the Tilemap that this layer represents.
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2013-10-14 18:37:44 +00:00
|
|
|
this.layer = null;
|
2013-10-11 17:18:27 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
2013-12-10 12:23:42 +00:00
|
|
|
* @property {number} index - The index of this layer within the Tilemap.
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
|
|
|
this.index = 0;
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} dirty - Flag controlling when to re-render the layer.
|
|
|
|
*/
|
2013-10-11 17:18:27 +00:00
|
|
|
this.dirty = true;
|
|
|
|
|
2013-10-14 18:37:44 +00:00
|
|
|
if (tileset instanceof Phaser.Tileset || typeof tileset === 'string')
|
|
|
|
{
|
|
|
|
this.updateTileset(tileset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tilemap instanceof Phaser.Tilemap)
|
|
|
|
{
|
|
|
|
this.updateMapData(tilemap, layer);
|
|
|
|
}
|
|
|
|
|
2013-09-11 01:57:36 +00:00
|
|
|
};
|
|
|
|
|
2013-11-01 02:07:21 +00:00
|
|
|
Phaser.TilemapLayer.prototype = Object.create(Phaser.Sprite.prototype);
|
|
|
|
Phaser.TilemapLayer.prototype = Phaser.Utils.extend(true, Phaser.TilemapLayer.prototype, Phaser.Sprite.prototype, PIXI.Sprite.prototype);
|
2013-10-16 05:33:39 +00:00
|
|
|
Phaser.TilemapLayer.prototype.constructor = Phaser.TilemapLayer;
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
2013-12-01 01:40:24 +00:00
|
|
|
* Automatically called by World.postUpdate. Handles cache updates.
|
2013-11-27 16:33:49 +00:00
|
|
|
*
|
2013-12-01 01:40:24 +00:00
|
|
|
* @method Phaser.TilemapLayer#postUpdate
|
2013-11-27 16:33:49 +00:00
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
*/
|
2013-12-01 01:40:24 +00:00
|
|
|
Phaser.TilemapLayer.prototype.postUpdate = function () {
|
2013-10-16 01:09:12 +00:00
|
|
|
|
2013-12-05 18:12:16 +00:00
|
|
|
Phaser.Sprite.prototype.postUpdate.call(this);
|
2013-12-01 01:40:24 +00:00
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
// Stops you being able to auto-scroll the camera if it's not following a sprite
|
2013-11-07 18:44:04 +00:00
|
|
|
this.scrollX = this.game.camera.x * this.scrollFactorX;
|
|
|
|
this.scrollY = this.game.camera.y * this.scrollFactorY;
|
2013-10-16 01:09:12 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this.render();
|
2013-10-16 01:09:12 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
}
|
2013-10-16 01:09:12 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Sets the world size to match the size of this layer.
|
|
|
|
*
|
|
|
|
* @method Phaser.TilemapLayer#resizeWorld
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
*/
|
2013-10-16 05:33:39 +00:00
|
|
|
Phaser.TilemapLayer.prototype.resizeWorld = function () {
|
2013-10-16 01:09:12 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this.game.world.setBounds(0, 0, this.widthInPixels, this.heightInPixels);
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
}
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Updates the Tileset data.
|
|
|
|
*
|
|
|
|
* @method Phaser.TilemapLayer#updateTileset
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @param {Phaser.Tileset|string} tileset - The tileset to use for this layer.
|
|
|
|
*/
|
2013-10-16 05:33:39 +00:00
|
|
|
Phaser.TilemapLayer.prototype.updateTileset = function (tileset) {
|
2013-10-16 02:36:53 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
if (tileset instanceof Phaser.Tileset)
|
|
|
|
{
|
|
|
|
this.tileset = tileset;
|
|
|
|
}
|
|
|
|
else if (typeof tileset === 'string')
|
|
|
|
{
|
|
|
|
this.tileset = this.game.cache.getTileset('tiles');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this.tileWidth = this.tileset.tileWidth;
|
|
|
|
this.tileHeight = this.tileset.tileHeight;
|
2013-10-16 20:25:51 +00:00
|
|
|
this.tileMargin = this.tileset.tileMargin;
|
|
|
|
this.tileSpacing = this.tileset.tileSpacing;
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this.updateMax();
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
}
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Updates the Tilemap data.
|
|
|
|
*
|
|
|
|
* @method Phaser.TilemapLayer#updateMapData
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @param {Phaser.Tilemap} tilemap - The tilemap to which this layer belongs.
|
|
|
|
* @param {number} layer - The layer index within the map.
|
|
|
|
*/
|
2013-10-16 05:33:39 +00:00
|
|
|
Phaser.TilemapLayer.prototype.updateMapData = function (tilemap, layer) {
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
if (tilemap instanceof Phaser.Tilemap)
|
|
|
|
{
|
2013-12-10 12:23:42 +00:00
|
|
|
if (typeof layer === 'undefined')
|
|
|
|
{
|
|
|
|
layer = 0;
|
|
|
|
}
|
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this.tilemap = tilemap;
|
|
|
|
this.layer = this.tilemap.layers[layer];
|
2013-12-05 18:12:16 +00:00
|
|
|
this.tileWidth = this.layer.tileWidth;
|
|
|
|
this.tileHeight = this.layer.tileHeight;
|
2013-10-16 20:25:51 +00:00
|
|
|
this.index = layer;
|
2013-10-16 05:33:39 +00:00
|
|
|
this.updateMax();
|
2013-12-04 17:42:25 +00:00
|
|
|
this.tilemap.layers[layer].dirty = true;
|
2013-10-16 05:33:39 +00:00
|
|
|
}
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
}
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-11-07 18:44:04 +00:00
|
|
|
/**
|
2013-12-09 16:40:48 +00:00
|
|
|
* Take an x coordinate that doesn't account for scrollFactorX and 'fix' it
|
2013-11-27 16:33:49 +00:00
|
|
|
* into a scrolled local space. Used primarily internally
|
|
|
|
* @method Phaser.TilemapLayer#_fixX
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @private
|
|
|
|
* @param {number} x - x coordinate in camera space
|
|
|
|
* @return {number} x coordinate in scrollFactor-adjusted dimensions
|
|
|
|
*/
|
2013-11-13 20:57:09 +00:00
|
|
|
Phaser.TilemapLayer.prototype._fixX = function(x) {
|
|
|
|
|
2013-12-09 16:40:48 +00:00
|
|
|
if (x < 0)
|
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this.scrollFactorX === 1)
|
2013-11-13 20:57:09 +00:00
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:40:48 +00:00
|
|
|
return this._x + (x - (this._x / this.scrollFactorX));
|
2013-11-13 20:57:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-07 18:44:04 +00:00
|
|
|
/**
|
2013-12-09 16:40:48 +00:00
|
|
|
* Take an x coordinate that _does_ account for scrollFactorX and 'unfix' it
|
2013-11-27 16:33:49 +00:00
|
|
|
* back to camera space. Used primarily internally
|
|
|
|
* @method Phaser.TilemapLayer#_unfixX
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @private
|
|
|
|
* @param {number} x - x coordinate in scrollFactor-adjusted dimensions
|
|
|
|
* @return {number} x coordinate in camera space
|
|
|
|
*/
|
2013-11-13 20:57:09 +00:00
|
|
|
Phaser.TilemapLayer.prototype._unfixX = function(x) {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this.scrollFactorX === 1)
|
2013-11-13 20:57:09 +00:00
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:40:48 +00:00
|
|
|
return (this._x / this.scrollFactorX) + (x - this._x);
|
2013-11-13 20:57:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-07 18:44:04 +00:00
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* Take a y coordinate that doesn't account for scrollFactorY and 'fix' it
|
|
|
|
* into a scrolled local space. Used primarily internally
|
|
|
|
* @method Phaser.TilemapLayer#_fixY
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @private
|
|
|
|
* @param {number} y - y coordinate in camera space
|
|
|
|
* @return {number} y coordinate in scrollFactor-adjusted dimensions
|
|
|
|
*/
|
2013-11-13 20:57:09 +00:00
|
|
|
Phaser.TilemapLayer.prototype._fixY = function(y) {
|
|
|
|
|
2013-12-09 16:40:48 +00:00
|
|
|
if (y < 0)
|
|
|
|
{
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this.scrollFactorY === 1)
|
2013-11-13 20:57:09 +00:00
|
|
|
{
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:40:48 +00:00
|
|
|
return this._y + (y - (this._y / this.scrollFactorY));
|
2013-11-13 20:57:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-07 18:44:04 +00:00
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* Take a y coordinate that _does_ account for scrollFactorY and 'unfix' it
|
|
|
|
* back to camera space. Used primarily internally
|
|
|
|
* @method Phaser.TilemapLayer#_unfixY
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @private
|
|
|
|
* @param {number} y - y coordinate in scrollFactor-adjusted dimensions
|
|
|
|
* @return {number} y coordinate in camera space
|
|
|
|
*/
|
2013-11-13 20:57:09 +00:00
|
|
|
Phaser.TilemapLayer.prototype._unfixY = function(y) {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this.scrollFactorY === 1)
|
2013-11-13 20:57:09 +00:00
|
|
|
{
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2013-12-09 16:40:48 +00:00
|
|
|
return (this._y / this.scrollFactorY) + (y - this._y);
|
2013-11-13 20:57:09 +00:00
|
|
|
|
|
|
|
}
|
2013-11-07 18:44:04 +00:00
|
|
|
|
2013-10-16 20:25:51 +00:00
|
|
|
/**
|
|
|
|
* Convert a pixel value to a tile coordinate.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @method Phaser.TilemapLayer#getTileX
|
|
|
|
* @memberof Phaser.TilemapLayer
|
2013-10-16 20:25:51 +00:00
|
|
|
* @param {number} x - X position of the point in target tile.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @return {Phaser.Tile} The tile with specific properties.
|
2013-10-16 20:25:51 +00:00
|
|
|
*/
|
|
|
|
Phaser.TilemapLayer.prototype.getTileX = function (x) {
|
|
|
|
|
|
|
|
var tileWidth = this.tileWidth * this.scale.x;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return this.game.math.snapToFloor(this._fixX(x), tileWidth) / tileWidth;
|
2013-10-16 20:25:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a pixel value to a tile coordinate.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @method Phaser.TilemapLayer#getTileY
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @param {number} y - Y position of the point in target tile.
|
|
|
|
* @return {Phaser.Tile} The tile with specific properties.
|
2013-10-16 20:25:51 +00:00
|
|
|
*/
|
|
|
|
Phaser.TilemapLayer.prototype.getTileY = function (y) {
|
|
|
|
|
|
|
|
var tileHeight = this.tileHeight * this.scale.y;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return this.game.math.snapToFloor(this._fixY(y), tileHeight) / tileHeight;
|
2013-10-16 20:25:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Convert a pixel value to a tile coordinate.
|
|
|
|
* @method Phaser.TilemapLayer#getTileXY
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @param {number} x - X position of the point in target tile.
|
|
|
|
* @param {number} y - Y position of the point in target tile.
|
|
|
|
* @return {Phaser.Tile} The tile with specific properties.
|
|
|
|
*/
|
2013-10-16 20:25:51 +00:00
|
|
|
Phaser.TilemapLayer.prototype.getTileXY = function (x, y, point) {
|
|
|
|
|
|
|
|
point.x = this.getTileX(x);
|
|
|
|
point.y = this.getTileY(y);
|
|
|
|
|
|
|
|
return point;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
/**
|
2013-12-17 05:07:00 +00:00
|
|
|
* Get all tiles that exist within the given area, defined by the top-left corner, width and height. Values given are in pixels, not tiles.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @method Phaser.TilemapLayer#getTiles
|
|
|
|
* @memberof Phaser.TilemapLayer
|
2013-12-17 05:07:00 +00:00
|
|
|
* @param {number} x - X position of the top left corner.
|
|
|
|
* @param {number} y - Y position of the top left corner.
|
|
|
|
* @param {number} width - Width of the area to get.
|
|
|
|
* @param {number} height - Height of the area to get.
|
2013-12-05 18:12:16 +00:00
|
|
|
* @param {boolean} [collides=false] - If true only return tiles that collide on one or more faces.
|
2013-10-16 05:33:39 +00:00
|
|
|
* @return {array} Array with tiles informations (each contains x, y, and the tile).
|
|
|
|
*/
|
2013-12-17 05:07:00 +00:00
|
|
|
Phaser.TilemapLayer.prototype.getTiles = function (x, y, width, height, collides) {
|
|
|
|
|
|
|
|
if (this.tilemap === null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should we only get tiles that have at least one of their collision flags set? (true = yes, false = no just get them all)
|
|
|
|
if (typeof collides === 'undefined') { collides = false; }
|
|
|
|
|
|
|
|
// adjust the x,y coordinates for scrollFactor
|
|
|
|
x = this._fixX(x);
|
|
|
|
y = this._fixY(y);
|
|
|
|
|
|
|
|
if (width > this.widthInPixels)
|
|
|
|
{
|
|
|
|
width = this.widthInPixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (height > this.heightInPixels)
|
|
|
|
{
|
|
|
|
height = this.heightInPixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tileWidth = this.tileWidth * this.scale.x;
|
|
|
|
var tileHeight = this.tileHeight * this.scale.y;
|
|
|
|
|
|
|
|
// Convert the pixel values into tile coordinates
|
|
|
|
this._tx = this.game.math.snapToFloor(x, tileWidth) / tileWidth;
|
|
|
|
this._ty = this.game.math.snapToFloor(y, tileHeight) / tileHeight;
|
|
|
|
this._tw = (this.game.math.snapToCeil(width, tileWidth) + tileWidth) / tileWidth;
|
|
|
|
this._th = (this.game.math.snapToCeil(height, tileHeight) + tileHeight) / tileHeight;
|
|
|
|
|
|
|
|
// This should apply the layer x/y here
|
|
|
|
this._results.length = 0;
|
|
|
|
|
|
|
|
// var _index = 0;
|
|
|
|
var _tile = null;
|
|
|
|
// var sx = 0;
|
|
|
|
// var sy = 0;
|
|
|
|
|
|
|
|
for (var wy = this._ty; wy < this._ty + this._th; wy++)
|
|
|
|
{
|
|
|
|
for (var wx = this._tx; wx < this._tx + this._tw; wx++)
|
|
|
|
{
|
|
|
|
if (this.layer.data[wy] && this.layer.data[wy][wx])
|
|
|
|
{
|
|
|
|
_tile = this.layer.data[wy][wx];
|
|
|
|
|
|
|
|
if (_tile)
|
|
|
|
{
|
|
|
|
// sx = this.tileWidth * this.scale.x;
|
|
|
|
// sy = this.tileHeight * this.scale.y;
|
|
|
|
|
|
|
|
if (collides === false || (collides && _tile.collides))
|
|
|
|
{
|
|
|
|
// convert tile coordinates back to camera space for return
|
|
|
|
var _wx = this._unfixX(wx * tileWidth) / this.tileWidth;
|
|
|
|
var _wy = this._unfixY(wy * tileHeight) / this.tileHeight;
|
|
|
|
|
|
|
|
this._results.push({
|
|
|
|
x: _wx * tileWidth,
|
|
|
|
y: _wy * tileHeight,
|
|
|
|
right: (_wx * tileWidth) + tileWidth,
|
|
|
|
bottom: (_wy * tileHeight) + tileHeight,
|
|
|
|
tile: _tile
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._results;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all tiles that exist within the given area, defined by the top-left corner, width and height. Values given are in pixels, not tiles.
|
|
|
|
* This function also draws to the context all of the debug areas.
|
|
|
|
* @method Phaser.TilemapLayer#debugGetTiles
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @param {number} x - X position of the top left corner.
|
|
|
|
* @param {number} y - Y position of the top left corner.
|
|
|
|
* @param {number} width - Width of the area to get.
|
|
|
|
* @param {number} height - Height of the area to get.
|
|
|
|
* @param {boolean} [collides=false] - If true only return tiles that collide on one or more faces.
|
|
|
|
* @return {array} Array with tiles informations (each contains x, y, and the tile).
|
|
|
|
*/
|
|
|
|
Phaser.TilemapLayer.prototype.debugGetTiles = function (x, y, width, height, collides) {
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
if (this.tilemap === null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
// Should we only get tiles that have at least one of their collision flags set? (true = yes, false = no just get them all)
|
|
|
|
if (typeof collides === 'undefined') { collides = false; }
|
2013-12-06 04:34:27 +00:00
|
|
|
if (typeof debug === 'undefined') { debug = false; }
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// adjust the x,y coordinates for scrollFactor
|
2013-12-05 18:12:16 +00:00
|
|
|
x = this._fixX(x);
|
|
|
|
y = this._fixY(y);
|
2013-11-07 18:44:04 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
if (width > this.widthInPixels)
|
|
|
|
{
|
|
|
|
width = this.widthInPixels;
|
|
|
|
}
|
2013-10-16 01:09:12 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
if (height > this.heightInPixels)
|
|
|
|
{
|
|
|
|
height = this.heightInPixels;
|
|
|
|
}
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
console.log('x', x, 'y', y, 'w', width, 'h', height);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this.context.fillStyle = 'rgba(255,0,255,0.5)';
|
2013-12-05 18:12:16 +00:00
|
|
|
// this.context.fillRect(x, y, width, height);
|
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
var tileWidth = this.tileWidth * this.scale.x;
|
|
|
|
var tileHeight = this.tileHeight * this.scale.y;
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
// Convert the pixel values into tile coordinates
|
|
|
|
this._tx = this.game.math.snapToFloor(x, tileWidth) / tileWidth;
|
|
|
|
this._ty = this.game.math.snapToFloor(y, tileHeight) / tileHeight;
|
|
|
|
this._tw = (this.game.math.snapToCeil(width, tileWidth) + tileWidth) / tileWidth;
|
|
|
|
this._th = (this.game.math.snapToCeil(height, tileHeight) + tileHeight) / tileHeight;
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
console.log('tx', this._tx, 'ty', this._ty, 'tw', this._tw, 'th', this._th);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this.context.fillRect(this._tx * tileWidth, this._ty * tileHeight, this._tw * tileWidth, this._th * tileHeight);
|
2013-10-28 10:17:36 +00:00
|
|
|
|
2013-12-05 18:12:16 +00:00
|
|
|
// This should apply the layer x/y here
|
|
|
|
this._results.length = 0;
|
2013-10-16 05:33:39 +00:00
|
|
|
|
|
|
|
var _index = 0;
|
|
|
|
var _tile = null;
|
|
|
|
var sx = 0;
|
|
|
|
var sy = 0;
|
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
this.context.fillStyle = 'rgba(255,0,0,1)';
|
|
|
|
// this.context.strokeStyle = 'rgba(0,0,0,1)';
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
for (var wy = this._ty; wy < this._ty + this._th; wy++)
|
|
|
|
{
|
2013-12-06 04:34:27 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
console.log('wy', wy);
|
|
|
|
}
|
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
for (var wx = this._tx; wx < this._tx + this._tw; wx++)
|
2013-10-15 03:28:16 +00:00
|
|
|
{
|
2013-12-06 04:34:27 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
console.log('wx', wx);
|
|
|
|
}
|
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
if (this.layer.data[wy] && this.layer.data[wy][wx])
|
2013-10-15 03:28:16 +00:00
|
|
|
{
|
2013-10-16 05:33:39 +00:00
|
|
|
// Could combine
|
2013-12-05 18:12:16 +00:00
|
|
|
// _index = this.layer.data[wy][wx] - 1;
|
|
|
|
// _tile = this.tileset.getTile(_index);
|
|
|
|
_tile = this.layer.data[wy][wx];
|
2013-12-06 04:34:27 +00:00
|
|
|
|
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
console.log('tile', _tile);
|
|
|
|
}
|
2013-10-16 05:33:39 +00:00
|
|
|
|
2013-12-05 18:12:16 +00:00
|
|
|
if (_tile)
|
2013-10-15 03:28:16 +00:00
|
|
|
{
|
2013-12-05 18:12:16 +00:00
|
|
|
|
|
|
|
// sx = _tile.width * this.scale.x;
|
|
|
|
// sy = _tile.height * this.scale.y;
|
|
|
|
sx = this.tileWidth * this.scale.x;
|
|
|
|
sy = this.tileHeight * this.scale.y;
|
|
|
|
|
|
|
|
if (collides === false || (collides && _tile.collides))
|
|
|
|
{
|
2013-12-06 04:34:27 +00:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
this.context.fillRect(_tile.x * this.tileWidth, _tile.y * this.tileHeight, this.tileWidth, this.tileHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this.context.strokeRect(_tile.x * this.tileWidth, _tile.y * this.tileHeight, this.tileWidth, this.tileHeight);
|
2013-12-05 18:12:16 +00:00
|
|
|
|
|
|
|
// convert tile coordinates back to camera space for return
|
|
|
|
var _wx = this._unfixX(wx * sx) / this.tileWidth;
|
|
|
|
var _wy = this._unfixY(wy * sy) / this.tileHeight;
|
|
|
|
|
|
|
|
this._results.push({
|
|
|
|
x: _wx * sx,
|
|
|
|
y: _wy * sy,
|
|
|
|
width: sx,
|
|
|
|
height: sy,
|
|
|
|
right: (_wx * sx) + sx,
|
|
|
|
bottom: (_wy * sy) + sy,
|
|
|
|
tx: _wx,
|
|
|
|
ty: _wy,
|
|
|
|
tile: _tile
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-15 03:28:16 +00:00
|
|
|
}
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2013-10-15 03:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-16 05:33:39 +00:00
|
|
|
}
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
return this._results;
|
2013-10-14 18:37:44 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
}
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Internal function to update maximum values.
|
|
|
|
* @method Phaser.TilemapLayer#updateMax
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
*/
|
2013-10-16 05:33:39 +00:00
|
|
|
Phaser.TilemapLayer.prototype.updateMax = function () {
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this._maxX = this.game.math.ceil(this.canvas.width / this.tileWidth) + 1;
|
|
|
|
this._maxY = this.game.math.ceil(this.canvas.height / this.tileHeight) + 1;
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
if (this.layer)
|
|
|
|
{
|
|
|
|
if (this._maxX > this.layer.width)
|
2013-10-11 19:02:12 +00:00
|
|
|
{
|
2013-10-16 05:33:39 +00:00
|
|
|
this._maxX = this.layer.width;
|
2013-10-11 05:30:28 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
if (this._maxY > this.layer.height)
|
|
|
|
{
|
|
|
|
this._maxY = this.layer.height;
|
|
|
|
}
|
2013-10-14 18:37:44 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this.widthInPixels = this.layer.width * this.tileWidth;
|
|
|
|
this.heightInPixels = this.layer.height * this.tileHeight;
|
|
|
|
}
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this.dirty = true;
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
// console.log('updateMax', this._maxX, this._maxY, 'px', this.widthInPixels, this.heightInPixels, 'rwh', this.width, this.height);
|
2013-12-06 04:34:27 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
}
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Renders the tiles to the layer canvas and pushes to the display.
|
|
|
|
* @method Phaser.TilemapLayer#render
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
*/
|
2013-10-16 05:33:39 +00:00
|
|
|
Phaser.TilemapLayer.prototype.render = function () {
|
2013-10-13 00:29:57 +00:00
|
|
|
|
2013-12-05 18:12:16 +00:00
|
|
|
if (this.tilemap && this.tilemap.layers[this.index].dirty)
|
2013-10-16 20:25:51 +00:00
|
|
|
{
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
2013-12-10 12:23:42 +00:00
|
|
|
if (!this.dirty || !this.tilemap || !this.visible)
|
2013-10-16 05:33:39 +00:00
|
|
|
{
|
2013-12-09 16:40:48 +00:00
|
|
|
return;
|
2013-10-16 05:33:39 +00:00
|
|
|
}
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this._prevX = this._dx;
|
|
|
|
this._prevY = this._dy;
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this._dx = -(this._x - (this._startX * this.tileWidth));
|
|
|
|
this._dy = -(this._y - (this._startY * this.tileHeight));
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this._tx = this._dx;
|
|
|
|
this._ty = this._dy;
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
2013-12-17 05:07:00 +00:00
|
|
|
|
|
|
|
if (this.debug)
|
|
|
|
{
|
2013-12-18 00:44:04 +00:00
|
|
|
this.context.globalAlpha = this.debugAlpha;
|
2013-12-17 05:07:00 +00:00
|
|
|
}
|
2013-10-11 17:18:27 +00:00
|
|
|
|
2013-12-05 18:12:16 +00:00
|
|
|
for (var y = this._startY, lenY = this._startY + this._maxY; y < lenY; y++)
|
2013-10-16 05:33:39 +00:00
|
|
|
{
|
|
|
|
this._column = this.layer.data[y];
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-12-05 18:12:16 +00:00
|
|
|
for (var x = this._startX, lenX = this._startX + this._maxX; x < lenX; x++)
|
2013-10-16 05:33:39 +00:00
|
|
|
{
|
2013-12-05 18:12:16 +00:00
|
|
|
var tile = this._column[x];
|
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
if (tile && this.tileset)
|
2013-12-05 18:12:16 +00:00
|
|
|
{
|
2013-12-17 05:07:00 +00:00
|
|
|
this.context.drawImage(
|
|
|
|
this.tileset.image,
|
|
|
|
this.tileset.getTileX(tile.index),
|
|
|
|
this.tileset.getTileY(tile.index),
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight,
|
|
|
|
Math.floor(this._tx),
|
|
|
|
Math.floor(this._ty),
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight
|
|
|
|
);
|
|
|
|
}
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this._tx += this.tileWidth;
|
2013-10-11 05:30:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this._tx = this._dx;
|
|
|
|
this._ty += this.tileHeight;
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
}
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-12-18 00:44:04 +00:00
|
|
|
if (this.debug)
|
|
|
|
{
|
|
|
|
this.context.globalAlpha = 1;
|
|
|
|
this.renderDebug();
|
|
|
|
}
|
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
// Only needed if running in WebGL, otherwise this array will never get cleared down I don't think!
|
2013-12-05 18:12:16 +00:00
|
|
|
if (this.game.renderType === Phaser.WEBGL)
|
2013-10-16 05:33:39 +00:00
|
|
|
{
|
|
|
|
PIXI.texturesToUpdate.push(this.baseTexture);
|
2013-10-11 17:18:27 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
this.dirty = false;
|
|
|
|
|
2013-12-05 18:12:16 +00:00
|
|
|
if (this.tilemap.layers[this.index].dirty)
|
2013-12-04 17:42:25 +00:00
|
|
|
{
|
|
|
|
this.tilemap.layers[this.index].dirty = false;
|
|
|
|
}
|
2013-10-16 20:25:51 +00:00
|
|
|
|
2013-10-16 05:33:39 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-12-18 00:44:04 +00:00
|
|
|
Phaser.TilemapLayer.prototype.renderDebug = function () {
|
|
|
|
|
|
|
|
this._tx = this._dx;
|
|
|
|
this._ty = this._dy;
|
|
|
|
|
|
|
|
this.context.fillStyle = 'rgba(0, 255, 0, 0.3)';
|
|
|
|
this.context.strokeStyle = 'rgb(0, 255, 0)';
|
|
|
|
|
|
|
|
for (var y = this._startY, lenY = this._startY + this._maxY; y < lenY; y++)
|
|
|
|
{
|
|
|
|
this._column = this.layer.data[y];
|
|
|
|
|
|
|
|
for (var x = this._startX, lenX = this._startX + this._maxX; x < lenX; x++)
|
|
|
|
{
|
|
|
|
var tile = this._column[x];
|
|
|
|
|
|
|
|
if (tile && (tile.faceTop || tile.faceBottom || tile.faceLeft || tile.faceRight))
|
|
|
|
{
|
|
|
|
this._tx = Math.floor(this._tx);
|
|
|
|
|
|
|
|
// this.context.fillRect(this._tx, this._ty, this.tileWidth, this.tileHeight);
|
|
|
|
|
|
|
|
this.context.beginPath();
|
|
|
|
|
|
|
|
if (tile.faceTop)
|
|
|
|
{
|
|
|
|
this.context.moveTo(this._tx, this._ty);
|
|
|
|
this.context.lineTo(this._tx + this.tileWidth, this._ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tile.faceBottom)
|
|
|
|
{
|
|
|
|
this.context.moveTo(this._tx, this._ty + this.tileHeight);
|
|
|
|
this.context.lineTo(this._tx + this.tileWidth, this._ty + this.tileHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tile.faceLeft)
|
|
|
|
{
|
|
|
|
this.context.moveTo(this._tx, this._ty);
|
|
|
|
this.context.lineTo(this._tx, this._ty + this.tileHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tile.faceRight)
|
|
|
|
{
|
|
|
|
this.context.moveTo(this._tx + this.tileWidth, this._ty);
|
|
|
|
this.context.lineTo(this._tx + this.tileWidth, this._ty + this.tileHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.context.stroke();
|
|
|
|
// this.context.strokeRect(this._tx, this._ty, this.tileWidth, this.tileHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._tx += this.tileWidth;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this._tx = this._dx;
|
|
|
|
this._ty += this.tileHeight;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Returns the absolute delta x value.
|
|
|
|
* @method Phaser.TilemapLayer#deltaAbsX
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @return {number} Absolute delta X value
|
|
|
|
*/
|
2013-10-11 17:18:27 +00:00
|
|
|
Phaser.TilemapLayer.prototype.deltaAbsX = function () {
|
|
|
|
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
|
|
|
|
}
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Returns the absolute delta y value.
|
|
|
|
* @method Phaser.TilemapLayer#deltaAbsY
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @return {number} Absolute delta Y value
|
|
|
|
*/
|
2013-10-11 17:18:27 +00:00
|
|
|
Phaser.TilemapLayer.prototype.deltaAbsY = function () {
|
|
|
|
return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
|
|
|
|
}
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Returns the delta x value.
|
|
|
|
* @method Phaser.TilemapLayer#deltaX
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @return {number} Delta X value
|
|
|
|
*/
|
2013-10-11 17:18:27 +00:00
|
|
|
Phaser.TilemapLayer.prototype.deltaX = function () {
|
2013-10-13 00:29:57 +00:00
|
|
|
return this._dx - this._prevX;
|
2013-10-11 17:18:27 +00:00
|
|
|
}
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Returns the delta y value.
|
|
|
|
* @method Phaser.TilemapLayer#deltaY
|
|
|
|
* @memberof Phaser.TilemapLayer
|
|
|
|
* @return {number} Delta Y value
|
|
|
|
*/
|
2013-10-11 17:18:27 +00:00
|
|
|
Phaser.TilemapLayer.prototype.deltaY = function () {
|
2013-10-13 00:29:57 +00:00
|
|
|
return this._dy - this._prevY;
|
2013-10-11 17:18:27 +00:00
|
|
|
}
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @name Phaser.TilemapLayer#scrollX
|
|
|
|
* @property {number} scrollX - Scrolls the map horizontally or returns the current x position.
|
|
|
|
*/
|
2013-10-16 05:33:39 +00:00
|
|
|
Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollX", {
|
2013-10-11 17:18:27 +00:00
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this._x;
|
|
|
|
},
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
set: function (value) {
|
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
if (value !== this._x && value >= 0 && this.layer && this.widthInPixels > this.renderWidth)
|
2013-10-11 17:18:27 +00:00
|
|
|
{
|
|
|
|
this._x = value;
|
2013-12-06 04:34:27 +00:00
|
|
|
|
2013-10-11 19:02:12 +00:00
|
|
|
if (this._x > (this.widthInPixels - this.renderWidth))
|
|
|
|
{
|
|
|
|
this._x = this.widthInPixels - this.renderWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._startX = this.game.math.floor(this._x / this.tileWidth);
|
|
|
|
|
|
|
|
if (this._startX < 0)
|
|
|
|
{
|
|
|
|
this._startX = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._startX + this._maxX > this.layer.width)
|
|
|
|
{
|
|
|
|
this._startX = this.layer.width - this._maxX;
|
|
|
|
}
|
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
this.dirty = true;
|
|
|
|
}
|
2013-09-11 01:57:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
});
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @name Phaser.TilemapLayer#scrollY
|
|
|
|
* @property {number} scrollY - Scrolls the map vertically or returns the current y position.
|
|
|
|
*/
|
2013-10-16 05:33:39 +00:00
|
|
|
Object.defineProperty(Phaser.TilemapLayer.prototype, "scrollY", {
|
2013-10-11 17:18:27 +00:00
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this._y;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
2013-12-06 04:34:27 +00:00
|
|
|
if (value !== this._y && value >= 0 && this.layer && this.heightInPixels > this.renderHeight)
|
2013-10-11 17:18:27 +00:00
|
|
|
{
|
|
|
|
this._y = value;
|
2013-10-11 19:02:12 +00:00
|
|
|
|
|
|
|
if (this._y > (this.heightInPixels - this.renderHeight))
|
|
|
|
{
|
|
|
|
this._y = this.heightInPixels - this.renderHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._startY = this.game.math.floor(this._y / this.tileHeight);
|
|
|
|
|
|
|
|
if (this._startY < 0)
|
|
|
|
{
|
|
|
|
this._startY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._startY + this._maxY > this.layer.height)
|
|
|
|
{
|
|
|
|
this._startY = this.layer.height - this._maxY;
|
|
|
|
}
|
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-09-12 03:24:01 +00:00
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
});
|