Files
Terrarum/src/net/torvald/terrarum/gameworld/MapLayer.kt
Song Minjae c1d72c29af new actor type "HistoricalFigure" that has date of birth and death
Former-commit-id: ec7ca90f29e0c56b3553e87b5a95fd023d9c55e1
Former-commit-id: 685958754f78542843913731880eb73f90e8e4c9
2016-10-10 18:25:29 +09:00

59 lines
1.5 KiB
Kotlin

package net.torvald.terrarum.gameworld
/**
* Created by minjaesong on 16-01-17.
*/
class MapLayer(val width: Int, val height: Int) : Iterable<Byte> {
internal @Volatile var data: Array<ByteArray> // in parallel programming: do not trust your register; always read freshly from RAM!
init {
data = Array(height) { ByteArray(width) }
}
/**
* Returns an iterator over elements of type `T`.
* @return an Iterator.
*/
override fun iterator(): Iterator<Byte> {
return object : Iterator<Byte> {
private var iteratorCount = 0
override fun hasNext(): Boolean {
return iteratorCount < width * height
}
override fun next(): Byte {
val y = iteratorCount / width
val x = iteratorCount % width
// advance counter
iteratorCount += 1
return data[y][x]
}
}
}
internal fun getTile(x: Int, y: Int): Int? {
return if (x !in 0..width - 1 || y !in 0..height - 1)
null
else
data[y][x].toUint()
}
internal fun setTile(x: Int, y: Int, tile: Byte) {
data[y][x] = tile
}
fun isInBound(x: Int, y: Int) = (x >= 0 && y >= 0 && x < width && y < height)
companion object {
@Transient const val RANGE = 256
@Transient const val SIZEOF: Byte = 1 // 1 for 8-bit, 2 for 16-bit, ...
}
}
fun Byte.toUint() = java.lang.Byte.toUnsignedInt(this)