Dungeon json parsing
This commit is contained in:
parent
186144c8cc
commit
97e28ca0ee
@ -21,6 +21,8 @@ class Color(r: Float, g: Float, b: Float, a: Float = 1f) : Vector4f(r, g, b, a)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val BLACK = Color(0f, 0f, 0f)
|
||||
val TRANSLUCENT = Color(0f, 0f, 0f, 0f)
|
||||
val WHITE = Color(1f, 1f, 1f)
|
||||
|
||||
val RED = Color(1f, 0f, 0f)
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ru.dbotthepony.kstarbound
|
||||
|
||||
import com.google.gson.JsonObject
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import org.lwjgl.Version
|
||||
import org.lwjgl.glfw.GLFW.glfwSetWindowShouldClose
|
||||
@ -9,6 +10,7 @@ import ru.dbotthepony.kbox2d.collision.shapes.PolygonShape
|
||||
import ru.dbotthepony.kstarbound.client.StarboundClient
|
||||
import ru.dbotthepony.kstarbound.defs.TileDefinition
|
||||
import ru.dbotthepony.kstarbound.defs.projectile.ProjectilePhysics
|
||||
import ru.dbotthepony.kstarbound.defs.world.dungeon.DungeonWorldDef
|
||||
import ru.dbotthepony.kstarbound.world.CHUNK_SIZE_FF
|
||||
import ru.dbotthepony.kstarbound.world.Chunk
|
||||
import ru.dbotthepony.kstarbound.world.ChunkPos
|
||||
|
@ -7,6 +7,9 @@ import ru.dbotthepony.kstarbound.api.PhysicalFS
|
||||
import ru.dbotthepony.kstarbound.api.getPathFolder
|
||||
import ru.dbotthepony.kstarbound.defs.*
|
||||
import ru.dbotthepony.kstarbound.defs.projectile.*
|
||||
import ru.dbotthepony.kstarbound.defs.world.SkyParameters
|
||||
import ru.dbotthepony.kstarbound.defs.world.SkyType
|
||||
import ru.dbotthepony.kstarbound.defs.world.dungeon.DungeonWorldDef
|
||||
import ru.dbotthepony.kstarbound.io.*
|
||||
import ru.dbotthepony.kstarbound.math.*
|
||||
import ru.dbotthepony.kvector.util2d.AABB
|
||||
@ -54,7 +57,9 @@ object Starbound : IVFS {
|
||||
.registerTypeAdapter(Vector2i::class.java, Vector2iTypeAdapter)
|
||||
.registerTypeAdapter(Poly::class.java, PolyTypeAdapter)
|
||||
|
||||
.also(ConfigurableProjectile::regisyterAdapters)
|
||||
.also(ConfigurableProjectile::registerGson)
|
||||
.also(SkyParameters::registerGson)
|
||||
.also(DungeonWorldDef::registerGson)
|
||||
|
||||
.registerTypeAdapter(DamageType::class.java, CustomEnumTypeAdapter(DamageType.values()).nullSafe())
|
||||
|
||||
|
@ -118,7 +118,7 @@ class ConfigurableProjectile : ConfigurableDefinition<ConfigurableProjectile, Co
|
||||
ConfigurableProjectile::power,
|
||||
)
|
||||
|
||||
fun regisyterAdapters(gson: GsonBuilder) {
|
||||
fun registerGson(gson: GsonBuilder) {
|
||||
gson.registerTypeAdapter(ConfigurableProjectile::class.java, ADAPTER)
|
||||
gson.registerTypeAdapter(ProjectilePhysics::class.java, CustomEnumTypeAdapter(ProjectilePhysics.values()).nullSafe())
|
||||
gson.registerTypeAdapter(ActionConfig::class.java, ActionConfig.ADAPTER)
|
||||
|
@ -0,0 +1,161 @@
|
||||
package ru.dbotthepony.kstarbound.defs.world
|
||||
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.TypeAdapter
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import ru.dbotthepony.kstarbound.io.ColorTypeAdapter
|
||||
import ru.dbotthepony.kstarbound.io.CustomEnumTypeAdapter
|
||||
import ru.dbotthepony.kstarbound.io.KTypeAdapter
|
||||
import ru.dbotthepony.kvector.vector.Color
|
||||
import ru.dbotthepony.kvector.vector.ndouble.Vector2d
|
||||
import kotlin.properties.Delegates
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class SkyParameters {
|
||||
var spaceLevel = 3000
|
||||
var ambientLightLevel = Color.BLACK
|
||||
var skyType = SkyType.ATMOSPHERIC
|
||||
var skyColoring = SkyColoring()
|
||||
var satellites = Array(0) { SkySatellite() }
|
||||
var planet: SkySatellite? = null
|
||||
var horizonImages = Array(0) { "" }
|
||||
var dayLength = 10_000.0
|
||||
var surfaceLevel = 1_200
|
||||
var seed = 0L
|
||||
|
||||
companion object {
|
||||
val ADAPTER = KTypeAdapter(::SkyParameters,
|
||||
SkyParameters::spaceLevel,
|
||||
SkyParameters::ambientLightLevel,
|
||||
SkyParameters::skyType,
|
||||
SkyParameters::skyColoring,
|
||||
SkyParameters::satellites,
|
||||
SkyParameters::planet,
|
||||
SkyParameters::horizonImages,
|
||||
SkyParameters::dayLength,
|
||||
SkyParameters::surfaceLevel,
|
||||
SkyParameters::seed,
|
||||
)
|
||||
|
||||
fun registerGson(gsonBuilder: GsonBuilder) {
|
||||
gsonBuilder.registerTypeAdapter(SkyParameters::class.java, ADAPTER)
|
||||
gsonBuilder.registerTypeAdapter(SkyColoringManifold::class.java, SkyColoringManifold.ADAPTER)
|
||||
gsonBuilder.registerTypeAdapter(SkyColoring::class.java, SkyColoring.ADAPTER)
|
||||
gsonBuilder.registerTypeAdapter(SkyType::class.java, CustomEnumTypeAdapter(SkyType.values()).nullSafe())
|
||||
SkySatellite.registerGson(gsonBuilder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SkyColoringManifold {
|
||||
var initial = Color.TRANSLUCENT
|
||||
var final = Color.TRANSLUCENT
|
||||
|
||||
companion object {
|
||||
val ADAPTER = object : TypeAdapter<SkyColoringManifold>() {
|
||||
override fun write(out: JsonWriter?, value: SkyColoringManifold?) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun read(reader: JsonReader): SkyColoringManifold {
|
||||
val instance = SkyColoringManifold()
|
||||
reader.beginArray()
|
||||
instance.initial = ColorTypeAdapter.read(reader)
|
||||
instance.final = ColorTypeAdapter.read(reader)
|
||||
reader.endArray()
|
||||
return instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SkyColoring {
|
||||
var mainColor = Color.TRANSLUCENT
|
||||
|
||||
var morningColors = SkyColoringManifold()
|
||||
var dayColors = SkyColoringManifold()
|
||||
var eveningColors = SkyColoringManifold()
|
||||
var nightColors = SkyColoringManifold()
|
||||
|
||||
var morningLightColor = Color.TRANSLUCENT
|
||||
var dayLightColor = Color.TRANSLUCENT
|
||||
var eveningLightColor = Color.TRANSLUCENT
|
||||
var nightLightColor = Color.TRANSLUCENT
|
||||
|
||||
companion object {
|
||||
val ADAPTER = KTypeAdapter(::SkyColoring,
|
||||
SkyColoring::mainColor,
|
||||
SkyColoring::morningColors,
|
||||
SkyColoring::dayColors,
|
||||
SkyColoring::eveningColors,
|
||||
SkyColoring::nightColors,
|
||||
SkyColoring::morningLightColor,
|
||||
SkyColoring::dayLightColor,
|
||||
SkyColoring::eveningLightColor,
|
||||
SkyColoring::nightLightColor,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class SkySatellite {
|
||||
class Layer {
|
||||
private inner class ImageDelegate : ReadWriteProperty<Layer, String> {
|
||||
private var value: String? = null
|
||||
|
||||
override fun getValue(thisRef: Layer, property: KProperty<*>): String {
|
||||
return value ?: throw IllegalStateException("Property is not initialized")
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: Layer, property: KProperty<*>, value: String) {
|
||||
val split = value.split('?')
|
||||
this.value = split[0]
|
||||
|
||||
for (prop in 1 until split.size) {
|
||||
val splitProp = split[prop].split('=')
|
||||
|
||||
if (splitProp.size == 1) {
|
||||
throw IllegalArgumentException("Invalid property: ${split[prop]}, near $prop, in $value")
|
||||
}
|
||||
|
||||
when (val propName = splitProp[0]) {
|
||||
"hueshift" -> hueShift = splitProp[1].toInt()
|
||||
"addmask" -> mask = splitProp[1]
|
||||
else -> LOGGER.warn("Unknown property $propName near $prop in $value")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var scale = 1.0
|
||||
var image by ImageDelegate()
|
||||
var hueShift = 0
|
||||
var mask: String? = null
|
||||
}
|
||||
|
||||
var pos = Vector2d.ZERO
|
||||
var layers = Array(0) { Layer() }
|
||||
|
||||
companion object {
|
||||
val ADAPTER = KTypeAdapter(::SkySatellite,
|
||||
SkySatellite::pos,
|
||||
SkySatellite::layers,
|
||||
)
|
||||
|
||||
val LAYER_ADAPTER = KTypeAdapter(::Layer,
|
||||
Layer::scale,
|
||||
Layer::image,
|
||||
Layer::hueShift,
|
||||
Layer::mask,
|
||||
)
|
||||
|
||||
private val LOGGER = LogManager.getLogger(SkySatellite::class.java)
|
||||
|
||||
fun registerGson(gsonBuilder: GsonBuilder) {
|
||||
gsonBuilder.registerTypeAdapter(SkySatellite::class.java, ADAPTER)
|
||||
gsonBuilder.registerTypeAdapter(Layer::class.java, LAYER_ADAPTER)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package ru.dbotthepony.kstarbound.defs.world
|
||||
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import ru.dbotthepony.kstarbound.io.IStringSerializable
|
||||
|
||||
enum class SkyType(vararg aliases: String) : IStringSerializable {
|
||||
ATMOSPHERIC,
|
||||
BARREN,
|
||||
;
|
||||
|
||||
private val aliases: Array<String> = Array(aliases.size) { aliases[it].uppercase() }
|
||||
|
||||
override fun match(name: String): Boolean {
|
||||
for (alias in aliases)
|
||||
if (name == alias)
|
||||
return true
|
||||
|
||||
return name == this.name
|
||||
}
|
||||
|
||||
override fun write(out: JsonWriter) {
|
||||
out.value(this.name)
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package ru.dbotthepony.kstarbound.defs.world
|
||||
|
||||
import com.google.gson.GsonBuilder
|
||||
import ru.dbotthepony.kstarbound.io.KTypeAdapter
|
||||
import ru.dbotthepony.kvector.vector.Color
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
class WorldProperties {
|
||||
var nonCombat = false
|
||||
|
||||
fun immutable(): ImmutableWorldProperties {
|
||||
return ImmutableWorldProperties(
|
||||
nonCombat = nonCombat
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val ADAPTER = KTypeAdapter(::WorldProperties,
|
||||
WorldProperties::nonCombat,
|
||||
)
|
||||
|
||||
fun registerGson(gsonBuilder: GsonBuilder) {
|
||||
gsonBuilder.registerTypeAdapter(WorldProperties::class.java, ADAPTER)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ImmutableWorldProperties(
|
||||
val nonCombat: Boolean
|
||||
)
|
@ -0,0 +1,25 @@
|
||||
package ru.dbotthepony.kstarbound.defs.world.dungeon
|
||||
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import ru.dbotthepony.kstarbound.io.IStringSerializable
|
||||
|
||||
enum class BeamUpRule(vararg aliases: String) : IStringSerializable {
|
||||
NOWHERE,
|
||||
ANYWHERE_WITH_WARNING("AnywhereWithWarning"),
|
||||
ANYWHERE("Anywhere"),
|
||||
;
|
||||
|
||||
private val aliases: Array<String> = Array(aliases.size) { aliases[it].uppercase() }
|
||||
|
||||
override fun match(name: String): Boolean {
|
||||
for (alias in aliases)
|
||||
if (name == alias)
|
||||
return true
|
||||
|
||||
return name == this.name
|
||||
}
|
||||
|
||||
override fun write(out: JsonWriter) {
|
||||
out.value(this.name)
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package ru.dbotthepony.kstarbound.defs.world.dungeon
|
||||
|
||||
import com.google.gson.GsonBuilder
|
||||
import ru.dbotthepony.kstarbound.defs.world.SkyParameters
|
||||
import ru.dbotthepony.kstarbound.defs.world.WorldProperties
|
||||
import ru.dbotthepony.kstarbound.io.CustomEnumTypeAdapter
|
||||
import ru.dbotthepony.kstarbound.io.KTypeAdapter
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
class DungeonWorldDef {
|
||||
var type by Delegates.notNull<DungeonType>()
|
||||
var dungeonWorld by Delegates.notNull<String>()
|
||||
var seed by Delegates.notNull<Long>()
|
||||
|
||||
var spawningEnabled = false
|
||||
var beamUpRule by Delegates.notNull<BeamUpRule>()
|
||||
|
||||
var disableDeathDrops = true
|
||||
|
||||
var worldProperties: WorldProperties? = null
|
||||
var skyParameters = SkyParameters()
|
||||
|
||||
/**
|
||||
* Milliseconds
|
||||
*/
|
||||
var tempWorldDeleteTime = -1L
|
||||
|
||||
var persistent = false
|
||||
var useUniverseClock = false
|
||||
|
||||
var planetType: String? = null
|
||||
var planetSize: String? = null
|
||||
|
||||
companion object {
|
||||
val ADAPTER = KTypeAdapter(::DungeonWorldDef,
|
||||
DungeonWorldDef::type,
|
||||
DungeonWorldDef::dungeonWorld,
|
||||
DungeonWorldDef::seed,
|
||||
DungeonWorldDef::spawningEnabled,
|
||||
DungeonWorldDef::beamUpRule,
|
||||
DungeonWorldDef::disableDeathDrops,
|
||||
DungeonWorldDef::worldProperties,
|
||||
DungeonWorldDef::skyParameters,
|
||||
DungeonWorldDef::tempWorldDeleteTime,
|
||||
DungeonWorldDef::persistent,
|
||||
DungeonWorldDef::useUniverseClock,
|
||||
DungeonWorldDef::planetType,
|
||||
DungeonWorldDef::planetSize,
|
||||
)
|
||||
|
||||
fun registerGson(gsonBuilder: GsonBuilder) {
|
||||
gsonBuilder.registerTypeAdapter(DungeonWorldDef::class.java, ADAPTER)
|
||||
gsonBuilder.registerTypeAdapter(BeamUpRule::class.java, CustomEnumTypeAdapter(BeamUpRule.values()).nullSafe())
|
||||
gsonBuilder.registerTypeAdapter(DungeonType::class.java, CustomEnumTypeAdapter(DungeonType.values()).nullSafe())
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package ru.dbotthepony.kstarbound.defs.world.dungeon
|
||||
|
||||
class CDungeonWorldDef {
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package ru.dbotthepony.kstarbound.defs.world.dungeon
|
||||
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import ru.dbotthepony.kstarbound.io.IStringSerializable
|
||||
|
||||
enum class DungeonType(vararg aliases: String) : IStringSerializable {
|
||||
FLOATING("FloatingDungeon"),
|
||||
TERRESTRIAL,
|
||||
;
|
||||
|
||||
private val aliases: Array<String> = Array(aliases.size) { aliases[it].uppercase() }
|
||||
|
||||
override fun match(name: String): Boolean {
|
||||
for (alias in aliases)
|
||||
if (name == alias)
|
||||
return true
|
||||
|
||||
return name == this.name
|
||||
}
|
||||
|
||||
override fun write(out: JsonWriter) {
|
||||
out.value(this.name)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user