Skip to content

Commit

Permalink
More work
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Mar 2, 2024
1 parent 73b3a94 commit 740de9f
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 41 deletions.
81 changes: 81 additions & 0 deletions src/commonMain/kotlin/TileMapDataInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import korlibs.datastructure.*
import korlibs.korge.view.tiles.*

val TileMap.map: TileMapDataInfo get() = TileMapDataInfo(this.stackedIntMap)

// @TODO: Add stackedIntMap.version

inline class TileMapDataInfo(val data: IStackedIntArray2) {
/** Annotation of where in [startX] this stack would be placed in a bigger container, not used for set or get methods */
val startX: Int get() = data.startX
/** Annotation of where in [startY] this stack would be placed in a bigger container, not used for set or get methods */
val startY: Int get() = data.startY

/** [width] of the data available here, get and set methods use values in the range x=0 until [width] */
val width: Int get() = data.width
/** [height] of the data available here, get and set methods use values in the range y=0 until [height] */
val height: Int get() = data.height

/** The [empty] value that will be returned if the specified cell it out of bounds, or empty */
val empty: TileInfo get() = korlibs.korge.view.tiles.TileInfo(data.empty)
/** The maximum level of layers available on the whole stack */
val maxLevel: Int get() = data.maxLevel

/** Shortcut for [IStackedIntArray2.startX] + [IStackedIntArray2.width] */
val endX: Int get() = startX + width
/** Shortcut for [IStackedIntArray2.startY] + [IStackedIntArray2.height] */
val endY: Int get() = startY + height

operator fun set(x: Int, y: Int, data: TileInfo) = setLast(x, y, data)

operator fun set(x: Int, y: Int, level: Int, data: TileInfo) {
if (this.data.inside(x, y)) {
this.data[x, y, level] = data.data
}
}

operator fun get(x: Int, y: Int): TileInfo = getLast(x, y)

operator fun get(x: Int, y: Int, level: Int): TileInfo = korlibs.korge.view.tiles.TileInfo(this.data[x, y, level])

/** Number of values available at this [x], [y] */
fun getStackLevel(x: Int, y: Int): Int = this.data.getStackLevel(x, y)

/** Adds a new [value] on top of [x], [y] */
fun push(x: Int, y: Int, value: TileInfo) {
this.data.push(x, y, value.data)
}

/** Removes the last value at [x], [y] */
fun removeLast(x: Int, y: Int) {
this.data.removeLast(x, y)
}

/** Set the first [value] of a stack in the cell [x], [y] */
fun setFirst(x: Int, y: Int, value: TileInfo) {
set(x, y, 0, value)
}

/** Gets the first value of the stack in the cell [x], [y] */
fun getFirst(x: Int, y: Int): TileInfo {
val level = getStackLevel(x, y)
if (level == 0) return empty
return get(x, y, 0)
}

/** Gets the last value of the stack in the cell [x], [y] */
fun getLast(x: Int, y: Int): TileInfo {
val level = getStackLevel(x, y)
if (level == 0) return empty
return get(x, y, level - 1)
}

fun setLast(x: Int, y: Int, value: TileInfo) {
if (!inside(x, y)) return
val level = (getStackLevel(x, y) - 1).coerceAtLeast(0)
set(x, y, level, value)
}

/** Checks if [x] and [y] are inside this array in the range x=0 until [width] and y=0 until [height] ignoring startX and startY */
fun inside(x: Int, y: Int): Boolean = x >= 0 && y >= 0 && x < width && y < height
}
25 changes: 25 additions & 0 deletions src/commonMain/kotlin/TileSliceOrientation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import korlibs.korge.view.tiles.*
import korlibs.math.geom.slice.*

fun TileInfo(tile: Int, orientation: SliceOrientation = SliceOrientation.NORMAL, offsetX: Int = 0, offsetY: Int = 0): TileInfo =
TileInfo(tile, offsetX, offsetY, orientation.tileFlipX, orientation.tileFlipY, orientation.tileRot)

//val flipX = listOf(0, 1, 1, 0, 1, 1, 0, 0)
val SliceOrientation.tileFlipX: Boolean get() = raw == 1 || raw == 2 || raw == 4 || raw == 5
//val flipY = listOf(0, 0, 1, 1, 0, 1, 1, 0)
val SliceOrientation.tileFlipY: Boolean get() = raw == 2 || raw == 3 || raw == 5 || raw == 6
//val rot = listOf(0, 1, 0, 1, 0, 1, 0, 1)
val SliceOrientation.tileRot: Boolean get() = raw % 2 == 1

