make the json a little less verbose by having separate position types instead of a discriminator

This commit is contained in:
Adam 2022-01-09 16:33:23 +01:00
parent f41def72e2
commit b9190b1948
6 changed files with 87 additions and 21 deletions

View file

@ -1,18 +1,59 @@
package dev.adamko.kafkatorio.events.schema
import kotlin.math.roundToInt
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.Serializable
const val MAP_CHUNK_SIZE = 32
/**
* Coordinates of a [EntityData] on a map.
*
* A [MapEntityPosition] can be translated to a [MapChunkPosition] by dividing the `x`/`y` values
* by [32][MAP_CHUNK_SIZE].
*/
@Serializable
data class PositionData(
data class MapEntityPosition(
val x: Double,
val y: Double,
val type: PositionType?,
)
) {
fun toMapChunkPosition() = MapChunkPosition(
(x / MAP_CHUNK_SIZE).roundToInt(),
(y / MAP_CHUNK_SIZE).roundToInt(),
)
}
/** Coordinates of a chunk in a [SurfaceData] where each integer `x`/`y` represents a different
* chunk.
*
* A [MapChunkPosition] can be translated to a [MapEntityPosition] by multiplying the `x`/`y`
* values by [32][MAP_CHUNK_SIZE].
*/
@Serializable
enum class PositionType {
MAP, CHUNK, TILE,
data class MapChunkPosition(
val x: Int,
val y: Int,
) {
fun toMapEntityPosition() = MapEntityPosition(
x * MAP_CHUNK_SIZE.toDouble(),
y * MAP_CHUNK_SIZE.toDouble(),
)
}
/**
* Coordinates of a tile in a chunk on a [SurfaceData] where each integer `x`/`y` represents a
* different [MapTile].
*
* It rounds any `x`/`y` down to whole numbers.
*/
@Serializable
data class MapTilePosition(
val x: Int,
val y: Int,
) {
constructor(x: Number, y: Number) : this(x.toInt(), y.toInt())
}
/**

View file

@ -77,7 +77,7 @@ data class PlayerData(
val associatedCharactersUnitNumbers: List<UInt>,
val characterUnitNumber: UInt?,
val name: String,
val position: PositionData,
val position: MapEntityPosition,
val colour: Colour,
val chatColour: Colour,
val lastOnline: UInt,
@ -92,7 +92,7 @@ data class EntityData(
val health: Double?,
val healthRatio: Double,
val name: String,
val position: PositionData,
val position: MapEntityPosition,
val surfaceIndex: Int,
val type: String,
val unitNumber: UInt? = null,
@ -116,6 +116,7 @@ data class SurfaceData(
@Serializable
data class MapChunk(
val tiles: List<MapTile>,
val position: MapChunkPosition,
) : FactorioObjectData() {
@EncodeDefault
override val objectName = ObjectName.MapChunk
@ -124,7 +125,7 @@ data class MapChunk(
@Serializable
data class MapTile(
val prototypeName: String,
val position: PositionData,
val position: MapTilePosition,
val surfaceIndex: Int,
) : FactorioObjectData() {
@EncodeDefault