com.uwsoft.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 overlap2d-runtime-libgdx Show documentation
Show all versions of overlap2d-runtime-libgdx Show documentation
overlap2d-runtime-libgdx provides functionality to load, manipulate and render scenes generated by Overlap2D.
The newest version!
package com.uwsoft.editor.renderer.components.sprite;
import com.badlogic.ashley.core.Component;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Array;
import com.uwsoft.editor.renderer.data.FrameRange;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SpriteAnimationStateComponent implements Component {
public Array allRegions;
public Animation currentAnimation;
public float time = 0.0f;
public SpriteAnimationStateComponent(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) {
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;
}
private Array sortAndGetRegions(Array regions) {
regions.sort(new SortRegionsComparator());
return regions;
}
private class SortRegionsComparator implements Comparator {
@Override
public int compare(TextureAtlas.AtlasRegion o1, TextureAtlas.AtlasRegion o2) {
int index1 = regNameToFrame(o1.name);
int index2 = regNameToFrame(o2.name);
return index1 < index2 ? -1 : index1 == index2 ? 0 : 1;
}
}
private int regNameToFrame(String name) {
final Pattern lastIntPattern = Pattern.compile("[^0-9]+([0-9]+)$");
Matcher matcher = lastIntPattern.matcher(name);
if (matcher.find()) {
String someNumberStr = matcher.group(1);
return Integer.parseInt(someNumberStr);
}
throw new RuntimeException(
"Frame name should be something like this '*0001', but not "
+ name + ".");
}
}