Skip to content

Commit 65beefc

Browse files
committed
Fix phaserjs#3169: add optional param to Tile#setCollision & Tile.resetCollision to recalc faces
1 parent b537ebd commit 65beefc

4 files changed

Lines changed: 36 additions & 21 deletions

File tree

src/gameobjects/tilemap/Tile.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,14 @@ var Tile = new Class({
402402
/**
403403
* Reset collision status flags.
404404
*
405+
* @param {boolean} [recalculateFaces=true] - Whether or not to recalculate interesting faces
406+
* for this tile and its neighbors.
405407
* @returns {this}
406408
*/
407-
resetCollision: function ()
409+
resetCollision: function (recalculateFaces)
408410
{
411+
if (recalculateFaces === undefined) { recalculateFaces = true; }
412+
409413
this.collideLeft = false;
410414
this.collideRight = false;
411415
this.collideUp = false;
@@ -416,6 +420,15 @@ var Tile = new Class({
416420
this.faceLeft = false;
417421
this.faceRight = false;
418422

423+
if (recalculateFaces)
424+
{
425+
var tilemapLayer = this.tilemapLayer;
426+
if (tilemapLayer)
427+
{
428+
this.tilemapLayer.calculateFacesAt(this.x, this.y);
429+
}
430+
}
431+
419432
return this;
420433
},
421434

@@ -441,13 +454,16 @@ var Tile = new Class({
441454
* @param {boolean} right - Indicating collide with any object on the right.
442455
* @param {boolean} up - Indicating collide with any object on the top.
443456
* @param {boolean} down - Indicating collide with any object on the bottom.
457+
* @param {boolean} [recalculateFaces=true] - Whether or not to recalculate interesting faces
458+
* for this tile and its neighbors.
444459
* @returns {this}
445460
*/
446-
setCollision: function (left, right, up, down)
461+
setCollision: function (left, right, up, down, recalculateFaces)
447462
{
448463
if (right === undefined) { right = left; }
449464
if (up === undefined) { up = left; }
450465
if (down === undefined) { down = left; }
466+
if (recalculateFaces === undefined) { recalculateFaces = true; }
451467

452468
this.collideLeft = left;
453469
this.collideRight = right;
@@ -459,6 +475,15 @@ var Tile = new Class({
459475
this.faceTop = up;
460476
this.faceBottom = down;
461477

478+
if (recalculateFaces)
479+
{
480+
var tilemapLayer = this.tilemapLayer;
481+
if (tilemapLayer)
482+
{
483+
this.tilemapLayer.calculateFacesAt(this.x, this.y);
484+
}
485+
}
486+
462487
return this;
463488
},
464489

src/gameobjects/tilemap/components/Fill.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var GetTilesWithin = require('./GetTilesWithin');
22
var CalculateFacesWithin = require('./CalculateFacesWithin');
3+
var SetTileCollision = require('./SetTileCollision');
34

45
/**
56
* Sets the tiles in the given rectangular area (in tile coordinates) of the layer with the
@@ -25,14 +26,7 @@ var Fill = function (index, tileX, tileY, width, height, recalculateFaces, layer
2526
{
2627
tiles[i].index = index;
2728

28-
if (doesIndexCollide)
29-
{
30-
tiles[i].setCollision(true);
31-
}
32-
else
33-
{
34-
tiles[i].resetCollision();
35-
}
29+
SetTileCollision(tiles[i], doesIndexCollide);
3630
}
3731

3832
if (recalculateFaces)

src/gameobjects/tilemap/components/PutTileAt.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Tile = require('../Tile');
22
var IsInLayerBounds = require('./IsInLayerBounds');
33
var CalculateFacesAt = require('./CalculateFacesAt');
4+
var SetTileCollision = require('./SetTileCollision');
45

56
/**
67
* Puts a tile at the given tile coordinates in the specified layer. You can pass in either an index
@@ -46,14 +47,8 @@ var PutTileAt = function (tile, tileX, tileY, recalculateFaces, layer)
4647

4748
// Updating colliding flag on the new tile
4849
var newTile = layer.data[tileY][tileX];
49-
if (layer.collideIndexes.indexOf(newTile.index) !== -1)
50-
{
51-
newTile.setCollision(true);
52-
}
53-
else
54-
{
55-
newTile.resetCollision();
56-
}
50+
var collides = layer.collideIndexes.indexOf(newTile.index) !== -1;
51+
SetTileCollision(newTile, collides);
5752

5853
// Recalculate faces only if the colliding flag at (tileX, tileY) has changed
5954
if (recalculateFaces && (oldTileCollides !== newTile.collides))

src/gameobjects/tilemap/components/SetTileCollision.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* Internally used method to set the colliding state of a tile.
2+
* Internally used method to set the colliding state of a tile. This does not recalculate
3+
* interesting faces.
34
*
45
* @param {Tile} tile - [description]
56
* @param {boolean} [collides=true] - [description]
@@ -8,11 +9,11 @@ var SetTileCollision = function (tile, collides)
89
{
910
if (collides)
1011
{
11-
tile.setCollision(true, true, true, true);
12+
tile.setCollision(true, true, true, true, false);
1213
}
1314
else
1415
{
15-
tile.resetCollision();
16+
tile.resetCollision(false);
1617
}
1718
};
1819

0 commit comments

Comments
 (0)