Skip to content

Commit

Permalink
Fix rendering of stacked tiles with offset (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jobe-m authored Oct 22, 2024
1 parent a522cb7 commit 4ab3c99
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import korlibs.korge.ldtk.*
import korlibs.korge.ldtk.view.*
import korlibs.memory.*
import korlibs.time.Stopwatch
import korlibs.math.max
import kotlin.collections.set
import kotlin.math.max


/**
Expand Down Expand Up @@ -216,12 +218,30 @@ class AssetStore {
val flipX = tile.f.hasBitSet(0)
val flipY = tile.f.hasBitSet(1)

val stackLevel =
if (dy != 0) tileMapData.data.getStackLevel(x, y + 1)
else tileMapData.data.getStackLevel(x, y)

tileMapData.data.set(x, y, stackLevel, value = Tile(tile = tileId, offsetX = dx, offsetY = dy, flipX = flipX, flipY = flipY, rotate = false).raw)
// tileMapData.push(x, y, value = Tile(tile = tileId, offsetX = dx, offsetY = dy, flipX = flipX, flipY = flipY, rotate = false))
// Get stack level depending on if the tile overlaps its neighbour cells i.e. the tile has an offset (dx, dy)
when {
(dx == 0 && dy == 0) -> {
val stackLevel = tileMapData.data.getStackLevel(x, y)
tileMapData.data.set(x, y, stackLevel, value = Tile(tile = tileId, offsetX = dx, offsetY = dy, flipX = flipX, flipY = flipY, rotate = false).raw)
}
(dx == 0 && dy != 0) -> {
val stackLevel = max(tileMapData.data.getStackLevel(x, y), tileMapData.data.getStackLevel(x, y + 1))
tileMapData.data.set(x, y, stackLevel, value = Tile(tile = tileId, offsetX = dx, offsetY = dy, flipX = flipX, flipY = flipY, rotate = false).raw)
tileMapData.data.set(x, y + 1, stackLevel, value = Tile.ZERO.raw)
}
(dx != 0 && dy == 0) -> {
val stackLevel = max(tileMapData.data.getStackLevel(x, y), tileMapData.data.getStackLevel(x + 1, y))
tileMapData.data.set(x, y, stackLevel, value = Tile(tile = tileId, offsetX = dx, offsetY = dy, flipX = flipX, flipY = flipY, rotate = false).raw)
tileMapData.data.set(x + 1, y, stackLevel, value = Tile.ZERO.raw)
}
else -> {
val stackLevel = max(tileMapData.data.getStackLevel(x, y), tileMapData.data.getStackLevel(x, y + 1), tileMapData.data.getStackLevel(x + 1, y), tileMapData.data.getStackLevel(x + 1, y + 1))
tileMapData.data.set(x, y, stackLevel, value = Tile(tile = tileId, offsetX = dx, offsetY = dy, flipX = flipX, flipY = flipY, rotate = false).raw)
tileMapData.data.set(x, y + 1, stackLevel, value = Tile.ZERO.raw)
tileMapData.data.set(x + 1, y, stackLevel, value = Tile.ZERO.raw)
tileMapData.data.set(x + 1, y + 1, stackLevel, value = Tile.ZERO.raw)
}
}
}
levelLayerTileMaps["${level}_${layer}"] = Pair(type, tileMapData)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package korlibs.korge.fleks.renderSystems

import com.github.quillraven.fleks.*
import com.github.quillraven.fleks.collection.*
import korlibs.event.*
import korlibs.image.color.*
import korlibs.korge.annotations.*
import korlibs.korge.fleks.assets.*
import korlibs.korge.fleks.components.*
import korlibs.korge.fleks.tags.*
import korlibs.korge.input.*
import korlibs.korge.render.*
import korlibs.korge.view.*
import korlibs.math.*
import korlibs.math.geom.*


Expand All @@ -28,6 +31,9 @@ class LDtkLevelMapRenderSystem(
private val family: Family = world.family { all(layerTag, LayerComponent, PositionComponent, LdtkLevelMapComponent) }
private val assetStore: AssetStore = world.inject(name = "AssetStore")

// Debugging layer rendering
private var renderLayer = 0

@OptIn(KorgeExperimental::class)
override fun renderInternal(ctx: RenderContext) {
// Sort level maps by their layerIndex
Expand All @@ -52,10 +58,13 @@ class LDtkLevelMapRenderSystem(

ctx.useBatcher { batch ->
for (l in 0 until tileMap.maxLevel) {
// val l = 2
val level =
if (renderLayer == 0) l
else (renderLayer - 1).clamp(0, l)

for (tx in 0 until xTiles) {
for (ty in 0 until yTiles) {
val tile = tileMap[tx, ty, l]
val tile = tileMap[tx, ty, level]
val info = tileSet.getInfo(tile.tile)
if (info != null) {
val px = x + (tx * tileSetWidth) + (tile.offsetX * offsetScale)
Expand Down Expand Up @@ -85,5 +94,20 @@ class LDtkLevelMapRenderSystem(

init {
name = layerTag.toString()

// For debugging layer rendering
keys {
justDown(Key.N0) { renderLayer = 0 }
justDown(Key.N1) { renderLayer = 1 }
justDown(Key.N2) { renderLayer = 2 }
justDown(Key.N3) { renderLayer = 3 }
justDown(Key.N4) { renderLayer = 4 }
justDown(Key.N5) { renderLayer = 5 }
justDown(Key.N6) { renderLayer = 6 }
justDown(Key.N7) { renderLayer = 7 }
justDown(Key.N8) { renderLayer = 8 }
justDown(Key.N9) { renderLayer = 9 }
}

}
}

0 comments on commit 4ab3c99

Please sign in to comment.