commonMain.de.fabmax.kool.physics.character.CharacterControllerManager.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kool-physics Show documentation
Show all versions of kool-physics Show documentation
A multiplatform OpenGL / Vulkan graphics engine written in kotlin
package de.fabmax.kool.physics.character
import de.fabmax.kool.physics.PhysicsWorld
import de.fabmax.kool.util.BaseReleasable
expect fun CharacterControllerManager(world: PhysicsWorld): CharacterControllerManager
abstract class CharacterControllerManager : BaseReleasable() {
protected val _controllers = mutableListOf()
val controllers: List
get() = _controllers
protected val onAdvanceListener: (Float) -> Unit = { timeStep ->
for (i in controllers.indices) {
controllers[i].onAdvancePhysics(timeStep)
}
}
protected val onUpdateListener: (Float) -> Unit = { timeStep ->
for (i in controllers.indices) {
controllers[i].onPhysicsUpdate(timeStep)
}
}
fun createController(): CharacterController {
val ctrl = doCreateController()
_controllers += ctrl
return ctrl
}
open fun removeController(charController: CharacterController) {
_controllers -= charController
}
protected abstract fun doCreateController(): CharacterController
override fun release() {
val copyControllers = mutableListOf()
copyControllers += controllers
copyControllers.forEach { it.release() }
_controllers.clear()
super.release()
}
}