generated from korlibs/korge-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
41 deletions.
There are no files selected for viewing
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
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 | ||
} |
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
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 | ||
} | ||
} |
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
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
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 } | ||
) | ||
} | ||
} |