-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle.java
474 lines (429 loc) · 16.2 KB
/
Puzzle.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//Puzzle.java
//Christine Wong
//Puzzle creates the segment of the game where user solves puzzles
//inside a room. It setup and coordinates the movement and interaction
//of user and between user and the different objects in the rooms. It
//also display the visual and audio information of the room and when
//user is interacting with the objects. The user can also receive
//information that can progress the game to ending and/or change
//the layout of the panel the next time user access it.
//The setup of the room will change based on the room information it is
//given at the Game class level.
import java.applet.AudioClip;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Puzzle extends JPanel {
//the game objects in the room
private Player user;
private Decoration[] decorations;
private OptionItem[] optionItems;
private KeyItem[] keyItems;
private Door door;
//total numbers of the room items in game
private ArrayList<Decoration[]> totDecorations = new ArrayList<Decoration[]>();
private ArrayList<KeyItem[]> totKeyItems = new ArrayList<KeyItem[]>();
private ArrayList<OptionItem[]> totOptionItems = new ArrayList<OptionItem[]>();
private boolean exit;//if the panel needs to change to Escaperoom
public static final int LEFT=0, RIGHT=1, UP=2, DOWN=3, PAUSE = 4;
private final int roomsNum = 4; //the total number of rooms the game has
private int room = 0; //current room the panel is set in
//visual and audio files to be used during the game
private static final AudioClip messageBox = Utilities.setupAudio("messageBox.wav");
private static final AudioClip openDoor = Utilities.setupAudio("door.wav");
private Image background;
//the information to locate the current items the user is interacting
private String currentItem;
private int indexOfItem;
private boolean reading;
//ROOM INFO
//the boundary the user cannot go past
private int lowX, highX;
private int lowY, highY;
//creates a new instance of the room based on the original room setup info
//the class has
public Puzzle(){
setPreferredSize(new Dimension(800, 600));
setupMansion();
setupRoom(room);
setupSurrounding(room);
exit = false;
reading = false;
}
//SETUP METHODS
//setup the total room items throughout the game
public void setupMansion(){
for(int i = 0; i < roomsNum; i++){ //created based on the number of rooms the game has
Decoration[] decors = Utilities.setupDecorations("map"+i);
totDecorations.add(decors);
KeyItem[] kIs = Utilities.setupKeyItems("map"+i);
totKeyItems.add(kIs);
OptionItem[] oIs = Utilities.setupOptionItems("map"+i);
totOptionItems.add(oIs);
}
}
//setup the basic room information of the room the user is in
public void setupRoom(int n){
try{
Scanner inFile = new Scanner(new BufferedReader(new FileReader(String.format("res/map/map%d/map%d.txt", n, n))));
//setup boundary of the room
int[] constraints = Utilities.parseInts(inFile.nextLine().strip().split(" "));
lowX = constraints[0];
highX = constraints[1];
lowY = constraints[2];
highY = constraints[3];
//setup Player's first position
int[] position = Utilities.parseInts(inFile.nextLine().strip().split(" "));
user = new Player(position[0],position[1]);
//setup where the user will go after exiting
String[] doorLine = inFile.nextLine().strip().split(" ");
int[] doorPos = Utilities.parseInts(Arrays.copyOfRange(doorLine, 0, doorLine.length-1));
door = new Door(doorPos[0], doorPos[1], doorPos[2], doorPos[3], doorPos[4], doorLine[5]);
//setup the background of the room (changes)
background = new ImageIcon(String.format("res/graphics/map%d.png", n)).getImage();
}
catch(IOException e){
System.out.println(e);
}
}
//reset the room when the user re-enters from another door
public void reset(int n){
setupRoom(n);
setupSurrounding(n);
room = n;
exit = false; //avoid accidental exit
}
//when the user is ready to leave this panel
public boolean getExit(){
return exit;
}
//SAVE METHODS
//converts changeable room items (keyItems) in the game
public ArrayList<String> keyItemsToTxt(){
ArrayList<String> tmp = new ArrayList<String>();
for(int x = 0; x < totKeyItems.size(); x++){//saved in the order of game progress
KeyItem[] k = totKeyItems.get(x); //the number of this object remains the same regardless of number of runs
for(int y = 0; y < k.length; y++){
tmp.add(k[y].toTxt());
}
}
return tmp;
}
//converts changeable room items (optionItems) in the game
public ArrayList<String> optionItemsToTxt(){
ArrayList<String> tmp = new ArrayList<String>();
for(int x = 0; x < totOptionItems.size(); x++){//saved in the order of game progress
OptionItem[] opI = totOptionItems.get(x); //the number of this object remains the same regardless of number of runs
for(int y = 0; y < opI.length; y++){
tmp.add(opI[y].toTxt());
}
}
return tmp;
}
//loads changeable room items (optionItems) in the game
public void optionItemsTxtUpdate(String[] txt){
int count = 0;
for(int x = 0; x < totOptionItems.size(); x++){
OptionItem[] opI = totOptionItems.get(x);
for(int y = 0; y < opI.length; y++){
opI[y].txtUpdate(txt[count]); //loaded in the order they're saved in
count++; //makes sure the right order is assigned
}
}
}
//loads changeable room items (keyItems) in the game
public void keyItemsTxtUpdate(String[] txt){
int count = 0;
for(int x = 0; x < totKeyItems.size(); x++){
KeyItem[] k = totKeyItems.get(x);
for(int y = 0; y < k.length; y++){
k[y].txtUpdate(txt[count]); //loaded in the order they're saved in
count++; //makes sure the right order is assigned
}
}
}
//setup the items the current room the user is in based on the room number given
public void setupSurrounding(int n){
//gets the same reference point from a database, so all changes are automatically saved.
decorations = totDecorations.get(n);
optionItems = totOptionItems.get(n);
keyItems = totKeyItems.get(n);
setupActivation();
}
//finds the information necessary for object to changed based on the state of other objects
public void setupActivation(){
for(OptionItem item: optionItems){
if(item.getOtherActivation()==true){//finds the index of that needed object in the collection
int otherIndex = Utilities.findIndex(optionItems,item.getOtherName());
item.setOtherIndex(otherIndex);
}
}
}
//KEYBOARD INPUT
public void pressedKey(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_RIGHT){//for movement
user.move(RIGHT, decorations, optionItems, keyItems); //get items for clearance to make sure doesn't get stuck
user.constrainX(lowX, highX);//constain based on boundary of the room
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT){//for movement
user.move(LEFT, decorations, optionItems, keyItems);
user.constrainX(lowX, highX);//constain based on boundary of the room
}
else if(e.getKeyCode()==KeyEvent.VK_UP){//for movement
user.move(UP, decorations, optionItems, keyItems);
user.constrainY(lowY, highY);//constain based on boundary of the room
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN){//for movement
user.move(DOWN, decorations, optionItems, keyItems);
user.constrainY(lowY, highY);//constain based on boundary of the room
}
if(e.getKeyCode()==KeyEvent.VK_SPACE){ //for room items
checkSurrounding();
}
if (e.getKeyCode()==KeyEvent.VK_Y || e.getKeyCode()==KeyEvent.VK_N){ //for optionItems
inputOptionItems(e);
}
if(e.getKeyCode()==KeyEvent.VK_BACK_SPACE){ //for keyItems
inputDeleteKeyItems(e);
}
}
//input for keyItems
public void typedKey(KeyEvent e){
if (e.getKeyCode()==KeyEvent.VK_BACK_SPACE){
inputDeleteKeyItems(e); //delete input
}
else{
inputKeyItems(e); //add/submit input to item
}
}
//input for movement animation
public void releasedKey(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_RIGHT){//makes sure user is standing still when not moving
user.move(PAUSE, decorations, optionItems, keyItems);
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT){//makes sure user is standing still when not moving
user.move(PAUSE, decorations, optionItems, keyItems);
}
else if(e.getKeyCode()==KeyEvent.VK_UP){//makes sure user is standing still when not moving
user.move(PAUSE, decorations, optionItems, keyItems);
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN){//makes sure user is standing still when not moving
user.move(PAUSE, decorations, optionItems, keyItems);
}
}
//add text to the keyItems currently reading
public void inputKeyItems(KeyEvent e){
if(reading && currentItem.equals("keyItem")){
keyItems[indexOfItem].addText(e);
}
}
//delete text to the keyItems currently reading
public void inputDeleteKeyItems(KeyEvent e){
if(reading && currentItem.equals("keyItem")){
keyItems[indexOfItem].deleteText();
}
}
//input information into the optionItem currently reading
public void inputOptionItems(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_Y){
if(reading && currentItem.equals("optionItem")){//avoid accidental change
messageBox.play(); //play sound effect
if(optionItems[indexOfItem].getOptionDisplay()){//makes sure the item is activated
optionItems[indexOfItem].selectOption(user);
}
}
}
else if (e.getKeyCode()==KeyEvent.VK_N){//exit the option page
if(reading && currentItem.equals("optionItem")){//avoid accidental change
messageBox.play(); //play sound effect
optionItems[indexOfItem].setDisplayText(false);
user.setPause(false); //user is locked when reading
reading = false;
}
}
}
//CHECK METHODS - for interactions
//check all the items in the room for interactions
public void checkSurrounding(){
messageBox.play(); //play sound effect
checkDecors();
checkOptionItems();
checkKeyItems();
checkDoor();
}
//check if the user wants to exit the panel
public void checkDoor(){
if(reading && currentItem.equals("door")){
//if the door is locked and message is displayed
if(door.endPage()){//when user exits message display
user.setPause(false);
door.setDisplayText(false);
reading = false;
}
else{//gets to exits message display
door.nextPage();
}
}
else if (reading == false){
if(door.collide(user)){//looks if there are any items in collision with user
door.checkKey(user);
if(door.getLocked()){//if locked, display message
user.setPause(true); //user can't move when reading
door.setDisplayText(true);
door.nextPage();
reading = true;
currentItem = "door"; //gets information for message display in panel
indexOfItem = 0;
}
else{//setup for exiting panel
user.updatePos(door.getDestX(), door.getDestY());
openDoor.play();
exit = true;
}
}
}
}
//checks for interaction with keyItems
public void checkKeyItems(){
if(reading){
if(currentItem.equals("keyItem")){//avoid accidental change when not reading the object
if(keyItems[indexOfItem].endPage()){//exit message display
keyItems[indexOfItem].setDisplayText(false);
user.setPause(false);
reading = false;
}
else if (keyItems[indexOfItem].inputPage() == false){//moves on with message display
keyItems[indexOfItem].nextPage();
}
else if (keyItems[indexOfItem].inputPage()){//input interactions
keyItems[indexOfItem].checkText(user);
}
}
}
else{
for(int i = 0; i < keyItems.length; i++){
if(keyItems[i].collide(user) && reading == false){//looks if there are any items in collision with user
reading = true;
user.setPause(true); //user can't move when reading
keyItems[i].setDisplayText(true);
currentItem = "keyItem"; //gets information for message display in panel
indexOfItem = i;
}
}
}
}
//checks for interaction with optionItems
public void checkOptionItems(){
if(reading){
if(currentItem.equals("optionItem")){//avoid accidental change
optionItems[indexOfItem].setDisplayText(false); //exit message display
user.setPause(false);
reading = false;
}
}
else{
for(int i = 0; i < optionItems.length; i++){
if(optionItems[i].collide(user) && reading == false){//looks if there are any items in collision with user
reading = true;
user.setPause(true); //user can't move when reading
if(optionItems[i].getOtherActivation()){//change the object if its associated object has been activated
optionItems[i].changeOptionDescription(optionItems[optionItems[i].getOtherIndex()].isActivated());
}
optionItems[i].setDisplayText(true);
currentItem = "optionItem"; //gets information for message display in panel
indexOfItem = i;
}
}
}
}
//checks for interaction with decorations
public void checkDecors(){
if(reading){
if(currentItem.equals("decoration")){//avoid accidental change
if (decorations[indexOfItem].endPage()==false){//move on with message display
decorations[indexOfItem].nextPage();
}
else{//exit message display
decorations[indexOfItem].setDisplayText(false);
user.setPause(false);
reading = false;
}
}
}
else{
for(int i = 0; i < decorations.length; i++){
if(decorations[i].collide(user) && reading == false){//looks if there are any items in collision with user
reading = true;
user.setPause(true);//user can't move when reading
decorations[i].setDisplayText(true);
decorations[i].nextPage();
currentItem = "decoration"; //gets information for message display in panel
indexOfItem = i;
}
}
}
}
//PAINTING METHODS
@Override
public void paintComponent(Graphics g){
paintRoom(g);
drawItems(g);
door.draw(g);
user.draw(g);
paintMessage(g); //avoid items from blocking the message box
}
//draws background
public void paintRoom(Graphics g){
g.drawImage(background, 0, 0, null);
}
//displays the message of the specific item the user is currently reading
public void paintMessage(Graphics g){
if(reading){
if(currentItem.equals("optionItem")){
optionItems[indexOfItem].displayText(g);
}
else if (currentItem.equals("decoration")){
decorations[indexOfItem].displayText(g);
}
else if (currentItem.equals("keyItem")){
keyItems[indexOfItem].displayText(g);
}
else if (currentItem.equals("door")){
door.displayText(g);
}
}
}
//draws the item in the current room user is in
public void drawItems(Graphics g){
for(Decoration item: decorations){
item.draw(g);
}
for(OptionItem item: optionItems){
item.draw(g);
}
for(KeyItem item: keyItems){
item.draw(g);
}
}
//gets the current room number
public int getRoomsNum(){
return roomsNum;
}
//gets user for other panel to update
public Player getPlayer(){
return user;
}
//used to update player object
public void setPlayer(Player newUser){
user = newUser;
}
}