changeset 843:1c3e74f24db0

Merge branch 'main' of github.com:saemideluxe/semicongine
author Sam <sam@basx.dev>
date Sat, 02 Dec 2023 22:26:26 +0700
parents 44ec744fbedc (current diff) 611f9cee7495 (diff)
children 094342d38c69
files semicongine/animation.nim
diffstat 1 files changed, 12 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/animation.nim	Sat Dec 02 22:23:29 2023 +0700
+++ b/semicongine/animation.nim	Sat Dec 02 22:26:26 2023 +0700
@@ -34,7 +34,7 @@
     iterations: int
   AnimationPlayer*[T] = object
     animation*: Animation[T]
-    currentTime: float32
+    currentTime*: float32
     playing*: bool
     currentDirection: int
     currentIteration: int
@@ -125,17 +125,18 @@
     iterations: iterations
   )
 
-func resetPlayer*(player: var AnimationPlayer) =
+proc reset*(player: var AnimationPlayer) =
+  player.currentValue = player.animation.animationFunction(0)
   player.currentTime = 0
   player.currentDirection = if player.animation.direction == Backward: -1 else : 1
   player.currentIteration = player.animation.iterations
 
 
-func newAnimationPlayer*[T](animation: Animation[T]): AnimationPlayer[T] =
+proc newAnimationPlayer*[T](animation: Animation[T]): AnimationPlayer[T] =
   result = AnimationPlayer[T](animation: animation, playing: false)
-  result.resetPlayer()
+  result.reset()
 
-func newAnimationPlayer*[T](value: T = default(T)): AnimationPlayer[T] =
+proc newAnimationPlayer*[T](value: T = default(T)): AnimationPlayer[T] =
   newAnimationPlayer[T](newAnimation[T]((t: AnimationTime) => value, 0))
 
 func start*(player: var AnimationPlayer) =
@@ -148,11 +149,12 @@
   # TODO: check this function, not 100% correct I think
   if player.playing:
     player.currentTime += float32(player.currentDirection) * dt
-    if abs(player.currentTime) > player.animation.duration:
+    if not (0 <= player.currentTime and player.currentTime < player.animation.duration):
       dec player.currentIteration
+      # last iteration reached
       if player.currentIteration <= 0 and player.animation.iterations != 0:
         player.stop()
-        player.resetPlayer()
+      # more iterations
       else:
         case player.animation.direction:
           of Forward:
@@ -163,5 +165,7 @@
             player.currentDirection = -player.currentDirection
             player.currentTime += float32(player.currentDirection) * dt * 2'f32
 
-  player.currentValue = player.animation.animationFunction((abs(player.currentTime) / player.animation.duration) mod high(AnimationTime))
+    player.currentValue = player.animation.animationFunction(
+      max(low(AnimationTime), min(player.currentTime / player.animation.duration, high(AnimationTime)))
+    )
   return player.currentValue