TileImage: remove getFrameEndTime() method
use "x instanceof Animation" to determine if a tile is animated
This commit is contained in:
parent
ec7641eadf
commit
357a58f094
3 changed files with 12 additions and 72 deletions
|
@ -11,6 +11,7 @@ class Animation extends TileImage
|
||||||
{
|
{
|
||||||
static final int DEFAULT_DURATION = 125;
|
static final int DEFAULT_DURATION = 125;
|
||||||
List<Frame> frames = new ArrayList<Frame>();
|
List<Frame> frames = new ArrayList<Frame>();
|
||||||
|
int totalDuration;
|
||||||
|
|
||||||
public static Animation load(File aniFile)
|
public static Animation load(File aniFile)
|
||||||
throws IOException
|
throws IOException
|
||||||
|
@ -21,8 +22,10 @@ class Animation extends TileImage
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addFrame(Frame f)
|
public void addFrame(TileImage img, int duration)
|
||||||
{
|
{
|
||||||
|
totalDuration += duration;
|
||||||
|
Frame f = new Frame(img, totalDuration);
|
||||||
frames.add(f);
|
frames.add(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,11 +51,7 @@ class Animation extends TileImage
|
||||||
int duration = tmp != null ? Integer.parseInt(tmp) : DEFAULT_DURATION;
|
int duration = tmp != null ? Integer.parseInt(tmp) : DEFAULT_DURATION;
|
||||||
|
|
||||||
tmp = in.getElementText();
|
tmp = in.getElementText();
|
||||||
addFrame(
|
addFrame( MakeTiles.parseFrameSpec(tmp), duration );
|
||||||
new Frame(
|
|
||||||
MakeTiles.parseFrameSpec(tmp),
|
|
||||||
duration
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// unrecognized element
|
// unrecognized element
|
||||||
|
@ -98,17 +97,4 @@ class Animation extends TileImage
|
||||||
// draw nothing
|
// draw nothing
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getFrameEndTime(int frameTime)
|
|
||||||
{
|
|
||||||
int t = 0;
|
|
||||||
for (int i = 0; i < frames.size(); i++) {
|
|
||||||
t += frames.get(i).duration;
|
|
||||||
if (frameTime < t) {
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,8 +102,6 @@ public class MakeTiles
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawWithTimeTo(Graphics2D gr, int time, int destX, int destY, int srcX, int srcY) { throw new Error("not implemented"); }
|
public void drawWithTimeTo(Graphics2D gr, int time, int destX, int destY, int srcX, int srcY) { throw new Error("not implemented"); }
|
||||||
@Override
|
|
||||||
public int getFrameEndTime(int frameTime) { throw new Error("not implemented"); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static class Composer
|
static class Composer
|
||||||
|
@ -216,21 +214,15 @@ public class MakeTiles
|
||||||
|
|
||||||
static TileImage prepareFrames(TileImage ref, Composer c)
|
static TileImage prepareFrames(TileImage ref, Composer c)
|
||||||
{
|
{
|
||||||
if (ref.getFrameEndTime(0) > 0) {
|
if (ref instanceof Animation) {
|
||||||
|
|
||||||
Animation ani = new Animation();
|
Animation mc = (Animation) ref;
|
||||||
int t = 0;
|
Animation dest = new Animation();
|
||||||
int n = ref.getFrameEndTime(t);
|
for (Animation.Frame f : mc.frames) {
|
||||||
while (n > 0) {
|
TileImage s = prepareFrames(f.frame, c);
|
||||||
TileImageSprite s = c.prepareTile(TILE_SIZE);
|
dest.addFrame(s, f.duration);
|
||||||
Animation.Frame f = new Animation.Frame(s, n-t);
|
|
||||||
|
|
||||||
ani.addFrame(f);
|
|
||||||
|
|
||||||
t = n;
|
|
||||||
n = ref.getFrameEndTime(t);
|
|
||||||
}
|
}
|
||||||
return ani;
|
return dest;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
TileImageSprite s = c.prepareTile(TILE_SIZE);
|
TileImageSprite s = c.prepareTile(TILE_SIZE);
|
||||||
|
|
|
@ -13,13 +13,6 @@ public abstract class TileImage
|
||||||
drawWithTimeTo(gr, 0, destX, destY, srcX, srcY);
|
drawWithTimeTo(gr, 0, destX, destY, srcX, srcY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the end-time of the animation frame identified by frameTime;
|
|
||||||
* -1 if not an animation, or if frameTime is past the end of the animation
|
|
||||||
*/
|
|
||||||
public abstract int getFrameEndTime(int frameTime);
|
|
||||||
|
|
||||||
|
|
||||||
public static class TileImageLayer extends TileImage
|
public static class TileImageLayer extends TileImage
|
||||||
{
|
{
|
||||||
public final TileImageLayer below;
|
public final TileImageLayer below;
|
||||||
|
@ -39,27 +32,6 @@ public abstract class TileImage
|
||||||
}
|
}
|
||||||
above.drawWithTimeTo(gr, time, destX, destY, srcX, srcY);
|
above.drawWithTimeTo(gr, time, destX, destY, srcX, srcY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getFrameEndTime(int frameTime)
|
|
||||||
{
|
|
||||||
if (below == null) {
|
|
||||||
return above.getFrameEndTime(frameTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
int belowEnd = below.getFrameEndTime(frameTime);
|
|
||||||
int aboveEnd = above.getFrameEndTime(frameTime);
|
|
||||||
|
|
||||||
if (belowEnd < 0) {
|
|
||||||
return aboveEnd;
|
|
||||||
}
|
|
||||||
else if (aboveEnd < 0 || belowEnd < aboveEnd) {
|
|
||||||
return belowEnd;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return aboveEnd;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class TileImageSprite extends TileImage
|
public static class TileImageSprite extends TileImage
|
||||||
|
@ -78,11 +50,6 @@ public abstract class TileImage
|
||||||
{
|
{
|
||||||
source.drawWithTimeTo(gr, time, destX, destY, srcX+offsetX, srcY+offsetY);
|
source.drawWithTimeTo(gr, time, destX, destY, srcX+offsetX, srcY+offsetY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getFrameEndTime(int frameTime) {
|
|
||||||
return source.getFrameEndTime(frameTime);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class SourceImage extends TileImage
|
public static class SourceImage extends TileImage
|
||||||
|
@ -112,10 +79,5 @@ public abstract class TileImage
|
||||||
srcX+basisSize, srcY+basisSize,
|
srcX+basisSize, srcY+basisSize,
|
||||||
null);
|
null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getFrameEndTime(int frameTime) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue