diff options
| author | Elizabeth Hunt <me@liz.coffee> | 2025-10-26 21:38:22 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <me@liz.coffee> | 2025-10-26 21:39:58 -0700 |
| commit | a8e5e723b7e1891c9b352261a3ee4c3d3563e8cf (patch) | |
| tree | 853df79c877d37d7e5d25f52b301aedcc3d5db55 /composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation | |
| parent | 395aa7d1c312e495517701be11c21425d9a5838e (diff) | |
| download | abstraction-engine-kt-a8e5e723b7e1891c9b352261a3ee4c3d3563e8cf.tar.gz abstraction-engine-kt-a8e5e723b7e1891c9b352261a3ee4c3d3563e8cf.zip | |
Diffstat (limited to 'composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation')
| -rw-r--r-- | composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation/AnimationComponents.kt | 35 | ||||
| -rw-r--r-- | composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation/AnimationSystem.kt | 14 |
2 files changed, 35 insertions, 14 deletions
diff --git a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation/AnimationComponents.kt b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation/AnimationComponents.kt index cb3708f..5a846fb 100644 --- a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation/AnimationComponents.kt +++ b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation/AnimationComponents.kt @@ -10,24 +10,29 @@ import kotlin.jvm.JvmInline enum class LoopMode { /** Play once **/ ONCE, + /** Repeat **/ LOOP, + /** Play forward, then backward, repeat **/ - PING_PONG + PING_PONG, } /** * Name of an animation clip. */ @JvmInline -value class AnimationName(val value: String) +value class AnimationName( + val value: String, +) /** * Name of a frame. */ @JvmInline -value class FrameName(val value: String) - +value class FrameName( + val value: String, +) /** * Animation Clip details. @@ -35,7 +40,7 @@ value class FrameName(val value: String) data class AnimationClip( val frameNames: List<FrameName>, val frameTicks: Int, - val loopMode: LoopMode = LoopMode.LOOP + val loopMode: LoopMode = LoopMode.LOOP, ) /** @@ -43,15 +48,19 @@ data class AnimationClip( */ data class SpriteSheet( val imagePath: String, - val frames: Map<FrameName, Rect> + val frames: Map<FrameName, Rect>, ) : Component -enum class AnimationDirection(val step: Int) { +enum class AnimationDirection( + val step: Int, +) { /** Play in forward direction **/ FORWARD(step = 1), + /** Play in reverse direction **/ - BACKWARD(step = -1); + BACKWARD(step = -1), } + /** * Current state of an animation. */ @@ -61,13 +70,17 @@ data class Animator( var frameIndex: Int = 0, var elapsedTicks: Int = 0, var playing: Boolean = true, - var direction: AnimationDirection = AnimationDirection.FORWARD + var direction: AnimationDirection = AnimationDirection.FORWARD, ) : Component { /** * Play a specific animation clip by name. */ - fun play(clipName: AnimationName, restart: Boolean = true) { - clipName.takeIf { currentClip != it || restart } + fun play( + clipName: AnimationName, + restart: Boolean = true, + ) { + clipName + .takeIf { currentClip != it || restart } .run { currentClip = clipName reset() diff --git a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation/AnimationSystem.kt b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation/AnimationSystem.kt index 7968250..db64ac6 100644 --- a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation/AnimationSystem.kt +++ b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/animation/AnimationSystem.kt @@ -7,8 +7,13 @@ import kotlin.time.Duration /** * Updates animation playback state for all [Animator] components in [World]. */ -class AnimationSystem(animationTickRate: Duration) : TickedSystem(animationTickRate) { - override fun update(world: World, ticks: Int) { +class AnimationSystem( + animationTickRate: Duration, +) : TickedSystem(animationTickRate) { + override fun update( + world: World, + ticks: Int, + ) { world.query(Animator::class).forEach { entity -> val animator = entity.get(Animator::class) if (!animator.playing) return@forEach @@ -30,7 +35,10 @@ class AnimationSystem(animationTickRate: Duration) : TickedSystem(animationTickR /** * Advances the animation to the next frame based on loop mode. */ - private fun advanceFrame(animator: Animator, clip: AnimationClip) { + private fun advanceFrame( + animator: Animator, + clip: AnimationClip, + ) { animator.frameIndex += animator.direction.step when (clip.loopMode) { |
