blob: 638a316f4389eac076fbb0eecaecde3ab20864d8 (
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
|
package coffee.liz.ecs
import kotlin.time.Duration.Companion.nanoseconds
abstract class GameLoop<Outside> {
private var world: World<Outside>? = null
private var lastUpdate: Long? = null
fun load(world: World<Outside>) {
this.world = world
this.lastUpdate = null
}
abstract fun render(world: World<Outside>)
abstract fun outsideState(): Outside
fun loop(now: Long) {
val world = this.world ?: error("World not loaded")
val outsideState = outsideState()
val dt = (now - (lastUpdate ?: now)).nanoseconds
world.update(outsideState, dt)
render(world)
lastUpdate = now
}
}
|