net.mostlyoriginal.api.operation.temporal.TweenOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of contrib-plugin-operations Show documentation
Show all versions of contrib-plugin-operations Show documentation
Scheduled operations on entities and components.
package net.mostlyoriginal.api.operation.temporal;
import com.artemis.Component;
import com.artemis.Entity;
import com.artemis.utils.reflect.ClassReflection;
import com.artemis.utils.reflect.ReflectionException;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.MathUtils;
import net.mostlyoriginal.api.component.common.Tweenable;
import net.mostlyoriginal.api.operation.common.TemporalOperation;
import net.mostlyoriginal.api.plugin.extendedcomponentmapper.M;
import net.mostlyoriginal.api.utils.Preconditions;
/**
* Tween between two managed component states.
*
* From/to states are owned by this class, safe from garbage collection.
*
* @author Daan van Yperen
*/
public abstract class TweenOperation extends TemporalOperation {
protected final Component a;
protected final Component b;
protected M m;
public TweenOperation(Class type) {
try {
a = ClassReflection.newInstance(type);
b = ClassReflection.newInstance(type);
} catch (ReflectionException e) {
String error = "Couldn't instantiate object of type " + type.getName();
throw new RuntimeException(error, e);
}
}
@Override
public void act(float percentage, Entity e) {
applyTween(e, percentage);
}
@SuppressWarnings("unchecked")
protected final void applyTween(Entity e, float a) {
if (m == null) {
// resolve component mapper if not set yet.
// gets cleared every reset for non managed tweens.
m = M.getFor(this.a.getClass(), e.getWorld());
}
// apply tween to component, create if missing.
((Tweenable) m.create(e))
.tween(this.a, b, MathUtils.clamp(a, 0, 1));
}
public TweenOperation setup(Interpolation interpolation, float duration) {
Preconditions.checkArgument(duration != 0, "Duration cannot be zero.");
this.interpolation = Preconditions.checkNotNull(interpolation);
this.duration = duration;
return this;
}
@Override
public void reset() {
super.reset();
m = null; // might be after world reset.
}
@SuppressWarnings("unchecked")
public final T getFrom() {
return (T) a;
}
@SuppressWarnings("unchecked")
public final T getTo() {
return (T) b;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy