map tiles are now saved!
This commit is contained in:
parent
8533be0af8
commit
a7a095ecbf
14 changed files with 220 additions and 151 deletions
|
|
@ -0,0 +1,64 @@
|
|||
package dev.adamko.kafkatorio.events.schema.converters
|
||||
|
||||
import dev.adamko.kafkatorio.events.schema.Colour
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
|
||||
/** True if any value is greater than 1, so the values are hexadecimal. */
|
||||
fun Colour.isHexadecimal(): Boolean {
|
||||
return red > 1f
|
||||
|| green > 1f
|
||||
|| blue > 1f
|
||||
|| alpha > 1f
|
||||
}
|
||||
|
||||
/** True if all values are between `[0..1]`. */
|
||||
fun Colour.isPercentage(): Boolean = !isHexadecimal()
|
||||
|
||||
fun Colour.toHexadecimal() = if (isPercentage()) {
|
||||
Colour(
|
||||
(red * 255).coerceIn(0f..255f),
|
||||
(green * 255).coerceIn(0f..255f),
|
||||
(blue * 255).coerceIn(0f..255f),
|
||||
(alpha * 255).coerceIn(0f..255f),
|
||||
)
|
||||
} else {
|
||||
this
|
||||
}
|
||||
|
||||
fun Colour.toPercentage() = if (isPercentage()) {
|
||||
this
|
||||
} else {
|
||||
Colour(
|
||||
(red / 255).coerceIn(0f..1f),
|
||||
(green / 255).coerceIn(0f..1f),
|
||||
(blue / 255).coerceIn(0f..1f),
|
||||
(alpha / 255).coerceIn(0f..1f),
|
||||
)
|
||||
}
|
||||
|
||||
fun Colour.toHexString(includeAlpha: Boolean = false): String {
|
||||
|
||||
val hexConverter: Float.() -> String = if (isHexadecimal()) {
|
||||
Float::decimalToHex
|
||||
} else {
|
||||
Float::percentageToHex
|
||||
}
|
||||
|
||||
val length = if (includeAlpha) 9 else 7
|
||||
return buildString(length) {
|
||||
append('#')
|
||||
append(red.hexConverter())
|
||||
append(green.hexConverter())
|
||||
append(blue.hexConverter())
|
||||
if (includeAlpha) {
|
||||
append(alpha.hexConverter())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Float.decimalToHex(): String =
|
||||
roundToInt().toString(16).padStart(2, ' ')
|
||||
|
||||
fun Float.percentageToHex(): String =
|
||||
times(255).roundToInt().toString(16).padStart(2, ' ')
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
package dev.adamko.kafkatorio.events.schema.converters
|
||||
|
||||
import dev.adamko.kafkatorio.events.schema.MAP_CHUNK_SIZE
|
||||
import dev.adamko.kafkatorio.events.schema.MapBoundingBox
|
||||
import dev.adamko.kafkatorio.events.schema.MapChunkPosition
|
||||
import dev.adamko.kafkatorio.events.schema.MapEntityPosition
|
||||
import dev.adamko.kafkatorio.events.schema.MapTilePosition
|
||||
import kotlin.math.floor
|
||||
|
||||
operator fun MapTilePosition.times(factor: Int) =
|
||||
MapTilePosition(x * factor, y * factor)
|
||||
|
||||
operator fun MapTilePosition.div(divisor: Int) =
|
||||
MapTilePosition(x / divisor, y / divisor)
|
||||
|
||||
operator fun MapTilePosition.plus(addend: Int) =
|
||||
MapTilePosition(x + addend, y + addend)
|
||||
|
||||
|
||||
fun MapTilePosition.toMapChunkPosition() =
|
||||
MapChunkPosition(
|
||||
floor(x.toDouble() / MAP_CHUNK_SIZE.toDouble()).toInt(),
|
||||
floor(y.toDouble() / MAP_CHUNK_SIZE.toDouble()).toInt(),
|
||||
)
|
||||
|
||||
|
||||
fun MapEntityPosition.toMapTilePosition() =
|
||||
MapChunkPosition(floor(x).toInt(), floor(y).toInt())
|
||||
|
||||
fun MapEntityPosition.toMapChunkPosition() =
|
||||
MapChunkPosition(
|
||||
floor(x / MAP_CHUNK_SIZE.toDouble()).toInt(),
|
||||
floor(y / MAP_CHUNK_SIZE.toDouble()).toInt(),
|
||||
)
|
||||
|
||||
|
||||
operator fun MapChunkPosition.times(factor: Int) =
|
||||
MapChunkPosition(x * factor, y * factor)
|
||||
|
||||
operator fun MapChunkPosition.plus(addend: Int) =
|
||||
MapChunkPosition(x + addend, y + addend)
|
||||
|
||||
operator fun MapChunkPosition.minus(subtrahend: Int) =
|
||||
MapChunkPosition(x - subtrahend, y - subtrahend)
|
||||
|
||||
|
||||
val MapChunkPosition.leftTopTile: MapTilePosition
|
||||
get() = MapTilePosition(
|
||||
x * MAP_CHUNK_SIZE,
|
||||
y * MAP_CHUNK_SIZE,
|
||||
)
|
||||
|
||||
val MapChunkPosition.rightBottomTile: MapTilePosition
|
||||
get() = leftTopTile + (MAP_CHUNK_SIZE - 1)
|
||||
|
||||
|
||||
val MapChunkPosition.boundingBox: MapBoundingBox
|
||||
get() = MapBoundingBox(leftTopTile, rightBottomTile)
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
package dev.adamko.kafkatorio.events.schema
|
||||
|
||||
import kotlin.math.roundToInt
|
||||
import kotlinx.serialization.EncodeDefault
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
|
||||
const val MAP_CHUNK_SIZE = 32
|
||||
|
||||
/**
|
||||
|
|
@ -16,14 +16,8 @@ const val MAP_CHUNK_SIZE = 32
|
|||
data class MapEntityPosition(
|
||||
val x: Double,
|
||||
val y: Double,
|
||||
) {
|
||||
)
|
||||
|
||||
fun toMapChunkPosition() = MapChunkPosition(
|
||||
(x / MAP_CHUNK_SIZE).toInt(),
|
||||
(y / MAP_CHUNK_SIZE).toInt(),
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
/** Coordinates of a chunk in a [SurfaceData] where each integer `x`/`y` represents a different
|
||||
* chunk.
|
||||
|
|
@ -35,19 +29,8 @@ data class MapEntityPosition(
|
|||
data class MapChunkPosition(
|
||||
val x: Int,
|
||||
val y: Int,
|
||||
) {
|
||||
)
|
||||
|
||||
fun toMapEntityPosition() = MapEntityPosition(
|
||||
x * MAP_CHUNK_SIZE.toDouble(),
|
||||
y * MAP_CHUNK_SIZE.toDouble(),
|
||||
)
|
||||
|
||||
fun toMapTilePosition() = MapChunkPosition(
|
||||
x * MAP_CHUNK_SIZE,
|
||||
y * MAP_CHUNK_SIZE,
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Coordinates of a tile in a chunk on a [SurfaceData] where each integer `x`/`y` represents a
|
||||
|
|
@ -59,15 +42,7 @@ data class MapChunkPosition(
|
|||
data class MapTilePosition(
|
||||
val x: Int,
|
||||
val y: Int,
|
||||
) {
|
||||
constructor(x: Number, y: Number) : this(x.toInt(), y.toInt())
|
||||
|
||||
fun toMapChunkPosition() = MapChunkPosition(
|
||||
x / MAP_CHUNK_SIZE,
|
||||
y / MAP_CHUNK_SIZE,
|
||||
)
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@Serializable
|
||||
|
|
@ -93,39 +68,4 @@ data class Colour(
|
|||
val blue: Float = 0f,
|
||||
@EncodeDefault
|
||||
val alpha: Float = 1f,
|
||||
) {
|
||||
|
||||
/** True if any value is greater than 1, so the values are hexadecimal. */
|
||||
fun isHexadecimal(): Boolean {
|
||||
return red > 1f
|
||||
|| green > 1f
|
||||
|| blue > 1f
|
||||
|| alpha > 1f
|
||||
}
|
||||
|
||||
/** True if all values are between `[0..1]`. */
|
||||
fun isPercentage(): Boolean = !isHexadecimal()
|
||||
|
||||
fun asHexadecimal() = if (isPercentage()) {
|
||||
Colour(
|
||||
(red * 255).coerceIn(0f..255f),
|
||||
(green * 255).coerceIn(0f..255f),
|
||||
(blue * 255).coerceIn(0f..255f),
|
||||
(alpha * 255).coerceIn(0f..255f),
|
||||
)
|
||||
} else {
|
||||
this
|
||||
}
|
||||
|
||||
fun asPercentage() = if (isPercentage()) {
|
||||
this
|
||||
} else {
|
||||
Colour(
|
||||
(red / 255).coerceIn(0f..1f),
|
||||
(green / 255).coerceIn(0f..1f),
|
||||
(blue / 255).coerceIn(0f..1f),
|
||||
(alpha / 255).coerceIn(0f..1f),
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ object FactorioObjectDataSerializer : JsonContentPolymorphicSerializer<FactorioO
|
|||
FactorioObjectData.ObjectName.LuaSurface -> SurfaceData.serializer()
|
||||
FactorioObjectData.ObjectName.MapChunk -> MapChunk.serializer()
|
||||
FactorioObjectData.ObjectName.LuaTile -> MapTile.serializer()
|
||||
FactorioObjectData.ObjectName.LuaTiles -> MapTiles.serializer()
|
||||
FactorioObjectData.ObjectName.LuaTiles -> MapTiles.serializer()
|
||||
FactorioObjectData.ObjectName.ConsoleChatMessage -> ConsoleChatMessage.serializer()
|
||||
null ->
|
||||
throw Exception("Unknown FactorioObjectData $key: '$type' ")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue