Skip to content

Commit 60f20aa

Browse files
committed
setTileLocationCallback
1 parent da79734 commit 60f20aa

6 files changed

Lines changed: 57 additions & 2 deletions

File tree

v3/src/gameobjects/tilemap/Tile.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,15 @@ var Tile = new Class({
120120

121121
setCollisionCallback: function (callback, context)
122122
{
123-
this.collisionCallback = callback;
124-
this.collisionCallbackContext = context;
123+
if (callback === null) {
124+
this.collisionCallback = undefined;
125+
this.collisionCallbackContext = undefined;
126+
}
127+
else
128+
{
129+
this.collisionCallback = callback;
130+
this.collisionCallbackContext = context;
131+
}
125132
},
126133

127134
setSize: function (tileWidth, tileHeight)

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,14 @@ var Tilemap = new Class({
557557
return this;
558558
},
559559

560+
setTileLocationCallback: function (tileX, tileY, width, height, callback, callbackContext, layer)
561+
{
562+
layer = this.getLayer(layer);
563+
if (layer === null) { return this; }
564+
TilemapComponents.SetTileLocationCallback(tileX, tileY, width, height, callback, callbackContext, layer);
565+
return this;
566+
},
567+
560568
setLayer: function (layer)
561569
{
562570
var index = this.getLayerIndex(layer);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var GetTilesWithin = require('./GetTilesWithin');
2+
3+
/**
4+
* Sets a collision callback for the given rectangular area (in tile coordindates) within the layer.
5+
* If a callback is already set for the tile index it will be replaced. Set the callback to null to
6+
* remove it.
7+
*
8+
* @param {number} [tileX=0] - [description]
9+
* @param {number} [tileY=0] - [description]
10+
* @param {number} [width=max width based on tileX] - [description]
11+
* @param {number} [height=max height based on tileY] - [description]
12+
* @param {function} callback - The callback that will be invoked when the tile is collided with.
13+
* @param {object} callbackContext - The context under which the callback is called.
14+
* @param {LayerData} layer - [description]
15+
*/
16+
var SetTileLocationCallback = function (tileX, tileY, width, height, callback, callbackContext, layer)
17+
{
18+
var tiles = GetTilesWithin(tileX, tileY, width, height, null, layer);
19+
20+
for (var i = 0; i < tiles.length; i++)
21+
{
22+
tiles[i].setCollisionCallback(callback, callbackContext);
23+
}
24+
25+
};
26+
27+
module.exports = SetTileLocationCallback;

v3/src/gameobjects/tilemap/components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
SetCollisionBetween: require('./SetCollisionBetween'),
2727
SetCollisionByExclusion: require('./SetCollisionByExclusion'),
2828
SetTileIndexCallback: require('./SetTileIndexCallback'),
29+
SetTileLocationCallback: require('./SetTileLocationCallback'),
2930
Shuffle: require('./Shuffle'),
3031
SwapByIndex: require('./SwapByIndex'),
3132
WorldToTileX: require('./WorldToTileX'),

v3/src/gameobjects/tilemap/dynamiclayer/DynamicTilemapLayer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ var DynamicTilemapLayer = new Class({
194194
return this;
195195
},
196196

197+
setTileLocationCallback: function (tileX, tileY, width, height, callback, callbackContext)
198+
{
199+
TilemapComponents.SetTileLocationCallback(tileX, tileY, width, height, callback, callbackContext, this.layer);
200+
return this;
201+
},
202+
197203
shuffle: function (tileX, tileY, width, height)
198204
{
199205
TilemapComponents.Shuffle(tileX, tileY, width, height, this.layer);

v3/src/gameobjects/tilemap/staticlayer/StaticTilemapLayer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ var StaticTilemapLayer = new Class({
303303
return this;
304304
},
305305

306+
setTileLocationCallback: function (tileX, tileY, width, height, callback, callbackContext)
307+
{
308+
TilemapComponents.SetTileLocationCallback(tileX, tileY, width, height, callback, callbackContext, this.layer);
309+
return this;
310+
},
311+
306312
worldToTileX: function (worldX, snapToFloor, camera)
307313
{
308314
return TilemapComponents.WorldToTileX(worldX, snapToFloor, camera, this.layer);

0 commit comments

Comments
 (0)