Last active
August 30, 2015 21:53
-
-
Save DaanVanYperen/5ad7cfff05d447b058cc to your computer and use it in GitHub Desktop.
odb cdi Pattern for Tweening
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
entity.edit() | |
.add(new Schedule() | |
.tween(colorA, colorB, 0.5f, Interpolation.exp5Out) | |
.wait(0.5f) | |
.deleteFromWorld()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Tint extends Component implements Tweenable<Tint> { | |
@Override | |
public Tint tween(Tint a, Tint b, float value) { | |
final Interpolation linear = Interpolation.linear; | |
final Color colorA = a.color; | |
final Color colorB = b.color; | |
color.r = linear.apply(colorA.r, colorB.r, value); | |
color.g = linear.apply(colorA.g, colorB.g, value); | |
color.b = linear.apply(colorA.b, colorB.b, value); | |
color.a = linear.apply(colorA.a, colorB.a, value); | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Support tweening between two component states. | |
* | |
* @author Daan van Yperen | |
*/ | |
public interface Tweenable<T extends Component & Tweenable<T>> { | |
/** | |
* Set to tween of two component states. Uses linear tween. | |
* | |
* @param a start state at zero. | |
* @param b end state at one. | |
* @param value tween (0..1) | |
* @return {@code this} | |
*/ | |
T tween(T a, T b, float value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tween between two component states. | |
* | |
* @author Daan van Yperen | |
* @see Tweenable | |
* @see net.mostlyoriginal.api.component.script.Schedule | |
*/ | |
public class TweenStep extends Step { | |
protected Tweenable a; | |
protected Tweenable b; | |
protected Interpolation interpolation; | |
protected M m; | |
protected float duration; | |
protected float runtime; | |
/** | |
* Setup tween between two component states. | |
* | |
* @todo lifecycle management of components. | |
* @param a component a starting state. Tweening does not release pooled components after use. | |
* @param b component b starting state. Tweening does not release pooled components after use. | |
* @param duration duration of tween, in seconds. | |
* @param interpolation method of interpolation. | |
*/ | |
public <T extends Component & Tweenable<T>> void setup(T a, T b, Interpolation interpolation, float duration) { | |
final Class<?> typeA = a.getClass(); | |
final Class<?> typeB = b.getClass(); | |
if ( typeA != typeB ) { | |
throw new IllegalArgumentException("Can't tween between different types " + typeA + " and " + typeB + "."); | |
} | |
Preconditions.checkArgument(duration != 0, "Duration cannot be zero."); | |
this.a = Preconditions.checkNotNull(a); | |
this.b = Preconditions.checkNotNull(b); | |
this.interpolation = Preconditions.checkNotNull(interpolation); | |
this.duration = duration; | |
} | |
@Override | |
@SuppressWarnings("unchecked") | |
public boolean act(float delta, Entity e) { | |
runtime += delta; | |
if (m == null) { | |
m = new M(a.getClass(), e.getWorld()); | |
System.out.println("Allocated new mapper, prob want to pool this."); | |
} | |
float tween = interpolation.apply(runtime / duration); | |
applyTween(e, tween); | |
return runtime > duration; | |
} | |
@SuppressWarnings("unchecked") | |
protected final void applyTween(Entity e, float tween) { | |
// apply tween to component, create if missing. | |
((Tweenable) m.create(e)) | |
.tween((Component) a, (Component) b, MathUtils.clamp(tween, 0, 1)); | |
} | |
@Override | |
public void reset() { | |
a = null; | |
b = null; | |
m = null; | |
interpolation = null; | |
duration = 0; | |
runtime = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment