-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileMap2.java
More file actions
436 lines (327 loc) · 10.3 KB
/
Copy pathTileMap2.java
File metadata and controls
436 lines (327 loc) · 10.3 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import java.awt.Image;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.Iterator;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
/**
The TileMap class contains the data for a tile-based
map, including Sprites. Each tile is a reference to an
Image. Images are used multiple times in the tile map.
map.
*/
public class TileMap2 {
private static final int TILE_SIZE = 32;
private static final int TILE_SIZE_BITS = 6;
private Image[][] tiles;
private int screenWidth, screenHeight;
private int mapWidth, mapHeight;
private int offsetY;
private LinkedList sprites;
private LinkedList sprites2;
private LinkedList arrows;
private LinkedList arrows2;
private LinkedList bullets;
private LinkedList bullets2;
private Hunter player;
private Heart2 heart;
private boolean boss;
private int lastOffsetX;
BackgroundManager2 bgManager;
private GamePanel2 panel;
private Dimension dimension;
/**
Creates a new TileMap with the specified width and
height (in number of tiles) of the map.
*/
public TileMap2(GamePanel2 panel, int width, int height) {
this.panel = panel;
dimension = panel.getSize();
boss = false;
lastOffsetX = 0;
screenWidth = dimension.width;
screenHeight = dimension.height;
System.out.println ("Width: " + screenWidth);
System.out.println ("Height: " + screenHeight);
mapWidth = width;
mapHeight = height;
// get the y offset to draw all sprites and tiles
offsetY = screenHeight - tilesToPixels(mapHeight);
System.out.println("offsetY: " + offsetY);
bgManager = new BackgroundManager2 (panel, 12);
tiles = new Image[mapWidth][mapHeight];
player = new Hunter (panel, this, bgManager);
heart = new Heart2 (panel, player);
sprites = new LinkedList();
sprites2 = new LinkedList();
arrows = new LinkedList();
arrows2 = new LinkedList();
bullets = new LinkedList();
bullets2 = new LinkedList();
Image playerImage = player.getImage();
int playerHeight = playerImage.getHeight(null);
int x, y;
x = (dimension.width / 2) + TILE_SIZE; // position player in middle of screen
//x = 1000; // position player in 'random' location
y = dimension.height - (TILE_SIZE*4 + playerHeight);
player.setX(x);
player.setY(y);
System.out.println("Player coordinates: " + x + "," + y);
}
/**
Gets the width of this TileMap (number of pixels across).
*/
public int getWidthPixels() {
return tilesToPixels(mapWidth);
}
/**
Gets the width of this TileMap (number of tiles across).
*/
public int getWidth() {
return mapWidth;
}
/**
Gets the height of this TileMap (number of tiles down).
*/
public int getHeight() {
return mapHeight;
}
public int getOffsetY() {
return offsetY;
}
/**
Gets the tile at the specified location. Returns null if
no tile is at the location or if the location is out of
bounds.
*/
public Image getTile(int x, int y) {
if (x < 0 || x >= mapWidth ||
y < 0 || y >= mapHeight)
{
return null;
}
else {
return tiles[x][y];
}
}
/**
Sets the tile at the specified location.
*/
public void setTile(int x, int y, Image tile) {
tiles[x][y] = tile;
}
/**
Gets an Iterator of all the Sprites in this map,
excluding the player Sprite.
*/
public Iterator getSprites() {
sprites = sprites2;
return sprites.iterator();
}
/**
Class method to convert a pixel position to a tile position.
*/
public static int pixelsToTiles(float pixels) {
return pixelsToTiles(Math.round(pixels));
}
/**
Class method to convert a pixel position to a tile position.
*/
public static int pixelsToTiles(int pixels) {
return (int)Math.floor((float)pixels / TILE_SIZE);
}
/**
Class method to convert a tile position to a pixel position.
*/
public static int tilesToPixels(int numTiles) {
return numTiles * TILE_SIZE;
}
/**
Draws the specified TileMap.
*/
public void draw(Graphics2D g2)
{
int mapWidthPixels = tilesToPixels(mapWidth);
// get the scrolling position of the map
// based on player's position
if(player.getX() >= 11578 && (player.getY() + player.getImage().getHeight(null)) > 300)
boss = true;
int offsetX;
if(boss){
offsetX = lastOffsetX;
}else{
offsetX = screenWidth / 2 -
Math.round(player.getX()) - TILE_SIZE;
offsetX = Math.min(offsetX, 0);
offsetX = Math.max(offsetX, screenWidth - mapWidthPixels);
lastOffsetX = offsetX;
}
/*
// draw black background, if needed
if (background == null ||
screenHeight > background.getHeight(null))
{
g.setColor(Color.black);
g.fillRect(0, 0, screenWidth, screenHeight);
}
*/
// draw the background first
bgManager.draw (g2);
//Draw white background (for screen capture)
/*
g2.setColor (Color.WHITE);
g2.fill (new Rectangle2D.Double (0, 0, 600, 500));
*/
// draw the visible tiles
int firstTileX = pixelsToTiles(-offsetX);
int lastTileX = firstTileX + pixelsToTiles(screenWidth) + 1;
for (int y=0; y<mapHeight; y++) {
for (int x=firstTileX; x <= lastTileX; x++) {
Image image = getTile(x, y);
if (image != null) {
g2.drawImage(image,
tilesToPixels(x) + offsetX,
tilesToPixels(y) + offsetY,
null);
}
}
}
// draw player
g2.drawImage(player.getImage(),
Math.round(player.getX()) + offsetX,
Math.round(player.getY()), //+ offsetY,
null);
// draw Heart sprite
g2.drawImage(heart.getImage(),
Math.round(heart.getX()) + offsetX,
Math.round(heart.getY()), 40, 40, //+ offsetY, 50, 50,
null);
// draw sprites
Iterator i = getSprites();
while (i.hasNext()) {
Enemy sprite = (Enemy)i.next();
int x = Math.round(sprite.getX()) + offsetX;
int y = Math.round(sprite.getY()) + offsetY;
if (sprite instanceof Ramm) {
// If sprite is an instance of the Ramm class
g2.drawImage(sprite.getImage(), x, y - sprite.getHeight() + 5, null);
}
if (sprite instanceof Stein) {
// If sprite is an instance of the Ramm class
g2.drawImage(sprite.getImage(), x, y + 32, null);
}
// wake up the creature when it's on screen
if (x >= 0 && x < screenWidth){
((Enemy)sprite).wakeUp();
}
}
// draw arrow sprites
arrows = arrows2;
Iterator i2 = arrows.iterator();
while (i2.hasNext()) {
RammArrow sprite = (RammArrow)i2.next();
int x = Math.round(sprite.getX()) + offsetX;
int y = Math.round(sprite.getY()) + offsetY;
// Draw the arrow
g2.drawImage(sprite.getImage(), x, y - sprite.getHeight() + 35, null);
}
// draw bullets sprites
bullets = bullets2;
Iterator i3 = bullets.iterator();
while (i3.hasNext()) {
Bullet sprite = (Bullet)i3.next();
int x = Math.round(sprite.getX()) + offsetX;
int y = Math.round(sprite.getY()) + offsetY;
// Draw the bullet
g2.drawImage(sprite.getImage(), x, y - sprite.getHeight() + 115, null);
}
}
public void moveLeft(boolean[] directions) {
int x, y;
x = player.getX();
y = player.getY();
String mess = "Going left. x = " + x + " y = " + y;
System.out.println(mess);
player.move(directions);
}
public void stopMoveLeft(boolean[] directions) {
player.move(directions);
System.out.println("stoped going left");
}
public void moveRight(boolean[] directions) {
int x, y;
x = player.getX();
y = player.getY();
String mess = "Going right. x = " + x + " y = " + y;
System.out.println(mess);
player.move(directions);
}
public void stopMoveRight(boolean[] directions) {
player.move(directions);
System.out.println("stoped going right");
}
public void jump(boolean[] directions) {
int x, y;
x = player.getX();
y = player.getY();
String mess = "Jumping. x = " + x + " y = " + y;
System.out.println(mess);
player.move(directions);
}
public void stopJump(boolean[] directions) {
player.move(directions);
System.out.println("stoped jumping");
}
public void update() {
player.update();
if (heart.collidesWithPlayer()) {
panel.endLevel();
return;
}
heart.update();
if (heart.collidesWithPlayer()) {
panel.endLevel();
}
arrows2.clear();
sprites2 = new LinkedList(sprites);
Iterator i = sprites2.iterator();
while (i.hasNext()) {
Enemy sprite = (Enemy)i.next();
sprite.update();
if(sprite instanceof Stein){
if(((Stein)sprite).dead()){
i.remove();
}
}
if(sprite instanceof Ramm){
if(((Ramm)sprite).dead()){
i.remove();
}
arrows2.addAll(((Ramm)sprite).getArrows());
}
}
arrows = arrows2;
Iterator i2 = arrows.iterator();
while (i2.hasNext()) {
RammArrow sprite = (RammArrow)i2.next();
sprite.update();
}
bullets2 = player.getBullets();
Iterator i3 = bullets2.iterator();
while (i3.hasNext()) {
Bullet sprite = (Bullet)i3.next();
sprite.update();
}
}
@SuppressWarnings("unchecked")
public void addSprite(Enemy sprite) {
sprites.add(sprite);
return;
}
public Hunter getPlayer(){
return player;
}
}