-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHunter.java
More file actions
480 lines (390 loc) · 13.6 KB
/
Copy pathHunter.java
File metadata and controls
480 lines (390 loc) · 13.6 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import java.awt.*;
import javax.swing.*;
import java.awt.geom.Rectangle2D;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
public class Hunter {
private static final int DX = 10; // amount of X pixels to move in one keystroke
private static final int DY = 16; // amount of Y pixels to move in one keystroke
private static final int TILE_SIZE = 32;
private static final int hurtDuration = 8;
//public HashSet<Integer> directions;
int lastDirection;
private JPanel panel;
private int x, y, width, height, dx, dy;
private Image l2PlayerImage, l2PlayerLeftImage, l2PlayerRightImage, l2PlayerIdleRightImage, l2PlayerIdleLeftImage, l2PlayerShootRightImage, l2PlayerShootLeftImage;
private SoundManager soundManager;
private Dimension d;
private Color color;
private TileMap2 tileMap;
private BackgroundManager2 bgManager;
private boolean jumping;
private boolean boss;
private int timeElapsed;
private int startY;
private boolean goingUp;
private boolean goingDown;
private boolean inAir;
private int initialVelocity;
private int startAir;
private boolean shot;
private int count;
private LinkedList bullets;
private int health;
private boolean dead;
private boolean isHurting;
private Image l2PlayerImageHurt;
private int hurtTimer;
public Hunter(GamePanel2 panel, TileMap2 t, BackgroundManager2 b){
//directions = new HashSet<>();
this.panel = panel;
tileMap = t; // tile map on which the player's sprite is displayed
bgManager = b; // instance of BackgroundManager
goingUp = goingDown = false;
inAir = false;
boss = false;
shot = false; // use to make to fire
count = 0;
lastDirection = 2;
bullets = new LinkedList();
health = 10;
dead = false;
isHurting = false;
l2PlayerLeftImage = ImageManager2.loadImage("images2/character/walkLeft50x64.gif");
l2PlayerRightImage = ImageManager2.loadImage("images2/character/walkRight50x64.gif");
l2PlayerIdleRightImage = ImageManager2.loadImage("images2/character/idleRight50x64.gif");
l2PlayerIdleLeftImage = ImageManager2.loadImage("images2/character/idleLeft50x64.gif");
l2PlayerShootRightImage = ImageManager2.loadImage("images2/character/shootRight50x64.gif");
l2PlayerShootLeftImage = ImageManager2.loadImage("images2/character/shootLeft50x64.gif");
l2PlayerImage = l2PlayerIdleRightImage;
soundManager = SoundManager.getInstance();
}
public Point collidesWithTile(int newX, int newY) {
int playerWidth = l2PlayerImage.getWidth(null);
int offsetY = tileMap.getOffsetY();
int xTile = tileMap.pixelsToTiles(newX);
int yTile = tileMap.pixelsToTiles(newY - offsetY);
if (tileMap.getTile(xTile, yTile) != null) {
Point tilePos = new Point (xTile, yTile);
return tilePos;
}
else {
return null;
}
}
public Point collidesWithTileDown (int newX, int newY) {
int playerWidth = l2PlayerImage.getWidth(null);
int playerHeight = l2PlayerImage.getHeight(null);
int offsetY = tileMap.getOffsetY();
int xTile = tileMap.pixelsToTiles(newX);
int yTileFrom = tileMap.pixelsToTiles(y - offsetY);
int yTileTo = tileMap.pixelsToTiles(newY - offsetY + playerHeight);
for (int yTile=yTileFrom; yTile<=yTileTo; yTile++) {
if (tileMap.getTile(xTile, yTile) != null) {
Point tilePos = new Point (xTile, yTile);
return tilePos;
}
else {
if (tileMap.getTile(xTile+1, yTile) != null) {
int leftSide = (xTile + 1) * TILE_SIZE;
if (newX + playerWidth > leftSide) {
Point tilePos = new Point (xTile+1, yTile);
return tilePos;
}
}
}
}
return null;
}
public Point collidesWithTileUp (int newX, int newY) {
int playerWidth = l2PlayerImage.getWidth(null);
int offsetY = tileMap.getOffsetY();
int xTile = tileMap.pixelsToTiles(newX);
int yTileFrom = tileMap.pixelsToTiles(y - offsetY);
int yTileTo = tileMap.pixelsToTiles(newY - offsetY);
for (int yTile=yTileFrom; yTile>=yTileTo; yTile--) {
if (tileMap.getTile(xTile, yTile) != null) {
Point tilePos = new Point (xTile, yTile);
return tilePos;
}
else {
if (tileMap.getTile(xTile+1, yTile) != null) {
int leftSide = (xTile + 1) * TILE_SIZE;
if (newX + playerWidth > leftSide) {
Point tilePos = new Point (xTile+1, yTile);
return tilePos;
}
}
}
}
return null;
}
public synchronized void move (boolean[] directions) {
int newX = x;
Point tilePos = null;
if (newX >= 11578){
boss = true;
}
if(shot){
count++;
}
if(count > 10){
shot = false;
}
if (!panel.isVisible ()) return;
if (directions[1] == true) { // move left
l2PlayerImage = l2PlayerLeftImage;
lastDirection = 1;
newX = x - DX;
if (newX < 0) {
x = 0;
return;
}
tilePos = collidesWithTile(newX, y);
}
//else
if (directions[2] == true) { // move right
l2PlayerImage = l2PlayerRightImage;
lastDirection = 2;
int playerWidth = l2PlayerImage.getWidth(null);
newX = x + DX;
int tileMapWidth = tileMap.getWidthPixels();
if (newX + l2PlayerImage.getWidth(null) >= tileMapWidth) {
x = tileMapWidth - l2PlayerImage.getWidth(null);
return;
}
tilePos = collidesWithTile(newX+playerWidth, y);
}
//else // jump
if (directions[3] && !jumping && !goingDown) {
jump();
return;
}
if(directions[0] && !shot){
if(lastDirection == 1){
l2PlayerImage = l2PlayerShootLeftImage;
}
else{
l2PlayerImage = l2PlayerShootRightImage;
}
shoot();
count = 0;
shot = true;
}
//else
if (!directions[0] && !directions[1] && !directions[2] && lastDirection == 1){
l2PlayerImage = l2PlayerIdleLeftImage;
}
//else
if (!directions[0] && !directions[1] && !directions[2] && lastDirection == 2){
l2PlayerImage = l2PlayerIdleRightImage;
}
if (tilePos != null) {
if (directions[1]) {
System.out.println (": Collision going left");
x = ((int) tilePos.getX() + 1) * TILE_SIZE; // keep flush with right side of tile
}
else
if (directions[2]) {
System.out.println (": Collision going right");
int playerWidth = l2PlayerImage.getWidth(null);
x = ((int) tilePos.getX()) * TILE_SIZE - playerWidth; // keep flush with left side of tile
}
}
else {
if (directions[1] == true) {
x = newX;
if(!boss)
bgManager.moveLeft();
}
else
if (directions[2] == true) {
x = newX;
if(!boss)
bgManager.moveRight();
}
if (isInAir()) {
System.out.println("In the air. Starting to fall.");
if (directions[1] == true) { // make adjustment for falling on left side of tile
int playerWidth = l2PlayerImage.getWidth(null);
x = x + DX -10 ; //x = x - playerWidth + DX; having player width throws him off the left from far away, however having playwidth in the other location fixes that; only problem is he gets stuck; subbing 10 however fixes it well;
}
fall();
}
}
// Iterator i = bullets.iterator();
// while (i.hasNext()) {
// Bullet bullet = (Bullet)i.next();
// if(!arrow.fired()){
// arrows.remove(arrow);
// }
// }
}
public boolean isInAir() {
int playerHeight;
int playerWidth;
Point tilePos;
if (!jumping && !inAir) {
playerHeight = l2PlayerImage.getHeight(null);
playerWidth = l2PlayerImage.getWidth(null);
tilePos = collidesWithTile(x + playerWidth, y + playerHeight + 1); // check below player to see if there is a tile
if (tilePos == null) // there is no tile below player, so player is in the air
return true;
else // there is a tile below player, so the player is on a tile
return false;
}
return false;
}
private void fall() {
jumping = false;
inAir = true;
timeElapsed = 0;
goingUp = false;
goingDown = true;
startY = y;
initialVelocity = 0;
}
public void jump () {
if (!panel.isVisible ()) return;
jumping = true;
timeElapsed = 0;
goingUp = true;
goingDown = false;
startY = y;
initialVelocity = 50;
}
public void update () {
int distance = 0;
int newY = 0;
if(health <= 0){
dead = true;
((GamePanel2) panel).loseGame();
}
timeElapsed++;
if (jumping || inAir) {
distance = (int) (initialVelocity * timeElapsed -
4.9 * timeElapsed * timeElapsed);
newY = startY - distance;
if (newY > y && goingUp) {
goingUp = false;
goingDown = true;
}
if (goingUp) {
Point tilePos = collidesWithTileUp (x, newY);
if (tilePos != null) { // hits a tile going up
System.out.println ("Jumping: Collision Going Up!");
int offsetY = tileMap.getOffsetY();
int topTileY = ((int) tilePos.getY()) * TILE_SIZE + offsetY;
int bottomTileY = topTileY + TILE_SIZE;
y = bottomTileY;
fall();
}
else {
y = newY;
System.out.println ("Jumping: No collision.");
// the following if-statement is to pause the jump to capture the screen
/*
if (x > 1608 && y < 300) {
try {
Thread.sleep (1000);
}
catch (Exception e) {
System.out.println ("ERROR! " + e);
}
}
*/
}
}
else
if (goingDown) {
Point tilePos = collidesWithTileDown (x, newY);
if (tilePos != null) { // hits a tile going up
System.out.println ("Jumping: Collision Going Down!");
int playerHeight = l2PlayerImage.getHeight(null);
goingDown = false;
int offsetY = tileMap.getOffsetY();
int topTileY = ((int) tilePos.getY()) * TILE_SIZE + offsetY;
y = topTileY - playerHeight;
jumping = false;
inAir = false;
}
else {
y = newY;
System.out.println ("Jumping: No collision.");
}
}
}
Iterator i = bullets.iterator();
while (i.hasNext()) {
Bullet bullet = (Bullet)i.next();
if(!bullet.fired()){
bullets.remove(bullet);
}
}
l2PlayerImageHurt = ImageManager2.tintImage(l2PlayerImage, Color.RED);
if (isHurting) {
hurtTimer++;
if (hurtTimer >= hurtDuration) {
isHurting = false;
hurtTimer = 0; // Reset the timer
}
}
}
public void moveUp () {
if (!panel.isVisible ()) return;
y = y - DY;
}
public void draw(Graphics2D g2d){
// g2d.drawImage(image, x, y, width, height, panel);
}
public Rectangle2D.Double getBounds(){
int playerWidth = l2PlayerImage.getWidth(null);
int playerHeight = l2PlayerImage.getHeight(null);
return new Rectangle2D.Double (x, y- tileMap.getOffsetY(), playerWidth, playerHeight);
}
public int getX(){
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY(){
return y;
}
public void setY(int y) {
this.y = y;
}
public Image getImage() {
if (isHurting) {
return l2PlayerImageHurt;
} else {
return l2PlayerImage;
}
}
private void shoot() {
//pow pow pow
soundManager.playClip("shoot", false);
Bullet temp = new Bullet(tileMap, lastDirection, this.getX(), this.getY());
bullets.add(temp);
}
public LinkedList getBullets(){
return bullets;
}
public void takeDamage(int x){
health = health - x;
isHurting = true;
soundManager.playClip("hit", false);
return;
}
public int getHealth(){
return health;
}
public void setHealth(int x){
health = x;
return;
}
public boolean dead(){
return dead;
}
}