Created
March 6, 2023 08:07
-
-
Save fourlastor/e23aa41701b67771f835903fb54158a9 to your computer and use it in GitHub Desktop.
Tile merging for libgdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TileMerge { | |
/** | |
* Merges a grid of tiles together | |
* See <a href="https://love2d.org/wiki/TileMerging">original implementation for LÖVE2D</a> | |
*/ | |
public static ArrayList<Rectangle> mergeTiles(boolean[][] tiles, int width, int height) { | |
ArrayList<Rectangle> rectangles = new ArrayList<>(); | |
for (int x = -1; x <= width; x++) { | |
int startY = -1; | |
int endY = -1; | |
for (int y = -1; y <= height; y++) { | |
boolean inBounds = x >= 0 && x < width && y >= 0 && y < height; | |
if (inBounds && tiles[x][y]) { | |
if (startY < 0) { | |
startY = y; | |
} | |
endY = y; | |
} else if (startY >= 0) { | |
ArrayList<Rectangle> overlaps = new ArrayList<>(); | |
for (Rectangle r : rectangles) { | |
if (r.width == x - 1 && startY <= r.y && endY >= r.height) { | |
overlaps.add(r); | |
} | |
} | |
overlaps.sort(RECTANGLE_COMPARATOR); | |
for (Rectangle r : overlaps) { | |
if (startY < r.y) { | |
rectangles.add(new Rectangle(x, startY, x, r.y - 1)); | |
startY = (int) r.y; | |
} | |
if (startY == r.y) { | |
r.width += 1; | |
if (endY == r.height) { | |
startY = -1; | |
endY = -1; | |
} else if (endY > r.height) { | |
startY = (int) (r.height + 1); | |
} | |
} | |
} | |
if (startY >= 0) { | |
rectangles.add(new Rectangle(x, startY, x, endY)); | |
startY = -1; | |
endY = -1; | |
} | |
} | |
} | |
} | |
return rectangles; | |
} | |
private static final Comparator<Rectangle> RECTANGLE_COMPARATOR = (a, b) -> (int) (a.y - b.y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment