Skip to content

Commit

Permalink
When a system is added after configuration, call init. This mirrors t…
Browse files Browse the repository at this point in the history
…he onDestroy for system removal.
  • Loading branch information
RefuX committed Jul 10, 2024
1 parent 5caa655 commit 903c0bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/commonMain/kotlin/com/github/quillraven/fleks/world.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class World internal constructor(
if (system is IteratingSystem && (system is FamilyOnAdd || system is FamilyOnRemove)) {
updateAggregatedFamilyHooks(system.family)
}
system.onInit()
}

/**
Expand Down
35 changes: 33 additions & 2 deletions src/commonTest/kotlin/com/github/quillraven/fleks/WorldTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ private class WorldTestIteratingSystem(
}
}

private class WorldTestInitSystem : IteratingSystem(family { all(WorldTestComponent) }) {
private class WorldTestInitSystem(world: World = World.CURRENT_WORLD!!) :
IteratingSystem(world.family { all(WorldTestComponent) }, world = world) {
override fun onInit() {
super.onInit()
world.entity { it += WorldTestComponent() }
Expand All @@ -80,7 +81,8 @@ private class WorldTestInitSystem : IteratingSystem(family { all(WorldTestCompon
override fun onTickEntity(entity: Entity) = Unit
}

private class WorldTestInitSystemExtraFamily : IteratingSystem(family { all(WorldTestComponent) }) {
private class WorldTestInitSystemExtraFamily(world: World = World.CURRENT_WORLD!!) :
IteratingSystem(world.family { all(WorldTestComponent) }, world = world) {
val extraFamily = world.family { any(WorldTestComponent2).none(WorldTestComponent) }

override fun onInit() {
Expand Down Expand Up @@ -1126,4 +1128,33 @@ internal class WorldTest {
assertEquals(0, world.systems.size)
assertTrue(system1.disposed)
}

@Test
fun getFamilyAfterWorldCreationSystemAddedAfterWorldCreation() {
// WorldTestInitSystem creates an entity in its init block
// -> family must be dirty and has a size of 1
val w = configureWorld {}

w += WorldTestInitSystem(w)

val wFamily = w.family { all(WorldTestComponent) }

assertEquals(1, wFamily.mutableEntities.size)
assertEquals(1, wFamily.numEntities)
}

@Test
fun getFamilyWithinSystemConstructorSystemAddedAfterWorldCreation() {
// WorldTestInitSystemExtraFamily creates an entity in its init block and
// also a family with a different configuration that the system itself
// -> system family is empty and extra family contains 1 entity
val w = configureWorld {}

w += WorldTestInitSystemExtraFamily(w)

val s = w.system<WorldTestInitSystemExtraFamily>()

assertEquals(1, s.extraFamily.numEntities)
assertEquals(0, s.family.numEntities)
}
}

0 comments on commit 903c0bf

Please sign in to comment.