games.rednblack.editor.renderer.components.sprite.SpriteAnimationStateComponent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of runtime-libgdx Show documentation
Show all versions of runtime-libgdx Show documentation
HyperLap2D libGDX runtime to render exported scenes
The newest version!
package games.rednblack.editor.renderer.components.sprite;
import com.artemis.PooledComponent;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
import games.rednblack.editor.renderer.data.FrameRange;
import java.util.Comparator;
import java.util.Objects;
public class SpriteAnimationStateComponent extends PooledComponent {
public transient Array allRegions;
public transient Animation currentAnimation;
public float time = 0.0f;
public boolean paused = false;
private FrameRange lastFrameRange = null;
private int lastFPS = -1;
private Animation.PlayMode lastPlayMode = null;
public SpriteAnimationStateComponent() {
}
public void setAllRegions(Array allRegions) {
this.allRegions = sortAndGetRegions(allRegions);
}
public Animation get() {
return currentAnimation;
}
public void set(SpriteAnimationComponent sac) {
set(sac.frameRangeMap.get(sac.currentAnimation), sac.fps, sac.playMode);
}
public void set(FrameRange range, int fps, Animation.PlayMode playMode) {
if (Objects.equals(range, lastFrameRange) && fps == lastFPS && Objects.equals(playMode, lastPlayMode) && currentAnimation != null)
return;
Array textureRegions = new Array<>(range.endFrame - range.startFrame + 1);
for (int r = range.startFrame; r <= range.endFrame; r++) {
textureRegions.add(allRegions.get(r));
}
currentAnimation = new Animation(1f/fps, textureRegions, playMode);
time = 0.0f;
lastFrameRange = range;
lastFPS = fps;
lastPlayMode = playMode;
}
private Array sortAndGetRegions(Array regions) {
regions.sort(new SortRegionsComparator());
return regions;
}
@Override
public void reset() {
allRegions = null;
currentAnimation = null;
time = 0.0f;
paused = false;
lastFrameRange = null;
lastFPS = -1;
lastPlayMode = null;
}
private static class SortRegionsComparator implements Comparator {
@Override
public int compare(TextureAtlas.AtlasRegion o1, TextureAtlas.AtlasRegion o2) {
return Integer.compare(o1.index, o2.index);
}
}
}