Skip to content

Commit b268015

Browse files
committed
Exp animation clamp
1 parent a574797 commit b268015

File tree

1 file changed

+17
-3
lines changed
  • common/src/main/kotlin/com/lambda/graphics/animation

1 file changed

+17
-3
lines changed

common/src/main/kotlin/com/lambda/graphics/animation/Animation.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.lambda.graphics.animation
33
import com.lambda.Lambda.mc
44
import com.lambda.util.math.MathUtils.lerp
55
import com.lambda.util.primitives.extension.partialTicks
6+
import kotlin.math.abs
67
import kotlin.reflect.KProperty
78

89
class Animation(initialValue: Double, val update: (Double) -> Double) {
@@ -24,16 +25,29 @@ class Animation(initialValue: Double, val update: (Double) -> Double) {
2425

2526
companion object {
2627
fun AnimationTicker.exp(min: Double, max: Double, speed: Double, flag: () -> Boolean) =
27-
Animation(min) {
28-
val target = if (flag()) max else min
29-
lerp(it, target, speed)
28+
exp({ min }, { max }, speed, flag)
29+
30+
fun AnimationTicker.exp(min: () -> Double, max: () -> Double, speed: Double, flag: () -> Boolean) =
31+
Animation(min()) {
32+
val target = if (flag()) max() else min()
33+
if (abs(target - it) < CLAMP) target
34+
else lerp(it, target, speed)
3035
}.apply(::register)
3136

3237
fun AnimationTicker.linear(min: Double, max: Double, step: Double, flag: () -> Boolean) =
3338
Animation(min) {
3439
val target = if (flag()) max else min
3540
target.coerceIn(it - step, it + step)
3641
}.apply(::register)
42+
43+
fun AnimationTicker.linear(min: () -> Double, max: () -> Double, step: Double, flag: () -> Boolean) =
44+
Animation(min()) {
45+
val target = if (flag()) max() else min()
46+
target.coerceIn(it - step, it + step)
47+
}.apply(::register)
48+
49+
// Exponent animation will never reach target value
50+
private const val CLAMP = 0.01
3751
}
3852
}
3953

0 commit comments

Comments
 (0)