sound for units onClick
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / agent / AgentPrototypeFactory.java
blob33383697db292dc095289d7eceb30067951df986
1 package se.umu.cs.dit06ajnajs.agent;
3 import java.applet.Applet;
4 import java.applet.AudioClip;
5 import java.awt.Image;
6 import java.awt.image.BufferedImage;
7 import java.io.IOException;
8 import java.net.URL;
9 import javax.imageio.ImageIO;
10 import java.util.Set;
11 import java.util.HashMap;
12 import se.umu.cs.dit06ajnajs.AntiTD;
14 /**
15 * AgentPrototypeFactory is used to create new instances of different
16 * implementations of agents from a String. Implemented as a Singleton.
18 public class AgentPrototypeFactory {
19 private static final AgentPrototypeFactory INSTANCE
20 = new AgentPrototypeFactory();
21 // private static Logger logger = Logger.getLogger("AntiTD");
23 private java.util.Map<String, Unit> unitMap;
24 private java.util.Map<String, Tower> towerMap;
25 private int lowestCost;
27 /**
28 * Gets the only instance of this class, if none exists one is created.
30 * @return The only instance of this class.
32 public static AgentPrototypeFactory getInstance() {
33 return INSTANCE;
36 /**
37 * AgentPrototypeFactory initialices all different types of Units and Towers
38 * and puts them in a java.util.Map.
40 * Private constructor makes sure there can only be one instance of this
41 * class, and that instance is created by the class itself from the first
42 * invocation of getInstance()
45 private AgentPrototypeFactory() {
46 unitMap = new HashMap<String, Unit>();
47 towerMap = new HashMap<String, Tower>();
49 // Instantiate Units and Towers
50 try {
51 // Get directory containing images
52 // TODO: move to config class of file
53 URL imagesURL =
54 this.getClass().getResource("/resources/unit-images/");
55 URL soundsURL = getClass().getResource("/resources/sounds/");
59 * Init Units
62 /* Create Standard FootmanUnit */
63 // Images
64 HashMap<Direction, Image> images = new HashMap<Direction, Image>();
65 BufferedImage image = ImageIO.read(
66 new URL(imagesURL, "footmanDown.gif"));
67 images.put(Direction.DOWN, image);
68 image = ImageIO.read(new URL(imagesURL, "footmanUp.gif"));
69 images.put(Direction.UP, image);
70 image = ImageIO.read(new URL(imagesURL, "footmanLeft.gif"));
71 images.put(Direction.LEFT, image);
72 image = ImageIO.read(new URL(imagesURL, "footmanRight.gif"));
73 images.put(Direction.RIGHT, image);
75 // Sounds
76 HashMap<String, AudioClip> sounds =
77 new HashMap<String, AudioClip>();
78 AudioClip sound = Applet.newAudioClip(
79 new URL(soundsURL, "dead.wav"));
80 sounds.put("dead", sound);
81 sound = Applet.newAudioClip(new URL(soundsURL, "footman.wav"));
82 sounds.put("onClick", sound);
84 unitMap.put("Footman",
85 new FootmanUnit("Footman", 40, 40, 2, 100, 500,
86 images, sounds));
88 /* Create Seal */
89 // Images
90 images = new HashMap<Direction, Image>();
91 image = ImageIO.read(new URL(imagesURL, "sealDown.gif"));
92 images.put(Direction.DOWN, image);
93 image = ImageIO.read(new URL(imagesURL, "sealUp.gif"));
94 images.put(Direction.UP, image);
95 image = ImageIO.read(new URL(imagesURL, "sealLeft.gif"));
96 images.put(Direction.LEFT, image);
97 image = ImageIO.read(new URL(imagesURL, "sealRight.gif"));
98 images.put(Direction.RIGHT, image);
100 // Sounds
101 sounds = new HashMap<String, AudioClip>();
102 sound = Applet.newAudioClip(new URL(soundsURL, "sealdead.wav"));
103 sounds.put("dead", sound);
104 sound = Applet.newAudioClip(new URL(soundsURL, "seal.wav"));
105 sounds.put("onClick", sound);
107 unitMap.put("Seal",
108 new FootmanUnit("Seal", 33, 33, 1, 100, 100,
109 images, sounds));
111 // Create SuperFootmanUnit
112 // image = ImageIO.read(new URL(imagesURL, "knightDown.gif"));
113 // images.put(Direction.DOWN, image);
114 // image = ImageIO.read(new URL(imagesURL, "unit.gif"));
115 // images.put(Direction.UP, image);
116 // image = ImageIO.read(new URL(imagesURL, "unit.gif"));
117 // images.put(Direction.LEFT, image);
118 // image = ImageIO.read(new URL(imagesURL, "unit.gif"));
119 // images.put(Direction.RIGHT, image);
120 // image = ImageIO.read(new URL(imagesURL, "knight.gif"));
121 // unitMap.put("SuperFootmanUnit",
122 // new FootmanUnit("Knight", 40, 40, 4, 100, 1000, images));
125 // Init Towers
126 imagesURL = this.getClass().getResource("/resources/tower-images/");
128 // Create Basic Tower
129 image = ImageIO.read(new URL(imagesURL, "basicTower.gif"));
130 towerMap.put("BasicTower",
131 new BasicTower(41, 41, 3,
132 (int) (AntiTD.SQUARE_SIZE*1.5),
133 image));
135 } catch (IOException e) {
136 System.err.println("Couldn't find image. Exception: "
137 + e.getMessage());
140 // Find cost of cheapest unit
141 this.lowestCost = Integer.MAX_VALUE;
142 for (String s : getUnitTypes()) {
143 Unit unit = createUnit(s);
144 if (unit.getCost() < this.lowestCost) {
145 this.lowestCost = unit.getCost();
150 // TODO: Implement and think, is this crazy? Lot's of stuff shouldn't have
151 // to be in the parameterlist, needed info is x, y, Direction and map, the
152 // rest is information about the unit-type. Create a Unit with only a
153 // startSquare as a parameter?
154 public Unit createUnit(String type) {
155 Unit unitPrototype = unitMap.get(type);
156 if (unitPrototype == null) {
157 throw new IllegalArgumentException("The specified type '" + type
158 + "' doesn't exist");
160 Unit resultUnit = (Unit) unitPrototype.clone();
161 // resultUnit.setX(x);
162 // resultUnit.setY(y);
163 // resultUnit.setWidth(width);
164 // resultUnit.setHeight(health);
165 // resultUnit.setSpeed(speed);
166 // resultUnit.setHealth(health);
167 // resultUnit.setCost(cost);
168 // resultUnit.setDirection(direction);
169 // resultUnit.setMap(map);
170 return resultUnit;
173 public Tower createTower(String type) {
174 Tower towerPrototype = towerMap.get(type);
175 if (towerPrototype == null) {
176 throw new IllegalArgumentException("The specified type '" + type
177 + "' doesn't exist");
179 Tower resultTower = (Tower) towerPrototype.clone();
180 resultTower.init();
181 return resultTower;
184 // TODO: How to get all keys? Hashtable can return Enumeration
185 // public List<String> getUnitTypes() {}
187 @Override
188 public Object clone() throws CloneNotSupportedException {
189 throw new CloneNotSupportedException("AgentPrototypeFactory is a "
190 + "Singleton no clones are"
191 + " supported.");
195 * Returns a Set containing all unitTypes this Class can produce.
197 * @return A Set containing all unitTypes this Class can produce.
199 public Set<String> getUnitTypes() {
200 return unitMap.keySet();
204 * Returns int representing the cost for the cheapest unit
205 * @return int representing the cost for the cheapest unit
207 public int getLowestCost() {
208 return this.lowestCost;