Skip to content

Commit

Permalink
Prohibit world modification during configuration stage.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaphore committed Nov 30, 2023
1 parent 1544ecc commit 782b8df
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ class FleksWrongConfigurationUsageException :

class FleksWrongSystemInterfaceException(system: KClass<*>, `interface`: KClass<*>) :
FleksException("System ${system.simpleName} cannot have interface ${`interface`.simpleName}")

class FleksWorldModificationDuringConfigurationException :
FleksException("Entities were added during world configuration.")
4 changes: 4 additions & 0 deletions src/commonMain/kotlin/com/github/quillraven/fleks/world.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ class WorldConfiguration(@PublishedApi internal val world: World) {
world.systems = it.systems.toTypedArray()
}

if (world.numEntities > 0) {
throw FleksWorldModificationDuringConfigurationException()
}

setUpAggregatedFamilyHooks()

world.systems.forEach { it.onInit() }
Expand Down
23 changes: 22 additions & 1 deletion src/commonTest/kotlin/com/github/quillraven/fleks/SystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ private class SystemTestIteratingSystem : IteratingSystem(
private class SystemTestEntityCreation : IteratingSystem(family { any(SystemTestComponent) }) {
var numTicks = 0

init {
override fun onInit() {
super.onInit()
world.entity { it += SystemTestComponent() }
}

Expand Down Expand Up @@ -176,6 +177,15 @@ private class SystemTestEnable(enabled: Boolean) : IntervalSystem(enabled = enab
override fun onTick() = Unit
}

private class AddEntityInConstructorSystem() : IntervalSystem() {

init {
world.entity { }
}

override fun onTick() = Unit
}

internal class SystemTest {
@Test
fun systemWithIntervalEachFrameGetsCalledEveryTime() {
Expand Down Expand Up @@ -499,4 +509,15 @@ internal class SystemTest {
assertFalse(system.enabledCall)
assertFalse(system.disabledCall)
}

@Test
fun testWorldModificationDuringConfiguration() {
assertFailsWith<FleksWorldModificationDuringConfigurationException> {
configureWorld {
systems {
add(AddEntityInConstructorSystem())
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ private class WorldTestIteratingSystem(
}

private class WorldTestInitSystem : IteratingSystem(family { all(WorldTestComponent) }) {
init {
override fun onInit() {
super.onInit()
world.entity { it += WorldTestComponent() }
}

Expand All @@ -76,7 +77,8 @@ private class WorldTestInitSystem : IteratingSystem(family { all(WorldTestCompon
private class WorldTestInitSystemExtraFamily : IteratingSystem(family { all(WorldTestComponent) }) {
val extraFamily = world.family { any(WorldTestComponent2).none(WorldTestComponent) }

init {
override fun onInit() {
super.onInit()
world.entity { it += WorldTestComponent2() }
}

Expand Down

0 comments on commit 782b8df

Please sign in to comment.