1 package nl.tudelft.jpacman.board;
2
3 import nl.tudelft.jpacman.sprite.PacManSprites;
4 import nl.tudelft.jpacman.sprite.Sprite;
5
6 /**
7 * A factory that creates {@link Board} objects from 2-dimensional arrays of
8 * {@link Square}s.
9 *
10 * @author Jeroen Roosen
11 */
12 public class BoardFactory {
13
14 /**
15 * The sprite store providing the sprites for the background.
16 */
17 private final PacManSprites sprites;
18
19 /**
20 * Creates a new BoardFactory that will create a board with the provided
21 * background sprites.
22 *
23 * @param spriteStore
24 * The sprite store providing the sprites for the background.
25 */
26 public BoardFactory(PacManSprites spriteStore) {
27 this.sprites = spriteStore;
28 }
29
30 /**
31 * Creates a new board from a grid of cells and connects it.
32 *
33 * @param grid
34 * The square grid of cells, in which grid[x][y] corresponds to
35 * the square at position x,y.
36 * @return A new board, wrapping a grid of connected cells.
37 */
38 public Board createBoard(Square[][] grid) {
39 assert grid != null;
40
41 Board board = new Board(grid);
42
43 int width = board.getWidth();
44 int height = board.getHeight();
45 for (int x = 0; x < width; x++) {
46 for (int y = 0; y < height; y++) {
47 Square square = grid[x][y];
48 for (Direction dir : Direction.values()) {
49 int dirX = (width + x + dir.getDeltaX()) % width;
50 int dirY = (height + y + dir.getDeltaY()) % height;
51 Square neighbour = grid[dirX][dirY];
52 square.link(neighbour, dir);
53 }
54 }
55 }
56
57 return board;
58 }
59
60 /**
61 * Creates a new square that can be occupied by any unit.
62 *
63 * @return A new square that can be occupied by any unit.
64 */
65 public Square createGround() {
66 return new Ground(sprites.getGroundSprite());
67 }
68
69 /**
70 * Creates a new square that cannot be occupied by any unit.
71 *
72 * @return A new square that cannot be occupied by any unit.
73 */
74 public Square createWall() {
75 return new Wall(sprites.getWallSprite());
76 }
77
78 /**
79 * A wall is a square that is inaccessible to anyone.
80 *
81 * @author Jeroen Roosen
82 */
83 private static final class Wall extends Square {
84
85 /**
86 * The background for this square.
87 */
88 private final Sprite background;
89
90 /**
91 * Creates a new wall square.
92 *
93 * @param sprite
94 * The background for the square.
95 */
96 Wall(Sprite sprite) {
97 this.background = sprite;
98 }
99
100 @Override
101 public boolean isAccessibleTo(Unit unit) {
102 return false;
103 }
104
105 @Override
106 public Sprite getSprite() {
107 return background;
108 }
109 }
110
111 /**
112 * A ground square is a square that is accessible to anyone.
113 *
114 * @author Jeroen Roosen
115 */
116 private static final class Ground extends Square {
117
118 /**
119 * The background for this square.
120 */
121 private final Sprite background;
122
123 /**
124 * Creates a new ground square.
125 *
126 * @param sprite
127 * The background for the square.
128 */
129 Ground(Sprite sprite) {
130 this.background = sprite;
131 }
132
133 @Override
134 public boolean isAccessibleTo(Unit unit) {
135 return true;
136 }
137
138 @Override
139 public Sprite getSprite() {
140 return background;
141 }
142 }
143 }