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
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Description} sprite - Description.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.sprite = new PIXI.Sprite(this.texture);
|
2013-09-12 01:18:23 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
/**
|
|
|
|
* @property {array} mapData - Description.
|
|
|
|
*/
|
2013-09-11 01:57:36 +00:00
|
|
|
this.mapData = [];
|
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
|
|
|
|
|
|
|
this.widthInTiles = 0;
|
|
|
|
this.heightInTiles = 0;
|
|
|
|
|
|
|
|
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-09-11 01:57:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.TilemapLayer.prototype = {
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
create: function (width, height) {
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
this.mapData = [];
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
var data;
|
|
|
|
|
|
|
|
for (var y = 0; y < height; y++)
|
2013-09-11 01:57:36 +00:00
|
|
|
{
|
2013-10-11 03:42:11 +00:00
|
|
|
this.mapData[y] = [];
|
|
|
|
|
|
|
|
for (var x = 0; x < width; x++)
|
2013-09-11 01:57:36 +00:00
|
|
|
{
|
2013-10-11 03:42:11 +00:00
|
|
|
this.mapData[y][x] = 0;
|
2013-09-11 01:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
this.widthInTiles = width;
|
|
|
|
this.heightInTiles = height;
|
|
|
|
|
2013-09-11 01:57:36 +00:00
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-09-11 01:57:36 +00:00
|
|
|
* Set a specific tile with its x and y in tiles.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @method putTile
|
|
|
|
* @param {number} x - X position of this tile.
|
|
|
|
* @param {number} y - Y position of this tile.
|
|
|
|
* @param {number} index - The index of this tile type in the core map data.
|
2013-09-11 01:57:36 +00:00
|
|
|
*/
|
|
|
|
putTile: function (x, y, index) {
|
|
|
|
|
|
|
|
if (y >= 0 && y < this.mapData.length)
|
|
|
|
{
|
|
|
|
if (x >= 0 && x < this.mapData[y].length)
|
|
|
|
{
|
|
|
|
this.mapData[y][x] = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
|
|
|
|
if (this.visible == false)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Work out how many tiles we can fit into our canvas and round it up for the edges
|
|
|
|
this._maxX = this.game.math.ceil(this.canvas.width / this.tileWidth) + 1;
|
|
|
|
this._maxY = this.game.math.ceil(this.canvas.height / this.tileHeight) + 1;
|
|
|
|
|
|
|
|
// And now work out where in the tilemap the camera actually is
|
|
|
|
this._startX = this.game.math.floor(this.game.camera.x / this.tileWidth);
|
|
|
|
this._startY = this.game.math.floor(this.game.camera.y / this.tileHeight);
|
|
|
|
|
|
|
|
// Tilemap bounds check
|
|
|
|
if (this._startX < 0)
|
|
|
|
{
|
|
|
|
this._startX = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._startY < 0)
|
|
|
|
{
|
|
|
|
this._startY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._maxX > this.widthInTiles)
|
|
|
|
{
|
|
|
|
this._maxX = this.widthInTiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._maxY > this.heightInTiles)
|
|
|
|
{
|
|
|
|
this._maxY = this.heightInTiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._startX + this._maxX > this.widthInTiles)
|
|
|
|
{
|
|
|
|
this._startX = this.widthInTiles - this._maxX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._startY + this._maxY > this.heightInTiles)
|
|
|
|
{
|
|
|
|
this._startY = this.heightInTiles - this._maxY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally get the offset to avoid the blocky movement
|
|
|
|
this._dx = -(this.game.camera.x - (this._startX * this.tileWidth));
|
|
|
|
this._dy = -(this.game.camera.y - (this._startY * this.tileHeight));
|
|
|
|
|
|
|
|
this._tx = this._dx;
|
|
|
|
this._ty = this._dy;
|
|
|
|
|
|
|
|
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
|
|
|
|
|
for (var row = this._startY; row < this._startY + this._maxY; row++)
|
|
|
|
{
|
|
|
|
this._columnData = this.mapData[row];
|
|
|
|
|
|
|
|
for (var tile = this._startX; tile < this._startX + this._maxX; tile++)
|
|
|
|
{
|
|
|
|
if (this.tileset.checkTileIndex(this._columnData[tile]))
|
|
|
|
{
|
|
|
|
this.context.drawImage(
|
|
|
|
this.tileset.image,
|
|
|
|
this.tileset.tiles[this._columnData[tile]].x,
|
|
|
|
this.tileset.tiles[this._columnData[tile]].y,
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight,
|
|
|
|
this._tx,
|
|
|
|
this._ty,
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
dump: function () {
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
var txt = '';
|
|
|
|
var args = [''];
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
for (var y = 0; y < this.heightInTiles; y++)
|
2013-09-11 01:57:36 +00:00
|
|
|
{
|
2013-10-11 03:42:11 +00:00
|
|
|
for (var x = 0; x < this.widthInTiles; x++)
|
2013-09-12 14:39:52 +00:00
|
|
|
{
|
2013-10-11 03:42:11 +00:00
|
|
|
txt += "%c ";
|
2013-09-11 01:57:36 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
if (this.mapData[y][x] > 0)
|
2013-09-11 01:57:36 +00:00
|
|
|
{
|
2013-10-11 03:42:11 +00:00
|
|
|
args.push("background: rgb(50, 50, 50)");
|
2013-09-11 01:57:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-11 03:42:11 +00:00
|
|
|
args.push("background: rgb(0, 0, 0)");
|
2013-09-11 01:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
txt += "\n";
|
2013-09-11 01:57:36 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
args[0] = txt;
|
|
|
|
console.log.apply(console, args);
|
2013-09-11 01:57:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2013-09-12 03:24:01 +00:00
|
|
|
|