package gamegui; import java.awt.*; import java.util.*; import utils.*; public class Animation extends Member { public ArrayList frames; int currentFrame; public int drawInterval; long lastFrameChange; boolean reachedEnd; //set to true if the animation has gone through one complete cycle public boolean wrap; //if true, cycles through the animation. If false, keeps showing the last frame once the others //are shown public Animation(String newName, int newX, int newY, int newWidth, int newHeight, int newInterval, boolean wrap) { super(newName, newX, newY, newWidth, newHeight); frames = new ArrayList(); currentFrame = 0; drawInterval = newInterval; lastFrameChange = 0; reachedEnd = false; this.wrap = wrap; } public Animation(Animation copy) { super(copy.getName(), copy.getX(), copy.getY(), copy.getWidth(), copy.getHeight()); frames = copy.frames; currentFrame = copy.currentFrame; drawInterval = copy.drawInterval; lastFrameChange = 0; reachedEnd = false; wrap = copy.wrap; } public int getCurrentFrame() { return currentFrame; } public void addFrame(DynamicImage newFrame) { frames.add(newFrame); } public void draw(Graphics g) { if(lastFrameChange == 0) lastFrameChange = System.nanoTime(); if(System.nanoTime() - lastFrameChange > drawInterval*1000000) { currentFrame++; if(currentFrame >= frames.size()) { if(wrap) currentFrame = 0; else currentFrame--; reachedEnd = true; } lastFrameChange = System.nanoTime(); } frames.get(currentFrame).draw(g, getX(), getY()); } public void draw(Graphics g, int x, int y) { if(lastFrameChange == 0) lastFrameChange = System.nanoTime(); if((System.nanoTime() - lastFrameChange) > drawInterval*1000000) { currentFrame++; if(currentFrame >= frames.size()) { if(wrap) currentFrame = 0; else currentFrame--; reachedEnd = true; } lastFrameChange = System.nanoTime(); } frames.get(currentFrame).draw(g, getX()+x-frames.get(currentFrame).getWidth()/2, getY()+y-frames.get(currentFrame).getHeight()); } public boolean reachedEnd() { return reachedEnd; } public void reset() { reachedEnd = false; currentFrame = 0; lastFrameChange = 0; } }