Skip to content

Commit

Permalink
#124: add onEnable/onDisable functions to IntervalSystem (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quillraven authored Nov 28, 2023
1 parent 4d20d3d commit 98f9428
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/commonMain/kotlin/com/github/quillraven/fleks/system.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,27 @@ data class Fixed(val step: Float) : Interval
*/
abstract class IntervalSystem(
val interval: Interval = EachFrame,
var enabled: Boolean = true,
enabled: Boolean = true,
/**
* Returns the [world][World] to which this system belongs.
*/
val world: World = World.CURRENT_WORLD ?: throw FleksWrongConfigurationUsageException()
) : EntityComponentContext(world.componentService) {

var enabled: Boolean = enabled
set(value) {
if (value == field) {
return
}

field = value
if (value) {
onEnable()
} else {
onDisable()
}
}

private var accumulator: Float = 0.0f

/**
Expand All @@ -49,6 +63,16 @@ abstract class IntervalSystem(
val deltaTime: Float
get() = if (interval is Fixed) interval.step else world.deltaTime

/**
* This function gets called whenever the system gets [enabled].
*/
open fun onEnable() = Unit

/**
* This function gets called whenever the system gets [disabled][enabled].
*/
open fun onDisable() = Unit

/**
* Updates the system according to its [interval]. This function gets called from [World.update] when
* the system is [enabled].
Expand Down
52 changes: 52 additions & 0 deletions src/commonTest/kotlin/com/github/quillraven/fleks/SystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ private class SystemTestIteratingSystemQualifiedInjectable(
override fun onTickEntity(entity: Entity) = Unit
}

private class SystemTestEnable(enabled: Boolean) : IntervalSystem(enabled = enabled) {
var enabledCall = false
var disabledCall = false

override fun onEnable() {
enabledCall = true
}

override fun onDisable() {
disabledCall = true
}

override fun onTick() = Unit
}

internal class SystemTest {
@Test
fun systemWithIntervalEachFrameGetsCalledEveryTime() {
Expand Down Expand Up @@ -431,4 +446,41 @@ internal class SystemTest {
val system = world.system<SystemTestEntityCreation>()
assertEquals(1, system.numTicks)
}

@Test
fun testOnEnabledDisabled() {
val world = configureWorld {
systems {
add(SystemTestEnable(false))
}
}
val system = world.system<SystemTestEnable>()

assertFalse(system.enabledCall)
assertFalse(system.disabledCall)

system.enabled = true
assertTrue(system.enabled)
assertTrue(system.enabledCall)
assertFalse(system.disabledCall)
system.enabledCall = false

system.enabled = false
assertFalse(system.enabled)
assertFalse(system.enabledCall)
assertTrue(system.disabledCall)
system.disabledCall = false

// should not call methods when value does not change
system.enabled = false
assertFalse(system.enabledCall)
assertFalse(system.disabledCall)

system.enabled = true
system.enabledCall = false
system.disabledCall = false
system.enabled = true
assertFalse(system.enabledCall)
assertFalse(system.disabledCall)
}
}

0 comments on commit 98f9428

Please sign in to comment.