FXGL
FXGL copied to clipboard
Consider support for hexagonal grids
Some code to play with inside TilesetLoader:
// TODO: no support for stagger axis == "Y"
fun loadViewHex(layerName: String): Node {
log.debug("Loading view for layer $layerName")
val layer = map.getLayerByName(layerName)
// TODO: fix the "100"
val bufferBottom = WritableImage(
layer.width * map.tilewidth + 100,
layer.height * map.tileheight + 100
)
log.debug("Created buffer with size ${bufferBottom.width}x${bufferBottom.height}")
val delayedDrawings = arrayListOf<Runnable>()
var oldY = 0
for (i in 0 until layer.data.size) {
var gid = layer.data.get(i)
// empty tile
if (gid == 0)
continue
val tileset = findTileset(gid, map.tilesets)
// we offset because data is encoded as continuous
gid -= tileset.firstgid
// image destination
val x = i % layer.width
val y = i / layer.width
val isColumnEven = x % 2 == 0
val w = tileset.tilewidth
val h = tileset.tileheight
val sourceImage: Image
val srcx: Int
val srcy: Int
if (tileset.isSpriteSheet) {
sourceImage = loadImage(tileset.image, tileset.transparentcolor, tileset.imagewidth, tileset.imageheight)
// image source
val tilex = gid % tileset.columns
val tiley = gid / tileset.columns
srcx = tilex * w + tileset.margin + tilex * tileset.spacing
srcy = tiley * h + tileset.margin + tiley * tileset.spacing
} else {
// tileset is a collection of images
val tile = tileset.tiles.find { it.id == gid }
?: throw IllegalArgumentException("Tile with id=$gid not found")
sourceImage = loadImage(tile.image, tile.transparentcolor, tile.imagewidth, tile.imageheight)
srcx = 0
srcy = 0
}
log.debug("Writing to buffer: dst=${x * map.tilewidth},${y * map.tileheight}, w=$w,h=$h, src=$srcx,$srcy")
// TODO: why + 6?
val offsetX = -(map.hexsidelength + 6) * x
val offsetY = if (map.staggerindex == "even" && isColumnEven
|| map.staggerindex == "odd" && !isColumnEven)
map.tileheight / 2
else
0
// buffer.pixelWriter.setPixels(x * map.tilewidth + offsetX, y * map.tileheight + offsetY,
// w, h, sourceImage.pixelReader,
// srcx,
// srcy)
if (y > oldY) {
delayedDrawings.forEach { it.run() }
delayedDrawings.clear()
oldY = y
}
if (map.staggerindex == "odd" && isColumnEven
|| map.staggerindex == "even" && !isColumnEven) {
for (dy in 0 until h) {
for (dx in 0 until w) {
val c = sourceImage.pixelReader.getColor(srcx + dx, srcy + dy)
if (c != Color.TRANSPARENT) {
bufferBottom.pixelWriter.setColor(
x * map.tilewidth + offsetX + dx,
y * map.tileheight + offsetY + dy,
c
)
}
}
}
} else {
// we need delayed drawings to correctly draw shadows
delayedDrawings += Runnable {
for (dy in 0 until h) {
for (dx in 0 until w) {
val c = sourceImage.pixelReader.getColor(srcx + dx, srcy + dy)
if (c != Color.TRANSPARENT) {
bufferBottom.pixelWriter.setColor(
x * map.tilewidth + offsetX + dx,
y * map.tileheight + offsetY + dy,
c
)
}
}
}
}
}
}
return ImageView(bufferBottom)
}