-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
325 lines (262 loc) · 6.73 KB
/
Player.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
package entities;
import static utilz.Constants.PlayerConstants.*;
import static utilz.HelpMethods.*;
import static utilz.Constants.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import gamestates.Playing;
import main.Game;
import utilz.LoadSave;
public class Player extends Entity {
private BufferedImage[][] animations;
private boolean moving = false, attacking = false;
private boolean left, right, jump;
private int[][] lvlData;
private float xDrawOffset = 130 * Game.SCALE;
private float yDrawOffset = 140 * Game.SCALE;
// Jumping / Gravity
private float jumpSpeed = -2.25f * Game.SCALE;
private float fallSpeedAfterCollision = 0.5f * Game.SCALE;
// StatusBarUI
private BufferedImage statusBarImg;
private int statusBarWidth = (int) (192 * Game.SCALE);
private int statusBarHeight = (int) (58 * Game.SCALE);
private int statusBarX = (int) (10 * Game.SCALE);
private int statusBarY = (int) (10 * Game.SCALE);
private int healthBarWidth = (int) (150 * Game.SCALE);
private int healthBarHeight = (int) (4 * Game.SCALE);
private int healthBarXStart = (int) (34 * Game.SCALE);
private int healthBarYStart = (int) (14 * Game.SCALE);
private int healthWidth = healthBarWidth;
private int flipX = 0;
private int flipW = 1;
private boolean attackChecked;
private Playing playing;
public Player(float x, float y, int width, int height, Playing playing) {
super(x, y, width, height);
this.playing = playing;
this.state = IDLE;
this.maxHealth = 100;
this.currentHealth = 35;
this.walkSpeed = Game.SCALE * 1.0f;
loadAnimations();
initHitbox(20, 27);
initAttackBox();
}
public void setSpawn(Point spawn) {
this.x = spawn.x;
this.y = spawn.y;
hitbox.x = x;
hitbox.y = y;
}
private void initAttackBox() {
attackBox = new Rectangle2D.Float(x, y, (int) (20 * Game.SCALE), (int) (20 * Game.SCALE));
}
public void update() {
updateHealthBar();
if (currentHealth <= 0) {
playing.setGameOver(true);
return;
}
updateAttackBox();
updatePos();
if (moving) {
checkPotionTouched();
checkSpikesTouched();
}
if (attacking)
checkAttack();
updateAnimationTick();
setAnimation();
}
private void checkSpikesTouched() {
playing.checkSpikesTouched(this);
}
private void checkPotionTouched() {
playing.checkPotionTouched(hitbox);
}
private void checkAttack() {
if (attackChecked || aniIndex != 1)
return;
attackChecked = true;
playing.checkEnemyHit(attackBox);
playing.checkObjectHit(attackBox);
}
private void updateAttackBox() {
if (right)
attackBox.x = hitbox.x + hitbox.width + (int) (Game.SCALE * 10);
else if (left)
attackBox.x = hitbox.x - hitbox.width - (int) (Game.SCALE * 10);
attackBox.y = hitbox.y + (Game.SCALE * 10);
}
private void updateHealthBar() {
healthWidth = (int) ((currentHealth / (float) maxHealth) * healthBarWidth);
}
public void render(Graphics g, int lvlOffset) {
g.drawImage(animations[state][aniIndex], (int) (hitbox.x - xDrawOffset) - lvlOffset + flipX, (int) (hitbox.y - yDrawOffset), width * flipW, height, null);
// drawHitbox(g, lvlOffset);
// drawAttackBox(g, lvlOffset);
drawUI(g);
}
private void drawUI(Graphics g) {
g.drawImage(statusBarImg, statusBarX, statusBarY, statusBarWidth, statusBarHeight, null);
g.setColor(Color.red);
g.fillRect(healthBarXStart + statusBarX, healthBarYStart + statusBarY, healthWidth, healthBarHeight);
}
private void updateAnimationTick() {
aniTick++;
if (aniTick >= ANI_SPEED) {
aniTick = 0;
aniIndex++;
if (aniIndex >= GetSpriteAmount(state)) {
aniIndex = 0;
attacking = false;
attackChecked = false;
}
}
}
private void setAnimation() {
int startAni = state;
if (moving)
state = RUNNING;
else
state = IDLE;
if (inAir) {
if (airSpeed < 0)
state = JUMP;
else
state = FALLING;
}
if (attacking) {
state = ATTACK;
if (startAni != ATTACK) {
aniIndex = 1;
aniTick = 0;
return;
}
}
if (startAni != state)
resetAniTick();
}
private void resetAniTick() {
aniTick = 0;
aniIndex = 0;
}
private void updatePos() {
moving = false;
if (jump)
jump();
if (!inAir)
if ((!left && !right) || (right && left))
return;
float xSpeed = 0;
if (left) {
xSpeed -= walkSpeed;
flipX = width;
flipW = -1;
}
if (right) {
xSpeed += walkSpeed;
flipX = 0;
flipW = 1;
}
if (!inAir)
if (!IsEntityOnFloor(hitbox, lvlData))
inAir = true;
if (inAir) {
if (CanMoveHere(hitbox.x, hitbox.y + airSpeed, hitbox.width, hitbox.height, lvlData)) {
hitbox.y += airSpeed;
airSpeed += GRAVITY;
updateXPos(xSpeed);
} else {
hitbox.y = GetEntityYPosUnderRoofOrAboveFloor(hitbox, airSpeed);
if (airSpeed > 0)
resetInAir();
else
airSpeed = fallSpeedAfterCollision;
updateXPos(xSpeed);
}
} else
updateXPos(xSpeed);
moving = true;
}
private void jump() {
if (inAir)
return;
inAir = true;
airSpeed = jumpSpeed;
}
private void resetInAir() {
inAir = false;
airSpeed = 0;
}
private void updateXPos(float xSpeed) {
if (CanMoveHere(hitbox.x + xSpeed, hitbox.y, hitbox.width, hitbox.height, lvlData))
hitbox.x += xSpeed;
else
hitbox.x = GetEntityXPosNextToWall(hitbox, xSpeed);
}
public void changeHealth(int value) {
currentHealth += value;
if (currentHealth <= 0)
currentHealth = 0;
else if (currentHealth >= maxHealth)
currentHealth = maxHealth;
}
public void kill() {
currentHealth = 0;
}
public void changePower(int value) {
System.out.println("Added power!");
}
private void loadAnimations() {
BufferedImage img = LoadSave.GetSpriteAtlas(LoadSave.PLAYER_LILITH);
animations = new BufferedImage[9][6];
for (int j = 0; j < animations.length; j++)
for (int i = 0; i < animations[j].length; i++)
animations[j][i] = img.getSubimage(i * 288, j * 128, 288, 128);
statusBarImg = LoadSave.GetSpriteAtlas(LoadSave.STATUS_BAR);
}
public void loadLvlData(int[][] lvlData) {
this.lvlData = lvlData;
if (!IsEntityOnFloor(hitbox, lvlData))
inAir = true;
}
public void resetDirBooleans() {
left = false;
right = false;
}
public void setAttacking(boolean attacking) {
this.attacking = attacking;
}
public boolean isLeft() {
return left;
}
public void setLeft(boolean left) {
this.left = left;
}
public boolean isRight() {
return right;
}
public void setRight(boolean right) {
this.right = right;
}
public void setJump(boolean jump) {
this.jump = jump;
}
public void resetAll() {
resetDirBooleans();
inAir = false;
attacking = false;
moving = false;
state = IDLE;
currentHealth = maxHealth;
hitbox.x = x;
hitbox.y = y;
if (!IsEntityOnFloor(hitbox, lvlData))
inAir = true;
}
}