//val TILE_ORIENTATIONS =
val TileInfo.orientation: SliceOrientation
get() = when {
flipY -> when {
flipX -> if (rotate) SliceOrientation.MIRROR_HORIZONTAL_ROTATE_90 else SliceOrientation.ROTATE_180
else -> if (rotate) SliceOrientation.ROTATE_270 else SliceOrientation.MIRROR_HORIZONTAL_ROTATE_180
}
else -> when {
flipX -> if (rotate) SliceOrientation.ROTATE_90 else SliceOrientation.MIRROR_HORIZONTAL_ROTATE_0
else -> if (rotate) SliceOrientation.MIRROR_HORIZONTAL_ROTATE_270 else SliceOrientation.ROTATE_0
}
}
46 changes: 5 additions & 41 deletions src/commonMain/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import korlibs.datastructure.*
import korlibs.event.*
import korlibs.image.bitmap.*
import korlibs.korge.*
import korlibs.korge.scene.*
import korlibs.korge.view.*
import korlibs.image.color.*
import korlibs.image.format.*
import korlibs.image.tiles.*
import korlibs.io.file.std.*
import korlibs.korge.*
import korlibs.korge.input.*
import korlibs.korge.scene.*
import korlibs.korge.view.*
import korlibs.korge.view.tiles.*
import korlibs.math.geom.*
import korlibs.math.geom.slice.*
Expand All @@ -35,7 +35,8 @@ class MyScene : PixelatedScene(320, 240) {
//var tile = 11
var tile = 4
var orientation = SliceOrientation.NORMAL
tileMap.map[4, 4] = TileInfo(tile, orientation)
tileMap.map.push(4, 4, TileInfo(7))
tileMap.map.push(4, 4, TileInfo(tile, orientation))

fun updateOrientation(update: (SliceOrientation) -> SliceOrientation) {
orientation = update(orientation)
Expand All @@ -57,40 +58,3 @@ class MyScene : PixelatedScene(320, 240) {
//image(tiles)
}
}

fun TileInfo(tile: Int, rotation: SliceOrientation = SliceOrientation.NORMAL, offsetX: Int = 0, offsetY: Int = 0): TileInfo {
val flipX = listOf(0, 1, 1, 0, 1, 1, 0, 0)
val flipY = listOf(0, 0, 1, 1, 0, 1, 1, 0)
val rot = listOf(0, 1, 0, 1, 0, 1, 0, 1)
return TileInfo(tile, offsetX, offsetY, flipX[rotation.raw] != 0, flipY[rotation.raw] != 0, rot[rotation.raw] != 0)
}

val TileMap.map: TileMapDataInfo get() = TileMapDataInfo(this)

inline class TileMapDataInfo(val tileMap: TileMap) {
val data get() = tileMap.stackedIntMap

/** Annotation of where in [startX] this stack would be placed in a bigger container, not used for set or get methods */
val startX: Int get() = data.startX
/** Annotation of where in [startY] this stack would be placed in a bigger container, not used for set or get methods */
val startY: Int get() = data.startY

/** [width] of the data available here, get and set methods use values in the range x=0 until [width] */
val width: Int get() = data.width
/** [height] of the data available here, get and set methods use values in the range y=0 until [height] */
val height: Int get() = data.height

/** The [empty] value that will be returned if the specified cell it out of bounds, or empty */
val empty: TileInfo get() = TileInfo(data.empty)
/** The maximum level of layers available on the whole stack */
val maxLevel: Int get() = data.maxLevel

operator fun set(x: Int, y: Int, data: TileInfo) = set(x, y, 0, data)

operator fun set(x: Int, y: Int, level: Int, data: TileInfo) {
if (this.data.inside(x, y)) {
this.data[x, y, level] = data.data
tileMap.invalidate()
}
}
}
29 changes: 29 additions & 0 deletions src/commonTest/kotlin/TileSliceOrientationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import korlibs.math.*
import korlibs.math.geom.slice.*
import kotlin.test.*

class TileSliceOrientationTest {
@Test
fun testTileFlipXYRot() {
assertEquals(
listOf(0, 1, 1, 0, 1, 1, 0, 0),
(0 until 8).map { SliceOrientation(it).tileFlipX.toInt() }
)
assertEquals(
listOf(0, 0, 1, 1, 0, 1, 1, 0),
(0 until 8).map { SliceOrientation(it).tileFlipY.toInt() }
)
assertEquals(
listOf(0, 1, 0, 1, 0, 1, 0, 1),
(0 until 8).map { SliceOrientation(it).tileRot.toInt() }
)
}

@Test
fun testTileOrientation() {
assertEquals(
(0 until 8).map { SliceOrientation(it) },
(0 until 8).map { TileInfo(0, SliceOrientation(it)).orientation }
)
}
}

0 comments on commit 740de9f

Please sign in to comment.