KStarbound/src/main/kotlin/ru/dbotthepony/kstarbound/world/Helpers.kt

65 lines
1.5 KiB
Kotlin

package ru.dbotthepony.kstarbound.world
import ru.dbotthepony.kstarbound.world.api.CHUNK_SIZE
import ru.dbotthepony.kstarbound.world.api.IChunk
import ru.dbotthepony.kstarbound.world.api.IChunkCell
import ru.dbotthepony.kstarbound.world.api.ITileAccess
import ru.dbotthepony.kstarbound.world.api.ITileState
import ru.dbotthepony.kvector.vector.Vector2i
fun IChunk.iterate(fromX: Int = 0, fromY: Int = 0, toX: Int = fromX + CHUNK_SIZE, toY: Int = fromY + CHUNK_SIZE): Iterator<Pair<Vector2i, IChunkCell>> {
return object : Iterator<Pair<Vector2i, IChunkCell>> {
private var x = fromX
private var y = fromY
override fun hasNext(): Boolean {
return x < toX && y < toY
}
override fun next(): Pair<Vector2i, IChunkCell> {
if (!hasNext())
throw NoSuchElementException()
val tile = getCell(x, y)
val pos = Vector2i(x, y)
x++
if (x >= toX) {
y++
x = 0
}
return pos to tile
}
}
}
fun ITileAccess.iterateTiles(fromX: Int = 0, fromY: Int = 0, toX: Int = fromX + CHUNK_SIZE, toY: Int = fromY + CHUNK_SIZE): Iterator<Pair<Vector2i, ITileState>> {
return object : Iterator<Pair<Vector2i, ITileState>> {
private var x = fromX
private var y = fromY
override fun hasNext(): Boolean {
return x < toX && y < toY
}
override fun next(): Pair<Vector2i, ITileState> {
if (!hasNext())
throw NoSuchElementException()
val tile = getTile(x, y)
val pos = Vector2i(x, y)
x++
if (x >= toX) {
y++
x = 0
}
return pos to tile
}
}
}