addTower-method
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / Map.java
blobad432aaf7fcfc947a1a008180bb01f545923332e
1 package se.umu.cs.dit06ajnajs;
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 public class Map {
13 private int squareSize;
14 private int width;
15 private int height;
16 private MapSquare[][] squareMatrix;
18 public Map(int squareSize, MapSquare[][] squareMatrix) {
19 this.squareSize = squareSize;
20 this.squareMatrix = squareMatrix;
21 int numCol;
22 int numRow;
23 try {
24 numCol = squareMatrix.length;
25 numRow = squareMatrix[0].length;
27 catch(NullPointerException e) {
28 //TODO Felmeddelande
29 e.printStackTrace();
30 return;
33 // Set width and height for map
34 this.width = squareSize * numCol;
35 this.height = squareSize * numRow;
38 // TODO Gör metod för att få ut bakgrundsbild. I denna klass eller någon annanstns
40 public MapSquare getMapSquareAtPos(Point point) {
41 //TODO testa algoritmen
42 int x = point.x;
43 int y = point.y;
45 if (x > width || y > height) {
46 throw new IllegalArgumentException("Position is " +
47 "outside of map bounds.");
50 int col = x/squareSize;
51 int row = y/squareSize;
53 return squareMatrix[col][row];
56 public Image getMapImage() {
57 Image backgroundImage = new BufferedImage(width, height,
58 BufferedImage.TYPE_INT_RGB);
59 Graphics g = backgroundImage.getGraphics();
61 for (MapSquare[] row : squareMatrix) {
62 for (MapSquare square : row) {
63 square.paint(g);
66 return backgroundImage;
69 public Dimension getDimension() {
70 return new Dimension(width, height);
73 public TowerSquare getRandomFreeTowerSquare() {
74 List<TowerSquare> squares = getTowerSquares();
75 List<TowerSquare> freeSquares = new ArrayList<TowerSquare>();
77 for (TowerSquare square : squares) {
78 if (square.isAvailable()) {
79 freeSquares.add(square);
82 if (freeSquares.isEmpty()) {
83 // TODO What should happen if there are no free towersquares?
84 return null;
86 int index = (int) (freeSquares.size()*Math.random());
87 return freeSquares.get(index);
90 public List<TowerSquare> getTowerSquares() {
91 // TODO What should happen if there are no towersquares?
92 List<TowerSquare> squares = new ArrayList<TowerSquare>();
93 for (MapSquare[] row : squareMatrix) {
94 for (MapSquare square : row) {
95 if (square instanceof TowerSquare) {
96 squares.add((TowerSquare) square);
100 return squares;