diff --git a/src/commonMain/kotlin/com/github/quillraven/fleks/world.kt b/src/commonMain/kotlin/com/github/quillraven/fleks/world.kt index 6f11ab8..43e9c19 100644 --- a/src/commonMain/kotlin/com/github/quillraven/fleks/world.kt +++ b/src/commonMain/kotlin/com/github/quillraven/fleks/world.kt @@ -3,6 +3,8 @@ package com.github.quillraven.fleks import com.github.quillraven.fleks.World.Companion.CURRENT_WORLD import com.github.quillraven.fleks.collection.EntityBag import com.github.quillraven.fleks.collection.MutableEntityBag +import kotlinx.serialization.Contextual +import kotlinx.serialization.Serializable import kotlin.native.concurrent.ThreadLocal import kotlin.reflect.KClass @@ -204,10 +206,21 @@ fun configureWorld(entityCapacity: Int = 512, cfg: WorldConfiguration.() -> Unit /** * Snapshot for an [entity][Entity] that contains its [components][Component] and [tags][EntityTag]. */ +@Serializable data class Snapshot( - val components: List> = listOf(), - val tags: List> = listOf(), -) + val components: List>, + val tags: List>, +) { + constructor() : this(listOf(), listOf()) +} + +/** + * Utility function to manually create a [Snapshot]. + */ +@Suppress("UNCHECKED_CAST") +internal fun wildcardSnapshotOf(components: List>, tags: List>): Snapshot { + return Snapshot(components as List>, tags as List>) +} /** * A world to handle [entities][Entity] and [systems][IntervalSystem]. @@ -461,6 +474,7 @@ class World internal constructor( * Returns a list that contains all components of the given [entity] of this world. * If the entity does not have any components then an empty list is returned. */ + @Suppress("UNCHECKED_CAST") fun snapshotOf(entity: Entity): Snapshot { val comps = mutableListOf>() val tags = mutableListOf>() @@ -477,7 +491,7 @@ class World internal constructor( } } - return Snapshot(comps, tags) + return Snapshot(comps as List>, tags as List>) } /** diff --git a/src/commonTest/kotlin/com/github/quillraven/fleks/WorldTest.kt b/src/commonTest/kotlin/com/github/quillraven/fleks/WorldTest.kt index 77fbe2d..402dfe4 100644 --- a/src/commonTest/kotlin/com/github/quillraven/fleks/WorldTest.kt +++ b/src/commonTest/kotlin/com/github/quillraven/fleks/WorldTest.kt @@ -708,9 +708,9 @@ internal class WorldTest { val comp1 = WorldTestComponent() val comp2 = WorldTestComponent() val snapshot = mapOf( - Entity(3, version = 0u) to Snapshot(listOf(comp1, WorldTestComponent2()), emptyList()), - Entity(5, version = 0u) to Snapshot(listOf(comp2), emptyList()), - Entity(7, version = 0u) to Snapshot(listOf(), emptyList()) + Entity(3, version = 0u) to wildcardSnapshotOf(listOf(comp1, WorldTestComponent2()), emptyList()), + Entity(5, version = 0u) to wildcardSnapshotOf(listOf(comp2), emptyList()), + Entity(7, version = 0u) to wildcardSnapshotOf(listOf(), emptyList()) ) w.loadSnapshot(snapshot)