Tilemap.putTile now correctly re-calculates the collision data based on the new collideIndexes array (fixes #371)

Tilemap.putTile can now insert a tile into a null/blank area of the map (before it could only replace existing tiles)
This commit is contained in:
photonstorm
2014-03-04 03:08:35 +00:00
parent b5df53b4f6
commit 5a6ae5f696
6 changed files with 186 additions and 99 deletions
+7
View File
@@ -179,10 +179,12 @@ Phaser.Tile.prototype = {
if (left || right || up || down)
{
this.collides = true;
this.collideNone = false;
}
else
{
this.collides = false;
this.collideNone = true;
}
@@ -199,6 +201,11 @@ Phaser.Tile.prototype = {
this.collideRight = false;
this.collideUp = false;
this.collideDown = false;
this.collides = false;
this.faceTop = false;
this.faceBottom = false;
this.faceLeft = false;
this.faceRight = false;
},
+54 -4
View File
@@ -106,6 +106,11 @@ Phaser.Tilemap = function (game, key, tileWidth, tileHeight, width, height) {
*/
this.objects = data.objects;
/**
* @property {array} collideIndexes - An array of tile indexes that collide.
*/
this.collideIndexes = [];
/**
* @property {array} collision - An array of collision data (polylines, etc).
*/
@@ -763,6 +768,20 @@ Phaser.Tilemap.prototype = {
if (typeof layer === 'undefined') { layer = this.currentLayer; }
if (typeof recalculate === 'undefined') { recalculate = true; }
if (collides)
{
this.collideIndexes.push(index);
}
else
{
var i = this.collideIndexes.indexOf(index);
if (i > -1)
{
this.collideIndexes.splice(i, 1);
}
}
for (var y = 0; y < this.layers[layer].height ; y++)
{
for (var x = 0; x < this.layers[layer].width; x++)
@@ -848,6 +867,14 @@ Phaser.Tilemap.prototype = {
left = this.getTileLeft(layer, x, y);
right = this.getTileRight(layer, x, y);
if (tile.collides)
{
tile.faceTop = true;
tile.faceBottom = true;
tile.faceLeft = true;
tile.faceRight = true;
}
if (above && above.collides)
{
// There is a tile above this one that also collides, so the top of this tile is no longer interesting
@@ -974,6 +1001,15 @@ Phaser.Tilemap.prototype = {
},
/**
* Checks if there is a tile at the given location.
*
* @method Phaser.Tilemap#hasTile
* @param {number} x - X position to check if a tile exists at (given in tile units, not pixels)
* @param {number} y - Y position to check if a tile exists at (given in tile units, not pixels)
* @param {number|string|Phaser.TilemapLayer} layer - The layer to set as current.
* @return {boolean} True if there is a tile at the given location, otherwise false.
*/
hasTile: function (x, y, layer) {
return (this.layers[layer].data[y] !== null && this.layers[layer].data[y][x] !== null);
@@ -995,30 +1031,44 @@ Phaser.Tilemap.prototype = {
if (x >= 0 && x < this.layers[layer].width && y >= 0 && y < this.layers[layer].height)
{
var index;
if (tile instanceof Phaser.Tile)
{
index = tile.index;
if (this.hasTile(x, y, layer))
{
this.layers[layer].data[y][x].copy(tile);
}
else
{
//Phaser.Tile = function (layer, index, x, y, width, height) {
this.layers[layer].data[y][x] = new Phaser.Tile(layer, tile.index, x, y, tile.width, tile.height);
this.layers[layer].data[y][x] = new Phaser.Tile(layer, index, x, y, tile.width, tile.height);
}
}
else
{
index = tile;
if (this.hasTile(x, y, layer))
{
this.layers[layer].data[y][x].index = tile;
this.layers[layer].data[y][x].index = index;
}
else
{
this.layers[layer].data[y][x] = new Phaser.Tile(layer, tile, x, y, this.tileWidth, this.tileHeight);
this.layers[layer].data[y][x] = new Phaser.Tile(layer, index, x, y, this.tileWidth, this.tileHeight);
}
}
if (this.collideIndexes.indexOf(index) > -1)
{
this.layers[layer].data[y][x].setCollision(true, true, true, true);
}
else
{
this.layers[layer].data[y][x].resetCollision();
}
this.layers[layer].dirty = true;
this.calculateFaces(layer);
+6 -6
View File
@@ -837,12 +837,12 @@ Phaser.TilemapLayer.prototype.renderDebug = function () {
}
// Collision callback
if (tile && (tile.collisionCallback || tile.layer.callbacks[tile.index]))
{
this.context.fillStyle = this.debugCallbackColor;
this.context.fillRect(this._tx, this._ty, this._cw, this._ch);
this.context.fillStyle = this.debugFillColor;
}
// if (tile && (tile.collisionCallback || tile.layer.callbacks[tile.index]))
// {
// this.context.fillStyle = this.debugCallbackColor;
// this.context.fillRect(this._tx, this._ty, this._cw, this._ch);
// this.context.fillStyle = this.debugFillColor;
// }
this._tx += this.map.tileWidth;