2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, mapData, tileset) {
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - Description.
|
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.game = game;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Description} canvas - Description.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.canvas = Phaser.Canvas.create(renderWidth, renderHeight);
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Description} context - Description.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.context = this.canvas.getContext('2d');
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Description} baseTexture - Description.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.baseTexture = new PIXI.BaseTexture(this.canvas);
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Description} texture - Description.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.texture = new PIXI.Texture(this.baseTexture);
|
2013-10-01 12:54:29 +00:00
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
this.frame = new Phaser.Frame(0, 0, 0, renderWidth, renderHeight, 'tilemaplayer', game.rnd.uuid());
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {Description} sprite - Description.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-10-11 17:18:27 +00:00
|
|
|
this.sprite = new Phaser.Sprite(this.game, x, y, this.texture, this.frame);
|
2013-10-11 03:42:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Description} tileset - Description.
|
|
|
|
*/
|
2013-10-11 05:30:28 +00:00
|
|
|
this.tileset = null;
|
|
|
|
|
|
|
|
this.tileWidth = 0;
|
|
|
|
this.tileHeight = 0;
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-10-11 19:02:12 +00:00
|
|
|
/**
|
|
|
|
* Read-only variable, do NOT recommend changing after the map is loaded!
|
|
|
|
* @property {number} widthInPixels
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.widthInPixels = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read-only variable, do NOT recommend changing after the map is loaded!
|
|
|
|
* @property {number} heightInPixels
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.heightInPixels = 0;
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
|
|
|
|
this.renderWidth = renderWidth;
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-10-11 17:18:27 +00:00
|
|
|
this.tilemap;
|
|
|
|
this.layer;
|
|
|
|
|
|
|
|
this._x = 0;
|
|
|
|
this._y = 0;
|
|
|
|
this._prevX = 0;
|
|
|
|
this._prevY = 0;
|
|
|
|
this.dirty = true;
|
|
|
|
|
2013-09-11 01:57:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.TilemapLayer.prototype = {
|
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
updateMapData: function (tilemap, layerID) {
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
this.tilemap = tilemap;
|
|
|
|
this.layer = this.tilemap.layers[layerID];
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-10-11 19:02:12 +00:00
|
|
|
this.updateMax();
|
2013-09-11 01:57:36 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-11 05:30:28 +00:00
|
|
|
updateTileset: function (tileset) {
|
|
|
|
|
|
|
|
this.tileset = this.game.cache.getTileset(tileset);
|
|
|
|
this.tileWidth = this.tileset.tileWidth;
|
|
|
|
this.tileHeight = this.tileset.tileHeight;
|
|
|
|
|
2013-10-11 19:02:12 +00:00
|
|
|
this.updateMax();
|
2013-10-11 05:30:28 +00:00
|
|
|
},
|
|
|
|
|
2013-10-11 19:02:12 +00:00
|
|
|
updateMax: function () {
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-11 19:02:12 +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-11 19:02:12 +00:00
|
|
|
if (this.layer)
|
|
|
|
{
|
|
|
|
if (this._maxX > this.layer.width)
|
|
|
|
{
|
|
|
|
this._maxX = this.layer.width;
|
|
|
|
}
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-11 19:02:12 +00:00
|
|
|
if (this._maxY > this.layer.height)
|
|
|
|
{
|
|
|
|
this._maxY = this.layer.height;
|
|
|
|
}
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-11 19:02:12 +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-11 19:02:12 +00:00
|
|
|
},
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-11 19:02:12 +00:00
|
|
|
render: function () {
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-11 19:02:12 +00:00
|
|
|
if (this.visible == false || this.dirty == false)
|
2013-10-11 05:30:28 +00:00
|
|
|
{
|
2013-10-11 19:02:12 +00:00
|
|
|
return;
|
2013-10-11 05:30:28 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 17:18:27 +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
|
|
|
|
|
|
|
this._tx = this._dx;
|
|
|
|
this._ty = this._dy;
|
|
|
|
|
|
|
|
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
for (var y = this._startY; y < this._startY + this._maxY; y++)
|
2013-10-11 05:30:28 +00:00
|
|
|
{
|
2013-10-11 17:18:27 +00:00
|
|
|
this._column = this.layer.data[y];
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
for (var x = this._startX; x < this._startX + this._maxX; x++)
|
2013-10-11 05:30:28 +00:00
|
|
|
{
|
2013-10-11 17:18:27 +00:00
|
|
|
// only -1 on TILED maps, not CSV
|
|
|
|
var tile = this.tileset.tiles[this._column[x]-1];
|
|
|
|
|
|
|
|
// if (this.tileset.checkTileIndex(tile))
|
|
|
|
// {
|
2013-10-11 05:30:28 +00:00
|
|
|
this.context.drawImage(
|
|
|
|
this.tileset.image,
|
2013-10-11 17:18:27 +00:00
|
|
|
tile.x,
|
|
|
|
tile.y,
|
2013-10-11 05:30:28 +00:00
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight,
|
|
|
|
this._tx,
|
|
|
|
this._ty,
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight
|
|
|
|
);
|
2013-10-11 17:18:27 +00:00
|
|
|
// }
|
2013-10-11 05:30:28 +00:00
|
|
|
|
|
|
|
this._tx += this.tileWidth;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this._tx = this._dx;
|
|
|
|
this._ty += this.tileHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only needed if running in WebGL, otherwise this array will never get cleared down I don't think!
|
|
|
|
if (this.game.renderType == Phaser.WEBGL)
|
|
|
|
{
|
|
|
|
PIXI.texturesToUpdate.push(this.baseTexture);
|
|
|
|
}
|
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
this.dirty = false;
|
|
|
|
|
2013-10-11 05:30:28 +00:00
|
|
|
return true;
|
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2013-10-11 05:30:28 +00:00
|
|
|
|
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-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-10-11 17:18:27 +00:00
|
|
|
Phaser.TilemapLayer.prototype.deltaX = function () {
|
|
|
|
return this._x - this._prevX;
|
|
|
|
}
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
Phaser.TilemapLayer.prototype.deltaY = function () {
|
|
|
|
return this._y - this._prevY;
|
|
|
|
}
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 17:18:27 +00:00
|
|
|
Object.defineProperty(Phaser.TilemapLayer.prototype, "x", {
|
|
|
|
|
|
|
|
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-10-11 19:02:12 +00:00
|
|
|
if (value !== this._x && value >= 0)
|
2013-10-11 17:18:27 +00:00
|
|
|
{
|
|
|
|
this._x = value;
|
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
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(Phaser.TilemapLayer.prototype, "y", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this._y;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
2013-10-11 19:02:12 +00:00
|
|
|
if (value !== this._y && value >= 0)
|
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
|
|
|
});
|