Skip to content

Commit 68634a0

Browse files
committed
Tilemap & layer methods: FilterTiles & CreateFromTiles
1 parent 3129950 commit 68634a0

6 files changed

Lines changed: 140 additions & 0 deletions

File tree

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ var Tilemap = new Class({
200200
return this;
201201
},
202202

203+
createFromTiles: function (indexes, replacements, spriteConfig, scene, camera, layer)
204+
{
205+
layer = this.getLayer(layer);
206+
if (layer === null) { return null; }
207+
return TilemapComponents.CreateFromTiles(indexes, replacements, spriteConfig, scene, camera, layer);
208+
},
209+
203210
destroy: function ()
204211
{
205212
this.layers.length = 0;
@@ -220,6 +227,13 @@ var Tilemap = new Class({
220227
return this;
221228
},
222229

230+
filterTiles: function (callback, context, tileX, tileY, width, height, filteringOptions, layer)
231+
{
232+
layer = this.getLayer(layer);
233+
if (layer === null) { return null; }
234+
return TilemapComponents.FilterTiles(callback, context, tileX, tileY, width, height, filteringOptions, layer);
235+
},
236+
223237
findByIndex: function (findIndex, skip, reverse, layer)
224238
{
225239
layer = this.getLayer(layer);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var TileToWorldX = require('./TileToWorldX');
2+
var TileToWorldY = require('./TileToWorldY');
3+
var GetTilesWithin = require('./GetTilesWithin');
4+
var ReplaceByIndex = require('./ReplaceByIndex');
5+
6+
/**
7+
* Creates a Sprite for every object matching the given tile indexes in the layer. You can
8+
* optionally specify if each tile will be replaced with a new tile after the Sprite has been
9+
* created. This is useful if you want to lay down special tiles in a level that are converted to
10+
* Sprites, but want to replace the tile itself with a floor tile or similar once converted.
11+
*
12+
* @param {number|array} indexes - The tile index, or array of indexes, to create Sprites from.
13+
* @param {number|array} replacements - The tile index, or array of indexes, to change a converted
14+
* tile to. Set to `null` to leave the tiles unchanged. If an array is given, it is assumed to be a
15+
* one-to-one mapping with the indexes array.
16+
* @param {object} spriteConfig - The config object to pass into the Sprite creator (i.e.
17+
* scene.make.sprite).
18+
* @param {Scene} [scene=scene the map is within] - The Scene to create the Sprites within.
19+
* @param {Camera} [camera=main camera] - The Camera to use when determining the world XY
20+
* @param {LayerData} layer - [description]
21+
* @return {array} An array of the Sprites that were created.
22+
*/
23+
var CreateFromTiles = function (indexes, replacements, spriteConfig, scene, camera, layer)
24+
{
25+
if (spriteConfig === undefined) { spriteConfig = {}; }
26+
27+
if (!Array.isArray(indexes)) { indexes = [ indexes ]; }
28+
29+
var tilemapLayer = layer.tilemapLayer;
30+
if (scene === undefined) { scene = tilemapLayer.scene; }
31+
if (camera === undefined) { camera = scene.cameras.main; }
32+
33+
var tiles = GetTilesWithin(0, 0, layer.width, layer.height, null, layer);
34+
var sprites = [];
35+
var i;
36+
37+
for (i = 0; i < tiles.length; i++)
38+
{
39+
var tile = tiles[i];
40+
41+
if (indexes.indexOf(tile.index) !== -1)
42+
{
43+
spriteConfig.x = TileToWorldX(tile.x, camera, layer);
44+
spriteConfig.y = TileToWorldY(tile.y, camera, layer);
45+
46+
var sprite = scene.make.sprite(spriteConfig);
47+
sprites.push(sprite);
48+
}
49+
}
50+
51+
if (typeof replacements === 'number')
52+
{
53+
// Assume 1 replacement for all types of tile given
54+
for (i = 0; i < indexes.length; i++)
55+
{
56+
ReplaceByIndex(indexes[i], replacements, 0, 0, layer.width, layer.height, layer);
57+
}
58+
}
59+
else if (Array.isArray(replacements))
60+
{
61+
// Assume 1 to 1 mapping with indexes array
62+
for (i = 0; i < indexes.length; i++)
63+
{
64+
ReplaceByIndex(indexes[i], replacements[i], 0, 0, layer.width, layer.height, layer);
65+
}
66+
}
67+
68+
return sprites;
69+
};
70+
71+
module.exports = CreateFromTiles;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var GetTilesWithin = require('./GetTilesWithin');
2+
3+
/**
4+
* For each tile in the given rectangular area (in tile coordinates) of the layer, run the given
5+
* filter callback function. Any tiles that pass the filter test (i.e. where the callback returns
6+
* true) will returned as a new array. Similar to Array.prototype.Filter in vanilla JS.
7+
*
8+
* @param {number} callback - The callback. Each tile in the given area will be passed to this
9+
* callback as the first and only parameter. The callback should return true for tiles that pass the
10+
* filter.
11+
* @param {number} [context] - The context under which the callback should be run.
12+
* @param {number} [tileX=0] - [description]
13+
* @param {number} [tileY=0] - [description]
14+
* @param {number} [width=max width based on tileX] - [description]
15+
* @param {number} [height=max height based on tileY] - [description]
16+
* @param {object} [filteringOptions] - Optional filters to apply when getting the tiles.
17+
* @param {boolean} [filteringOptions.isNotEmpty=false] - If true, only return tiles that don't have
18+
* -1 for an index.
19+
* @param {boolean} [filteringOptions.isColliding=false] - If true, only return tiles that collide
20+
* on at least one side.
21+
* @param {boolean} [filteringOptions.hasInterestingFace=false] - If true, only return tiles that
22+
* have at least one interesting face.
23+
* @param {LayerData} layer - [description]
24+
* @returns {array} The filtered array of Tiles.
25+
*/
26+
var FilterTiles = function (callback, context, tileX, tileY, width, height, filteringOptions, layer)
27+
{
28+
var tiles = GetTilesWithin(tileX, tileY, width, height, filteringOptions, layer);
29+
return tiles.filter(callback, context);
30+
};
31+
32+
module.exports = FilterTiles;
33+

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module.exports = {
22

33
Copy: require('./Copy'),
4+
CreateFromTiles: require('./CreateFromTiles'),
45
CullTiles: require('./CullTiles'),
56
Fill: require('./Fill'),
67
FindByIndex: require('./FindByIndex'),
8+
FilterTiles: require('./FilterTiles'),
79
ForEachTile: require('./ForEachTile'),
810
GetTileAt: require('./GetTileAt'),
911
GetTileAtWorldXY: require('./GetTileAtWorldXY'),

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ var DynamicTilemapLayer = new Class({
5555
return this;
5656
},
5757

58+
createFromTiles: function (indexes, replacements, spriteConfig, scene, camera)
59+
{
60+
return TilemapComponents.CreateFromTiles(indexes, replacements, spriteConfig, scene, camera, this.layer);
61+
},
62+
5863
cull: function (camera)
5964
{
6065
TilemapComponents.CullTiles(this.layer, camera, this.culledTiles);
@@ -82,6 +87,11 @@ var DynamicTilemapLayer = new Class({
8287
return this;
8388
},
8489

90+
filterTiles: function (callback, context, tileX, tileY, width, height, filteringOptions)
91+
{
92+
return TilemapComponents.FilterTiles(callback, context, tileX, tileY, width, height, filteringOptions, this.layer);
93+
},
94+
8595
findByIndex: function (findIndex, skip, reverse)
8696
{
8797
return TilemapComponents.FindByIndex(findIndex, skip, reverse, this.layer);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ var StaticTilemapLayer = new Class({
209209
return this;
210210
},
211211

212+
createFromTiles: function (indexes, replacements, spriteConfig, scene, camera)
213+
{
214+
return TilemapComponents.CreateFromTiles(indexes, replacements, spriteConfig, scene, camera, this.layer);
215+
},
216+
212217
cull: function (camera)
213218
{
214219
TilemapComponents.CullTiles(this.layer, camera, this.culledTiles);
@@ -228,6 +233,11 @@ var StaticTilemapLayer = new Class({
228233
return TilemapComponents.FindByIndex(findIndex, skip, reverse, this.layer);
229234
},
230235

236+
filterTiles: function (callback, context, tileX, tileY, width, height, filteringOptions)
237+
{
238+
return TilemapComponents.FilterTiles(callback, context, tileX, tileY, width, height, filteringOptions, this.layer);
239+
},
240+
231241
forEachTile: function (callback, context, tileX, tileY, width, height, filteringOptions)
232242
{
233243
TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, filteringOptions, this.layer);

0 commit comments

Comments
 (0)