Skip to content

Commit d7ba7b3

Browse files
committed
Improve TileMap's setCollsion functions
Now one can decide in 'setCollision', 'setCollisionBetween', 'setCollisionByExclusion' and 'setCollisionByIndex' to recalculate the collision faces or not. I was able to decrease the time it takes to set the collisions from 1880ms to 440ms in my case,see http://www.html5gamedevs.com/topic/6328-camera-rendered-area-move-tilemaplayer/?p=38037
1 parent 63ca126 commit d7ba7b3

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

src/tilemap/Tilemap.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,13 @@ Phaser.Tilemap.prototype = {
685685
* @param {number|array} indexes - Either a single tile index, or an array of tile IDs to be checked for collision.
686686
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
687687
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
688+
* @param {boolean} [recalculate=true] - Recalculates the tile faces after the update.
688689
*/
689-
setCollision: function (indexes, collides, layer) {
690+
setCollision: function (indexes, collides, layer, recalculate) {
690691

691692
if (typeof collides === 'undefined') { collides = true; }
692-
693+
if (typeof recalculate === 'undefined') { recalculate = true; }
694+
693695
layer = this.getLayer(layer);
694696

695697
if (typeof indexes === 'number')
@@ -704,8 +706,11 @@ Phaser.Tilemap.prototype = {
704706
this.setCollisionByIndex(indexes[i], collides, layer, false);
705707
}
706708

707-
// Now re-calculate interesting faces
708-
this.calculateFaces(layer);
709+
if (recalculate)
710+
{
711+
// Now re-calculate interesting faces
712+
this.calculateFaces(layer);
713+
}
709714
}
710715

711716
},
@@ -720,11 +725,13 @@ Phaser.Tilemap.prototype = {
720725
* @param {number} stop - The last index of the tile to be set for collision.
721726
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
722727
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
728+
* @param {boolean} [recalculate=true] - Recalculates the tile faces after the update.
723729
*/
724-
setCollisionBetween: function (start, stop, collides, layer) {
730+
setCollisionBetween: function (start, stop, collides, layer, recalculate) {
725731

726732
if (typeof collides === 'undefined') { collides = true; }
727-
733+
if (typeof recalculate === 'undefined') { recalculate = true; }
734+
728735
layer = this.getLayer(layer);
729736

730737
if (start > stop)
@@ -737,8 +744,11 @@ Phaser.Tilemap.prototype = {
737744
this.setCollisionByIndex(index, collides, layer, false);
738745
}
739746

740-
// Now re-calculate interesting faces
741-
this.calculateFaces(layer);
747+
if (recalculate)
748+
{
749+
// Now re-calculate interesting faces
750+
this.calculateFaces(layer);
751+
}
742752

743753
},
744754

@@ -750,11 +760,13 @@ Phaser.Tilemap.prototype = {
750760
* @param {array} indexes - An array of the tile IDs to not be counted for collision.
751761
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
752762
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
763+
* @param {boolean} [recalculate=true] - Recalculates the tile faces after the update.
753764
*/
754-
setCollisionByExclusion: function (indexes, collides, layer) {
765+
setCollisionByExclusion: function (indexes, collides, layer, recalculate) {
755766

756767
if (typeof collides === 'undefined') { collides = true; }
757-
768+
if (typeof recalculate === 'undefined') { recalculate = true; }
769+
758770
layer = this.getLayer(layer);
759771

760772
// Collide everything, except the IDs given in the indexes array
@@ -766,8 +778,11 @@ Phaser.Tilemap.prototype = {
766778
}
767779
}
768780

769-
// Now re-calculate interesting faces
770-
this.calculateFaces(layer);
781+
if (recalculate)
782+
{
783+
// Now re-calculate interesting faces
784+
this.calculateFaces(layer);
785+
}
771786

772787
},
773788

0 commit comments

Comments
 (0)