-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathAnimation.kt
More file actions
104 lines (89 loc) · 3.85 KB
/
Animation.kt
File metadata and controls
104 lines (89 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright 2024 Lambda
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.lambda.graphics.animation
import com.lambda.Lambda.mc
import com.lambda.util.extension.partialTicks
import com.lambda.util.math.lerp
import kotlin.math.abs
import kotlin.reflect.KProperty
class Animation(initialValue: Double, val update: (Double) -> Double) {
private var prevValue = initialValue
private var currValue = initialValue
/**
* Retrieves the current interpolated animation value.
*
* This operator function enables property delegation by returning the animation's
* current value as computed by the [value] method, which interpolates between the previous
* and current states.
*/
operator fun getValue(thisRef: Any?, property: KProperty<*>) = value()
/**
* Delegated setter that updates the animation's value.
*
* This operator function is triggered when a delegated property is assigned a new value. It sets both the previous
* and current values of the animation to the provided [valueIn].
*
* @param thisRef The object owning the delegated property (unused).
* @param property Metadata for the property being assigned (unused).
* @param valueIn The new value to set for the animation.
*/
operator fun setValue(thisRef: Any?, property: KProperty<*>, valueIn: Double) = setValue(valueIn)
/**
* Computes and returns the current interpolated animation value.
*
* This function uses linear interpolation between the previous and current animation values,
* leveraging the partial tick value from the rendering context (mc.partialTicks) to ensure smooth transitions.
*
* @return the interpolated animation value.
*/
fun value(): Double = lerp(mc.partialTicks, prevValue, currValue)
/**
* Resets the animation state by updating both the previous and current values.
*
* This ensures that the animation starts from a consistent value without any interpolation.
*
* @param valueIn The new value to set as both the previous and current animation state.
*/
fun setValue(valueIn: Double) {
prevValue = valueIn
currValue = valueIn
}
fun tick() {
prevValue = currValue
currValue = update(currValue)
}
companion object {
fun AnimationTicker.exp(min: () -> Double, max: () -> Double, speed: Double, flag: () -> Boolean) =
exp(min, max, { speed }, flag)
fun AnimationTicker.exp(min: Double, max: Double, speed: () -> Double, flag: () -> Boolean) =
exp({ min }, { max }, speed, flag)
fun AnimationTicker.exp(min: Double, max: Double, speed: Double, flag: () -> Boolean) =
exp({ min }, { max }, { speed }, flag)
fun AnimationTicker.exp(target: () -> Double, speed: Double) =
exp(target, target, { speed }, { true })
fun AnimationTicker.exp(min: () -> Double, max: () -> Double, speed: () -> Double, flag: () -> Boolean) =
Animation(min()) {
val min = min()
val max = max()
val target = if (flag()) max else min
if (abs(target - it) < CLAMP * abs(max - min)) target
else lerp(speed(), it, target)
}.apply(::register)
// Exponent animation never reaches target value
private const val CLAMP = 0.01
}
}