Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tiled: implement SpriteCaching for staggered & isometric maps #260

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,36 @@ class TiledTilesLayer(
}

private fun updateCacheIsometrically(cache: SpriteCache) {
TODO("Not yet implemented")
cacheAnimatedTilesIds.forEach { (id, tileDataId) ->
tiles[tileDataId]?.let {
val slice = it.updateFramesAndGetSlice()
cache.updateSprite(id, slice) {
// do nothing since we only want to update the slice and not anything else
}
}
}
}

private fun updateCacheStaggeredXAxis(cache: SpriteCache) {
TODO("Not yet implemented")
cacheAnimatedTilesIds.forEach { (id, tileDataId) ->
tiles[tileDataId]?.let {
val slice = it.updateFramesAndGetSlice()
cache.updateSprite(id, slice) {
// do nothing since we only want to update the slice and not anything else
}
}
}
}

private fun updateCacheStaggeredYAxis(cache: SpriteCache) {
TODO("Not yet implemented")
cacheAnimatedTilesIds.forEach { (id, tileDataId) ->
tiles[tileDataId]?.let {
val slice = it.updateFramesAndGetSlice()
cache.updateSprite(id, slice) {
// do nothing since we only want to update the slice and not anything else
}
}
}
}

private fun addToCacheOrthographically(cache: SpriteCache, x: Float, y: Float, scale: Float) {
Expand Down Expand Up @@ -194,15 +215,160 @@ class TiledTilesLayer(
}

private fun addToCacheIsometrically(cache: SpriteCache, x: Float, y: Float, scale: Float) {
TODO("Not yet implemented")
val tileWidth = tileWidth * scale
val tileHeight = tileHeight * scale
val offsetX = offsetX * scale
val offsetY = offsetY * scale

for (cy in height downTo 0) {
for (cx in 0 until width) {
val cid = getCoordId(cx, cy)
if (cid in tileData.indices) {
val tileData = tileData[cid].bitsToTileData(flipData)
tiles[tileData.id]?.let {
val slice = it.updateFramesAndGetSlice()
val spriteId =
cache.add(slice) {
val halfWidth = tileWidth * 0.5f
val halfHeight = tileHeight * 0.5f

val tx =
(cx * halfWidth) +
(cy * halfWidth) +
offsetX +
x +
it.offsetX * scale
val ty =
(cy * halfHeight) - (cx * halfHeight) +
offsetY +
y +
it.offsetY * scale

val scaleX = if (tileData.flipX) -scale else scale
val scaleY = if (tileData.flipY) -scale else scale
position.set(tx, ty)
this.scale.set(scaleX, scaleY)
rotation = tileData.rotation
color.set(tintColor ?: Color.WHITE)
}
if (it.frames.isNotEmpty()) {
cacheAnimatedTilesIds[spriteId] = tileData.id
}
cacheIds += spriteId
}
}
}
}
}

private fun addToCacheStaggeredXAxis(cache: SpriteCache, x: Float, y: Float, scale: Float) {
TODO("Not yet implemented")
val tileWidth = tileWidth * scale
val tileHeight = tileHeight * scale
val offsetX = offsetX * scale
val offsetY = offsetY * scale
val staggerIndexEven = staggerIndex == TiledMap.StaggerIndex.EVEN
val minXA = if (staggerIndexEven) 1 else 0
val minXB = if (staggerIndexEven) 0 else 1
for (cy in height downTo 0) {
for (cx in minXA until width step 2) {
val cid = getCoordId(cx, cy)
if (cid in tileData.indices) {
val tileData = tileData[cid].bitsToTileData(flipData)
tiles[tileData.id]?.let {
val slice = it.updateFramesAndGetSlice()
val spriteId =
cache.add(slice) {
val halfWidth = tileWidth * 0.5f
val halfHeight = tileHeight * 0.5f
val tx = cx * halfWidth + offsetX + x + it.offsetX * scale
val ty =
(cy * tileHeight) +
halfHeight +
offsetY +
y +
it.offsetY * scale

val scaleX = if (tileData.flipX) -scale else scale
val scaleY = if (tileData.flipY) -scale else scale
position.set(tx, ty)
this.scale.set(scaleX, scaleY)
rotation = tileData.rotation
color.set(tintColor ?: Color.WHITE)
}
if (it.frames.isNotEmpty()) {
cacheAnimatedTilesIds[spriteId] = tileData.id
}
cacheIds += spriteId
}
}
}
for (cx in minXB until width step 2) {
val cid = getCoordId(cx, cy)
if (cid in tileData.indices) {
val tileData = tileData[cid].bitsToTileData(flipData)
tiles[tileData.id]?.let {
val slice = it.updateFramesAndGetSlice()
val spriteId =
cache.add(slice) {
val halfWidth = tileWidth * 0.5f
val tx = cx * halfWidth + offsetX + x + it.offsetX * scale
val ty = cy * tileHeight + offsetY + y + it.offsetY * scale

val scaleX = if (tileData.flipX) -scale else scale
val scaleY = if (tileData.flipY) -scale else scale
position.set(tx, ty)
this.scale.set(scaleX, scaleY)
rotation = tileData.rotation
color.set(tintColor ?: Color.WHITE)
}
if (it.frames.isNotEmpty()) {
cacheAnimatedTilesIds[spriteId] = tileData.id
}
cacheIds += spriteId
}
}
}
}
}

private fun addToCacheStaggeredYAxis(cache: SpriteCache, x: Float, y: Float, scale: Float) {
TODO("Not yet implemented")
val tileWidth = tileWidth * scale
val tileHeight = tileHeight * scale
val offsetX = offsetX * scale
val offsetY = offsetY * scale
val halfWidth = tileWidth * 0.5f
val halfHeight = tileHeight * 0.5f
val staggerIndexValue = if (staggerIndex == TiledMap.StaggerIndex.EVEN) 0 else 1

for (cy in height downTo 0) {
val tileOffsetX = if (cy % 2 == staggerIndexValue) halfWidth else 0f
for (cx in 0 until width) {
val cid = getCoordId(cx, cy)
if (cid in tileData.indices) {
val tileData = tileData[cid].bitsToTileData(flipData)
tiles[tileData.id]?.let {
val slice = it.updateFramesAndGetSlice()
val spriteId =
cache.add(slice) {
val tx =
cx * tileWidth - tileOffsetX + offsetX + x + it.offsetX * scale
val ty = cy * halfHeight + offsetY + y + it.offsetY * scale

val scaleX = if (tileData.flipX) -scale else scale
val scaleY = if (tileData.flipY) -scale else scale
position.set(tx, ty)
this.scale.set(scaleX, scaleY)
rotation = tileData.rotation
color.set(tintColor ?: Color.WHITE)
}
if (it.frames.isNotEmpty()) {
cacheAnimatedTilesIds[spriteId] = tileData.id
}
cacheIds += spriteId
}
}
}
}
}

private fun renderOrthographically(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
}],
"nextlayerid":4,
"nextobjectid":1,
"orientation":"orthogonal",
"orientation":"staggered",
"renderorder":"right-down",
"staggeraxis":"x",
"staggerindex":"odd",
"tiledversion":"1.8.2",
"tileheight":12,
"tilesets":[
Expand Down
Loading