MakeTiles: Animation: draw the frames for different times during the animation

This commit is contained in:
Jason Long 2014-06-19 16:13:57 -04:00
parent 56a4b3da27
commit 8acf22d790
3 changed files with 31 additions and 12 deletions

View file

@ -81,10 +81,21 @@ class Animation extends TileImage
}
@Override
public void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY)
public void drawWithTimeTo(Graphics2D gr, int time, int destX, int destY, int srcX, int srcY)
{
if (frames.isEmpty()) { return; }
frames.get(0).frame.drawTo(gr, destX, destY, srcX, srcY);
int t = 0;
for (int i = 0; i < frames.size(); i++) {
Frame f = frames.get(i);
int d = t + f.duration;
if (time < d) {
f.frame.drawTo(gr, destX, destY, srcX, srcY);
return;
}
t = d;
}
// draw nothing
return;
}
@Override

View file

@ -135,9 +135,12 @@ public class MakeTiles
if (m.dest instanceof Animation) {
Animation ani = (Animation) m.dest;
for (Animation.Frame f : ani.frames) {
int t = 0;
for (int i = 0; i < ani.frames.size(); i++) {
Animation.Frame f = ani.frames.get(i);
TileImageSprite s = (TileImageSprite) f.frame;
m.ref.drawTo(gr, s.offsetX, s.offsetY, 0, 0);
m.ref.drawWithTimeTo(gr, t, s.offsetX, s.offsetY, 0, 0);
t += f.duration;
}
}
else {

View file

@ -7,7 +7,12 @@ public abstract class TileImage
{
public static final int STD_SIZE = 16;
public abstract void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY);
public abstract void drawWithTimeTo(Graphics2D gr, int time, int destX, int destY, int srcX, int srcY);
public final void drawTo(Graphics2D gr, int destX, int destY, int srcX, int 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
@ -21,12 +26,12 @@ public abstract class TileImage
TileImage above;
@Override
public void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY)
public void drawWithTimeTo(Graphics2D gr, int time, int destX, int destY, int srcX, int srcY)
{
if (below != null) {
below.drawTo(gr, destX, destY, srcX, srcY);
below.drawWithTimeTo(gr, time, destX, destY, srcX, srcY);
}
above.drawTo(gr, destX, destY, srcX, srcY);
above.drawWithTimeTo(gr, time, destX, destY, srcX, srcY);
}
@Override
@ -58,9 +63,9 @@ public abstract class TileImage
int offsetY;
@Override
public void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY)
public void drawWithTimeTo(Graphics2D gr, int time, int destX, int destY, int srcX, int srcY)
{
source.drawTo(gr, destX, destY, srcX+offsetX, srcY+offsetY);
source.drawWithTimeTo(gr, time, destX, destY, srcX+offsetX, srcY+offsetY);
}
@Override
@ -82,7 +87,7 @@ public abstract class TileImage
}
@Override
public void drawTo(Graphics2D gr, int destX, int destY, int srcX, int srcY)
public void drawWithTimeTo(Graphics2D gr, int time, int destX, int destY, int srcX, int srcY)
{
srcX = srcX * basisSize / STD_SIZE;
srcY = srcY * basisSize / STD_SIZE;