Skip to content

Commit

Permalink
add World.update with Duration argument (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
RefuX authored Aug 30, 2024
1 parent 8b17bb5 commit c370093
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/commonMain/kotlin/com/github/quillraven/fleks/world.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.github.quillraven.fleks.collection.MutableEntityBag
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
import kotlin.native.concurrent.ThreadLocal
import kotlin.time.Duration
import kotlin.time.DurationUnit

/**
* Snapshot for an [entity][Entity] that contains its [components][Component] and [tags][EntityTag].
Expand Down Expand Up @@ -448,6 +450,14 @@ class World internal constructor(
}
}

/**
* Updates all [enabled][IntervalSystem.enabled] [systems][IntervalSystem] of the world
* using the given [deltaTime].
*/
fun update(deltaTime: Duration) {
update(deltaTime.toDouble(DurationUnit.MILLISECONDS).toFloat())
}

/**
* Removes all [entities][Entity] of the world and calls the
* [onDispose][IntervalSystem.onDispose] function of each system.
Expand Down
22 changes: 22 additions & 0 deletions src/commonTest/kotlin/com/github/quillraven/fleks/WorldTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.quillraven.fleks.World.Companion.inject
import com.github.quillraven.fleks.collection.compareEntity
import com.github.quillraven.fleks.collection.compareEntityBy
import kotlin.test.*
import kotlin.time.Duration.Companion.milliseconds

private data class WorldTestComponent(
var x: Float = 0f,
Expand Down Expand Up @@ -316,6 +317,27 @@ internal class WorldTest {
assertEquals(0, w.system<WorldTestIteratingSystem>().numCalls)
}

@Test
fun updateWorldWithDurationOf1() {
val w = configureWorld {
injectables {
add("42")
}

systems {
add(WorldTestIntervalSystem())
add(WorldTestIteratingSystem())
}
}
w.system<WorldTestIteratingSystem>().enabled = false

w.update(1.milliseconds)

assertEquals(1f, w.deltaTime)
assertEquals(1, w.system<WorldTestIntervalSystem>().numCalls)
assertEquals(0, w.system<WorldTestIteratingSystem>().numCalls)
}

@Test
fun removeAllEntities() {
val w = configureWorld {}
Expand Down

0 comments on commit c370093

Please sign in to comment.