[ebd3538] | 1 | package gamegui;
|
---|
| 2 |
|
---|
| 3 | import java.awt.Graphics;
|
---|
| 4 | import utils.DynamicImage;
|
---|
| 5 | import java.util.ArrayList;
|
---|
| 6 |
|
---|
| 7 | public class Animation extends Member
|
---|
| 8 | {
|
---|
| 9 | public ArrayList<DynamicImage> frames;
|
---|
| 10 | int currentFrame;
|
---|
| 11 | public int drawInterval;
|
---|
| 12 | long lastFrameChange;
|
---|
| 13 | boolean reachedEnd;
|
---|
| 14 | public boolean wrap;
|
---|
| 15 |
|
---|
| 16 | public Animation(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final int newInterval, final boolean wrap) {
|
---|
| 17 | super(newName, newX, newY, newWidth, newHeight);
|
---|
| 18 | this.frames = new ArrayList<DynamicImage>();
|
---|
| 19 | this.currentFrame = 0;
|
---|
| 20 | this.drawInterval = newInterval;
|
---|
| 21 | this.lastFrameChange = 0L;
|
---|
| 22 | this.reachedEnd = false;
|
---|
| 23 | this.wrap = wrap;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | public Animation(final Animation copy) {
|
---|
| 27 | super(copy.getName(), copy.getX(), copy.getY(), copy.getWidth(), copy.getHeight());
|
---|
| 28 | this.frames = copy.frames;
|
---|
| 29 | this.currentFrame = copy.currentFrame;
|
---|
| 30 | this.drawInterval = copy.drawInterval;
|
---|
| 31 | this.lastFrameChange = 0L;
|
---|
| 32 | this.reachedEnd = false;
|
---|
| 33 | this.wrap = copy.wrap;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public int getCurrentFrame() {
|
---|
| 37 | return this.currentFrame;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public void addFrame(final DynamicImage newFrame) {
|
---|
| 41 | this.frames.add(newFrame);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | @Override
|
---|
| 45 | public void draw(final Graphics g) {
|
---|
| 46 | if (this.lastFrameChange == 0L) {
|
---|
| 47 | this.lastFrameChange = System.nanoTime();
|
---|
| 48 | }
|
---|
| 49 | if (System.nanoTime() - this.lastFrameChange > this.drawInterval * 1000000) {
|
---|
| 50 | ++this.currentFrame;
|
---|
| 51 | if (this.currentFrame >= this.frames.size()) {
|
---|
| 52 | if (this.wrap) {
|
---|
| 53 | this.currentFrame = 0;
|
---|
| 54 | }
|
---|
| 55 | else {
|
---|
| 56 | --this.currentFrame;
|
---|
| 57 | }
|
---|
| 58 | this.reachedEnd = true;
|
---|
| 59 | }
|
---|
| 60 | this.lastFrameChange = System.nanoTime();
|
---|
| 61 | }
|
---|
| 62 | this.frames.get(this.currentFrame).draw(g, this.getX(), this.getY());
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public void draw(final Graphics g, final int x, final int y) {
|
---|
| 66 | if (this.lastFrameChange == 0L) {
|
---|
| 67 | this.lastFrameChange = System.nanoTime();
|
---|
| 68 | }
|
---|
| 69 | if (System.nanoTime() - this.lastFrameChange > this.drawInterval * 1000000) {
|
---|
| 70 | ++this.currentFrame;
|
---|
| 71 | if (this.currentFrame >= this.frames.size()) {
|
---|
| 72 | if (this.wrap) {
|
---|
| 73 | this.currentFrame = 0;
|
---|
| 74 | }
|
---|
| 75 | else {
|
---|
| 76 | --this.currentFrame;
|
---|
| 77 | }
|
---|
| 78 | this.reachedEnd = true;
|
---|
| 79 | }
|
---|
| 80 | this.lastFrameChange = System.nanoTime();
|
---|
| 81 | }
|
---|
| 82 | 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());
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public boolean reachedEnd() {
|
---|
| 86 | return this.reachedEnd;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public void reset() {
|
---|
| 90 | this.reachedEnd = false;
|
---|
| 91 | this.currentFrame = 0;
|
---|
| 92 | this.lastFrameChange = 0L;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|