Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#124: add onEnabled/onDisabled functions to IntervalSystem #127

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}