-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapBase.java
246 lines (214 loc) · 7.26 KB
/
MapBase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import java.awt.*;
import java.util.LinkedList;
public class MapBase {
/**
* Border = blockt die Seiten
* Item = Blockt alle Items (eingestellten TileIDs)
* = Blockt beide oberen
*/
public Tile[][] mapTiles;
public int mapSizeX;
public int mapSizeY;
protected GamePanel gamePanel;
protected TileSet tileSet;
protected int graphicID = 22;
protected int chapterXOffset, chapterYOffset;
protected String mapStatus;
protected boolean active;
protected boolean gridLines = false;
public MapBase(GamePanel gp, TileSet pTileSet, String pStatus, Point pChapterOffset) {
gamePanel = gp;
tileSet = pTileSet;
mapStatus = pStatus;
if (pStatus == null) {
mapStatus = "null";
} else {
mapStatus = pStatus;
}
if (pChapterOffset == null) {
chapterYOffset = 0;
chapterXOffset = 0;
} else {
chapterXOffset = (int) pChapterOffset.getX() * Tile.TILEWIDTH;
chapterYOffset = (int) pChapterOffset.getY() * Tile.TILEHEIGHT;
}
}
public void createBaseMap() {
Tile.setTILEHEIGHT(64);
Tile.setTILEWIDTH(64);
mapTiles = new Tile[mapSizeX][mapSizeY];
int i = 2;
for (int zeile = 0; zeile < mapSizeX; zeile++) {
for (int spalte = 0; spalte < mapSizeY; spalte++) {
mapTiles[zeile][spalte] = tileSet.tileSet[22].clone();
mapTiles[zeile][spalte].setID(22);
mapTiles[zeile][spalte].setFocusable(false);
i++;
}
}
setBorderOrItemsBlocked();
setNeighbours();
}
public void setBorderOrItemsBlocked() {
if (mapStatus.equals("null")) {
return;
}
if (mapStatus.equals("Item")) {
itemBlock();
return;
}
if (mapStatus.equals("Border")) {
borderBlock();
return;
}
if (mapStatus.equals("All")) {
itemBlock();
borderBlock();
return;
}
System.err.println("Möglicherweise ein Schreibfehler beim Status!");
}
public void borderBlock() {
//Oben:
for (int i = 0; i < mapSizeX - 1; i++) {
mapTiles[i][0].setBlocked(true);
}
//Unten:
for (int i = 0; i < mapSizeX - 1; i++) {
mapTiles[i][mapSizeY - 1].setBlocked(true);
}
//Links:
for (int i = 0; i < mapSizeY - 1; i++) {
mapTiles[0][i].setBlocked(true);
}
//Rechts:
for (int i = 0; i < mapSizeY - 1; i++) {
mapTiles[mapSizeX - 1][i].setBlocked(true);
}
}
public void itemBlock() {
LinkedList<Integer> blockedIDs = tileSet.getBlockedTiles();
for (int i = 0; i < blockedIDs.size(); i++) {
System.out.println("Blocked: " + blockedIDs.get(i));
}
for (int zeile = 0; zeile < mapSizeX; zeile++) {
for (int spalte = 0; spalte < mapSizeY; spalte++) {
for (int i = 0; i < blockedIDs.size(); i++) {
if (blockedIDs.get(i) == mapTiles[zeile][spalte].getID()) {
mapTiles[zeile][spalte].setBlocked(true);
}
}
}
}
}
public boolean isActiveInPosition(Point position) {
return isActiveInTileID(new Point((int) position.getX() * Tile.TILEWIDTH, (int) position.getY() * Tile.TILEHEIGHT));
}
public boolean isActiveInTileID(Point tileID) {
active = tileID.getX() < getMapSizeX() && tileID.getY() < getMapSizeY() && tileID.getX() >= 0 && tileID.getY() >= 0;
return active;
}
public void renderMap(Graphics2D g2d) {
for (int zeile = 0; zeile < mapSizeY; zeile++) {
for (int spalte = 0; spalte < mapSizeX; spalte++) {
mapTiles[zeile][spalte].renderTile(g2d, chapterXOffset + zeile * Tile.TILEWIDTH - gamePanel.getCamera().getXOffset(), chapterYOffset + spalte * Tile.TILEHEIGHT - gamePanel.getCamera().getYOffset());
}
}
}
public void setNeighbours() {
for (int zeile = 0; zeile < mapTiles.length; zeile++) {
for (int spalte = 0; spalte < mapTiles.length; spalte++) {
LinkedList temp = new LinkedList();
try {
temp.addFirst(mapTiles[zeile - 1][spalte - 1]);
} catch (Exception e) {
}
try {
temp.addFirst(mapTiles[zeile][spalte - 1]);
} catch (Exception e) {
}
try {
temp.addFirst(mapTiles[zeile + 1][spalte - 1]);
} catch (Exception e) {
}
try {
temp.addFirst(mapTiles[zeile - 1][spalte]);
} catch (Exception e) {
}
try {
temp.addFirst(mapTiles[zeile + 1][spalte]);
} catch (Exception e) {
}
try {
temp.addFirst(mapTiles[zeile - 1][spalte + 1]);
} catch (Exception e) {
}
try {
temp.addFirst(mapTiles[zeile][spalte + 1]);
} catch (Exception e) {
}
try {
temp.addFirst(mapTiles[zeile + 1][spalte]);
} catch (Exception e) {
}
mapTiles[spalte][zeile].setNeighbours(temp);
} // end of for
} // end of for
}
public int getMapSizeY() {
return mapSizeY;
}
public void setMapSizeY(int mapSizeY) {
this.mapSizeY = mapSizeY;
}
public int getMapSizeX() {
return mapSizeX;
}
public void setMapSizeX(int mapSizeX) {
this.mapSizeX = mapSizeX;
}
public int getGraphicID() {
return graphicID;
}
public TileSet getTileSet() {
return tileSet;
}
public void setchapterXOffset(int pChapterOffset) {
chapterXOffset = pChapterOffset * Tile.TILEWIDTH;
}
public void setchapterYOffset(int pChapterOffset) {
chapterYOffset = pChapterOffset * Tile.TILEHEIGHT;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public Point getChapterOffset() {
return new Point(chapterXOffset, chapterYOffset);
}
public void setGridLines(boolean gridLines) {
Insets border;
boolean showImage;
boolean fill;
if (gridLines) {
border = new Insets(2, 2, 2, 2);
showImage = false;
fill = true;
} else {
border = new Insets(0, 0, 0, 0);
showImage = true;
fill = false;
}
for (int spalte = 0; spalte < mapTiles.length; spalte++) {
for (int zeile = 0; zeile < mapTiles.length; zeile++) {
mapTiles[spalte][zeile].showImage(showImage);
mapTiles[spalte][zeile].setBorderInsets(border);
if (mapTiles[spalte][zeile].isBlocked()) {
mapTiles[spalte][zeile].setFill(fill);
}
}
}
}
}