KStarbound/src/main/kotlin/ru/dbotthepony/kstarbound/defs/dungeon/TiledPartReader.kt
2024-04-22 16:41:05 +07:00

58 lines
1.7 KiB
Kotlin

package ru.dbotthepony.kstarbound.defs.dungeon
import com.google.common.collect.ImmutableList
import com.google.gson.JsonSyntaxException
import ru.dbotthepony.kommons.util.KOptional
import ru.dbotthepony.kstarbound.math.vector.Vector2i
import ru.dbotthepony.kstarbound.Starbound
import ru.dbotthepony.kstarbound.util.AssetPathStack
import java.util.stream.Stream
class TiledPartReader(part: DungeonPart, parts: Stream<String>) : PartReader(part) {
val maps: ImmutableList<TiledMap> = parts
.map { Starbound.locate(AssetPathStack.remap(it)) }
.peek { check(it.exists) { "Dungeon references part which does not exist: $it" } }
// well, i don't think anyone gonna patch THESE json files, i guess
// so ignore json patches and just read directly
.map {
try {
TiledMap(Starbound.gson.fromJson(it.jsonReader(), TiledMap.JsonData::class.java))
} catch (err: Throwable) {
throw JsonSyntaxException("Exception while reading tile map $it", err)
}
}
.collect(ImmutableList.toImmutableList())
// also why would you ever want multiple maps specified lmao
// it already has layers and everything else you would ever need
override val size: Vector2i by lazy {
if (maps.isEmpty())
return@lazy Vector2i.ZERO
Vector2i(maps.maxOf { it.size.x }, maps.maxOf { it.size.y })
}
override fun bind(def: DungeonDefinition) {
}
override fun <T> walkTiles(callback: TileCallback<T>): KOptional<T> {
for (map in maps) {
val result = map.walkTiles(callback)
if (result.isPresent) return result
}
return KOptional()
}
override fun <T> walkTilesAt(x: Int, y: Int, callback: TileCallback<T>): KOptional<T> {
for (map in maps) {
val result = map.walkTilesAt(x, y, callback)
if (result.isPresent) return result
}
return KOptional()
}
}