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