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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
package ch.epfl.maze.graphics;
import ch.epfl.maze.physical.Animal;
import ch.epfl.maze.util.Action;
import ch.epfl.maze.util.Direction;
import ch.epfl.maze.util.Vector2D;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* Handles the animation of a {@code Simulation} by extrapolating the positions
* of animals.
*
* @author EPFL
*/
public final class Animation {
/**
* Default number of waiting frames to display when animation is aborting.
*/
public static final int DEFAULT_WAITING_FRAMES = 2;
/**
* Maps animals identity to graphical components that will be animated.
*/
private Map<Integer, GraphicComponent> mGraphMap;
/**
* Buffer of images of animals. Key format: "superclass.class"
*/
private Map<String, BufferedImage> mImages;
/**
* Drawing ratio variable.
*/
private float mRatio;
/**
* Control variable.
*/
private boolean mDone;
/**
* Current number of waiting frames, to prevent screen from flashing.
*/
private int mWaitingFrames;
/**
* Constructs an animation handler that will animate animals on a graphic
* environment by extrapolating their position.
*
* @param animals The {@code List} of animals that will be shown on the first
* frame
*/
public Animation(List<Animal> animals) {
mGraphMap = new TreeMap<Integer, GraphicComponent>();
mImages = new HashMap<String, BufferedImage>();
// sanity check
if (animals != null) {
// puts default action to draw animals and loads corresponding image
Action none = new Action(Direction.NONE);
for (int i = 0; i < animals.size(); i++) {
Animal animal = animals.get(i);
BufferedImage img = loadImage(animal);
Vector2D position = animal.getPosition().mul(Display.SQUARE_SIZE);
mGraphMap.put(i, new GraphicComponent(img, position, none));
}
}
// default values
mDone = true;
mWaitingFrames = 0;
}
/**
* Asks the animation to update an animal on the screen with a corresponding
* action. The animal is identified by a number, so it can be overwritten in
* case of a future update.
*
* @param animal Animal to update with action
* @param id Unique identifier for animal
* @param action Action that animal needs to perform
*/
public void update(Animal animal, int id, Action action) {
// sanity checks
if (action == null) {
action = new Action(Direction.NONE, false);
}
if (animal != null) {
// retrieves BufferedImage
String folder = animal.getClass().getSuperclass().getSimpleName();
String file = animal.getClass().getSimpleName();
BufferedImage img = mImages.get(folder + "." + file);
if (img == null) {
img = loadImage(animal);
}
// transforms position
Vector2D position = animal.getPosition().mul(Display.SQUARE_SIZE);
mGraphMap.put(id, new GraphicComponent(img, position, action));
}
}
/**
* Asks the animation to make the animal corresponding to the identifier die
* between two squares. This will be done by animating only half of its
* action.
*
* @param id Identifier of animal to kill
*/
public void updateDying(int id) {
GraphicComponent graphComp = mGraphMap.get(id);
if (graphComp != null) {
graphComp.willDieMoving();
}
}
/**
* Notifies the animation that updates were done, and that it can start
* animating from now.
*/
public void doneUpdating() {
mDone = false;
}
/**
* Paints the dt-step of the animation.
*
* @param dt The elapsed time between two frames
* @param g The graphics environment on which the graphic components will
* be painted (assumed non-null)
* @param targetWindow The window on which the graphic components will be painted
* (assumed non-null)
*/
public void paint(float dt, Graphics2D g, ImageObserver targetWindow) {
mRatio += dt;
if (mRatio > 1) {
mRatio = 1;
}
// paints every graphic component stored so far
for (Map.Entry<Integer, GraphicComponent> entry : mGraphMap.entrySet()) {
GraphicComponent comp = entry.getValue();
comp.paint(mRatio, g, targetWindow);
}
// decides whether the animation is done
if (mDone || mRatio == 1 || mWaitingFrames == 1) {
mWaitingFrames = 0;
mDone = true;
mGraphMap.clear();
mRatio = 0;
}
// prevents screen from flashing when aborting
if (mWaitingFrames > 0) {
mWaitingFrames--;
}
}
/**
* Determines whether the animation has finished.
*
* @return <b>true</b> if the animation is done, <b>false</b> otherwise
*/
public boolean isDone() {
return mDone;
}
/**
* Resets the animation with a new {@code List} of animals. If it is set to
* {@code null}, it just informs that it needs to abort its current job. A
* number of frames will still be painted to prevent the screen from
* flashing.
*/
public void reset(List<Animal> animals) {
mGraphMap.clear();
if (animals != null) {
// puts default action to draw animals
Action none = new Action(Direction.NONE);
for (int i = 0; i < animals.size(); i++) {
Animal animal = animals.get(i);
// loads corresponding image only if not already existing
String folder = animal.getClass().getSuperclass().getSimpleName();
String file = animal.getClass().getSimpleName();
BufferedImage img = mImages.get(folder + "." + file);
if (img == null) {
img = loadImage(animal);
}
// transforms position
Vector2D position = animal.getPosition().mul(Display.SQUARE_SIZE);
mGraphMap.put(i, new GraphicComponent(img, position, none));
}
}
mWaitingFrames = DEFAULT_WAITING_FRAMES;
}
/**
* Buffers and returns the image of an animal. It does not load its image if
* it's already been loaded.
*
* @param animal Animal whose image needs to be loaded or returned
* @return The buffered image of the animal
*/
private BufferedImage loadImage(Animal animal) {
// path = "img/superclass/class.png"
Class<?> superClass = animal.getClass().getSuperclass();
File f = new File("");
String file = animal.getClass().getSimpleName();
String folder = "";
while (superClass != null && !f.exists()) {
folder = superClass.getSimpleName();
String path = "img/" + folder + File.separator + file + ".png";
f = new File(path);
superClass = superClass.getSuperclass();
}
// adds image to buffer if not already there
BufferedImage img = mImages.get(folder + "." + file);
if (img == null) {
try {
img = ImageIO.read(f);
mImages.put(folder + "." + file, img);
} catch (IOException e) {
e.printStackTrace();
}
}
return img;
}
}
|