summaryrefslogtreecommitdiff
path: root/composeApp/src/commonTest/kotlin/coffee/liz/ecs/DAGWorldTest.kt
diff options
context:
space:
mode:
Diffstat (limited to 'composeApp/src/commonTest/kotlin/coffee/liz/ecs/DAGWorldTest.kt')
-rw-r--r--composeApp/src/commonTest/kotlin/coffee/liz/ecs/DAGWorldTest.kt187
1 files changed, 125 insertions, 62 deletions
diff --git a/composeApp/src/commonTest/kotlin/coffee/liz/ecs/DAGWorldTest.kt b/composeApp/src/commonTest/kotlin/coffee/liz/ecs/DAGWorldTest.kt
index bdf7e99..a9f6249 100644
--- a/composeApp/src/commonTest/kotlin/coffee/liz/ecs/DAGWorldTest.kt
+++ b/composeApp/src/commonTest/kotlin/coffee/liz/ecs/DAGWorldTest.kt
@@ -4,20 +4,28 @@ import kotlin.reflect.KClass
import kotlin.test.*
// Test systems for circular dependency test
-class CircularSystemA : System {
- override val dependencies: Set<KClass<out System>>
- get() = setOf(CircularSystemB::class)
- override fun update(world: World, deltaTime: Float) {}
+class Outside
+
+class CircularSystemA : System<Outside> {
+ override val dependencies get() = setOf(CircularSystemB::class)
+
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {}
}
class CircularSystemB : System {
override val dependencies: Set<KClass<out System>>
get() = setOf(CircularSystemA::class)
- override fun update(world: World, deltaTime: Float) {}
+
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {}
}
class DAGWorldTest {
-
@Test
fun `can create entities with unique ids`() {
val world = DAGWorld(emptyList())
@@ -60,13 +68,19 @@ class DAGWorldTest {
@Test
fun `query with multiple components returns intersection`() {
val world = DAGWorld(emptyList())
- val entity1 = world.createEntity()
- .add(Position(10f, 20f))
- .add(Velocity(1f, 2f))
- val entity2 = world.createEntity()
- .add(Position(30f, 40f))
- val entity3 = world.createEntity()
- .add(Velocity(3f, 4f))
+ val entity1 =
+ world
+ .createEntity()
+ .add(Position(10f, 20f))
+ .add(Velocity(1f, 2f))
+ val entity2 =
+ world
+ .createEntity()
+ .add(Position(30f, 40f))
+ val entity3 =
+ world
+ .createEntity()
+ .add(Velocity(3f, 4f))
world.update(0f) // Trigger cache update
@@ -80,25 +94,39 @@ class DAGWorldTest {
fun `systems execute in dependency order`() {
val executionOrder = mutableListOf<String>()
- val systemA = object : System {
- override fun update(world: World, deltaTime: Float) {
- executionOrder.add("A")
+ val systemA =
+ object : System {
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ executionOrder.add("A")
+ }
}
- }
- val systemB = object : System {
- override val dependencies = setOf(systemA::class)
- override fun update(world: World, deltaTime: Float) {
- executionOrder.add("B")
+ val systemB =
+ object : System {
+ override val dependencies = setOf(systemA::class)
+
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ executionOrder.add("B")
+ }
}
- }
- val systemC = object : System {
- override val dependencies = setOf(systemB::class)
- override fun update(world: World, deltaTime: Float) {
- executionOrder.add("C")
+ val systemC =
+ object : System {
+ override val dependencies = setOf(systemB::class)
+
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ executionOrder.add("C")
+ }
}
- }
val world = DAGWorld(listOf(systemC, systemA, systemB))
world.update(0f)
@@ -110,17 +138,25 @@ class DAGWorldTest {
fun `systems with no dependencies can execute in any order`() {
val executionOrder = mutableListOf<String>()
- val systemA = object : System {
- override fun update(world: World, deltaTime: Float) {
- executionOrder.add("A")
+ val systemA =
+ object : System {
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ executionOrder.add("A")
+ }
}
- }
- val systemB = object : System {
- override fun update(world: World, deltaTime: Float) {
- executionOrder.add("B")
+ val systemB =
+ object : System {
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ executionOrder.add("B")
+ }
}
- }
val world = DAGWorld(listOf(systemA, systemB))
world.update(0f)
@@ -144,11 +180,15 @@ class DAGWorldTest {
fun `system receives correct deltaTime`() {
var receivedDelta = 0f
- val system = object : System {
- override fun update(world: World, deltaTime: Float) {
- receivedDelta = deltaTime
+ val system =
+ object : System {
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ receivedDelta = deltaTime
+ }
}
- }
val world = DAGWorld(listOf(system))
world.update(0.016f)
@@ -160,11 +200,15 @@ class DAGWorldTest {
fun `systems can query entities during update`() {
var queriedEntities: Set<Entity>? = null
- val system = object : System {
- override fun update(world: World, deltaTime: Float) {
- queriedEntities = world.query<Position>()
+ val system =
+ object : System {
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ queriedEntities = world.query<Position>()
+ }
}
- }
val world = DAGWorld(listOf(system))
val entity = world.createEntity().add(Position(10f, 20f))
@@ -203,32 +247,51 @@ class DAGWorldTest {
// \ /
// D
- val systemA = object : System {
- override fun update(world: World, deltaTime: Float) {
- executionOrder.add("A")
+ val systemA =
+ object : System {
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ executionOrder.add("A")
+ }
}
- }
- val systemB = object : System {
- override val dependencies = setOf(systemA::class)
- override fun update(world: World, deltaTime: Float) {
- executionOrder.add("B")
+ val systemB =
+ object : System {
+ override val dependencies = setOf(systemA::class)
+
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ executionOrder.add("B")
+ }
}
- }
- val systemC = object : System {
- override val dependencies = setOf(systemA::class)
- override fun update(world: World, deltaTime: Float) {
- executionOrder.add("C")
+ val systemC =
+ object : System {
+ override val dependencies = setOf(systemA::class)
+
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ executionOrder.add("C")
+ }
}
- }
- val systemD = object : System {
- override val dependencies = setOf(systemB::class, systemC::class)
- override fun update(world: World, deltaTime: Float) {
- executionOrder.add("D")
+ val systemD =
+ object : System {
+ override val dependencies = setOf(systemB::class, systemC::class)
+
+ override fun update(
+ world: World,
+ deltaTime: Float,
+ ) {
+ executionOrder.add("D")
+ }
}
- }
val world = DAGWorld(listOf(systemD, systemC, systemB, systemA))
world.update(0f)