blob: f80c1ad2d9ff0f805949f7f55ea3df9f582bc76f (
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
31
32
33
34
|
package coffee.liz.ecs.input
import coffee.liz.ecs.TickedSystem
import coffee.liz.ecs.World
import kotlin.time.Duration
data class InputState(
val activeInputs: Set<String>,
)
interface InputProvider {
val inputState: InputState
}
class InputSystem<Outside : InputProvider>(
pollRate: Duration,
) : TickedSystem<Outside>(pollRate) {
private val activeInputs = mutableMapOf<String, Int>()
override fun update(
world: World<Outside>,
state: Outside,
ticks: Int,
) {
state.inputState.activeInputs.forEach {
activeInputs[it] = (activeInputs[it] ?: 0) + ticks
}
activeInputs.keys.retainAll(state.inputState.activeInputs)
world.query(Controlled::class).forEach {
it.get(Controlled::class).handleInput(activeInputs.toMap())
}
}
}
|