MaImplemented landOn in PathSquare
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / map / Map.java
blobcec6927a7f7bce677caae1f97526e3522d067422
1 package se.umu.cs.dit06ajnajs.map;
3 import java.awt.Dimension;
4 import java.awt.Graphics;
5 import java.awt.Image;
6 import java.awt.Point;
7 import java.awt.image.BufferedImage;
8 import java.util.ArrayList;
9 import java.util.List;
11 import se.umu.cs.dit06ajnajs.AntiTD;
13 public class Map {
15 private String name;
16 private int squareSize;
17 private int width;
18 private int height;
19 private int numCol;
20 private int numRow;
21 private MapSquare[][] squareMatrix;
22 private GoalSquare[] goalSquares;
24 public Map(String name, MapSquare[][] squareMatrix) {
25 this.name = name;
26 this.squareSize = AntiTD.SQUARE_SIZE;
27 this.squareMatrix = squareMatrix;
28 try {
29 this.numCol = squareMatrix.length;
30 this.numRow = squareMatrix[0].length;
32 catch(NullPointerException e) {
33 //TODO Felmeddelande
34 e.printStackTrace();
35 return;
38 // Set width and height for map
39 this.width = squareSize * numCol;
40 this.height = squareSize * numRow;
42 this.goalSquares = extractGoalSquares();
45 public MapSquare getMapSquareAtPoint(int x, int y) {
46 return getMapSquareAtPoint(new Point(x, y));
49 public MapSquare getMapSquareAtPoint(Point point) {
50 //TODO testa algoritmen
51 int x = point.x;
52 int y = point.y;
54 if (x > width || y > height) {
55 throw new IllegalArgumentException("Position is " +
56 "outside of map bounds.");
59 int col = x / AntiTD.SQUARE_SIZE;
60 int row = y / AntiTD.SQUARE_SIZE;
62 return squareMatrix[col][row];
65 public Image getMapImage() {
66 Image backgroundImage = new BufferedImage(width, height,
67 BufferedImage.TYPE_INT_RGB);
68 Graphics g = backgroundImage.getGraphics();
70 for (MapSquare[] row : squareMatrix) {
71 for (MapSquare square : row) {
72 square.paint(g);
75 return backgroundImage;
78 public Dimension getDimension() {
79 return new Dimension(width, height);
82 private GoalSquare[] extractGoalSquares() {
83 List<GoalSquare> squares = new ArrayList<GoalSquare>();
84 for (MapSquare[] row : squareMatrix) {
85 for (MapSquare square : row) {
86 if (square instanceof GoalSquare) {
87 squares.add((GoalSquare) square);
91 GoalSquare[] goalSquares = squares.toArray(new GoalSquare[squares.size()]);
92 //arr = list.toArray(new MyBean[list.size()]);
93 return goalSquares;
96 public TowerSquare getRandomFreeTowerSquare() {
97 List<TowerSquare> squares = extractTowerSquares();
98 List<TowerSquare> freeSquares = new ArrayList<TowerSquare>();
100 for (TowerSquare square : squares) {
101 if (square.isAvailable()) {
102 freeSquares.add(square);
105 if (freeSquares.isEmpty()) {
106 // TODO What should happen if there are no free towersquares?
107 return null;
109 int index = (int) (freeSquares.size()*Math.random());
110 return freeSquares.get(index);
113 public List<TowerSquare> extractTowerSquares() {
114 // TODO What should happen if there are no towersquares?
115 List<TowerSquare> squares = new ArrayList<TowerSquare>();
116 for (MapSquare[] row : squareMatrix) {
117 for (MapSquare square : row) {
118 if (square instanceof TowerSquare) {
119 squares.add((TowerSquare) square);
123 return squares;
126 // TODO: test implementation.
127 public List<MapSquare> getNeighbours(MapSquare square, int range) {
128 List<MapSquare> neighbours = new ArrayList<MapSquare>();
129 int xPos = square.getX();
130 int yPos = square.getY();
132 int scanWidth = range * 2 + 1;
134 int colLeft = (xPos / AntiTD.SQUARE_SIZE) - range;
135 int rowTop = (yPos / AntiTD.SQUARE_SIZE) - range;
137 for (int tmpRow = rowTop; tmpRow < scanWidth; tmpRow++) {
138 for (int tmpCol = colLeft; tmpCol < scanWidth; tmpCol++) {
139 if (tmpCol < 0 || tmpCol > numCol
140 || tmpRow < 0 || tmpRow > numRow) {
141 neighbours.add(squareMatrix[tmpCol][tmpRow]);
145 return neighbours;
148 public GoalSquare[] getGoalSquares() {
149 return this.goalSquares;
153 * Gets the name of this Map.
155 * @return The name of this Map.
157 public String getName() {
158 return this.name;