1 package se
.umu
.cs
.dit06ajnajs
;
3 import java
.awt
.Dimension
;
4 import java
.awt
.Graphics
;
7 import java
.awt
.image
.BufferedImage
;
8 import java
.util
.ArrayList
;
13 private int squareSize
;
16 private MapSquare
[][] squareMatrix
;
18 public Map(int squareSize
, MapSquare
[][] squareMatrix
) {
19 this.squareSize
= squareSize
;
20 this.squareMatrix
= squareMatrix
;
24 numCol
= squareMatrix
.length
;
25 numRow
= squareMatrix
[0].length
;
27 catch(NullPointerException e
) {
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
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
) {
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?
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
);