Skip to content

Commit 9cd4157

Browse files
committed
Fixed case where tiles are getting off grid, throwing an exception
1 parent 60b1fa4 commit 9cd4157

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

C7Engine/C7GameData/Tile.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -640,35 +640,46 @@ public Tile GetTileAtNeighborIndex(int neighborIndex) {
640640
/// <returns></returns>
641641
public Tile FindInRing(int rank, Func<Tile, bool> predicate, bool clockwise = true) {
642642
Tile startingTile = map.tileAt(this.XCoordinate, this.YCoordinate - (2 * rank));
643-
644-
if (predicate(startingTile)) return startingTile;
645-
646643
int dx = startingTile.XCoordinate;
647644
int dy = startingTile.YCoordinate;
648645

646+
if (startingTile != Tile.NONE && predicate(startingTile)) return startingTile;
647+
649648
// Going SW(counter-clockwise) or SE(clockwise)
650649
for (int _ = 1; _ < (2 * rank) + 1; _++) {
651650
if (clockwise) { dx++; dy++; } else { dx--; dy++; }
652-
if (predicate(map.tileAt(dx, dy))) return map.tileAt(dx, dy);
651+
if (!IsInValidCoordinates(dx, dy, out Tile currentTile) || !predicate(currentTile)) continue;
652+
return currentTile;
653653
}
654654
// Going SE(counter-clockwise) or SW(clockwise)
655655
for (int _ = 1; _ < (2 * rank) + 1; _++) {
656656
if (clockwise) { dx--; dy++; } else { dx++; dy++; }
657-
if (predicate(map.tileAt(dx, dy))) return map.tileAt(dx, dy);
657+
if (!IsInValidCoordinates(dx, dy, out Tile currentTile) || !predicate(currentTile)) continue;
658+
return currentTile;
658659
}
659660
// Going NE(counter-clockwise) or NW(clockwise)
660661
for (int _ = 1; _ < (2 * rank) + 1; _++) {
661662
if (clockwise) { dx--; dy--; } else { dx++; dy--; }
662-
if (predicate(map.tileAt(dx, dy))) return map.tileAt(dx, dy);
663+
if (!IsInValidCoordinates(dx, dy, out Tile currentTile) || !predicate(currentTile)) continue;
664+
return currentTile;
663665
}
664666
// Going NW(counter-clockwise) or NE(clockwise)
665667
for (int _ = 1; _ < (2 * rank); _++) {
666668
if (clockwise) { dx++; dy--; } else { dx--; dy--; }
667-
if (predicate(map.tileAt(dx, dy))) return map.tileAt(dx, dy);
669+
if (!IsInValidCoordinates(dx, dy, out Tile currentTile) || !predicate(currentTile)) continue;
670+
return currentTile;
668671
}
669672
return null;
670673
}
671674

675+
private bool IsInValidCoordinates(int dx, int dy, out Tile currentTile)
676+
{
677+
if (map.wrapHorizontally) dx %= map.numTilesWide;
678+
if (map.wrapVertically) dy %= map.numTilesTall;
679+
currentTile = map.tileAt(dx, dy);
680+
return currentTile != null && currentTile != Tile.NONE;
681+
}
682+
672683
// Returns the tiles in the spiral ordering defined by
673684
// GetTileAtNeighborIndex(i).
674685
public List<Tile> GetTilesWithinRankDistance(int rank) {

0 commit comments

Comments
 (0)