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

commonMain.ru.casperix.spine.Skeleton.kt Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package ru.casperix.spine

import ru.casperix.math.color.Color
import ru.casperix.math.color.Colors
import ru.casperix.math.color.float32.Color4f
import ru.casperix.math.quad_matrix.float32.Matrix3f
import ru.casperix.misc.toFloat
import ru.casperix.spine.animation.Animation
import ru.casperix.spine.animation.AnimationContext
import kotlin.time.Duration
import kotlin.time.DurationUnit

class Skeleton(
    val data: SkeletonData,
) {
    val bones: List
    val slots: List
    val ikConstraints: List
    val pathConstraints: List
    val physicsConstraints: List
    val transformConstraints: List

    val drawOrder = mutableListOf()

    var parentMatrix = Matrix3f.IDENTITY

    var color: Color = Colors.WHITE; private set
    var skin: Skin? = null; private set
    var time: Float = 0f; private set

    init {
        val boneCache = mutableListOf()
        bones = data.bones.map { boneData ->
            val bone = if (boneData.parent == null) {
                Bone(this, boneData, null)
            } else {
                val parent: Bone = boneCache[boneData.parent.index]
                val bone = Bone(this, boneData, parent)
                parent.children.add(bone)
                bone
            }
            boneCache.add(bone)
            bone
        }

        slots = data.slots.map { slotData ->
            val bone = bones[slotData.boneData.index]
            val slot = Slot(this, slotData, bone)
            drawOrder += slot
            slot
        }

        ikConstraints = data.ikConstraints.map {
            IkConstraint(this, it)
        }

        transformConstraints = data.transformConstraints.map {
            TransformConstraint(this, it)
        }

        pathConstraints = data.pathConstraints.map {
            PathConstraint(this, it)
        }

        physicsConstraints = data.physicConstraints.map {
            PhysicsConstraint(this, it)
        }

        color = Color4f(1f, 1f, 1f, 1f)

    }



    fun setSkin(skin: Skin?) {
        this.skin = skin
        slots.forEach {
            it.setToSetupPose()
        }
    }

    fun setTime(time: Float) {
        this.time = time
    }

    fun updateWorldTransform() {
        bones.forEach {
            it.updateTransform()
        }
    }

    fun getAttachment(slotIndex: Int, attachmentName: String): Attachment? {
        val actualSkin = skin ?: data.defaultSkin ?: return null
        return actualSkin.getAttachment(slotIndex, attachmentName)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy