1 package nl.tudelft.jpacman.sprite;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import nl.tudelft.jpacman.PacmanConfigurationException;
8 import nl.tudelft.jpacman.board.Direction;
9 import nl.tudelft.jpacman.npc.ghost.GhostColor;
10
11 /**
12 * Sprite Store containing the classic Pac-Man sprites.
13 *
14 * @author Jeroen Roosen
15 */
16 public class PacManSprites extends SpriteStore {
17
18 /**
19 * The sprite files are vertically stacked series for each direction, this
20 * array denotes the order.
21 */
22 private static final Direction[] DIRECTIONS = {
23 Direction.NORTH,
24 Direction.EAST,
25 Direction.SOUTH,
26 Direction.WEST
27 };
28
29 /**
30 * The image size in pixels.
31 */
32 private static final int SPRITE_SIZE = 16;
33
34 /**
35 * The amount of frames in the pacman animation.
36 */
37 private static final int PACMAN_ANIMATION_FRAMES = 4;
38
39 /**
40 * The amount of frames in the pacman dying animation.
41 */
42 private static final int PACMAN_DEATH_FRAMES = 11;
43
44 /**
45 * The amount of frames in the ghost animation.
46 */
47 private static final int GHOST_ANIMATION_FRAMES = 2;
48
49 /**
50 * The delay between frames.
51 */
52 private static final int ANIMATION_DELAY = 200;
53
54 /**
55 * @return A map of animated Pac-Man sprites for all directions.
56 */
57 public Map<Direction, Sprite> getPacmanSprites() {
58 return directionSprite("/sprite/pacman.png", PACMAN_ANIMATION_FRAMES);
59 }
60
61 /**
62 * @return The animation of a dying Pac-Man.
63 */
64 public AnimatedSprite getPacManDeathAnimation() {
65 String resource = "/sprite/dead.png";
66
67 Sprite baseImage = loadSprite(resource);
68 AnimatedSprite animation = createAnimatedSprite(baseImage, PACMAN_DEATH_FRAMES,
69 ANIMATION_DELAY, false);
70 animation.setAnimating(false);
71
72 return animation;
73 }
74
75 /**
76 * Returns a new map with animations for all directions.
77 *
78 * @param resource
79 * The resource name of the sprite.
80 * @param frames
81 * The number of frames in this sprite.
82 * @return The animated sprite facing the given direction.
83 */
84 private Map<Direction, Sprite> directionSprite(String resource, int frames) {
85 Map<Direction, Sprite> sprite = new HashMap<>();
86
87 Sprite baseImage = loadSprite(resource);
88 for (int i = 0; i < DIRECTIONS.length; i++) {
89 Sprite directionSprite = baseImage.split(0, i * SPRITE_SIZE, frames
90 * SPRITE_SIZE, SPRITE_SIZE);
91 AnimatedSprite animation = createAnimatedSprite(directionSprite,
92 frames, ANIMATION_DELAY, true);
93 animation.setAnimating(true);
94 sprite.put(DIRECTIONS[i], animation);
95 }
96
97 return sprite;
98 }
99
100 /**
101 * Returns a map of animated ghost sprites for all directions.
102 *
103 * @param color
104 * The colour of the ghost.
105 * @return The Sprite for the ghost.
106 */
107 public Map<Direction, Sprite> getGhostSprite(GhostColor color) {
108 assert color != null;
109
110 String resource = "/sprite/ghost_" + color.name().toLowerCase()
111 + ".png";
112 return directionSprite(resource, GHOST_ANIMATION_FRAMES);
113 }
114
115 /**
116 * @return The sprite for the wall.
117 */
118 public Sprite getWallSprite() {
119 return loadSprite("/sprite/wall.png");
120 }
121
122 /**
123 * @return The sprite for the ground.
124 */
125 public Sprite getGroundSprite() {
126 return loadSprite("/sprite/floor.png");
127 }
128
129 /**
130 * @return The sprite for the
131 */
132 public Sprite getPelletSprite() {
133 return loadSprite("/sprite/pellet.png");
134 }
135
136 /**
137 * Overloads the default sprite loading, ignoring the exception. This class
138 * assumes all sprites are provided, hence the exception will be thrown as a
139 * {@link RuntimeException}.
140 *
141 * {@inheritDoc}
142 */
143 @Override
144 public Sprite loadSprite(String resource) {
145 try {
146 return super.loadSprite(resource);
147 } catch (IOException e) {
148 throw new PacmanConfigurationException("Unable to load sprite: " + resource, e);
149 }
150 }
151 }