diff --git a/src/micropolisj/graphics/Animation.java b/src/micropolisj/graphics/Animation.java index e0a3ccc..13f60b5 100644 --- a/src/micropolisj/graphics/Animation.java +++ b/src/micropolisj/graphics/Animation.java @@ -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 { diff --git a/src/micropolisj/graphics/TileImage.java b/src/micropolisj/graphics/TileImage.java index bcc85bc..11e7ebc 100644 --- a/src/micropolisj/graphics/TileImage.java +++ b/src/micropolisj/graphics/TileImage.java @@ -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 frames = new ArrayList(); - - 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); diff --git a/src/micropolisj/gui/TileImages.java b/src/micropolisj/gui/TileImages.java index 9dd6352..8c6f156 100644 --- a/src/micropolisj/gui/TileImages.java +++ b/src/micropolisj/gui/TileImages.java @@ -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); }