-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.java
More file actions
162 lines (102 loc) · 3.45 KB
/
Animation.java
File metadata and controls
162 lines (102 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.awt.Image;
import java.util.ArrayList;
public class Animation {
private ArrayList<AnimFrame> frames;
private int currFrameIndex;
private long animTime;
private long startTime;
private long totalDuration;
private boolean loop;
private boolean isActive;
// Constructor to create a new animation
public Animation(boolean loop) {
frames = new ArrayList<AnimFrame>();
totalDuration = 0;
this.loop = loop;
isActive = false;
}
// Adds a frame to the animation with the specified image and duration
public synchronized void addFrame(Image image, long duration) {
totalDuration += duration;
frames.add(new AnimFrame(image, totalDuration));
}
// Starts playing the animation from the beginning
public synchronized void start() {
isActive = true;
animTime = 0;
currFrameIndex = 0;
startTime = System.currentTimeMillis();
}
// Stops the animation
public synchronized void stop() {
isActive = false;
}
// Updates the animation state based on the elapsed time
public synchronized void update() {
if (!isActive)
return;
long currTime = System.currentTimeMillis();
long elapsedTime = currTime - startTime;
startTime = currTime;
if (frames.size() > 1) {
animTime += elapsedTime;
if (animTime >= totalDuration) {
if (loop) {
animTime = animTime % totalDuration;
currFrameIndex = 0;
}
else {
isActive = false;
}
}
if (!isActive)
return;
while (animTime > getFrame(currFrameIndex).endTime) {
currFrameIndex++;
}
}
}
// Returns the index of the current frame being displayed
public int getCurrentFrameIndex() {
return currFrameIndex;
}
// Gets the image of the current frame
public synchronized Image getImage() {
if (frames.size() == 0) {
return null;
}
else {
return getFrame(currFrameIndex).image;
}
}
// Returns the total number of frames in the animation
public int getNumFrames() {
return frames.size();
}
// Retrieves the frame at the specified index
private AnimFrame getFrame(int i) {
return frames.get(i);
}
// Checks if the animation is currently active
public boolean isStillActive () {
return isActive;
}
// Animation class representing frames
private class AnimFrame {
Image image;
long endTime;
public AnimFrame(Image image, long endTime) {
this.image = image;
this.endTime = endTime;
}
}
// Loads the needed animations
/* private Animation loadAnimation(String basePath, String action, int frameCount, int frameDuration, boolean loop) {
Animation anim = new Animation(loop);
for (int i = 1; i <= frameCount; i++) {
Image frame = ImageManager.loadImage(basePath + "/" + action + "_" + i + ".png");
anim.addFrame(frame, frameDuration);
}
return anim;
} */
}