-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
361 lines (316 loc) · 12.2 KB
/
Player.java
File metadata and controls
361 lines (316 loc) · 12.2 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//Player.java
//Christine Wong
//Player creates object that the user directly controls
//via keyboard input. It can move and interact with other objects
//while in Puzzle and Escaperoom panel, and these panels
//determines what happens when the user interacts with it.
//The object will also change image (animation) when it moves.
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import javax.swing.ImageIcon;
public class Player{
//the position of the player (top left corner)
private int x, y;
private int width, height;
//graphics
//4 , 4 - number of direction, number of frames per direction
private Image[][] pics; //for storing the frames of the walking animation
//user information
private String base = "res/sprite/playersprite/player"; //for loading the graphic
public static final int UNIT = 5; //how much pixel the player moves for every key press
private boolean pause = false; //if the player is being paused right now
private int dir, frame, delay;
//frame - current frame of the animation
//delay - keeps track of how long the current frame has been displayed
//the directions the player can move in
public static final int LEFT=0, RIGHT=1, UP=2, DOWN=3, PAUSE = 4, WAIT = 4;
//pause - when the user is not moving; wait - number of frame of the animation before
//it moves onto the next frame
//INVENTORY
private int [] keys; //the keys the user needs to obtain in order to go to different rooms.
//the position of the key corresponds to different rooms. Obtaining the key in that position grants
//user the permission to go to that room.
//creates new instance based on position
public Player(int x, int y){
//loading in the position
this.x = x;
this.y = y;
//loading in basic user information
keys = new int[5];
dir = RIGHT; //the default direction and animation frame
frame = 0;
delay = 0;
//loads the walking animation frames
pics = loadWalking(4,4);
}
//loads the graphics of the walking animation
public Image[][] loadWalking(int d, int f){
Image[][] tmp = new Image[d][f]; //d - number of direction, f - number of frames per direction
for(int px = 0; px < d; px++){
for(int py = 0; py < f; py++){
tmp[px][py] = new ImageIcon(base+0+px+(py+1)+".png").getImage();
}
}
return tmp;
}
//for moving the player object's position in Puzzle panel
public void move(int direction, Decoration[] decors, OptionItem[] options, KeyItem[] keys){
if(pause == false){//makes sure the player is able to move right now
//updates the direction even when not moving for animation
if(direction==LEFT){
if (clearItems(decors, options, keys, -UNIT,0)){//makes sure doesn't collide with tangible items
x-=UNIT;
}
dir = LEFT;
}
else if(direction==RIGHT){
if(clearItems(decors, options, keys, UNIT,0)){//makes sure doesn't collide with tangible items
x+=UNIT;
}
dir = RIGHT;
}
else if (direction==UP){
if (clearItems(decors, options, keys, 0, -UNIT)){//makes sure doesn't collide with tangible items
y-=UNIT;
}
dir = UP;
}
else if(direction==DOWN){
if (clearItems(decors, options, keys, 0, UNIT)){//makes sure doesn't collide with tangible items
y+=UNIT;
}
dir = DOWN;
}
//update visual information
updateFrame(direction);
}
}
//for moving the player object in the Escaperoom panel
public void move(int direction){//checking for clearance is done at the panel level
if(pause == false){//makes sure the user is able to move right now
if(direction==LEFT){
x-=UNIT;
dir = LEFT;
}
else if(direction==RIGHT){//uses else-if to make sure user doesn't move diagonally
x+=UNIT;
dir = RIGHT;
}
else if (direction==UP){
y-=UNIT;
dir = UP;
}
else if(direction==DOWN){
y+=UNIT;
dir = DOWN;
}
}
}
//check if the user is colliding any of the tangible items before moving
private boolean clearItems(Decoration[] decors, OptionItem[] options, KeyItem[] kIs, int x_change, int y_change){
return clearDecorations(decors, x_change, y_change) &&
clearOptionItems(options, x_change, y_change) &&
clearKeyItems(kIs, x_change, y_change); //user can move only when not colliding with any of the items
}
//sub-method from clearItems; check if the user is colliding with any of the tangible optionItems
private boolean clearOptionItems(OptionItem[] options, int x_change, int y_change){
if(options != null){//avoid null exception
for(OptionItem item: options){
if(item.collideItem(this.getInteractRect(x_change, y_change))){//uses the preview position to avoid getting stuck
return false;
}
}
}
return true;
}
//sub-method from clearItems; check if the user is colliding with any of the tangible keyItems
private boolean clearKeyItems(KeyItem[] kIs, int x_change, int y_change){
if(kIs!=null){//avoid null exception
for(KeyItem item: kIs){
if(item.collideItem(this.getInteractRect(x_change, y_change))){//uses the preview position to avoid getting stuck
return false;
}
}
}
return true;
}
//sub-method from clearItems; check if the user is colliding with any of the tangible decorations
private boolean clearDecorations(Decoration[] decors, int x_change, int y_change){
if(decors != null){//avoid null exception
for(Decoration item: decors){
if(item.collideItem(this.getInteractRect(x_change, y_change))){//uses the preview position to avoid getting stuck
return false;
}
}
}
return true;
}
//clearance method used in Escaperoom panel
//makes sure the user is not in collision with any of the items or the wall/boundaries
public boolean clearEscaperoom(BufferedImage mask, Decoration[] decors){
if(dir==RIGHT){//check based on what position the user will be in after movement
//to avoid getting stuck
return clearMask(mask, UNIT) && clearDecorations(decors, UNIT, 0);
}
else if(dir==LEFT){//check based on what position the user will be in after movement
//to avoid getting stuck
return clearMask(mask, UNIT) && clearDecorations(decors, -UNIT, 0);
}
else if(dir==UP){//check based on what position the user will be in after movement
//to avoid getting stuck
return clearMask(mask, UNIT) && clearDecorations(decors, 0, -UNIT);
}
else if(dir==DOWN){//check based on what position the user will be in after movement
//to avoid getting stuck
return clearMask(mask, UNIT) && clearDecorations(decors, 0, UNIT);
}
return true; //the user can move if there is no collision (true)
}
//used when the user is in Escaperoom panel
//checks if the bottom half of the user (uses 4 reference points) are in collision of the mask
private boolean clearMask(BufferedImage mask, int dist){
if(dir == RIGHT){//uses preview position to avoid getting stuck
return Utilities.clear(mask, x+dist, getCY()) && Utilities.clear(mask, x+width+dist, getCY())
&& Utilities.clear(mask, x+dist, y+height) && Utilities.clear(mask, x+width+dist, y+height);
}
else if(dir == LEFT){//uses preview position to avoid getting stuck
return Utilities.clear(mask, x-dist, getCY()) && Utilities.clear(mask, x+width-dist, getCY())
&& Utilities.clear(mask, x-dist, y+height) && Utilities.clear(mask, x+width-dist, y+height);
}
else if(dir == UP){//uses preview position to avoid getting stuck
return Utilities.clear(mask, x, getCY()-dist) && Utilities.clear(mask, x, y+height-dist)
&& Utilities.clear(mask, x+width, getCY()-dist) && Utilities.clear(mask, x+width, y+height-dist);
}
else if(dir==DOWN){//uses preview position to avoid getting stuck
return Utilities.clear(mask, x, getCY()+dist) && Utilities.clear(mask, x, y+height+dist)
&& Utilities.clear(mask, x+width, getCY()+dist) && Utilities.clear(mask, x+width, y+height+dist);
}
return true; //the user can move if there is no collision (true)
}
//determines what frame the animation is in
public void updateFrame(int direction){
delay += 1;
//WAIT here dictates the speed of the object animation
if(delay % WAIT == 0){
frame = (frame + 1) % pics[dir].length;
}
if (direction==PAUSE){//so the user looks standing still
frame=0;
}
}
//makes sure the user doesn't go out of bound in the panel
public void constrainX(int low, int high){
if(x < low){
x = low;
}
else if(x + width > high){ //if the right side of the image goes out of bound
x= high - width;
}
}
//makes sure the user doesn't go out of bound in the panel
public void constrainY(int low, int high){
if(y + (height/2) < low){ //if the centre in y-position is out of bound
y = low - (height/2);
}
else if (y + height > high){ //if the bottom side is out of bound
y = high - height;
}
}
//draws the user when in Puzzle panel
public void draw(Graphics g){
//update image lengths from the frames
width = pics[dir][frame].getWidth(null);
height = pics[dir][frame].getHeight(null);
g.drawImage(pics[dir][frame], x, y, null);
}
//draws the user when in Escaperoom panel
public void drawWorld(Graphics g){
//update image lengths from the frames
width = pics[dir][frame].getWidth(null);
height = pics[dir][frame].getHeight(null);
int halfX = width/2;
int halfY = height/2;
//makes sure the centre of the user is at the centre of the screen
g.drawImage(pics[dir][frame], x-(x-400)-halfX, y-(y-300)-halfY, null);
}
//returns the centre of the user (x-position)
public int getCX(){
return x+(width/2);
}
//returns the centre of the user (y-position)
public int getCY(){
return y+(height/2);
}
//returns the x-position of the user
public int getX(){
return x;
}
//returns the y-position of the user
public int getY(){
return y;
}
//returns the keys and key information of the user
public int[] getKeys(){
return keys;
}
//update the keys
public void setKeys(int[] newKeys){
keys = Arrays.copyOf(newKeys, newKeys.length);
}
//update user position
public void updatePos(int x, int y){
this.x = x;
this.y = y;
}
//determines if the player can move or not (get paused)
public void setPause(boolean state){
pause = state;
}
//get the state of paused for the player object
public boolean getPause(){
return pause;
}
//sets the direction the user is facing/moving
public void setDir(int direction){
dir = direction;
}
//get the area of the player that can interact with the surrounding objects
//(at the feet of the player image)
public Rectangle getInteractRect(){
int size = 2; //the length of that "feet"
return new Rectangle(getCX() - size, y + height - size, size*2, size);
}
//gets the area the enemy can collide with the user
//(at the centre of the player image)
public Rectangle getEnemyRect(){
int size = 5; //the length of the area
return new Rectangle(x + size, getCY() - size, width - size*2, size*2);
}
//overload method of getInteractRect(), for when need to predict the location of the
//interacting area after movement
public Rectangle getInteractRect(int x_change, int y_change){
int size = 2; //the height of that "feet"
return new Rectangle(getCX()+x_change - size, y+height-size+y_change,size*2,size);
}
//SAVING METHODS
//convert changeable user information into text to be saved in datafile
public String toTxt(){
String keyString = Arrays.toString(keys).substring(1, Arrays.toString(keys).length()-1).replaceAll(",",""); //clean the key information
//so it is easier to load
return x+" "+y+" "+dir+" "+keyString;
}
//load & update the changeable user information from datafile to continue the previous progress
public void txtUpdate(String line){
//split information into parts based on purpose
String[] data = line.split(" ");
int[] val = Utilities.parseInts(Arrays.copyOfRange(data, 0, data.length));
//update information in the order they are saved in
x = val[0];
y = val[1];
dir = val[2];
keys = Arrays.copyOfRange(val, 3, val.length);
}
}