Skip to content

Commit bdea565

Browse files
committed
Better optimized calc/recalc faces methods for tiles
These eliminate the need for the preventRecalc method from v2. If an individual tile is changed with putTileAt, only the min number of faces will be recalculated (vs all faces being recalculated in v2)
1 parent dd8bbfa commit bdea565

4 files changed

Lines changed: 85 additions & 7 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var GetTileAt = require('./GetTileAt');
2+
var GetTilesWithin = require('./GetTilesWithin');
3+
4+
var CalculateFacesWithin = function (tileX, tileY, width, height, layer)
5+
{
6+
var above = null;
7+
var below = null;
8+
var left = null;
9+
var right = null;
10+
11+
var tiles = GetTilesWithin(tileX, tileY, width, height, layer);
12+
13+
for (var i = 0; i < tiles.length; i++)
14+
{
15+
var tile = tiles[i];
16+
17+
if (tile && tile.collides)
18+
{
19+
above = GetTileAt(tile.x, tile.y - 1, true, layer);
20+
below = GetTileAt(tile.x, tile.y + 1, true, layer);
21+
left = GetTileAt(tile.x - 1, tile.y, true, layer);
22+
right = GetTileAt(tile.x + 1, tile.y, true, layer);
23+
24+
tile.faceTop = (above && above.collides) ? false : true;
25+
tile.faceBottom = (below && below.collides) ? false : true;
26+
tile.faceLeft = (left && left.collides) ? false : true;
27+
tile.faceRight = (right && right.collides) ? false : true;
28+
}
29+
}
30+
};
31+
32+
module.exports = CalculateFacesWithin;

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

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var GetTileAt = require('./GetTileAt');
2+
3+
// Recalculate the faces, assuming only one tile location has been changed
4+
// Used internally to update faces quickly for PutTileAt/RemoveTileAt/etc. Alternate approach to
5+
// 951.
6+
var RecalculateFacesAt = function (tileX, tileY, layer)
7+
{
8+
var tile = GetTileAt(tileX, tileY, true, layer);
9+
var above = GetTileAt(tileX, tileY - 1, true, layer);
10+
var below = GetTileAt(tileX, tileY + 1, true, layer);
11+
var left = GetTileAt(tileX - 1, tileY, true, layer);
12+
var right = GetTileAt(tileX + 1, tileY, true, layer);
13+
var tileCollides = tile && tile.collides;
14+
15+
// Assume the changed tile has all interesting edges
16+
if (tileCollides)
17+
{
18+
tile.faceBottom = true;
19+
tile.faceBottom = true;
20+
tile.faceLeft = true;
21+
tile.faceRight = true;
22+
}
23+
24+
// Reset edges that are shared between tile and its neighbors
25+
if (above && above.collides)
26+
{
27+
if (tileCollides) { tile.faceTop = false; }
28+
above.faceBottom = !tileCollides;
29+
}
30+
31+
if (below && below.collides)
32+
{
33+
if (tileCollides) { tile.faceBottom = false; }
34+
below.faceTop = !tileCollides;
35+
}
36+
37+
if (left && left.collides)
38+
{
39+
if (tileCollides) { tile.faceLeft = false; }
40+
left.faceRight = !tileCollides;
41+
}
42+
43+
if (right && right.collides)
44+
{
45+
if (tileCollides) { tile.faceRight = false; }
46+
right.faceLeft = !tileCollides;
47+
}
48+
49+
return tile;
50+
};
51+
52+
module.exports = RecalculateFacesAt;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
PutTileAt: require('./PutTileAt'),
1515
PutTileAtWorldXY: require('./PutTileAtWorldXY'),
1616
Randomize: require('./Randomize'),
17-
RecalculateFaces: require('./RecalculateFaces'),
17+
CalculateFacesWithin: require('./CalculateFacesWithin'),
1818
RemoveTileAt: require('./RemoveTileAt'),
1919
RemoveTileAtWorldXY: require('./RemoveTileAtWorldXY'),
2020
ReplaceByIndex: require('./ReplaceByIndex'),

0 commit comments

Comments
 (0)