-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.java
380 lines (328 loc) · 9.51 KB
/
Map.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import java.awt.Graphics;
import java.awt.*;
import java.util.*;
/**
* A Map is a three layered tile map.
* Layer 1 is the base layer. If a tile in this layer is null, it is
* replaced by the base tile (the first tile in the tile set).<p>
* Layer 2 is a special layer in which tiles are rendered in front of or behind
* any sprites, depending on its location.<p>
* Layer 3 is rendered last, over the top of everything else.<p>
*
* Tiles do not have to be the same size. A standard tile is 32 by 32
* pixels. If a tile is larger, it will be rendered in place, with the origin
* at the bottom-right corner of the image. This means a large tile will
* extend above and to the left over the top of other tiles behind it.<p>
*
* the map is only responsble for drawing unchanging tilesets. All interactive
* stuff usually happens in the "Scene", using sprites, although the map does
* support changing tiles on the fly if for example a switch were to move a wall.
*
*
*/
public class Map
{
/* change the below two numbers if you want the grid size to be different.
* individual tile images may be any size. */
int tileWidth = 32;
int tileHeight = 32;
int zoomWidth = 32;
int zoomHeight = 32;
int viewWidth = 400;
int viewHeight = 400;
GraphicsBank gfx;
ArrayList changeListeners;
final static int LAYERS = 3;
Tile[][][] tiles; //ground layer
/**
* Maps are constructed with a width and height, originally
* having all null tiles.
*/
public Map(int width, int height)
{
tiles = new Tile[width][height][LAYERS];
changeListeners = new ArrayList();
}
/**
* Maps are constructed with a width and height, originally
* having all null tiles.
*
* You can also specify the base tile width and height.
*/
public Map(int width, int height, int tileWidth, int tileHeight)
{
this(width, height);
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
zoomWidth = tileWidth;
zoomHeight = tileHeight;
}
/**
* set the tile at location x, y in layer z.
* Layers are:
* 0: ground level.
* 1: on or just above ground.
* 2: above ground (and everything else).
* @param x the X-location of the tile
* @param y the Y-location of the tile.
* @param z the layer the tile is on.
* @param t the new Tile to set.
*/
public void setTile(int x, int y, int z, Tile t)
{
tiles[x][y][z] = t;
}
void setZoom(float z) {
zoomWidth = (int)(tileWidth * z);
zoomHeight = (int)(tileHeight * z);
}
/**
* used only by the game. Sets the width and height in pixels
* to draw when rendering this map.
*/
public void setViewSize(int width, int height)
{
this.viewWidth = width;
this.viewHeight = height;
}
/**
* used by the game. Renders the map to the given graphics context
* at the given offsets.
*/
public void render(Graphics g, int offsetX, int offsetY)
{
//System.out.println("Render Map");
int minX = Math.max(offsetX/zoomWidth, 0);
int maxX = Math.min((offsetX+viewWidth)/zoomWidth, tiles.length);
int minY = Math.max(offsetY/zoomHeight, 0);
int maxY = Math.min((offsetY+viewHeight)/zoomHeight, tiles[0].length);
for(int z=0; z<LAYERS; z++)
{
for(int x=minX; x<maxX; x++)
{
for(int y=minY; y<maxY; y++)
{
if(tiles[x][y][z]!= null)
tiles[x][y][z].render(g, x*zoomWidth -offsetX, y*zoomHeight -offsetY);
}
}
}
}
public void render(Graphics g, Camera c) {
setViewSize(c.viewWidth, c.viewHeight);
render(g, (int)(c.viewx - viewWidth/2), (int)(c.viewy - viewHeight/2));
}
/**
* used by the level editor, renders a portion of the map with the
* given origin and visible dimension.<p>
* The origin and size are needed to increase efficiency by
* only rendering what is on screen.
*
* @param origin the top-left visible corner of the map.
* @param size the size to render.
*/
public void render(Graphics g, Point origin, Dimension size)
{
//System.out.println("Render Map");
double minX = Math.max(origin.getX()/zoomWidth, 0);
double maxX = Math.min((origin.getX()+size.getWidth())/zoomWidth, tiles.length);
double minY = Math.max(origin.getY()/zoomHeight, 0);
double maxY = Math.min((origin.getY()+size.getHeight())/zoomHeight, tiles[0].length);
for(int z=0; z<LAYERS; z++)
{
for(int y=(int)minY; y<maxY; y++)
{
for(int x=(int)minX; x<maxX; x++)
{
if(tiles[x][y][z]!= null) {
tiles[x][y][z].render(g, x*zoomWidth+zoomWidth, y*zoomHeight+zoomHeight);
}
}
}
}
}
public void render(Graphics g, Point origin, Dimension size, int layer)
{
//System.out.println("Render Map");
double minX = Math.max(origin.getX()/zoomWidth, 0);
double maxX = Math.min((origin.getX()+size.getWidth())/zoomWidth, tiles.length);
double minY = Math.max(origin.getY()/zoomHeight, 0);
double maxY = Math.min((origin.getY()+size.getHeight())/zoomHeight, tiles[0].length);
int z = layer;
for(int y=(int)minY; y<maxY; y++)
{
for(int x=(int)minX; x<maxX; x++)
{
if(tiles[x][y][z]!= null) {
tiles[x][y][z].render(g, x*zoomWidth+zoomWidth, y*zoomHeight+zoomHeight);
}
}
}
}
/**
* gets the width of the map in tiles
*/
public int getWidth()
{
return tiles.length;
}
/**
*gets the height of the map in tiles.
*/
public int getHeight()
{
return tiles[0].length;
}
/**
* gets the standard width of a tile in the map.
*/
public int getTileWidth()
{
return tileWidth;
}
/**
* gets the standard height of a tile in the map.
*/
public int getTileHeight()
{
return tileHeight;
}
public int getZoomWidth() {
return zoomWidth;
}
public int getZoomHeight() {
return zoomHeight;
}
/**
*gets the tile at the given location.
*/
public Tile getTile(int x, int y, int z)
{
return tiles[x][y][z];
}
/**
* Resize the map to newWidth, newHeight.
* may clip the edges.
**/
void resize(int newWidth, int newHeight)
{
resize(newWidth, newHeight, LAYERS);
}
/**
* Resize with layers
**/
void resize(int newWidth, int newHeight, int newLayers)
{
System.out.println("Call resize");
int w, h, l;
newWidth = Math.max(1, newWidth);
newHeight = Math.max(1, newHeight);
Tile[][][] newTiles = new Tile[newWidth][newHeight][newLayers];
w = Math.min(newWidth, tiles.length);
h = Math.min(newHeight, tiles[0].length);
l = Math.min(newLayers, tiles[0][0].length);
for(int x = 0; x < w; x++) {
for(int y = 0; y < h; y++) {
for(int j = 0; j < l; j++) {
newTiles[x][y][j] = tiles[x][y][j];
}
}
}
tiles = newTiles;
}
/**
* Move the map tiles
**/
void shift(int offX, int offY)
{
System.out.println("Shift to new offset " + offX + ", " + offY + ".");
Tile[][][] newTiles = new Tile[tiles.length][tiles[0].length][LAYERS];
int xStart = Math.max(0, -offX);
int yStart = Math.max(0, -offY);
int xEnd = Math.min(tiles.length, tiles.length - offX);
int yEnd = Math.min(tiles[0].length, tiles[0].length - offY);
for(int x = xStart; x < xEnd; x++) {
for(int y = yStart; y < yEnd; y++) {
for(int l = 0; l < LAYERS; l++) {
newTiles[x + offX][y + offY][l] = tiles[x][y][l];
}
}
}
tiles = newTiles;
}
void clear()
{
for(int x = 0; x < tiles.length; x++) {
for(int y = 0; y < tiles[0].length; y++) {
for(int l = 0; l < LAYERS; l++) {
tiles[x][y][l] = null;
}
}
}
}
/**
* Provides a no-nonsense integer array version of this map.
* The numbers are the tile IDs.
* The dimensions are: x, y, layer.
**/
public int[][][] toIntArray() {
int set[][][] = new int[tiles.length][tiles[0].length][tiles[0][0].length];
for(int x = 0; x < tiles.length; x++) {
for(int y = 0; y < tiles[0].length; y++) {
for(int l = 0; l < LAYERS; l++) {
if(tiles[x][y][l] != null) {
set[x][y][l] = tiles[x][y][l].number;
} else {
set[x][y][l] = 0;
}
}
}
}
return set;
}
/**
* Means of setting all the tiles on a map easily.
* You can set a different GraphicsBank as well.
**/
public void setAllTiles(int[][][] set, GraphicsBank bank) {
gfx = bank;
resize(tiles.length, tiles[0].length, tiles[0][0].length);
/*
if(set.length == tiles.length &&
set[0].length == tiles[0].length &&
set[0][0].length == tiles[0][0].length) {
*/
for(int x = 0; x < tiles.length; x++) {
for(int y = 0; y < tiles[0].length; y++) {
for(int l = 0; l < LAYERS; l++) {
tiles[x][y][l] = bank.getTile(set[x][y][l]);
}
}
}
/*
} else {
System.out.println("Use resize() Before calling setAllTiles().");
throw new RuntimeException("The int array provided does not match the map dimensions.");
}
*/
}
/* Note: Behaviour unknown. */
public void setTileset(GraphicsBank gfx) {
int[][][] set = this.toIntArray();
this.setAllTiles(set, gfx);
}
public void addChangeListener(MapChangeListener l) {
changeListeners.add(l);
}
public void removeChangeListener(MapChangeListener l) {
changeListeners.remove(l);
}
private void fireChangingEvent(boolean m) {
Iterator i = changeListeners.iterator();
((MapChangeListener)i.next()).mapChanging(m);
}
private void fireChangedEvent(boolean m) {
Iterator i = changeListeners.iterator();
((MapChangeListener)i.next()).mapChanged(m);
}
}