1 package se
.umu
.cs
.dit06ajnajs
.agent
;
3 import java
.awt
.image
.BufferedImage
;
4 import java
.io
.IOException
;
6 import javax
.imageio
.ImageIO
;
7 import se
.umu
.cs
.dit06ajnajs
.agent
.Direction
;
8 import se
.umu
.cs
.dit06ajnajs
.map
.Map
;
10 import java
.util
.HashMap
;
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;
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
>();
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: "
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
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);
86 // TODO: How to get all keys? Hashtable can return Enumeration
87 // public List<String> getUnitTypes() {}
90 public Object
clone() throws CloneNotSupportedException
{
91 throw new CloneNotSupportedException("AgentPrototypeFactory is a "
92 + "Singleton no clones are"
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();