summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/graphics/Animation.java
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-11-21 10:44:42 +0100
committerPacien TRAN-GIRARD2015-11-21 10:44:42 +0100
commit90bd766f361083f6fd9e39ff080c83fcc606832f (patch)
treedf81a931c9565a4b227068680087f58fdace1859 /src/ch/epfl/maze/graphics/Animation.java
parent655ac88f4e73b2df532a451aedf5a561ea1b0d2c (diff)
downloadmaze-solver-90bd766f361083f6fd9e39ff080c83fcc606832f.tar.gz
Reformat imported code
Diffstat (limited to 'src/ch/epfl/maze/graphics/Animation.java')
-rw-r--r--src/ch/epfl/maze/graphics/Animation.java453
1 files changed, 227 insertions, 226 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 @@
1package ch.epfl.maze.graphics; 1package ch.epfl.maze.graphics;
2 2
3import java.awt.Graphics2D; 3import ch.epfl.maze.physical.Animal;
4import ch.epfl.maze.util.Action;
5import ch.epfl.maze.util.Direction;
6import ch.epfl.maze.util.Vector2D;
7
8import javax.imageio.ImageIO;
9import java.awt.*;
4import java.awt.image.BufferedImage; 10import java.awt.image.BufferedImage;
5import java.awt.image.ImageObserver; 11import java.awt.image.ImageObserver;
6import java.io.File; 12import java.io.File;
@@ -10,236 +16,231 @@ import java.util.List;
10import java.util.Map; 16import java.util.Map;
11import java.util.TreeMap; 17import java.util.TreeMap;
12 18
13import javax.imageio.ImageIO;
14
15import ch.epfl.maze.physical.Animal;
16import ch.epfl.maze.util.Action;
17import ch.epfl.maze.util.Direction;
18import 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
26public final class Animation { 24public 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 101 if (animal != null) {
104 // transforms position 102 // retrieves BufferedImage
105 Vector2D position = animal.getPosition().mul(Display.SQUARE_SIZE); 103 String folder = animal.getClass().getSuperclass().getSimpleName();
106 104 String file = animal.getClass().getSimpleName();
107 mGraphMap.put(id, new GraphicComponent(img, position, action)); 105 BufferedImage img = mImages.get(folder + "." + file);
108 } 106 if (img == null) {
109 } 107 img = loadImage(animal);
110 108 }
111 /** 109
112 * Asks the animation to make the animal corresponding to the identifier die 110 // transforms position
113 * between two squares. This will be done by animating only half of its 111 Vector2D position = animal.getPosition().mul(Display.SQUARE_SIZE);
114 * action. 112
115 * 113 mGraphMap.put(id, new GraphicComponent(img, position, action));
116 * @param id 114 }
117 * Identifier of animal to kill 115 }
118 */ 116
119 117 /**
120 public void updateDying(int id) { 118 * Asks the animation to make the animal corresponding to the identifier die
121 GraphicComponent graphComp = mGraphMap.get(id); 119 * between two squares. This will be done by animating only half of its
122 if (graphComp != null) { 120 * action.
123 graphComp.willDieMoving(); 121 *
124 } 122 * @param id Identifier of animal to kill
125 } 123 */
126 124
127 /** 125 public void updateDying(int id) {
128 * Notifies the animation that updates were done, and that it can start 126 GraphicComponent graphComp = mGraphMap.get(id);
129 * animating from now. 127 if (graphComp != null) {
130 */ 128 graphComp.willDieMoving();
131 129 }
132 public void doneUpdating() { 130 }
133 mDone = false; 131
134 } 132 /**
135 133 * Notifies the animation that updates were done, and that it can start
136 /** 134 * animating from now.
137 * Paints the dt-step of the animation. 135 */
138 * 136
139 * @param dt 137 public void doneUpdating() {
140 * The elapsed time between two frames 138 mDone = false;
141 * @param g