1 package se
.umu
.cs
.dit06ajnajs
.agent
;
4 import java
.awt
.image
.BufferedImage
;
5 import java
.io
.IOException
;
7 import javax
.imageio
.ImageIO
;
9 import java
.util
.HashMap
;
10 import se
.umu
.cs
.dit06ajnajs
.AntiTD
;
13 * AgentPrototypeFactory is used to create new instances of different
14 * implementations of agents 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 int lowestCost
;
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() {
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 and Towers
49 // Get directory containing images
50 // TODO: move to config class of file
51 URL imagesURL
= this.getClass().getResource("/resources/unit-images/");
55 // Create Standard FootmanUnit
56 HashMap
<Direction
, Image
> images
= new HashMap
<Direction
, Image
>();
58 BufferedImage image
= ImageIO
.read(new URL(imagesURL
, "footmanDown.gif"));
59 images
.put(Direction
.DOWN
, image
);
60 image
= ImageIO
.read(new URL(imagesURL
, "footmanUp.gif"));
61 images
.put(Direction
.UP
, image
);
62 image
= ImageIO
.read(new URL(imagesURL
, "footmanLeft.gif"));
63 images
.put(Direction
.LEFT
, image
);
64 image
= ImageIO
.read(new URL(imagesURL
, "footmanRight.gif"));
65 images
.put(Direction
.RIGHT
, image
);
66 unitMap
.put("Footman",
67 new FootmanUnit("Footman", 40, 40, 2, 100, 500, images
));
70 images
= new HashMap
<Direction
, Image
>();
72 image
= ImageIO
.read(new URL(imagesURL
, "sealDown.gif"));
73 images
.put(Direction
.DOWN
, image
);
74 image
= ImageIO
.read(new URL(imagesURL
, "sealUp.gif"));
75 images
.put(Direction
.UP
, image
);
76 image
= ImageIO
.read(new URL(imagesURL
, "sealLeft.gif"));
77 images
.put(Direction
.LEFT
, image
);
78 image
= ImageIO
.read(new URL(imagesURL
, "sealRight.gif"));
79 images
.put(Direction
.RIGHT
, image
);
81 new FootmanUnit("Seal", 33, 33, 1, 100, 100, images
));
83 // Create SuperFootmanUnit
84 // image = ImageIO.read(new URL(imagesURL, "knightDown.gif"));
85 // images.put(Direction.DOWN, image);
86 // image = ImageIO.read(new URL(imagesURL, "unit.gif"));
87 // images.put(Direction.UP, image);
88 // image = ImageIO.read(new URL(imagesURL, "unit.gif"));
89 // images.put(Direction.LEFT, image);
90 // image = ImageIO.read(new URL(imagesURL, "unit.gif"));
91 // images.put(Direction.RIGHT, image);
92 // image = ImageIO.read(new URL(imagesURL, "knight.gif"));
93 // unitMap.put("SuperFootmanUnit",
94 // new FootmanUnit("Knight", 40, 40, 4, 100, 1000, images));
98 imagesURL
= this.getClass().getResource("/resources/tower-images/");
100 // Create Basic Tower
101 image
= ImageIO
.read(new URL(imagesURL
, "basicTower.gif"));
102 towerMap
.put("BasicTower",
103 new BasicTower(41, 41, 3,
104 (int) (AntiTD
.SQUARE_SIZE
*1.5),
107 } catch (IOException e
) {
108 System
.err
.println("Couldn't find image. Exception: "
112 // Find cost of cheapest unit
113 this.lowestCost
= Integer
.MAX_VALUE
;
114 for (String s
: getUnitTypes()) {
115 Unit unit
= createUnit(s
);
116 if (unit
.getCost() < this.lowestCost
) {
117 this.lowestCost
= unit
.getCost();
122 // TODO: Implement and think, is this crazy? Lot's of stuff shouldn't have
123 // to be in the parameterlist, needed info is x, y, Direction and map, the
124 // rest is information about the unit-type. Create a Unit with only a
125 // startSquare as a parameter?
126 public Unit
createUnit(String type
) {
127 Unit unitPrototype
= unitMap
.get(type
);
128 if (unitPrototype
== null) {
129 throw new IllegalArgumentException("The specified type '" + type
130 + "' doesn't exist");
132 Unit resultUnit
= (Unit
) unitPrototype
.clone();
133 // resultUnit.setX(x);
134 // resultUnit.setY(y);
135 // resultUnit.setWidth(width);
136 // resultUnit.setHeight(health);
137 // resultUnit.setSpeed(speed);
138 // resultUnit.setHealth(health);
139 // resultUnit.setCost(cost);
140 // resultUnit.setDirection(direction);
141 // resultUnit.setMap(map);
145 public Tower
createTower(String type
) {
146 Tower towerPrototype
= towerMap
.get(type
);
147 if (towerPrototype
== null) {
148 throw new IllegalArgumentException("The specified type '" + type
149 + "' doesn't exist");
151 Tower resultTower
= (Tower
) towerPrototype
.clone();
156 // TODO: How to get all keys? Hashtable can return Enumeration
157 // public List<String> getUnitTypes() {}
160 public Object
clone() throws CloneNotSupportedException
{
161 throw new CloneNotSupportedException("AgentPrototypeFactory is a "
162 + "Singleton no clones are"
167 * Returns a Set containing all unitTypes this Class can produce.
169 * @return A Set containing all unitTypes this Class can produce.
171 public Set
<String
> getUnitTypes() {
172 return unitMap
.keySet();
176 * Returns int representing the cost for the cheapest unit
177 * @return int representing the cost for the cheapest unit
179 public int getLowestCost() {
180 return this.lowestCost
;