Changed the way to add new units. Important settings are now done in addUnit in ATDModel.
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / agent / AgentPrototypeFactory.java
blob32b68703b65f217b409679ed89813efec9772582
1 package se.umu.cs.dit06ajnajs.agent;
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
5 import java.net.URL;
6 import javax.imageio.ImageIO;
7 import se.umu.cs.dit06ajnajs.agent.Direction;
8 import se.umu.cs.dit06ajnajs.map.Map;
9 import java.util.Set;
10 import java.util.HashMap;
12 /**
13 * MapSquarePrototypeFactory is used to create new instances of different
14 * implementations of MapSquare from a String. Implemented as a Singleton.
16 public class AgentPrototypeFactory {
17 private static final AgentPrototypeFactory INSTANCE
18 = new AgentPrototypeFactory();
19 // private static Logger logger = Logger.getLogger("AntiTD");
21 private java.util.Map<String, Unit> unitMap;
22 private java.util.Map<String, Tower> towerMap;
23 private static final int UNSET = -3333;
25 /**
26 * Gets the only instance of this class, if none exists one is created.
28 * @return The only instance of this class.
30 public static AgentPrototypeFactory getInstance() {
31 return INSTANCE;
34 /**
35 * AgentPrototypeFactory initialices all different types of Units and Towers
36 * and puts them in a java.util.Map.
38 * Private constructor makes sure there can only be one instance of this
39 * class, and that instance is created by the class itself from the first
40 * invocation of getInstance()
43 private AgentPrototypeFactory() {
44 unitMap = new HashMap<String, Unit>();
45 towerMap = new HashMap<String, Tower>();
47 // Instantiate Units
48 try {
49 // Get directory containing images
50 // TODO: move to config class of file
51 URL imagesURL = this.getClass().getResource("/resources/");
53 // Create Standard FootmanUnit
54 BufferedImage image = ImageIO.read(new URL(imagesURL, "unit1.gif"));
55 unitMap.put("FootmanUnit",
56 new FootmanUnit(20, 20, 2, 100, 10, image));
57 } catch (IOException e) {
58 System.err.println("Couldn't find image. Exception: "
59 + e.getMessage());
63 // TODO: Implement and think, is this crazy? Lot's of stuff shouldn't have
64 // to be in the parameterlist, needed info is x, y, Direction and map, the
65 // rest is information about the unit-type. Create a Unit with only a
66 // startSquare as a parameter?
67 public Unit createUnit(String type) {
68 Unit unitPrototype = unitMap.get(type);
69 if (unitPrototype == null) {
70 throw new IllegalArgumentException("The specified type '" + type
71 + "' doesn't exist");
73 Unit resultUnit = (Unit) unitPrototype.clone();
74 // resultUnit.setX(x);
75 // resultUnit.setY(y);
76 // resultUnit.setWidth(width);
77 // resultUnit.setHeight(health);
78 // resultUnit.setSpeed(speed);
79 // resultUnit.setHealth(health);
80 // resultUnit.setCost(cost);
81 // resultUnit.setDirection(direction);
82 // resultUnit.setMap(map);
83 return resultUnit;
86 // TODO: How to get all keys? Hashtable can return Enumeration
87 // public List<String> getUnitTypes() {}
89 @Override
90 public Object clone() throws CloneNotSupportedException {
91 throw new CloneNotSupportedException("AgentPrototypeFactory is a "
92 + "Singleton no clones are"
93 + " supported.");
96 /**
97 * Returns a Set containing all unitTypes this Class can produce.
99 * @return A Set containing all unitTypes this Class can produce.
101 public Set<String> getUnitTypes() {
102 return unitMap.keySet();