-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoor.java
194 lines (166 loc) · 5.77 KB
/
Door.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
//Door.java
//Christine Wong
//Door creates object that helps user to switch between the Puzzle
//and Escaperoom panel while in game. It requires the user to have
//a key in the corresponding position (int[]) for the door to be
//able to be used. When it is unlocked and being accessed, the door
//help switch from the panel the user is in to the other panel in game.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Door {
//information for distinction, text and image display
private String lockedDescription; //message to display when the door is locked
private Image image;
public static final Image msBoxPic = new ImageIcon("res/graphics/message_box.png").getImage(); //the image for the message box; same for every object
public static final Font displayFont = Utilities.getFont("res/font/Happy Clover.ttf",28f); //font is same for every object
//door information
private int x,y;
private int bx, by, ex, ey, height, width; //the interactive area the user can access
private String dir; //the direction the door is facing
private boolean locked; //if the door is locked and requires a key
private int destX, destY; //the destination position user will be in
private int roomNum; //the position of the room the door takes you to
public static final int KEY = 1; //if the key is available
//determines the message to be displayed in the message box
private boolean displayText = false;
private int currentPage;
private int endPage;
//creates a new instance of Door based on data file input
public Door(int x, int y, int destX, int destY, int roomNum, String dir) {
//setup basic room information
this.x = x;
this.y = y;
this.destX = destX;
this.destY = destY;
this.dir = dir;
this.roomNum = roomNum;
//setup display information
image = new ImageIcon("res/items_graphics/door_"+dir+".png").getImage();
width = image.getWidth(null);
height = image.getHeight(null);
setupBoundary(); //to make sure the boundary is available to user
locked = true;
//setup message
lockedDescription = "The door is locked.";
currentPage = 0;
endPage = 1;
}
//setup the interactive boundary based on the direction the door is facing
public void setupBoundary(){
if(dir.equals("LEFT")){ //the door is facing left, there is area at the left side
bx = x - 30;
by = y;
ex = width;
ey = height;
}
else if(dir.equals("RIGHT")){ //the door is facing right, there is area at the right side
bx = x;
by = y;
ex = width + 30;
ey = height;
}
else if(dir.equals("UP")){ //the door is facing up, there is area at the top side
bx = x;
by = y - 30;
ex = width;
ey = height;
}
else{//if the door is facing down; makes sure the door is activated to avoid crashing
bx = x;
by = y;
ex = width;
ey = height + 30;
}
}
//returns if user is at the interactive area of the object
public boolean collide(Player user){
return getRect().intersects(user.getInteractRect());
}
//draws the object when it is at Puzzle panel
public void draw(Graphics g){
g.drawImage(image, x, y, null);
if (displayText){
displayText(g);
}
}
//draws the object when it is at Escaperoom panel
public void drawWorld(Graphics g, int x_change, int y_change){
//keep the object at a secured position relative to the player
g.drawImage(image, x-x_change, y-y_change, null);
if(displayText){
displayText(g);
}
}
//display the message assigned
public void displayText(Graphics g){
//setup the border
g.drawImage(msBoxPic,0,406,null);
//set up the messages
g.setFont(displayFont);
g.setColor(Color.white);
g.drawString(lockedDescription, 20, 450);
}
//checks if the user has the needed key to unlock the door
public void checkKey(Player user){
if (user.getKeys()[roomNum] == KEY){ //if there is a key at the location
unlock();
}
}
//sets the state of message display
public void setDisplayText(boolean state){
displayText = state;
if(displayText == false){
resetPage(); //avoid skipping the first page/out of bound when next access
}
}
//resets index for message display
public void resetPage(){
currentPage = 0;
}
//returns if the message is being displayed
public boolean getDisplayText(){
return displayText;
}
//returns if the message is at the last index of description
public boolean endPage(){
return currentPage >= endPage;
}
//go to the next section for message display
public void nextPage(){
if(currentPage<endPage){ //avoid out of bound
currentPage++;
}
}
//returns the current section of description the message is in
public int getCurrentPage(){
return currentPage;
}
//makes the door accessible
public void unlock(){
locked = false;
}
//gets the state of accessibility
public boolean getLocked(){
return locked;
}
//returns x position used by other classes while transporting user
public int getDestX(){
return destX;
}
//returns y position used by other classes while transporting user
public int getDestY(){
return destY;
}
//returns the room number for other classes to transport user to
public int getRoomNum(){
return roomNum;
}
//returns the interact area for user
public Rectangle getRect(){
return new Rectangle(bx, by, ex, ey);
}
}