TileImage: eliminate redundant Animation class

This commit is contained in:
Jason Long 2014-09-20 12:43:09 -04:00
parent 5863323d2f
commit f278a8bc19
3 changed files with 22 additions and 40 deletions

View file

@ -59,6 +59,23 @@ public class Animation extends TileImage
frames.add(f);
}
public TileImage getFrameByTime(int acycle)
{
assert frames.size() >= 1;
assert totalDuration > 0;
int t = (acycle*125) % totalDuration;
int nframesLessOne = frames.size() - 1;
for (int i = 0; i < nframesLessOne; i++) {
Frame f = frames.get(i);
t -= f.duration;
if (t < 0) {
return f.frame;
}
}
return frames.get(nframesLessOne).frame;
}
void load(XMLStreamReader in, LoaderContext ctx)
throws XMLStreamException
{

View file

@ -170,41 +170,6 @@ public abstract class TileImage
return img;
}
public static class AnimatedTile extends TileImage
{
public SimpleTileImage [] frames;
public SimpleTileImage getFrameByTime(int acycle)
{
return frames[acycle % frames.length];
}
@Override
public void drawFragment(Graphics2D gr, int destX, int destY, int srcX, int srcY) {
throw new UnsupportedOperationException();
}
}
static AnimatedTile readAnimation(XMLStreamReader in, LoaderContext ctx)
throws XMLStreamException
{
assert in.getLocalName().equals("animation");
ArrayList<SimpleTileImage> frames = new ArrayList<SimpleTileImage>();
while (in.nextTag() != XMLStreamConstants.END_ELEMENT) {
String tagName = in.getLocalName();
if (tagName.equals("frame")) {
frames.add(readSimpleImage(in, ctx));
}
skipToEndElement(in);
}
AnimatedTile anim = new AnimatedTile();
anim.frames = frames.toArray(new SimpleTileImage[0]);
return anim;
}
static TileImage readLayeredImage(XMLStreamReader in, LoaderContext ctx)
throws XMLStreamException
{
@ -242,7 +207,7 @@ public abstract class TileImage
return readSimpleImage(in, ctx);
}
else if (tagName.equals("animation")) {
return readAnimation(in, ctx);
return Animation.read(in, ctx);
}
else if (tagName.equals("layered-image")) {
return readLayeredImage(in, ctx);

View file

@ -18,7 +18,7 @@ import javax.swing.*;
import javax.xml.stream.*;
import micropolisj.engine.*;
import micropolisj.graphics.TileImage;
import micropolisj.graphics.*;
import static micropolisj.engine.TileConstants.*;
import static micropolisj.XML_Helper.*;
import static micropolisj.graphics.TileImage.*;
@ -203,9 +203,9 @@ public class TileImages
return new ImageInfo(sti, false);
}
else if (ti instanceof AnimatedTile) {
final AnimatedTile anim = (AnimatedTile) ti;
final SimpleTileImage sti = anim.getFrameByTime(acycle);
else if (ti instanceof Animation) {
final Animation anim = (Animation) ti;
final SimpleTileImage sti = (SimpleTileImage) anim.getFrameByTime(acycle);
return new ImageInfo(sti, true);
}