summaryrefslogtreecommitdiff
path: root/composeApp/src/commonMain/kotlin/coffee/liz/ecs/TickedSystem.kt
blob: fa4566dd4f5b4dd4adff38c9ff88afd4a3c8fc46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package coffee.liz.ecs

import kotlin.time.Duration

abstract class TickedSystem<Outside>(
    protected val tickRate: Duration,
    private var lastTick: Duration = Duration.ZERO,
) : System<Outside> {
    abstract fun update(
        world: World<Outside>,
        state: Outside,
        ticks: Int,
    )

    override fun update(
        world: World<Outside>,
        state: Outside,
        deltaTime: Duration,
    ) {
        val ticks = ((deltaTime - lastTick) / tickRate).toInt()
        lastTick =
            if (ticks == 0) {
                (lastTick + deltaTime).also {
                    update(world, state, ticks)
                }
            } else {
                Duration.ZERO
            }
    }
}