mirror of
https://github.com/wassname/phaser.git
synced 2026-06-29 16:30:29 +08:00
New examples HTML page added.
This commit is contained in:
@@ -1,183 +0,0 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2013 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
* @module Phaser.Tile
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Create a new <code>Tile</code>.
|
||||
*
|
||||
* @class Phaser.Tile
|
||||
* @classdesc A Tile is a single representation of a tile within a Tilemap.
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - A reference to the currently running game.
|
||||
* @param {Tilemap} tilemap - The tilemap this tile belongs to.
|
||||
* @param {number} index - The index of this tile type in the core map data.
|
||||
* @param {number} width - Width of the tile.
|
||||
* @param {number} height - Height of the tile.
|
||||
*/
|
||||
Phaser.Tile = function (game, tilemap, index, width, height) {
|
||||
|
||||
/**
|
||||
* @property {number} mass - The virtual mass of the tile.
|
||||
* @default
|
||||
*/
|
||||
this.mass = 1.0;
|
||||
|
||||
/**
|
||||
* @property {boolean} collideNone - Indicating this Tile doesn't collide at all.
|
||||
* @default
|
||||
*/
|
||||
this.collideNone = true;
|
||||
|
||||
/**
|
||||
* @property {boolean} collideLeft - Indicating collide with any object on the left.
|
||||
* @default
|
||||
*/
|
||||
this.collideLeft = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} collideRight - Indicating collide with any object on the right.
|
||||
* @default
|
||||
*/
|
||||
this.collideRight = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} collideUp - Indicating collide with any object on the top.
|
||||
* @default
|
||||
*/
|
||||
this.collideUp = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} collideDown - Indicating collide with any object on the bottom.
|
||||
* @default
|
||||
*/
|
||||
this.collideDown = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} separateX - Enable separation at x-axis.
|
||||
* @default
|
||||
*/
|
||||
this.separateX = true;
|
||||
|
||||
/**
|
||||
* @property {boolean} separateY - Enable separation at y-axis.
|
||||
* @default
|
||||
*/
|
||||
this.separateY = true;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {boolean} tilemap - The tilemap this tile belongs to.
|
||||
*/
|
||||
this.tilemap = tilemap;
|
||||
|
||||
/**
|
||||
* @property {number} index - The index of this tile type in the core map data.
|
||||
*/
|
||||
this.index = index;
|
||||
|
||||
/**
|
||||
* @property {number} width - The width of the tile.
|
||||
*/
|
||||
this.width = width;
|
||||
|
||||
/**
|
||||
* @property {number} height - The height of the tile.
|
||||
*/
|
||||
this.height = height;
|
||||
|
||||
};
|
||||
|
||||
Phaser.Tile.prototype = {
|
||||
|
||||
/**
|
||||
* Clean up memory.
|
||||
* @method destroy
|
||||
*/
|
||||
destroy: function () {
|
||||
this.tilemap = null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Set collision configs.
|
||||
* @method setCollision
|
||||
* @param {boolean} left - Indicating collide with any object on the left.
|
||||
* @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.
|
||||
* @param {boolean} reset - Description.
|
||||
* @param {boolean} separateX - Separate at x-axis.
|
||||
* @param {boolean} separateY - Separate at y-axis.
|
||||
*/
|
||||
setCollision: function (left, right, up, down, reset, separateX, separateY) {
|
||||
|
||||
if (reset)
|
||||
{
|
||||
this.resetCollision();
|
||||
}
|
||||
|
||||
this.separateX = separateX;
|
||||
this.separateY = separateY;
|
||||
|
||||
this.collideNone = true;
|
||||
this.collideLeft = left;
|
||||
this.collideRight = right;
|
||||
this.collideUp = up;
|
||||
this.collideDown = down;
|
||||
|
||||
if (left || right || up || down)
|
||||
{
|
||||
this.collideNone = false;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Reset collision status flags.
|
||||
* @method resetCollision
|
||||
*/
|
||||
resetCollision: function () {
|
||||
|
||||
this.collideNone = true;
|
||||
this.collideLeft = false;
|
||||
this.collideRight = false;
|
||||
this.collideUp = false;
|
||||
this.collideDown = false;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Object.defineProperty(Phaser.Tile.prototype, "bottom", {
|
||||
|
||||
/**
|
||||
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
|
||||
* @method bottom
|
||||
* @return {number}
|
||||
**/
|
||||
get: function () {
|
||||
return this.y + this.height;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Object.defineProperty(Phaser.Tile.prototype, "right", {
|
||||
|
||||
/**
|
||||
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
|
||||
* However it does affect the width property.
|
||||
* @method right
|
||||
* @return {number}
|
||||
**/
|
||||
get: function () {
|
||||
return this.x + this.width;
|
||||
}
|
||||
|
||||
});
|
||||
@@ -1,134 +1,5 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2013 Photon Storm Ltd.
|
||||
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
|
||||
* @module Phaser.Tilemap
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Create a new <code>Tilemap</code>.
|
||||
* @class Phaser.Tilemap
|
||||
* @classdesc This GameObject allows for the display of a tilemap within the game world. Tile maps consist of an image, tile data and a size.
|
||||
* Internally it creates a TilemapLayer for each layer in the tilemap.
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - Current game instance.
|
||||
* @param {string} key - Asset key for this map.
|
||||
* @param {object} x - Description.
|
||||
* @param {object} y - Description.
|
||||
* @param {boolean} resizeWorld - Resize the world bound automatically based on this tilemap?
|
||||
* @param {number} tileWidth - Width of tiles in this map (used for CSV maps).
|
||||
* @param {number} tileHeight - Height of tiles in this map (used for CSV maps).
|
||||
*/
|
||||
Phaser.Tilemap = function (game, key, x, y, resizeWorld, tileWidth, tileHeight) {
|
||||
|
||||
if (typeof resizeWorld === "undefined") { resizeWorld = true; }
|
||||
if (typeof tileWidth === "undefined") { tileWidth = 0; }
|
||||
if (typeof tileHeight === "undefined") { tileHeight = 0; }
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {Description} group - Description.
|
||||
*/
|
||||
this.group = null;
|
||||
|
||||
/**
|
||||
* @property {string} name - The user defined name given to this Description.
|
||||
* @default
|
||||
*/
|
||||
this.name = '';
|
||||
|
||||
/**
|
||||
* @property {Description} key - Description.
|
||||
*/
|
||||
this.key = key;
|
||||
|
||||
/**
|
||||
* @property {number} renderOrderID - Render iteration counter
|
||||
* @default
|
||||
*/
|
||||
this.renderOrderID = 0;
|
||||
|
||||
/**
|
||||
* @property {boolean} collisionCallback - Tilemap collision callback.
|
||||
* @default
|
||||
*/
|
||||
this.collisionCallback = null;
|
||||
|
||||
/**
|
||||
* @property {boolean} exists - Description.
|
||||
* @default
|
||||
*/
|
||||
this.exists = true;
|
||||
|
||||
/**
|
||||
* @property {boolean} visible - Description.
|
||||
* @default
|
||||
*/
|
||||
this.visible = true;
|
||||
|
||||
this.width = 0;
|
||||
this.height = 0;
|
||||
|
||||
/**
|
||||
* @property {boolean} tiles - Description.
|
||||
* @default
|
||||
*/
|
||||
this.tiles = [];
|
||||
|
||||
/**
|
||||
* @property {boolean} layers - Description.
|
||||
* @default
|
||||
*/
|
||||
this.layers = [];
|
||||
|
||||
var map = this.game.cache.getTilemap(key);
|
||||
|
||||
PIXI.DisplayObjectContainer.call(this);
|
||||
|
||||
/**
|
||||
* @property {Description} position - Description.
|
||||
*/
|
||||
this.position.x = x;
|
||||
this.position.y = y;
|
||||
|
||||
/**
|
||||
* @property {Description} type - Description.
|
||||
*/
|
||||
this.type = Phaser.TILEMAP;
|
||||
|
||||
/**
|
||||
* @property {Description} renderer - Description.
|
||||
*/
|
||||
this.renderer = new Phaser.TilemapRenderer(this.game);
|
||||
|
||||
this.fixedToCamera = true;
|
||||
|
||||
/**
|
||||
* @property {Description} mapFormat - Description.
|
||||
*/
|
||||
this.mapFormat = map.format;
|
||||
|
||||
switch (this.mapFormat)
|
||||
{
|
||||
case Phaser.Tilemap.CSV:
|
||||
this.parseCSV(map.mapData, key, tileWidth, tileHeight);
|
||||
break;
|
||||
|
||||
case Phaser.Tilemap.JSON:
|
||||
this.parseTiledJSON(map.mapData, key);
|
||||
break;
|
||||
}
|
||||
|
||||
if (this.currentLayer && resizeWorld)
|
||||
{
|
||||
this.game.world.setBounds(0, 0, this.width, this.heightIn);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Needed to keep the PIXI.Sprite constructor in the prototype chain (as the core pixi renderer uses an instanceof check sadly)
|
||||
@@ -138,393 +9,3 @@ Phaser.Tilemap.prototype.constructor = Phaser.Tilemap;
|
||||
Phaser.Tilemap.CSV = 0;
|
||||
Phaser.Tilemap.JSON = 1;
|
||||
|
||||
/**
|
||||
* Parse csv map data and generate tiles.
|
||||
*
|
||||
* @method Phaser.Tilemap.prototype.parseCSV
|
||||
* @param {string} data - CSV map data.
|
||||
* @param {string} key - Asset key for tileset image.
|
||||
* @param {number} tileWidth - Width of its tile.
|
||||
* @param {number} tileHeight - Height of its tile.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.parseCSV = function (data, key, tileWidth, tileHeight) {
|
||||
|
||||
var layer = new Phaser.TilemapLayer(this, 0, key, Phaser.Tilemap.CSV, 'TileLayerCSV' + this.layers.length.toString(), tileWidth, tileHeight);
|
||||
|
||||
// Trim any rogue whitespace from the data
|
||||
data = data.trim();
|
||||
|
||||
var rows = data.split("\n");
|
||||
|
||||
for (var i = 0; i < rows.length; i++)
|
||||
{
|
||||
var column = rows[i].split(",");
|
||||
|
||||
if (column.length > 0)
|
||||
{
|
||||
layer.addColumn(column);
|
||||
}
|
||||
}
|
||||
|
||||
layer.updateBounds();
|
||||
layer.createCanvas();
|
||||
|
||||
var tileQuantity = layer.parseTileOffsets();
|
||||
|
||||
this.currentLayer = layer;
|
||||
this.collisionLayer = layer;
|
||||
this.layers.push(layer);
|
||||
|
||||
this.width = this.currentLayer.widthInPixels;
|
||||
this.height = this.currentLayer.heightInPixels;
|
||||
|
||||
this.generateTiles(tileQuantity);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse JSON map data and generate tiles.
|
||||
*
|
||||
* @method Phaser.Tilemap.prototype.parseTiledJSON
|
||||
* @param {string} data - JSON map data.
|
||||
* @param {string} key - Asset key for tileset image.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.parseTiledJSON = function (json, key) {
|
||||
|
||||
for (var i = 0; i < json.layers.length; i++)
|
||||
{
|
||||
var layer = new Phaser.TilemapLayer(this, i, key, Phaser.Tilemap.JSON, json.layers[i].name, json.tilewidth, json.tileheight);
|
||||
|
||||
// Check it's a data layer
|
||||
if (!json.layers[i].data)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// layer.createQuadTree(json.tilewidth * json.layers[i].width, json.tileheight * json.layers[i].height);
|
||||
|
||||
layer.alpha = json.layers[i].opacity;
|
||||
layer.visible = json.layers[i].visible;
|
||||
layer.tileMargin = json.tilesets[0].margin;
|
||||
layer.tileSpacing = json.tilesets[0].spacing;
|
||||
|
||||
var c = 0;
|
||||
var row;
|
||||
|
||||
for (var t = 0; t < json.layers[i].data.length; t++)
|
||||
{
|
||||
if (c == 0)
|
||||
{
|
||||
row = [];
|
||||
}
|
||||
|
||||
row.push(json.layers[i].data[t]);
|
||||
c++;
|
||||
|
||||
if (c == json.layers[i].width)
|
||||
{
|
||||
layer.addColumn(row);
|
||||
c = 0;
|
||||
}
|
||||
}
|
||||
|
||||
layer.updateBounds();
|
||||
layer.createCanvas();
|
||||
|
||||
var tileQuantity = layer.parseTileOffsets();
|
||||
|
||||
this.currentLayer = layer;
|
||||
this.collisionLayer = layer;
|
||||
this.layers.push(layer);
|
||||
|
||||
if (this.currentLayer.widthInPixels > this.width)
|
||||
{
|
||||
this.width = this.currentLayer.widthInPixels;
|
||||
}
|
||||
|
||||
if (this.currentLayer.heightInPixels > this.height)
|
||||
{
|
||||
this.height = this.currentLayer.heightInPixels;
|
||||
}
|
||||
}
|
||||
|
||||
this.generateTiles(tileQuantity);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Create tiles of given quantity.
|
||||
* @method Phaser.Tilemap.prototype.generateTiles
|
||||
* @param {number} qty - Quantity of tiles to be generated.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.generateTiles = function (qty) {
|
||||
|
||||
for (var i = 0; i < qty; i++)
|
||||
{
|
||||
this.tiles.push(new Phaser.Tile(this.game, this, i, this.currentLayer.tileWidth, this.currentLayer.tileHeight));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Set callback to be called when this tilemap collides.
|
||||
*
|
||||
* @method Phaser.Tilemap.prototype.setCollisionCallback
|
||||
* @param {object} context - Callback will be called with this context.
|
||||
* @param {Function} callback - Callback function.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.setCollisionCallback = function (context, callback) {
|
||||
|
||||
this.collisionCallbackContext = context;
|
||||
this.collisionCallback = callback;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Set collision configs of tiles in a range index.
|
||||
*
|
||||
* @method Phaser.Tilemap.prototype.setCollisionRange
|
||||
* @param {number} start - First index of tiles.
|
||||
* @param {number} end - Last index of tiles.
|
||||
* @param {number} collision - Bit field of flags. (see Tile.allowCollision)
|
||||
* @param {boolean} resetCollisions - Reset collision flags before set.
|
||||
* @param {boolean} separateX - Enable separate at x-axis.
|
||||
* @param {boolean} separateY - Enable separate at y-axis.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.setCollisionRange = function (start, end, left, right, up, down, resetCollisions, separateX, separateY) {
|
||||
|
||||
if (typeof resetCollisions === "undefined") { resetCollisions = false; }
|
||||
if (typeof separateX === "undefined") { separateX = true; }
|
||||
if (typeof separateY === "undefined") { separateY = true; }
|
||||
|
||||
for (var i = start; i < end; i++)
|
||||
{
|
||||
this.tiles[i].setCollision(left, right, up, down, resetCollisions, separateX, separateY);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Set collision configs of tiles with given index.
|
||||
* @param {number[]} values - Index array which contains all tile indexes. The tiles with those indexes will be setup with rest parameters.
|
||||
* @param {number} collision - Bit field of flags (see Tile.allowCollision).
|
||||
* @param {boolean} resetCollisions - Reset collision flags before set.
|
||||
* @param {boolean} left - Indicating collide with any object on the left.
|
||||
* @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.
|
||||
* @param {boolean} separateX - Enable separate at x-axis.
|
||||
* @param {boolean} separateY - Enable separate at y-axis.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.setCollisionByIndex = function (values, left, right, up, down, resetCollisions, separateX, separateY) {
|
||||
|
||||
if (typeof resetCollisions === "undefined") { resetCollisions = false; }
|
||||
if (typeof separateX === "undefined") { separateX = true; }
|
||||
if (typeof separateY === "undefined") { separateY = true; }
|
||||
|
||||
for (var i = 0; i < values.length; i++)
|
||||
{
|
||||
this.tiles[values[i]].setCollision(left, right, up, down, resetCollisions, separateX, separateY);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Tile Management
|
||||
|
||||
/**
|
||||
* Get the tile by its index.
|
||||
* @param {number} value - Index of the tile you want to get.
|
||||
* @return {Tile} The tile with given index.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.getTileByIndex = function (value) {
|
||||
|
||||
if (this.tiles[value])
|
||||
{
|
||||
return this.tiles[value];
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the tile located at specific position and layer.
|
||||
* @param {number} x - X position of this tile located.
|
||||
* @param {number} y - Y position of this tile located.
|
||||
* @param {number} [layer] - layer of this tile located.
|
||||
* @return {Tile} The tile with specific properties.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.getTile = function (x, y, layer) {
|
||||
|
||||
if (typeof layer === "undefined") { layer = this.currentLayer.ID; }
|
||||
|
||||
return this.tiles[this.layers[layer].getTileIndex(x, y)];
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the tile located at specific position (in world coordinate) and layer (thus you give a position of a point which is within the tile).
|
||||
* @param {number} x - X position of the point in target tile.
|
||||
* @param {number} y - Y position of the point in target tile.
|
||||
* @param {number} [layer] - layer of this tile located.
|
||||
* @return {Tile} The tile with specific properties.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.getTileFromWorldXY = function (x, y, layer) {
|
||||
|
||||
if (typeof layer === "undefined") { layer = this.currentLayer.ID; }
|
||||
|
||||
return this.tiles[this.layers[layer].getTileFromWorldXY(x, y)];
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the tile underneath the Input.x/y position.
|
||||
* @param {number} layer - The layer to check, defaults to 0.
|
||||
* @return {Tile}
|
||||
*/
|
||||
Phaser.Tilemap.prototype.getTileFromInputXY = function (layer) {
|
||||
|
||||
if (typeof layer === "undefined") { layer = this.currentLayer.ID; }
|
||||
|
||||
return this.tiles[this.layers[layer].getTileFromWorldXY(this.game.input.worldX, this.game.input.worldY)];
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Get tiles overlaps the given object.
|
||||
* @param {GameObject} object - Tiles you want to get that overlaps this.
|
||||
* @return {array} Array with tiles information (Each contains x, y and the tile).
|
||||
*/
|
||||
Phaser.Tilemap.prototype.getTileOverlaps = function (object) {
|
||||
|
||||
return this.currentLayer.getTileOverlaps(object);
|
||||
|
||||
};
|
||||
|
||||
// COLLIDE
|
||||
|
||||
/**
|
||||
* Check whether this tilemap collides with the given game object or group of objects.
|
||||
* @param {Function} objectOrGroup - Target object of group you want to check.
|
||||
* @param {Function} callback - This is called if objectOrGroup collides the tilemap.
|
||||
* @param {object} context - Callback will be called with this context.
|
||||
* @return {boolean} Return true if this collides with given object, otherwise return false.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.collide = function (objectOrGroup, callback, context) {
|
||||
|
||||
objectOrGroup = objectOrGroup || this.game.world.group;
|
||||
callback = callback || null;
|
||||
context = context || null;
|
||||
|
||||
if (callback && context)
|
||||
{
|
||||
this.collisionCallback = callback;
|
||||
this.collisionCallbackContext = context;
|
||||
}
|
||||
|
||||
if (objectOrGroup instanceof Phaser.Group)
|
||||
{
|
||||
objectOrGroup.forEachAlive(this.collideGameObject, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.collideGameObject(objectOrGroup);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Check whether this tilemap collides with the given game object.
|
||||
* @param {GameObject} object - Target object you want to check.
|
||||
* @return {boolean} Return true if this collides with given object, otherwise return false.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.collideGameObject = function (object) {
|
||||
|
||||
if (object instanceof Phaser.Group || object instanceof Phaser.Tilemap)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (object.exists && object.body.allowCollision.none == false)
|
||||
{
|
||||
this._tempCollisionData = this.collisionLayer.getTileOverlaps(object);
|
||||
|
||||
if (this.collisionCallback && this._tempCollisionData.length > 0)
|
||||
{
|
||||
this.collisionCallback.call(this.collisionCallbackContext, object, this._tempCollisionData);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Set a tile to a specific layer.
|
||||
* @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.
|
||||
* @param {number} [layer] - Which layer you want to set the tile to.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.putTile = function (x, y, index, layer) {
|
||||
|
||||
if (typeof layer === "undefined") { layer = this.currentLayer.ID; }
|
||||
|
||||
this.layers[layer].putTile(x, y, index);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Calls the renderer.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.update = function () {
|
||||
|
||||
this.renderer.render(this);
|
||||
|
||||
if (this.fixedToCamera)
|
||||
{
|
||||
// this.displayObject.position.x = this.game.camera.view.x + this.x;
|
||||
// this.displayObject.position.y = this.game.camera.view.y + this.y;
|
||||
this.position.x = this.game.camera.view.x + 0;
|
||||
this.position.y = this.game.camera.view.y + 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Description.
|
||||
*/
|
||||
Phaser.Tilemap.prototype.destroy = function () {
|
||||
|
||||
this.tiles.length = 0;
|
||||
this.layers.length = 0;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Get width in pixels.
|
||||
* @return {number}
|
||||
*/
|
||||
Object.defineProperty(Phaser.Tilemap.prototype, "widthInPixels", {
|
||||
|
||||
get: function () {
|
||||
return this.currentLayer.widthInPixels;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* Get height in pixels.
|
||||
* @return {number}
|
||||
*/
|
||||
Object.defineProperty(Phaser.Tilemap.prototype, "heightInPixels", {
|
||||
|
||||
get: function () {
|
||||
return this.currentLayer.heightInPixels;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@@ -1,239 +0,0 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2013 Photon Storm Ltd.
|
||||
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
|
||||
* @module Phaser.TilemapRenderer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tilemap renderer.
|
||||
*
|
||||
* @class Phaser.TilemapRenderer
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
Phaser.TilemapRenderer = function (game) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {number} _ga - Local rendering related temp vars to help avoid gc spikes through constant var creation.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._ga = 1;
|
||||
|
||||
/**
|
||||
* @property {number} _dx - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._dx = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _dy - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._dy = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _dw - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._dw = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _dh - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._dh = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _tx - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._tx = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _ty - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._ty = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _tl - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._tl = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _maxX - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._maxX = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _maxY - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._maxY = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _startX - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._startX = 0;
|
||||
|
||||
/**
|
||||
* @property {number} _startY - Description.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._startY = 0;
|
||||
|
||||
};
|
||||
|
||||
Phaser.TilemapRenderer.prototype = {
|
||||
|
||||
/**
|
||||
* Render a tilemap to a canvas.
|
||||
* @method render
|
||||
* @param tilemap {Tilemap} The tilemap data to render.
|
||||
* @return {boolean} Description.
|
||||
*/
|
||||
render: function (tilemap) {
|
||||
|
||||
// Loop through the layers
|
||||
this._tl = tilemap.layers.length;
|
||||
|
||||
for (var i = 0; i < this._tl; i++)
|
||||
{
|
||||
if (tilemap.layers[i].visible == false || tilemap.layers[i].alpha < 0.1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var layer = tilemap.layers[i];
|
||||
|
||||
// Work out how many tiles we can fit into our canvas and round it up for the edges
|
||||
this._maxX = this.game.math.ceil(layer.canvas.width / layer.tileWidth) + 1;
|
||||
this._maxY = this.game.math.ceil(layer.canvas.height / layer.tileHeight) + 1;
|
||||
|
||||
// And now work out where in the tilemap the camera actually is
|
||||
this._startX = this.game.math.floor(this.game.camera.x / layer.tileWidth);
|
||||
this._startY = this.game.math.floor(this.game.camera.y / layer.tileHeight);
|
||||
|
||||
// Tilemap bounds check
|
||||
if (this._startX < 0)
|
||||
{
|
||||
this._startX = 0;
|
||||
}
|
||||
|
||||
if (this._startY < 0)
|
||||
{
|
||||
this._startY = 0;
|
||||
}
|
||||
|
||||
if (this._maxX > layer.widthInTiles)
|
||||
{
|
||||
this._maxX = layer.widthInTiles;
|
||||
}
|
||||
|
||||
if (this._maxY > layer.heightInTiles)
|
||||
{
|
||||
this._maxY = layer.heightInTiles;
|
||||
}
|
||||
|
||||
if (this._startX + this._maxX > layer.widthInTiles)
|
||||
{
|
||||
this._startX = layer.widthInTiles - this._maxX;
|
||||
}
|
||||
|
||||
if (this._startY + this._maxY > layer.heightInTiles)
|
||||
{
|
||||
this._startY = layer.heightInTiles - this._maxY;
|
||||
}
|
||||
|
||||
// Finally get the offset to avoid the blocky movement
|
||||
this._dx = -(this.game.camera.x - (this._startX * layer.tileWidth));
|
||||
this._dy = -(this.game.camera.y - (this._startY * layer.tileHeight));
|
||||
|
||||
this._tx = this._dx;
|
||||
this._ty = this._dy;
|
||||
|
||||
// Alpha
|
||||
if (layer.alpha !== 1)
|
||||
{
|
||||
this._ga = layer.context.globalAlpha;
|
||||
layer.context.globalAlpha = layer.alpha;
|
||||
}
|
||||
|
||||
layer.context.clearRect(0, 0, layer.canvas.width, layer.canvas.height);
|
||||
|
||||
for (var row = this._startY; row < this._startY + this._maxY; row++)
|
||||
{
|
||||
this._columnData = layer.mapData[row];
|
||||
|
||||
for (var tile = this._startX; tile < this._startX + this._maxX; tile++)
|
||||
{
|
||||
if (layer.tileOffsets[this._columnData[tile]])
|
||||
{
|
||||
layer.context.drawImage(
|
||||
layer.tileset,
|
||||
layer.tileOffsets[this._columnData[tile]].x,
|
||||
layer.tileOffsets[this._columnData[tile]].y,
|
||||
layer.tileWidth,
|
||||
layer.tileHeight,
|
||||
this._tx,
|
||||
this._ty,
|
||||
layer.tileWidth,
|
||||
layer.tileHeight
|
||||
);
|
||||
|
||||
if (tilemap.tiles[this._columnData[tile]].collideNone == false)
|
||||
{
|
||||
layer.context.fillStyle = 'rgba(255,255,0,0.5)';
|
||||
layer.context.fillRect(this._tx, this._ty, layer.tileWidth, layer.tileHeight);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this._tx += layer.tileWidth;
|
||||
|
||||
}
|
||||
|
||||
this._tx = this._dx;
|
||||
this._ty += layer.tileHeight;
|
||||
|
||||
}
|
||||
|
||||
if (this._ga > -1)
|
||||
{
|
||||
layer.context.globalAlpha = this._ga;
|
||||
}
|
||||
|
||||
// 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(layer.baseTexture);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user