-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHunt.java
More file actions
339 lines (274 loc) · 10.2 KB
/
Copy pathHunt.java
File metadata and controls
339 lines (274 loc) · 10.2 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
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
public class Hunt extends Entities{
private KeyHandler key;
private StripAnimation walking, idle, die, attack;
public int screenX = 0;
public int screenY = 0;
private int health = 10;
public int life = health;
public int hasKey = 0;
private int hostilesKilled =0, neutralsKilled = 0;
GameWindow window;
public Hunt(GamePanel gp, KeyHandler key, GameWindow window){
super(gp);
this.key = key;
screenX = (gp.getScreenWidth() / 2) - (gp.getTileSize() / 2);
screenY = (gp.getScreenHeight()/ 2) - (gp.getTileSize() / 2);
setDefaultValues();
Image();
boundingBox = new Rectangle();
boundingBox.x = 50;
boundingBox.y = 75;
boundingBox.width = 15;
boundingBox.height = 20;
boundsX = boundingBox.x;
boundsY = boundingBox.y;
this.window = window;
System.out.println("ScreenX: " + screenX + " ScreenY: " + screenY);
//this.width = 50;
//this.height = 50;
}
public void setDefaultValues(){
Worldx = gp.getTileSize() * 15;
Worldy = gp.getTileSize() * 45;
speed = 4;
direction = "idle";
}
public void update(){
if(life <=0){
gp.die();
}
boolean directionKeyPressed = false; // Flag to track if any direction key is pressed
if(key.up){
direction = "up";
directionKeyPressed = true;
//gp.getSoundManager().playClip("walking1", false);
}
if(key.down){
direction = "down";
directionKeyPressed = true;
//gp.getSoundManager().playClip("walking1", false);
}
if(key.left){
direction = "left";
directionKeyPressed = true;
//gp.getSoundManager().playClip("walking1", false);
}
if(key.right){
direction = "right";
directionKeyPressed = true;
//gp.getSoundManager().playClip("walking1", false);
}
if(key.attack){
direction = "attack";
directionKeyPressed = true;
attack();
if(directionKeyPressed){
key.attack = false;
}else{
direction = "idle";
}
}
// Only set to idle if no direction key is pressed
if (!directionKeyPressed) {
direction = "idle";
}
// Tile Collision
collision = false;
gp.cc.checkTile(this);
// Boat Collision
int index = gp.cc.checkBoat(this, true);
Interact(index);
int creatures = gp.cc.checkEntity(this, gp.hostile);
Battle(creatures);
if(collision == false){
switch (direction) {
case "down": Worldy += speed; break;
case "left": Worldx -= speed; break;
case "right": Worldx += speed; break;
case "up": Worldy -= speed; break;
}
}
//System.out.println("Worldx: " + Worldx + " Worldy: " + Worldy);
}
public void attack() {
for (int i = 0; i < gp.hostile.length; i++) {
if (gp.hostile[i] != null && gp.hostile[i] instanceof Bear && canAttackBear(gp.hostile[i])) {
Bear bear = (Bear) gp.hostile[i];
bear.life -= 1;
if (bear.life <= 0) {
isDead = true;
gp.hostile[i] = null;
hostilesKilled++;
gp.ui.showMessage("you have slain a bear!");
gp.getSoundManager().playClip("hit", false);
}
}
}
for (int i = 0; i < gp.neutral.length; i++) {
if (gp.neutral[i] != null && gp.neutral[i] instanceof Deer && canAttackDeer(gp.neutral[i])) {
Deer deer = (Deer) gp.neutral[i];
deer.life -= 1;
if (deer.life <= 0) {
gp.neutral[i] = null;
neutralsKilled++;
gp.ui.showMessage("you have slain a deer!");
gp.getSoundManager().playClip("hit", false);
}
}
}
}
public void Battle(int index){
if(index != 99){
if (index != 99 && gp.hostile[index] != null) {
Entities bear = gp.hostile[index];
bear.life -= 1; // Decrement the life of the Bear
if (bear.life <= 0) {
gp.hostile[index] = null; // Remove the Bear from the game
// Add any additional logic for handling a defeated Bear
}
}
}
}
public boolean canAttackDeer(Entities neutral) {
// Check if the player is within a certain distance from the deer
int distanceX = Math.abs(Worldx - neutral.Worldx);
int distanceY = Math.abs(Worldy - neutral.Worldy);
int attackRange = gp.getTileSize()+5; // Adjust the attack range as needed
return (distanceX <= attackRange && distanceY <= attackRange);
}
private boolean canAttackBear(Entities hostile) {
// Check if the player is within a certain distance from the bear
int distanceX = Math.abs(Worldx - hostile.Worldx);
int distanceY = Math.abs(Worldy - hostile.Worldy);
int attackRange = gp.getTileSize()+3; // Adjust the attack range as needed
return (distanceX <= attackRange && distanceY <= attackRange);
}
public void Interact(int index){
if(index !=99){
String object = gp.obj[index].getName();
switch(object){
case "Key":
hasKey = 1;
gp.obj[index] = null;
gp.ui.showMessage("You have found the Skull key!");
gp.getSoundManager().playClip("repair", false);
break;
case "Boat":
gp.gameState = gp.dialoueState;
if(hasKey == 1 && hostilesKilled >= 5 && neutralsKilled >= 2){
//gp.ui.showMessage("You have escaped the island!");
gp.getSoundManager().stopClip("level1_loop");
gp.getSoundManager().playClip("level2_intro", false);
changeLevel();
//gp.ui.getLevelComplete();
}
else{
gp.ui.Mission = "The boat is broken, Defeat 5 Bears, 2 Deers and find the Skull Key! [Enter....]";
}
break;
}
}
}
public void Image(){
walking = new StripAnimation("images//character//Walk.png", 7, 50);
idle = new StripAnimation("images//character//Idle.png", 8, 50);
die = new StripAnimation("images//character//dieNob.png", 5, 75);
attack = new StripAnimation("images//character//attackNob.png", 4, 90);
}
public void draw(Graphics2D g2d) {
int size = gp.getTileSize() * 2;
BufferedImage image = null;
if (isDead) {
image = die.getImage();
gp.getSoundManager().playClip("death", false);
} else {
switch (direction) {
case "up":
image = walking.getImage();
image = rotateImageByDegrees(image, 270);
break;
case "down":
image = walking.getImage();
image = rotateImageByDegrees(image, 90);
break;
case "left":
image = walking.getImage();
image = flipImageHorizontally(image);
break;
case "right":
image = walking.getImage();
break;
default:
image = idle.getImage();
break;
}
}
g2d.drawImage(image, screenX, screenY, size, size, null);
//g2d.setColor(Color.RED);
//g2d.drawRect(screenX, screenY, size, size);
}
private BufferedImage rotateImageByDegrees(BufferedImage img, double angle) {
double rads = Math.toRadians(angle);
double sin = Math.abs(Math.sin(rads));
double cos = Math.abs(Math.cos(rads));
int w = img.getWidth();
int h = img.getHeight();
int newWidth = (int) Math.floor(w * cos + h * sin);
int newHeight = (int) Math.floor(h * cos + w * sin);
BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
AffineTransform at = new AffineTransform();
at.translate((newWidth - w) / 2, (newHeight - h) / 2);
at.rotate(rads, w / 2, h / 2);
AffineTransformOp rotateOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
rotateOp.filter(img, rotated);
return rotated;
}
private BufferedImage flipImageHorizontally(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage flipped = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
flipped.setRGB(w - x - 1, y, img.getRGB(x, y));
}
}
return flipped;
}
public int getWorldX(){
return Worldx;
}
public int getWorldY(){
return Worldy;
}
public int getScreenX(){
return screenX;
}
public int getScreenY(){
return screenY;
}
public int getHealth(){
return health;
}
public Image getImage(){
return idle.getImage();
}
public int getLife(){
return life;
}
public void changeLevel(){
window.startLevel2();
}
public int hostilesKilled(){
return hostilesKilled;
}
public int neutralsKilled(){
return neutralsKilled;
}
}