Skip to content

Commit

Permalink
Add serializer for parallax plane speed factors
Browse files Browse the repository at this point in the history
  • Loading branch information
jobe-m committed Jul 25, 2024
1 parent c4e60d7 commit 315b395
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import korlibs.datastructure.iterators.*
import korlibs.image.atlas.*
import korlibs.image.format.*
import korlibs.io.file.*
import korlibs.korge.fleks.utils.*
import kotlinx.serialization.*
import kotlinx.serialization.EncodeDefault.Mode.NEVER

Expand Down Expand Up @@ -190,17 +191,18 @@ data class ParallaxConfig(
* Both attached layer types will scroll depending on their position on the parallax plane.
*/
@Serializable @SerialName("ParallaxPlaneConfig")
data class ParallaxPlaneConfig @OptIn(ExperimentalSerializationApi::class) constructor(
data class ParallaxPlaneConfig(
val offset: Int = 0,
val name: String,
val speedFactor: Float = 1f,
// TODO move parallaxPlaneSpeedFactors to static Parallax entity config (which is always re-created after deserialization)
@EncodeDefault(NEVER) var parallaxPlaneSpeedFactors: FloatArray = floatArrayOf(), // create empty list, the real size will come from the texture size
// val parallaxPlaneSpeedFactors: FloatArrayList = FloatArrayList(capacity = 0), // create empty list, the real size will come from the texture size
val selfSpeed: Float = 0f,
val attachedLayersFront: ArrayList<ParallaxAttachedLayerConfig>? = null,
val attachedLayersRear: ArrayList<ParallaxAttachedLayerConfig>? = null
)
) {
// This is computed after loading of parallax image data from Aseprite
@Serializable(with = ParallaxSpeedFactors::class)
lateinit var parallaxPlaneSpeedFactors: FloatArray
}

/**
* This is the configuration for layers which are attached to the parallax plane. These layers are moving depending
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package korlibs.korge.fleks.utils

import com.github.quillraven.fleks.*
import korlibs.datastructure.*
import korlibs.image.color.*
import korlibs.image.format.*
import korlibs.image.text.*
Expand Down Expand Up @@ -188,6 +189,16 @@ class SnapshotSerializer {
}
}

/**
* A special serializer to prohibit serialization of FloatArray in Parallax plane config.
* For some reason @Default
*/
object ParallaxSpeedFactors : KSerializer<FloatArray> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ParallaxSpeedFactors", PrimitiveKind.INT)
override fun serialize(encoder: Encoder, value: FloatArray) = encoder.encodeInt(0)
override fun deserialize(decoder: Decoder): FloatArray = floatArrayOf()
}

/**
* A serializer strategy for Korge [HorizontalAlign] type. The alignment ratio will be saved as double.
*/
Expand Down

0 comments on commit 315b395

Please sign in to comment.