Skip to content

Commit 63fb2e8

Browse files
committed
Tilemap & layer: ReplaceByIndex, FindByIndex, SwapByIndex
1 parent 4c306d6 commit 63fb2e8

8 files changed

Lines changed: 159 additions & 58 deletions

File tree

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ var Tilemap = new Class({
9494
},
9595

9696
// Creates & selects
97-
createBlankDynamicLayer: function (name, tileset, x, y, width, height, tileWidth, tileHeight) {
97+
createBlankDynamicLayer: function (name, tileset, x, y, width, height, tileWidth, tileHeight)
98+
{
9899
if (tileWidth === undefined) { tileWidth = this.tileWidth; }
99100
if (tileHeight === undefined) { tileHeight = this.tileHeight; }
100101
if (width === undefined) { width = this.width; }
@@ -107,7 +108,7 @@ var Tilemap = new Class({
107108
if (index !== null)
108109
{
109110
console.warn('Cannot create blank layer: layer with matching name already exists ' + name);
110-
return;
111+
return null;
111112
}
112113

113114
var layer = GenerateEmptyMapLayer(name, tileWidth, tileHeight, width, height);
@@ -140,8 +141,8 @@ var Tilemap = new Class({
140141

141142
if (index === null)
142143
{
143-
console.warn('Cannot create tilemap layer: invalid layer ID given: ' + index);
144-
return;
144+
console.warn('Cannot create tilemap layer: invalid layer ID given: ' + layerID);
145+
return null;
145146
}
146147

147148
// TODO: new feature, allow multiple CSV layers
@@ -161,8 +162,8 @@ var Tilemap = new Class({
161162

162163
if (index === null)
163164
{
164-
console.warn('Cannot create tilemap layer: invalid layer ID given: ' + index);
165-
return;
165+
console.warn('Cannot create tilemap layer: invalid layer ID given: ' + layerID);
166+
return null;
166167
}
167168

168169
// TODO: new feature, allow multiple CSV layers
@@ -195,6 +196,13 @@ var Tilemap = new Class({
195196
return this;
196197
},
197198

199+
findByIndex: function (findIndex, skip, reverse, layer)
200+
{
201+
layer = this.getLayer(layer);
202+
if (layer === null) { return null; }
203+
return TilemapComponents.FindByIndex(findIndex, skip, reverse, layer);
204+
},
205+
198206
forEachTile: function (callback, context, tileX, tileY, width, height, layer)
199207
{
200208
layer = this.getLayer(layer);
@@ -321,6 +329,16 @@ var Tilemap = new Class({
321329
return TilemapComponents.RemoveTile(tileX, tileY, replaceWithNull, layer);
322330
},
323331

332+
replaceByIndex: function (findIndex, newIndex, tileX, tileY, width, height, layer)
333+
{
334+
layer = this.getLayer(layer);
335+
if (layer !== null)
336+
{
337+
TilemapComponents.ReplaceByIndex(findIndex, newIndex, tileX, tileY, width, height, layer);
338+
}
339+
return this;
340+
},
341+
324342
setLayer: function (layer)
325343
{
326344
var index = this.getLayerIndex(layer);
@@ -339,6 +357,16 @@ var Tilemap = new Class({
339357
TilemapComponents.Shuffle(tileX, tileY, width, height, layer);
340358
}
341359
return this;
360+
},
361+
362+
swapByIndex: function (indexA, indexB, tileX, tileY, width, height, layer)
363+
{
364+
layer = this.getLayer(layer);
365+
if (layer !== null)
366+
{
367+
TilemapComponents.SwapByIndex(indexA, indexB, tileX, tileY, width, height, layer);
368+
}
369+
return this;
342370
}
343371

344372
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Find first tile with given index, or null if nothing found.
2+
// Searches from top left to bottom right.
3+
// Skip a number of matches or reverse to search from bottom-right to top-left.
4+
var FindByIndex = function (findIndex, skip, reverse, layer)
5+
{
6+
if (skip === undefined) { skip = 0; }
7+
if (reverse === undefined) { reverse = false; }
8+
9+
var count = 0;
10+
var tx;
11+
var ty;
12+
var tile;
13+
14+
if (reverse)
15+
{
16+
for (ty = layer.height - 1; ty >= 0; ty--)
17+
{
18+
for (tx = layer.width - 1; tx >= 0; tx--)
19+
{
20+
tile = layer.data[ty][tx];
21+
if (tile && tile.index === findIndex)
22+
{
23+
if (count === skip)
24+
{
25+
return tile;
26+
}
27+
else
28+
{
29+
count += 1;
30+
}
31+
}
32+
}
33+
}
34+
}
35+
else
36+
{
37+
for (ty = 0; ty < layer.height; ty++)
38+
{
39+
for (tx = 0; tx < layer.width; tx++)
40+
{
41+
tile = layer.data[ty][tx];
42+
if (tile && tile.index === findIndex)
43+
{
44+
if (count === skip)
45+
{
46+
return tile;
47+
}
48+
else
49+
{
50+
count += 1;
51+
}
52+
}
53+
}
54+
}
55+
}
56+
57+
return null;
58+
};
59+
60+
module.exports = FindByIndex;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var GetTilesWithin = require("./GetTilesWithin");
1+
var GetTilesWithin = require('./GetTilesWithin');
22

33
var ForEachTile = function (callback, context, tileX, tileY, width, height, layer)
44
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var GetTilesWithin = require('./GetTilesWithin');
2+
3+
var ReplaceByIndex = function (findIndex, newIndex, tileX, tileY, width, height, layer)
4+
{
5+
var tiles = GetTilesWithin(tileX, tileY, width, height, layer);
6+
for (var i = 0; i < tiles.length; i++)
7+
{
8+
if (tiles[i] && tiles[i].index === findIndex)
9+
{
10+
tiles[i].index = newIndex;
11+
}
12+
}
13+
};
14+
15+
module.exports = ReplaceByIndex;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var GetTilesWithin = require('./GetTilesWithin');
2+
3+
// Swaps indices, not other properties (matches v2 functionality)
4+
var SwapByIndex = function (indexA, indexB, tileX, tileY, width, height, layer)
5+
{
6+
var tiles = GetTilesWithin(tileX, tileY, width, height, layer);
7+
for (var i = 0; i < tiles.length; i++)
8+
{
9+
if (tiles[i])
10+
{
11+
if (tiles[i].index === indexA)
12+
{
13+
tiles[i].index = indexB;
14+
}
15+
else if (tiles[i].index === indexB)
16+
{
17+
tiles[i].index = indexA;
18+
}
19+
}
20+
}
21+
};
22+
23+
module.exports = SwapByIndex;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22

33
Copy: require('./Copy'),
44
Fill: require('./Fill'),
5+
FindByIndex: require('./FindByIndex'),
56
ForEachTile: require('./ForEachTile'),
67
GetTileAt: require('./GetTileAt'),
78
GetTileAtWorldXY: require('./GetTileAtWorldXY'),
@@ -11,6 +12,8 @@ module.exports = {
1112
PutTile: require('./PutTile'),
1213
Randomize: require('./Randomize'),
1314
RemoveTile: require('./RemoveTile'),
14-
Shuffle: require('./Shuffle')
15+
ReplaceByIndex: require('./ReplaceByIndex'),
16+
Shuffle: require('./Shuffle'),
17+
SwapByIndex: require('./SwapByIndex')
1518

1619
};

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

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ var DynamicTilemapLayer = new Class({
108108
return this;
109109
},
110110

111+
findByIndex: function (findIndex, skip, reverse)
112+
{
113+
return TilemapComponents.FindByIndex(findIndex, skip, reverse, this.layer);
114+
},
115+
111116
forEachTile: function (callback, context, tileX, tileY, width, height)
112117
{
113118
TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, this.layer);
@@ -150,61 +155,23 @@ var DynamicTilemapLayer = new Class({
150155
return TilemapComponents.RemoveTile(tileX, tileY, replaceWithNull, this.layer);
151156
},
152157

158+
replaceByIndex: function (findIndex, newIndex, tileX, tileY, width, height)
159+
{
160+
TilemapComponents.ReplaceByIndex(findIndex, newIndex, tileX, tileY, width, height, this.layer);
161+
return this;
162+
},
163+
153164
shuffle: function (tileX, tileY, width, height)
154165
{
155166
TilemapComponents.Shuffle(tileX, tileY, width, height, this.layer);
156167
return this;
157-
}
168+
},
158169

159-
// forEach: function (callback)
160-
// {
161-
// this.tileArray.forEach(callback);
162-
// },
163-
164-
// Returns Object containing:
165-
// {
166-
// alpha
167-
// frameWidth,
168-
// frameHeight,
169-
// frameX
170-
// frameY
171-
// id
172-
// index = the tile in the tileset to render
173-
// textureWidth = tileset texture size
174-
// textureHeight
175-
// tint
176-
// visible
177-
// width
178-
// x
179-
// y
180-
// }
181-
182-
// getTileAt: function (x, y)
183-
// {
184-
// var ix = (x|0);
185-
// var iy = (y|0);
186-
// var tiles = this.tileArray;
187-
// var index = iy * this.mapWidth + ix;
188-
189-
// if (index < tiles.length)
190-
// {
191-
// return tiles[index];
192-
// }
193-
194-
// return null;
195-
// },
196-
197-
// getTileAtIndex: function (index)
198-
// {
199-
// var tiles = this.tileArray;
200-
201-
// if (index < tiles.length)
202-
// {
203-
// return tiles[index];
204-
// }
205-
206-
// return null;
207-
// }
170+
swapByIndex: function (indexA, indexB, tileX, tileY, width, height)
171+
{
172+
TilemapComponents.SwapByIndex(indexA, indexB, tileX, tileY, width, height, this.layer);
173+
return this;
174+
}
208175

209176
});
210177

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ var StaticTilemapLayer = new Class({
269269
}
270270
},
271271

272+
findByIndex: function (findIndex, skip, reverse)
273+
{
274+
return TilemapComponents.FindByIndex(findIndex, skip, reverse, this.layer);
275+
},
276+
272277
forEachTile: function (callback, context, tileX, tileY, width, height)
273278
{
274279
TilemapComponents.ForEachTile(callback, context, tileX, tileY, width, height, this.layer);

0 commit comments

Comments
 (0)