-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCharacter.java
335 lines (255 loc) · 7.14 KB
/
Character.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
326
327
328
329
330
331
332
333
334
335
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Character {
private int x,y,height,width;
private double dx, dy;
private boolean alive = true;
private BufferedImage sprite;
private BufferedImage[] sprites = new BufferedImage[32];
int speed = 10;
private boolean affectGravity = true;
private int uniqueTag;
private double preserveDx = 0;
private boolean onIce = false;
private boolean canJump = false;
private boolean [] motionState = {false, false, false, false};
private boolean [] lastHoney = {false, false};
private double lastIce = 0;
//MotionState: Used in correspondance to keyListeners to ensure that velocities are only changed once (no acceleration)
//{leftState, rightState, upState, downState}
private boolean applyHoney = false;
private boolean honeyMotion = false;
private boolean iceMotion = false;
private String currentAction = "idle";
private String directionFacing = "right";
private BufferedImage activeFrame;
private int currentFrameIndex;
private boolean finished;
private Item killedBy;
private int platMotion;
public void setPMotion(int motion) {
platMotion = motion;
}
public void setHeight(int newHeight) {
height = newHeight;
}
public int getPMotion() {
return platMotion;
}
public Item getKilledBy() {
return killedBy;
}
public void setKilledBy(Item killedBy) {
this.killedBy = killedBy;
}
public boolean getFinished() {
return finished;
}
public void setFinished(boolean finished) {
this.finished = finished;
}
public void setLastI(double recentIce){
lastIce = recentIce;
}
public double getLastI() {
return lastIce;
}
public boolean getIMotion() {
return iceMotion;
}
public void setIMotion(boolean state) {
iceMotion = state;
}
public boolean[] getHMotion() {
return lastHoney;
}
public void setHMotion(boolean motion, int idx) {
lastHoney[idx] = motion;
}
public boolean getHoney() {
return applyHoney;
}
public void setHoney(boolean state) {
applyHoney = state;
}
public boolean[] getMotion() {
return motionState;
}
public void setMotion(boolean state, int idx) {
motionState[idx] = state;
}
Character(String name) throws IOException{
// currentAction = "crouch"
sprites[0] = ImageIO.read(new File("resources/"+name+"CrouchReference"+".png"));
// currentAction = "death"
for (int i = 1;i<7;i++){
sprites[0+i] = ImageIO.read(new File("resources/"+name+"DeathReference"+i+".png"));
}
// currentAction = "fall"
sprites[7] = ImageIO.read(new File("resources/"+name+"FallReference"+".png"));
// currentAction = "idle"
for (int i = 1;i<7;i++){
sprites[7+i] = ImageIO.read(new File("resources/"+name+"IdleReference"+i+".png"));
}
// currentAction = "jump"
sprites[14] = ImageIO.read(new File("resources/"+name+"JumpReference"+".png"));
// currentAction = "run"
for (int i = 1;i<9;i++){
sprites[14+i] = ImageIO.read(new File("resources/"+name+"RunReference"+i+".png"));
}
// currentAction = "slide"
sprites[23] = ImageIO.read(new File("resources/"+name+"SlideReference"+".png"));
// currentAction = "victory"
for (int i = 1;i<9;i++){
sprites[23+i] = ImageIO.read(new File("resources/"+name+"VictoryReference"+i+".png"));
}
}
Character(String name,int x, int y, int height, int width, int tag) throws IOException{
this.x = x;
this.y = y;
this.height = height;
this.width = width;
this.uniqueTag = tag;
this.dx = 0;
this.dy = 0;
// currentAction = "crouch"
sprites[0] = ImageIO.read(new File("resources/"+name+"CrouchReference"+".png"));
// currentAction = "death"
for (int i = 1;i<7;i++){
sprites[0+i] = ImageIO.read(new File("resources/"+name+"DeathReference"+i+".png"));
}
// currentAction = "fall"
sprites[7] = ImageIO.read(new File("resources/"+name+"FallReference"+".png"));
// currentAction = "idle"
for (int i = 1;i<7;i++){
sprites[7+i] = ImageIO.read(new File("resources/"+name+"IdleReference"+i+".png"));
}
// currentAction = "jump"
sprites[14] = ImageIO.read(new File("resources/"+name+"JumpReference"+".png"));
// currentAction = "run"
for (int i = 1;i<9;i++){
sprites[14+i] = ImageIO.read(new File("resources/"+name+"RunReference"+i+".png"));
}
// currentAction = "slide"
sprites[23] = ImageIO.read(new File("resources/"+name+"SlideReference"+".png"));
// currentAction = "victory"
for (int i = 1;i<9;i++){
sprites[23+i] = ImageIO.read(new File("resources/"+name+"VictoryReference"+i+".png"));
}
currentFrameIndex = 8;
}
Character(BufferedImage sprite) {
this.sprite = sprite;
}
Character(int x, int y, int height, int width, int tag){
this.x = x;
this.y = y;
this.height = height;
this.width = width;
this.uniqueTag = tag;
this.dx = 0;
this.dy = 0;
}
public boolean getIce() {
return onIce;
}
public void setIce(boolean state) {
onIce = state;
}
public void setPDx(double newDx) {
preserveDx = newDx;
}
public double getPDx() {
return preserveDx;
}
public int getTag() {
return uniqueTag;
}
public void setJump(boolean state) {
canJump = state;
}
public boolean getGravity() {
return affectGravity;
}
public void setGravity(boolean state) {
affectGravity = state;
}
public BufferedImage getSprite(){
return sprite;
}
public double[] getVelocity() {
double[] vel = {dx, dy};
return vel;
}
public void setVelocity(double [] newVel) {
dx = newVel[0];
dy = newVel[1];
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public void setPosition(int x,int y) {
this.x = x;
this.y = y;
}
public int[] getPosition() {
int[] location = new int[2];
location[0] = x;
location[1] = y;
return location;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public void crouch() {
}
public boolean getJump() {
return canJump;
}
public void resetGravity() {
affectGravity = true;
}
public void resetY() {
affectGravity = false;
if (dy > 0) {
dy = 0;
}
}
public void setCurrentAction(String action) {
this.currentAction = currentAction;
}
public String getDirectionFacing() {
return directionFacing;
}
public void setDirectionFacing(String directionFacing) {
this.directionFacing = directionFacing;
}
public void die() {
if(alive) {
this.setCurrentFrameIndex(1);
}
alive = false;
currentAction = "death";
}
public BufferedImage getActiveFrame() {
return activeFrame;
}
public int getCurrentFrameIndex() {
return currentFrameIndex;
}
public void setCurrentFrameIndex(int currentFrameIndex) {
this.currentFrameIndex = currentFrameIndex;
activeFrame = sprites[currentFrameIndex];
}
public BufferedImage[] getSprites(){
return sprites;
}
}