-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDragon.java
More file actions
329 lines (272 loc) · 6.95 KB
/
Dragon.java
File metadata and controls
329 lines (272 loc) · 6.95 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
package entity;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Dragon extends Entity {
private ArrayList<FireBall> fireBalls;
// animations
private ArrayList<BufferedImage[]> sprites;
private final int[] numFrames = {
2, 8, 1, 2, 2, 5, 4
};
// animation actions
private static final int IDLE = 0;
private static final int WALKING = 1;
private static final int JUMPING = 2;
private static final int FALLING = 3;
private static final int SHOOTING = 4;
private static final int PUNCHING = 5;
private static final int GLIDING = 6;
private static final int FLINCHING = 7;
public Dragon(double floor) {
super(floor);
width = 30;
height = 30;
collisionWidth = 20;
collisionHeight = 20;
moveSpeed = 0.25;
maxSpeed = 2.0;
stopSpeed = 0.4;
fallSpeed = 0.15;
maxFallSpeed = 4.0;
jumpStart = -4.8;
stopJumpSpeed = 0.3;
isFacingRight = true;
health = maxHealth = 50;
shootDamage = 2;
fireBalls = new ArrayList<FireBall>();
punchDamage = 4;
punchRange = 40;
// load sprites
try {
BufferedImage spriteSheet = ImageIO.read(getClass().getClassLoader().getResourceAsStream("resources/sprites/player/dragonSprites.gif"));
sprites = new ArrayList<BufferedImage[]>();
for (int i = 0; i < 7; i++) {
BufferedImage[] bi = new BufferedImage[numFrames[i]];
for(int j = 0; j < numFrames[i]; j++) {
if(i != PUNCHING){
bi[j] = spriteSheet.getSubimage(j * width, i * height, width, height);
}
else
bi[j] = spriteSheet.getSubimage(j * width * 2, i * height, width * 2, height);
}
sprites.add(bi);
}
}
catch (Exception e) {
e.printStackTrace();
}
animation = new Animation();
currentAction = IDLE;
animation.setFrames(sprites.get(IDLE));
animation.setDelay(400);
}
// getters
public int getHealth() { return health; }
public int getMaxHealth() { return maxHealth; }
// setters
public void setShooting() {
isShooting = true;
}
public void setPunching() {
isPunching = true;
}
public void checkCloseAttack(Entity enemy) {
if(isPunching) {
if(isFacingRight) {
if((enemy.getx() - enemy.getCollisionWidth()/2 ) > x &&
(enemy.getx() - enemy.getCollisionWidth() ) <= x + punchRange &&
enemy.gety() > y - height/2 &&
enemy.gety() < y + height/2) {
enemy.wasHit(punchDamage);
}
}
else {
if((enemy.getx() - (enemy.getWidth() - enemy.getCollisionWidth())) < x &&
(enemy.getx() - (enemy.getWidth() - enemy.getCollisionWidth())) > x - punchRange &&
enemy.gety() > y - height/2 &&
enemy.gety() < y + height/2) {
enemy.wasHit(punchDamage);
}
}
}
}
public void checkProjectiles(Entity enemy) {
// shoots
// checks if we are hitting the enemy
for (int i = 0; i < fireBalls.size(); i++) {
if(fireBalls.get(i).intersects(enemy)) {
fireBalls.get(i).setHit();
enemy.wasHit(shootDamage);
}
}
}
public void checkAttack(Entity enemy) {
this.checkProjectiles(enemy);
checkCloseAttack(enemy);
}
// this function determines where the next position of the player is by reading keyboard input
private void getNextPosition() {
// movement
if(isLeft) {
dx -= moveSpeed;
if(dx < -maxSpeed) {
dx = -maxSpeed;
}
}
else if(isRight) {
dx += moveSpeed;
if(dx > maxSpeed) {
dx = maxSpeed;
}
}
else {
if(dx > 0) {
dx -= stopSpeed;
if(dx < 0) {
dx = 0;
}
}
else if(dx < 0) {
dx += stopSpeed;
if(dx > 0) {
dx = 0;
}
}
}
// cannot move while attacking unless in the air yet
if((currentAction == PUNCHING || currentAction == SHOOTING) && !(isJumping || isFalling)) {
// cannot move;
dx = 0;
}
// jumping
if(isJumping && !isFalling) {
dy = jumpStart;
isFalling = true;
}
// falling
if(isFalling) {
if(isGliding) {
dy += 0.1 * fallSpeed;
if(dy > maxFallSpeed * 0.1) dy = maxFallSpeed * 0.1;
}
else {
dy += fallSpeed;
if(dy > 0) isJumping = false;
// this makes the longer you hold the jump button the higher you'll jump
if(dy < 0 && !isJumping) dy += stopJumpSpeed;
if(dy > maxFallSpeed) dy = maxFallSpeed;
}
}
}
public void update() {
// update position
getNextPosition();
checkTileMapCollision();
setPosition(xtemp, ytemp);
// check attacks
if(currentAction == PUNCHING) {
if(animation.hasPlayedOnce()) isPunching = false;
}
else if(currentAction == SHOOTING) {
if(animation.hasPlayedOnce()) isShooting = false;
}
// fireball attack and fix exploit of spamming shots
if(isShooting && currentAction != SHOOTING && !isPunching) {
FireBall fb = new FireBall(floor, isFacingRight);
fb.setPosition(x, y);
fireBalls.add(fb);
}
// update fireballs
for (int i = 0; i < fireBalls.size(); i++) {
fireBalls.get(i).update();
if(fireBalls.get(i).shouldRemove()) {
fireBalls.remove(i);
i--;
}
}
// fix bug of spamming shots
if(isPunching && isShooting) {
isShooting = false;
}
// set animation
if(isPunching) {
if(currentAction != PUNCHING) {
currentAction = PUNCHING;
animation.setFrames(sprites.get(PUNCHING));
animation.setDelay(50);
width = 60;
}
}
else if(isShooting) {
if(currentAction != SHOOTING) {
currentAction = SHOOTING;
animation.setFrames(sprites.get(SHOOTING));
animation.setDelay(300);
width = 30;
}
}
else if(isGliding) {
if(currentAction != GLIDING) {
currentAction = GLIDING;
animation.setFrames(sprites.get(GLIDING));
animation.setDelay(100);
width = 30;
}
}
else if(dy > 0) {
if(currentAction != FALLING) {
currentAction = FALLING;
animation.setFrames(sprites.get(FALLING));
animation.setDelay(100);
width = 30;
}
}
else if(dy < 0) {
if(currentAction != JUMPING) {
currentAction = JUMPING;
animation.setFrames(sprites.get(JUMPING));
animation.setDelay(-1);
width = 30;
}
}
else if(isLeft || isRight) {
if(currentAction != WALKING) {
currentAction = WALKING;
animation.setFrames(sprites.get(WALKING));
animation.setDelay(40);
width = 30;
}
}
else {
if(currentAction != IDLE) {
currentAction = IDLE;
animation.setFrames(sprites.get(IDLE));
animation.setDelay(400);
width = 30;
}
}
animation.update();
// set direction
// the player currently cannot move while attacking
if(isRight) isFacingRight = true;
if(isLeft) isFacingRight = false;
super.update();
}
public void draw(Graphics2D graphics) {
// draw fireballs
for (int i = 0; i < fireBalls.size(); i++) {
fireBalls.get(i).draw(graphics);
}
// flinching mechanic that blinks the player when he takes damage
if(isFlinching) {
long elapsed = (System.nanoTime() - flinchTimer) / 1000000;
if(elapsed / 100 % 2 == 0) {
return;
}
}
// draw player
super.draw(graphics);
}
}