package gamegui; import java.awt.Graphics; import utils.DynamicImage; import java.util.ArrayList; public class Animation extends Member { public ArrayList frames; int currentFrame; public int drawInterval; long lastFrameChange; boolean reachedEnd; public boolean wrap; public Animation(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final int newInterval, final boolean wrap) { super(newName, newX, newY, newWidth, newHeight); this.frames = new ArrayList(); this.currentFrame = 0; this.drawInterval = newInterval; this.lastFrameChange = 0L; this.reachedEnd = false; this.wrap = wrap; } public Animation(final Animation copy) { super(copy.getName(), copy.getX(), copy.getY(), copy.getWidth(), copy.getHeight()); this.frames = copy.frames; this.currentFrame = copy.currentFrame; this.drawInterval = copy.drawInterval; this.lastFrameChange = 0L; this.reachedEnd = false; this.wrap = copy.wrap; } public int getCurrentFrame() { return this.currentFrame; } public void addFrame(final DynamicImage newFrame) { this.frames.add(newFrame); } @Override public void draw(final Graphics g) { if (this.lastFrameChange == 0L) { this.lastFrameChange = System.nanoTime(); } if (System.nanoTime() - this.lastFrameChange > this.drawInterval * 1000000) { ++this.currentFrame; if (this.currentFrame >= this.frames.size()) { if (this.wrap) { this.currentFrame = 0; } else { --this.currentFrame; } this.reachedEnd = true; } this.lastFrameChange = System.nanoTime(); } this.frames.get(this.currentFrame).draw(g, this.getX(), this.getY()); } public void draw(final Graphics g, final int x, final int y) { if (this.lastFrameChange == 0L) { this.lastFrameChange = System.nanoTime(); } if (System.nanoTime() - this.lastFrameChange > this.drawInterval * 1000000) { ++this.currentFrame; if (this.currentFrame >= this.frames.size()) { if (this.wrap) { this.currentFrame = 0; } else { --this.currentFrame; } this.reachedEnd = true; } this.lastFrameChange = System.nanoTime(); } this.frames.get(this.currentFrame).draw(g, this.getX() + x - this.frames.get(this.currentFrame).getWidth() / 2, this.getY() + y - this.frames.get(this.currentFrame).getHeight()); } public boolean reachedEnd() { return this.reachedEnd; } public void reset() { this.reachedEnd = false; this.currentFrame = 0; this.lastFrameChange = 0L; } }