All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.de.fabmax.kool.physics.character.CharacterControllerManager.kt Maven / Gradle / Ivy

There is a newer version: 0.15.1
Show newest version
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()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy