-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
158 lines (120 loc) · 3.13 KB
/
Copy pathPlayer.java
File metadata and controls
158 lines (120 loc) · 3.13 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
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Player extends Enitity {
private BufferedImage[][] animations;
private int aniTick, aniIndex, aniSpeed = 25;
private int playerAction = Constant.PlayerConstants.IDLE;
private boolean moving = false, attacking = false;
private boolean left, up, right, down;
private float playerSpeed = 2.0f;
private int[][] lvlData;
private float xDrawOffset = 30 * game.SCALE;
private float yDrawOffset = 25* game.SCALE;
public Player(float x, float y, int width, int height) {
super(x, y, width, height);
loadAnimations();
initHitbox(x, y, 20 * game.SCALE, 28 * game.SCALE);
}
public void update() {
updatePos();
updateAnimationTick();
setAnimation();
}
public void render(Graphics g) {
g.drawImage(animations[playerAction][aniIndex], (int) (hitbox.x - xDrawOffset), (int) (hitbox.y - yDrawOffset), width, height, null);
drawHitbox(g);
}
private void updateAnimationTick() {
aniTick++;
if (aniTick >= aniSpeed) {
aniTick = 0;
aniIndex++;
if (aniIndex >= Constant.PlayerConstants.GetSpriteAmount(playerAction)) {
aniIndex = 0;
attacking = false;
}
}
}
private void setAnimation() {
int startAni = playerAction;
if (moving)
playerAction = Constant.PlayerConstants.RUNNING;
else
playerAction = Constant.PlayerConstants.IDLE;
if (attacking)
playerAction = Constant.PlayerConstants.ATTAK1;
if (startAni != playerAction)
resetAniTick();
}
private void resetAniTick() {
aniTick = 0;
aniIndex = 0;
}
private void updatePos() {
moving = false;
if (!left && !right && !up && !down)
return;
float xSpeed = 0, ySpeed = 0;
if (left && !right)
xSpeed = -playerSpeed;
else if (right && !left)
xSpeed = playerSpeed;
if (up && !down)
ySpeed = -playerSpeed;
else if (down && !up)
ySpeed = playerSpeed;
// if (CanMoveHere(x + xSpeed, y + ySpeed, width, height, lvlData)) {
// this.x += xSpeed;
// this.y += ySpeed;
// moving = true;
// }
if (HelpMethods.CanMoveHere(hitbox.x + xSpeed, hitbox.y + ySpeed, hitbox.width, hitbox.height, lvlData)) {
hitbox.x += xSpeed;
hitbox.y += ySpeed;
moving = true;
}
}
private void loadAnimations() {
BufferedImage img = LoadSave.GetSpriteAtlas(LoadSave.PLAYER_ATLAS);
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 * 64, j * 40, 64, 40);
}
public void loadLvlData(int[][] lvlData) {
this.lvlData = lvlData;
}
public void resetDirBooleans() {
left = false;
right = false;
up = false;
down = false;
}
public void setAttacking(boolean attacking) {
this.attacking = attacking;
}
public boolean isLeft() {
return left;
}
public void setLeft(boolean left) {
this.left = left;
}
public boolean isUp() {
return up;
}
public void setUp(boolean up) {
this.up = up;
}
public boolean isRight() {
return right;
}
public void setRight(boolean right) {
this.right = right;
}
public boolean isDown() {
return down;
}
public void setDown(boolean down) {
this.down = down;
}
}