public interface Interpolator<T>
The interpolation function for objects.
The initial value and current value are instantiated only once (on construction) to reduce object allocation.
// Example: Interpolating on health class Health { public float hp; public float maxHp; public void set(Health o) { set(o.hp, o.maxHp); } public void set(float hp, float maxHp) { this.hp = hp; this.maxHp = maxHp; } } class LinearHealthInterpolator implements Interpolator<Health> { void interpolate(Health flyweight, Health initialValue, Health currentValue, float alpha) { // Note that we're cascading the interpolators to individual fields within the Health component. flyweight.hp = Interpolators.linearFloat().interpolate(initialValue.hp, currentValue.hp, alpha); flyweight.maxHp = Interpolators.linearFloat().interpolate(initialValue.maxHp, currentValue.maxHp, alpha); } } // Initialize the interpolated health value InterpolatedValue<Health> health = new InterpolatedValue<>(new LinearHealthInterpolator(), new Health(), new Health()); health.getInitialValue().set(10f, 10f); Health currentValue = health.getCurrentValue().set(10f, 10f); // Update the interpolated health value health.getInitialValue().set(health.getCurrentValue()); // before updating the value, the initial value needs to be set health.getCurrentValue().hp = 5f; // The current value can be updated any time afterwards // Use the interpolated value at 50% the current value Health flyweight = new Health(); health.getInterpolatedValue(flyweight, 0.5f); Assert.assertEquals(7.5f, flyweight.hp);
Modifier and Type | Method and Description |
---|---|
void |
interpolate(T flyweight,
T initialValue,
T currentValue,
float alpha)
The interpolation algorithm.
|
void interpolate(T flyweight, T initialValue, T currentValue, float alpha)
flyweight
- the flyweight value is where the results are storedinitialValue
- the initial valuecurrentValue
- the current valuealpha
- a value between 0.0f to 1.0f. When alpha is 0.0f, the value should always be the initial value. When alpha is at 1.0f, the value should always be the current value.