mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-14 04:24:05 +09:00
new item type, "Dynamic Item"; working text terminal
Former-commit-id: 81e6d836f5f1e6490027d38146a32d404cf9ce3e Former-commit-id: af6557340f9cd0ea0b67eb7a8825aeffe75f9d82
This commit is contained in:
58
src/net/torvald/terrarum/gameworld/MapLayer.kt
Normal file
58
src/net/torvald/terrarum/gameworld/MapLayer.kt
Normal file
@@ -0,0 +1,58 @@
|
||||
package net.torvald.terrarum.gameworld
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-01-17.
|
||||
*/
|
||||
class MapLayer(var width: Int, var height: Int) : Iterable<Byte> {
|
||||
|
||||
internal var data: Array<ByteArray>
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user