diff options
Diffstat (limited to 'src')
37 files changed, 4543 insertions, 4744 deletions
diff --git a/src/ch/epfl/maze/graphics/Animation.java b/src/ch/epfl/maze/graphics/Animation.java index 0502a92..47e0fc8 100644 --- a/src/ch/epfl/maze/graphics/Animation.java +++ b/src/ch/epfl/maze/graphics/Animation.java | |||
@@ -1,6 +1,12 @@ | |||
1 | package ch.epfl.maze.graphics; | 1 | package ch.epfl.maze.graphics; |
2 | 2 | ||
3 | import java.awt.Graphics2D; | 3 | import ch.epfl.maze.physical.Animal; |
4 | import ch.epfl.maze.util.Action; | ||
5 | import ch.epfl.maze.util.Direction; | ||
6 | import ch.epfl.maze.util.Vector2D; | ||
7 | |||
8 | import javax.imageio.ImageIO; | ||
9 | import java.awt.*; | ||
4 | import java.awt.image.BufferedImage; | 10 | import java.awt.image.BufferedImage; |
5 | import java.awt.image.ImageObserver; | 11 | import java.awt.image.ImageObserver; |
6 | import java.io.File; | 12 | import java.io.File; |
@@ -10,236 +16,231 @@ import java.util.List; | |||
10 | import java.util.Map; | 16 | import java.util.Map; |
11 | import java.util.TreeMap; | 17 | import java.util.TreeMap; |
12 | 18 | ||
13 | import javax.imageio.ImageIO; | ||
14 | |||
15 | import ch.epfl.maze.physical.Animal; | ||
16 | import ch.epfl.maze.util.Action; | ||
17 | import ch.epfl.maze.util.Direction; | ||
18 | import ch.epfl.maze.util.Vector2D; | ||
19 | |||
20 | /** | 19 | /** |
21 | * Handles the animation of a {@code Simulation} by extrapolating the positions | 20 | * Handles the animation of a {@code Simulation} by extrapolating the positions |
22 | * of animals. | 21 | * of animals. |
23 | * | ||
24 | */ | 22 | */ |
25 | 23 | ||
26 | public final class Animation { | 24 | public final class Animation { |
27 | 25 | ||
28 | /** Default number of waiting frames to display when animation is aborting. */ | 26 | /** |
29 | public static final int DEFAULT_WAITING_FRAMES = 2; | 27 | * Default number of waiting frames to display when animation is aborting. |
30 | 28 | */ | |
31 | /** Maps animals identity to graphical components that will be animated. */ | 29 | public static final int DEFAULT_WAITING_FRAMES = 2; |
32 | private Map<Integer, GraphicComponent> mGraphMap; | 30 | |
33 | 31 | /** | |
34 | /** Buffer of images of animals. Key format: "superclass.class" */ | 32 | * Maps animals identity to graphical components that will be animated. |
35 | private Map<String, BufferedImage> mImages; | 33 | */ |
36 | 34 | private Map<Integer, GraphicComponent> mGraphMap; | |
37 | /** Drawing ratio variable. */ | 35 | |
38 | private float mRatio; | 36 | /** |
39 | 37 | * Buffer of images of animals. Key format: "superclass.class" | |
40 | /** Control variable. */ | 38 | */ |
41 | private boolean mDone; | 39 | private Map<String, BufferedImage> mImages; |
42 | 40 | ||
43 | /** Current number of waiting frames, to prevent screen from flashing. */ | 41 | /** |
44 | private int mWaitingFrames; | 42 | * Drawing ratio variable. |
45 | 43 | */ | |
46 | /** | 44 | private float mRatio; |
47 | * Constructs an animation handler that will animate animals on a graphic | 45 | |
48 | * environment by extrapolating their position. | 46 | /** |
49 | * | 47 | * Control variable. |
50 | * @param animals | 48 | */ |
51 | * The {@code List} of animals that will be shown on the first | 49 | private boolean mDone; |
52 | * frame | 50 | |
53 | */ | 51 | /** |
54 | 52 | * Current number of waiting frames, to prevent screen from flashing. | |
55 | public Animation(List<Animal> animals) { | 53 | */ |
56 | mGraphMap = new TreeMap<Integer, GraphicComponent>(); | 54 | private int mWaitingFrames; |
57 | mImages = new HashMap<String, BufferedImage>(); | 55 | |
58 | 56 | /** | |
59 | // sanity check | 57 | * Constructs an animation handler that will animate animals on a graphic |
60 | if (animals != null) { | 58 | * environment by extrapolating their position. |
61 | // puts default action to draw animals and loads corresponding image | 59 | * |
62 | Action none = new Action(Direction.NONE); | 60 | * @param animals The {@code List} of animals that will be shown on the first |
63 | for (int i = 0; i < animals.size(); i++) { | 61 | * frame |
64 | Animal animal = animals.get(i); | 62 | */ |
65 | BufferedImage img = loadImage(animal); | 63 | |
66 | Vector2D position = animal.getPosition().mul(Display.SQUARE_SIZE); | 64 | public Animation(List<Animal> animals) { |
67 | 65 | mGraphMap = new TreeMap<Integer, GraphicComponent>(); | |
68 | mGraphMap.put(i, new GraphicComponent(img, position, none)); | 66 | mImages = new HashMap<String, BufferedImage>(); |
69 | } | 67 | |
70 | } | 68 | // sanity check |
71 | 69 | if (animals != null) { | |
72 | // default values | 70 | // puts default action to draw animals and loads corresponding image |
73 | mDone = true; | 71 | Action none = new Action(Direction.NONE); |
74 | mWaitingFrames = 0; | 72 | for (int i = 0; i < animals.size(); i++) { |
75 | } | 73 | Animal animal = animals.get(i); |
76 | 74 | BufferedImage img = loadImage(animal); | |
77 | /** | 75 | Vector2D position = animal.getPosition().mul(Display.SQUARE_SIZE); |
78 | * Asks the animation to update an animal on the screen with a corresponding | 76 | |
79 | * action. The animal is identified by a number, so it can be overwritten in | 77 | mGraphMap.put(i, new GraphicComponent(img, position, none)); |
80 | * case of a future update. | 78 | } |
81 | * | 79 | } |
82 | * @param animal | 80 | |
83 | * Animal to update with action | 81 | // default values |
84 | * @param id | 82 | mDone = true; |
85 | * Unique identifier for animal | 83 | mWaitingFrames = 0; |
86 | * @param action | 84 | } |
87 | * Action that animal needs to perform | 85 | |
88 | */ | 86 | /** |
89 | 87 | * Asks the animation to update an animal on the screen with a corresponding | |
90 | public void update(Animal animal, int id, Action action) { | 88 | * action. The animal is identified by a number, so it can be overwritten in |
91 | // sanity checks | 89 | * case of a future update. |
92 | if (action == null) { | 90 | * |
93 | action = new Action(Direction.NONE, false); | 91 | * @param animal Animal to update with action |
94 | } | 92 | * @param id Unique identifier for animal |
95 | if (animal != null) { | 93 | * @param action Action that animal needs to perform |
96 | // retrieves BufferedImage | 94 | */ |
97 | String folder = animal.getClass().getSuperclass().getSimpleName(); | 95 | |
98 | String file = animal.getClass().getSimpleName(); | 96 | public void update(Animal animal, int id, Action action) { |
99 | BufferedImage img = mImages.get(folder + "." + file); | 97 | // sanity checks |
100 | if (img == null) { | 98 | if (action == null) { |
101 | img = loadImage(animal); | 99 | action = new Action(Direction.NONE, false); |
102 | } | 100 | } |
103 |