-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
323 lines (259 loc) · 8.29 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
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.awt.Color;
import javax.imageio.ImageIO;
public class Player extends GameElement{
public static enum PlayerState {
//values will be filled in when more animations are added
IDLE(1, 1, true, "Idle"),
CROUCHING(9, 1, false, "Crouch"),
IDLE_CROUCH(1, 1, false, "Idle_Crouch"),
WALKING(9, 3, true, "Walk"),
JUMPING(0, 1, true, ""),
PUNCHING(9, 2, false, "Punch"),
KICKING(9, 2, false, "Kick"),
BLOCKING(9, 1, false, "Block"),
IDLE_BLOCK(1, 1, false, "Idle_Block"),
STUNNED(9, 5, false, "Stun");
private final int frames;
private final int frameTime;
private final boolean interruptible;
private final String fileName;
PlayerState(int frames, int frameTime, boolean interruptible, String fileName) {
this.frames = frames;
this.frameTime = frameTime;
this.interruptible = interruptible;
this.fileName = fileName;
}
public int frames() {
return frames;
}
public int frameTime() {
return frameTime;
}
public String fileName() {
return fileName;
}
}
private int yAcceleration;
private boolean facingRight = true;
private int pID;
private int health = 100;
private int score = 0;
private Image image;
private int currFrame = 0;
private int frameCount = 0;
private HitBox hitBox;
private AttackBox attackBox;
private boolean attacked;
private boolean movementDisabled;
private PlayerState currState;
private ArrayList<PlayerState> stateQueue;
public Player(int id, int x, int y){
super(x, y, 0, 0, 85, 100);
yAcceleration = 30;
pID = id;
hitBox = new HitBox(getX()+20,getY()+10,getXSpeed(), getYSpeed(), getWidth()-40,getHeight()-10);
attackBox = new AttackBox(0,0,getXSpeed(), getYSpeed(),0,0);
attacked = false;
movementDisabled = false;
stateQueue = new ArrayList<PlayerState>();
currState = PlayerState.IDLE;
try{
image = ImageIO.read(new FileInputStream(new File("Animations/Player" + pID + "Idle/Player" + pID + "Idle0000.png")));
} catch(Exception e){
e.printStackTrace();
}
}
public void draw(Graphics window){
//applyGravity();
move(getXSpeed(), getYSpeed());
updateImage();
if(facingRight) {
window.drawImage(image, getX(), getY(), getWidth(), getHeight(),null);
} else {
window.drawImage(image, getX() + getWidth(), getY(), -getWidth(), getHeight(), null);
}
window.setColor(Color.BLACK);
//hitBox.draw(window);
updateAttackBox();
window.setColor(Color.RED);
//attackBox.draw(window);
window.setColor(Color.BLACK);
}
private void updateImage() {
//increments currFrame, possible values 0 - state.frames-1
frameCount = (frameCount+1) % currState.frameTime;
if(frameCount%currState.frameTime == 0) {
currFrame = (currFrame + 1) % currState.frames;
if(currFrame==0) {
currState = nextState();
}
}
try{
image = ImageIO.read(new FileInputStream(new File("Animations/Player" + pID + currState.fileName + "/Player" + pID + currState.fileName + "000" + currFrame + ".png")));
} catch(Exception e){
e.printStackTrace();
}
}
public HitBox getHitBox() {
return hitBox;
}
public void updateHitBox() {
hitBox.setX(getX()+20);
hitBox.setY(getY()+10);
hitBox.setXSpeed(getXSpeed());
hitBox.setYSpeed(getYSpeed());
hitBox.setWidth(getWidth()-40);
hitBox.setHeight(getHeight()-10);
if(currState == PlayerState.IDLE_CROUCH) {
hitBox.setY(getY()+60);
hitBox.setHeight(getHeight()-60);
} else if(currState == PlayerState.IDLE_BLOCK) {
hitBox.setY(getY()+20);
hitBox.setHeight(getHeight()-20);
}
}
public AttackBox attackBox() {
return attackBox;
}
public void deleteAttackBox() {
attacked = true;
}
public boolean attacked() {
return attacked;
}
public void updateAttackBox() {
attackBox.setWidth(0);
attackBox.setHeight(0);
attackBox.setPos(-100,-100);
if ((currState == PlayerState.PUNCHING || currState == PlayerState.KICKING) && currFrame == 0) {
attacked = false;
}
if ((currState == PlayerState.PUNCHING || currState == PlayerState.KICKING) && currFrame >= 4 && currFrame <= 6 && !attacked) {
if (facingRight) {
if(currState == PlayerState.PUNCHING)
attackBox.setPos(getX()+getWidth()-5, getY()+50);
else if(currState == PlayerState.KICKING)
attackBox.setPos(getX()+getWidth()-10, getY()+90);
} else {
if(currState == PlayerState.PUNCHING)
attackBox.setPos(getX(), getY()+50);
else if(currState == PlayerState.KICKING)
attackBox.setPos(getX()+5, getY()+90);
}
attackBox.setWidth(5);
attackBox.setHeight(5);
}
}
public void setYAccel(int s){
yAcceleration = s;
}
public int getYAccel(){
return yAcceleration;
}
public void setCurrFrame(int i) {
currFrame = i;
}
public int getCurrFrame() {
return currFrame;
}
@Override
public void setXSpeed(int s) {
if(!movementDisabled || s==0)
super.setXSpeed(s);
}
@Override
public void setYSpeed(int s) {
if(!movementDisabled || s==0)
super.setYSpeed(s);
}
public boolean isSupported(ArrayList<GameElement> objects) {
for(GameElement ge : objects)
if(hitBox.touchingTop(ge))
return true;
return false;
}
public boolean isObstructed(ArrayList<GameElement> objects) {
for(GameElement ge : objects)
if(hitBox.touchingSide(ge))
return true;
return false;
}
public void applyGravity(){
super.setYSpeed(getYSpeed() + 1);
}
public void setFacingRight(boolean isFacingRight) {
facingRight = isFacingRight;
}
public void decreaseHealth(int h){
health -= h;
}
public void setHealth(int h){
health = h;
}
public void increaseScore(int s){
score += s;
}
public void setScore(int s){
score = s;
}
public void enableState(PlayerState ps, PlayerState idlePs) {
if(currState == ps || currState == idlePs) {
fillQueue(idlePs);
} else {
setCurrState(ps);
}
}
public void setMovementDis(boolean b) {
movementDisabled = b;
}
public void disableState(PlayerState ps, PlayerState idlePs) {
stateQueue.remove(idlePs);
stateQueue.remove(ps);
}
public void printStates() {
System.out.println(currState.fileName + " curr");
for(int i=0; i < stateQueue.size(); i++)
System.out.println(stateQueue.get(i).fileName+" "+i);
}
public boolean emptyQueue() {
return stateQueue.size() < 1;
}
public void setCurrState(PlayerState ps) {
currFrame = 0;
currState = ps;
}
public PlayerState getCurrState() {
return currState;
}
public void fillQueue(PlayerState ps) {
for(int i = 0; i < stateQueue.size(); i++)
stateQueue.set(i, ps);
for(int i = 0; i < 5 - stateQueue.size(); i++)
stateQueue.add(ps);
}
public int getHealth(){
return health;
}
public int getScore(){
return score;
}
public void addState(PlayerState state) {
if(currState.interruptible && state != currState) {
setCurrState(state);
} else if(stateQueue.size() < 5 && !(stateQueue.contains(state) && state == currState)){
stateQueue.add(state);
}
}
public PlayerState nextState() {
if(emptyQueue()) {
return PlayerState.IDLE;
} else {
return stateQueue.remove(0);
}
}
}