Skip to content

Commit

Permalink
* fix empty Snapshot constructor
Browse files Browse the repository at this point in the history
* add kotlinx annotations to Snapshot
  • Loading branch information
Simon Klausner committed Oct 16, 2023
1 parent e61db4d commit 33c4751
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/commonMain/kotlin/com/github/quillraven/fleks/world.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<Component<*>> = listOf(),
val tags: List<UniqueId<*>> = listOf(),
)
val components: List<Component<out @Contextual Any>>,
val tags: List<UniqueId<out @Contextual Any>>,
) {
constructor() : this(listOf(), listOf())
}

/**
* Utility function to manually create a [Snapshot].
*/
@Suppress("UNCHECKED_CAST")
internal fun wildcardSnapshotOf(components: List<Component<*>>, tags: List<UniqueId<*>>): Snapshot {
return Snapshot(components as List<Component<out Any>>, tags as List<UniqueId<out Any>>)
}

/**
* A world to handle [entities][Entity] and [systems][IntervalSystem].
Expand Down Expand Up @@ -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<Component<*>>()
val tags = mutableListOf<UniqueId<*>>()
Expand All @@ -477,7 +491,7 @@ class World internal constructor(
}
}

return Snapshot(comps, tags)
return Snapshot(comps as List<Component<out Any>>, tags as List<UniqueId<out Any>>)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 33c4751

Please sign in to comment.