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