1 package se
.umu
.cs
.dit06ajnajs
.map
;
3 import java
.awt
.image
.BufferedImage
;
4 import java
.io
.IOException
;
6 import java
.util
.HashMap
;
8 import javax
.imageio
.ImageIO
;
12 * MapSquarePrototypeFactory is used to create new instances of different
13 * implementations of MapSquare from a String. Implemented as a Singleton.
15 * @author Anton Johansson, dit06ajn@cs.umu.se
18 public class MapSquarePrototypeFactory
{
19 // private static Logger logger = Logger.getLogger("AntiTD");
21 private static final MapSquarePrototypeFactory INSTANCE
22 = new MapSquarePrototypeFactory();
24 private java
.util
.Map
<String
, MapSquare
> squareMap
;
27 * Gets the only instance of this class, if none exists one is created.
29 * @return The only instance of this class.
31 public static MapSquarePrototypeFactory
getInstance() {
36 * MapSquarePrototypeFactory initialices all different types of MapSquares
37 * and puts them in a java.util.Map.
39 * Private constructor makes sure there can only be one instance of this
40 * class, and that instance is created by the class itself from the first
41 * invocation of getInstance()
44 private MapSquarePrototypeFactory() {
45 squareMap
= new HashMap
<String
, MapSquare
>();
47 //TODO read squaresettings from file
50 // Get directory containing images
51 // TODO: move to config class of file
52 URL imagesURL
= this.getClass().getResource("/resources/map-images/");
54 // Create TowerSquare Prototype
55 // URL url = this.getClass().getResource("/resources/grass.jpg");
56 URL url
= new URL(imagesURL
, "grass.jpg");
57 BufferedImage image
= ImageIO
.read(url
);
58 squareMap
.put("TowerSquare", new TowerSquare(-1, -1, image
));
60 // Create PathSquare Prototype
61 url
= new URL(imagesURL
, "path.jpg");
62 image
= ImageIO
.read(url
);
63 squareMap
.put("PathSquare", new PathSquare(-1, -1, image
));
65 // Create GoalSquare Prototype
66 url
= new URL(imagesURL
, "goal.gif");
67 image
= ImageIO
.read(url
);
68 squareMap
.put("GoalSquare", new GoalSquare(-1, -1, image
));
71 // Create GoalSquare Prototype
72 url
= new URL(imagesURL
, "start.gif");
73 image
= ImageIO
.read(url
);
74 squareMap
.put("StartSquare", new StartSquare(-1, -1, image
));
76 // Create BlockedSquare Prototype
77 url
= new URL(imagesURL
, "stone.gif");
78 image
= ImageIO
.read(url
);
79 squareMap
.put("BlockedSquare", new BlockedSquare(-1, -1, image
));
81 // Create TurSquare Prototype
82 url
= new URL(imagesURL
, "turnpath.gif");
83 image
= ImageIO
.read(url
);
85 squareMap
.put("TurnSquare", new TurnSquare(-1, -1, image
));
86 } catch (IOException e
) {
87 System
.err
.println("Couldn't find image. Exception: "
93 * Creates a new instance of a MapSquare with the type specified by the
96 * @param type A String representing a type exisiting in this factory.
97 * @param x The x-position of the resulting MapSquare.
98 * @param y The y-position of the resulting MapSquare
99 * @return A newly create MapSquare.
101 public MapSquare
createMapSquare(String type
, int x
, int y
) {
102 MapSquare squarePrototype
= squareMap
.get(type
);
103 if (squarePrototype
== null) {
104 throw new IllegalArgumentException("The specified type '" + type
105 + "' doesn't exist");
107 MapSquare resultSquare
= (MapSquare
) squarePrototype
.clone();
109 // Setup fields that should not be from original shallow clone
111 resultSquare
.setX(x
);
112 resultSquare
.setY(y
);
117 public Object
clone() throws CloneNotSupportedException
{
118 throw new CloneNotSupportedException("AgentPrototypeFactory is a "
119 + "Singleton no clones are"
124 * Returns a Set containing all unitTypes this Class can produce.
126 * @return A Set containing all unitTypes this Class can produce.
128 public Set
<String
> getMapSquareTypes() {
129 return squareMap
.keySet();