-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
760 lines (640 loc) · 18.7 KB
/
Player.java
File metadata and controls
760 lines (640 loc) · 18.7 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import javax.swing.*;
public class Player
{
private Room currentRoom;
private String currentRoomID;
private String previousRoomID; //for flee
private String playerName;
private int playerHP;
private int playerMaxHP;
private int playerDamage;
private double detectionLvl;
private boolean triggered;
private Item equippedItem;
private ArrayList<Item> playerInventory;
private ArrayList<Item> equippedItems;
private HashSet<String> visitedCheckpointRooms;
private boolean inPuzzleMode = false;
public Player(String startingRoomID)
{
currentRoomID = startingRoomID;
playerHP = 100;
playerMaxHP = 100;
playerDamage = 5;
detectionLvl = 0;
playerInventory = new ArrayList<>();
equippedItems = new ArrayList<>();
visitedCheckpointRooms = new HashSet<>();
// add starting items
for (Item template: FileLoader.getItemList()) {
if (template.getItemName().equalsIgnoreCase("Shipping Label") ||
template.getItemName().equalsIgnoreCase("Pocket Knife") ||
template.getItemName().equalsIgnoreCase("Lockpick")) {
Item starterItem = template.clone();
starterItem.setCurrentStack(1);
playerInventory.add(starterItem);
}
}
}
public boolean isInPuzzleMode() {
return inPuzzleMode;
}
public void setInPuzzleMode(boolean inPuzzleMode) {
this.inPuzzleMode = inPuzzleMode;
}
public String getPlayerName()
{
return playerName;
}
public int getPlayerHP()
{
return playerHP;
}
public int getPlayerDamage()
{
return playerDamage;
}
public double getDetectionLvl()
{
return detectionLvl;
}
public boolean getTriggered()
{
return triggered;
}
public void setTriggered(boolean triggered)
{
this.triggered = triggered;
}
public String getCurrentRoomID()
{
return currentRoomID;
}
public void setCurrentRoomID(String roomID)
{
this.currentRoomID = roomID;
}
public void setCurrentRoom(Room room)
{
this.currentRoom = room;
}
public Room getCurrentRoom()
{
return currentRoom;
}
public void exploreRoom(Map <String, Room> gameMap)
{
Room currentRoom = gameMap.get(currentRoomID);
System.out.println("Room name: " + currentRoom.getRoomName());
System.out.println("Desc: " + currentRoom.getRoomDescr());
System.out.println("Items: " + currentRoom.getItems());
System.out.println("Characters: " + currentRoom.getCharacterList());
System.out.println("Exits: " + currentRoom.getExits().keySet());
System.out.println();
}
public void accessInventory()
{
if (playerInventory.isEmpty())
{
System.out.println("Inventory is empty.\n");
return;
}
// mostrar items
for (int i = 0; i < playerInventory.size(); i++)
{
Item item = playerInventory.get(i);
System.out.println("Slot " + i + ": " + item.getItemName()
+ " (x" + item.getCurrentStack() + ")");
}
// mostrar slots vacíos hasta 8
for (int i = playerInventory.size(); i < 8; i++)
{
System.out.println("Slot " + i + ": [Empty]");
}
System.out.println();
}
public void pickUpItem(String item, Map <String, Room> gameMap)
{
// TODO Auto-generated method stub
if (playerInventory.size() >= 8)
{
System.out.println("Inventory full!"
+ " You cannot carry more than 8 stacks of items.\n");
return;
}
Room currentRoom = gameMap.get(currentRoomID);
Item itemToPickUp = null;
for (Item itemInRoom : currentRoom.getItems()) {
if (itemInRoom.getItemName().equalsIgnoreCase(item)) {
itemToPickUp = itemInRoom;
break;
}
}
if (itemToPickUp != null) {
playerInventory.add(itemToPickUp);
currentRoom.removeItem(itemToPickUp);
System.out.println(itemToPickUp.getItemName()
+ " added to inventory.\n");
} else {
System.out.println("Item is not found in this room.\n");
}
}//end pickUpItem method
public void dropItem(String item, Map <String, Room> gameMap)
{
// TODO Auto-generated method stub
Room currentRoom = gameMap.get(currentRoomID);
Item itemToDrop = null;
for(Item itemInInv: playerInventory){
if(itemInInv.getItemName().equalsIgnoreCase(item)){
itemToDrop = itemInInv;
break;
}
}
if (itemToDrop != null) {
playerInventory.remove(itemToDrop);
currentRoom.addItem(itemToDrop);
System.out.println("Dropped: " + itemToDrop.getItemName()
+ " from this room.\n");
} else {
System.out.println("Item can not be dropped.\n");
}
}//end dropItem method
public void useItem(String item, Map<String, Room> gameMap)
{
Item itemFound = null;
// Search for the item in the player's inventory
for (Item invInInv : playerInventory) {
if (invInInv.getItemName().equalsIgnoreCase(item)) {
itemFound = invInInv;
break;
}
}
if (itemFound == null) {
System.out.println("You don't have an item named '" +
item + "' in your inventory.\n");
return;
}
String type = itemFound.getItemType();
// If it's consumable, remove it after use
if (type.equals("consumable"))
{
Consumable itemToConsume = (Consumable) itemFound;
playerInventory.remove(itemFound);
System.out.println(itemFound.getItemName() + " has been consumed.");
playerHP += itemToConsume.getHP();
System.out.println("Current health point is: " + playerHP);
System.out.println();
}
else if(type.equals("useable"))
{
Useable itemToUse = (Useable) itemFound;
// ==== UNLOCK ROOMS BEFORE REMOVING THE KEY ====
Room current = this.getCurrentRoom();
for (String direction : current.getExits().keySet())
{
String nextRoomID = current.getExits().get(direction);
Room nextRoom = gameMap.get(nextRoomID);
if (nextRoom != null && nextRoom.isLocked())
{
if (itemFound.getItemID().equalsIgnoreCase(nextRoom.getRequiredKey()))
{
nextRoom.setLocked(false);
System.out.println("You used " + itemFound.getItemName()
+ " to unlock " + nextRoom.getRoomName() + "!");
}
}
}
// Use item and decrease stack
itemToUse.use();
if(itemToUse.getCurrentStack() <= 0 &&
!itemToUse.getItemID().startsWith("A-"))
{
playerInventory.remove(itemToUse);
System.out.println(itemToUse.getItemName()
+ " has been removed from inventory.\n");
}
}
else
{
System.out.println("Item type not valid.\n");
}
}//end useItem method
public void inspectItem(String item)
{
// TODO Auto-generated method stub
Item itemToInspect = null;
for(Item itemInInv: playerInventory){
if(itemInInv.getItemName().equalsIgnoreCase(item)){
itemToInspect = itemInInv;
System.out.println(itemToInspect.getItemName()
+ ": " + itemToInspect.getItemDesc());
return;
}
}
System.out.println("Item is not found in the Inventory.\n");
}//end inspectItem method
public void equipItem(String itemName)
{
// Check if the inventory exists and is not empty
if (playerInventory == null || playerInventory.isEmpty())
{
System.out.println("Your inventory is empty.\n");
return;
}
// Search for the item inside the player's inventory
Item itemToEquip = null;
for (Item item : playerInventory)
{
if (item.getItemName().equalsIgnoreCase(itemName))
{
itemToEquip = item;
break;
}
}
// If the item does not exist in the inventory
if (itemToEquip == null)
{
System.out.println("That item is not in your inventory.\n");
return;
}
// If the item exists but cannot be equipped
if (!itemToEquip.isEquipable())
{
System.out.println("You cannot equip this item.\n");
return;
}
// If the player already has an item equipped, unequip it first (optional)
// if (equippedItem != null)
// {
// System.out.println("You unequipped: " + equippedItem.getItemName());
// }
// Equip the new item
equippedItem = itemToEquip;
System.out.println("You equipped: " + equippedItem.getItemName());
System.out.println();
}
public void unequipItem(String itemName)
{
// Check if the player has an equipped item
if (equippedItem == null)
{
System.out.println("You have no item equipped.\n");
return;
}
// Unequip the currently equipped item
System.out.println("You unequipped: " + equippedItem.getItemName());
System.out.println();
equippedItem = null;
}
public void increaseDetectionLvl(double amount)
{
// Increase the detection level by the given amount
detectionLvl += amount;
// Optional: show the new detection level
System.out.println("Detection level increased to: " + detectionLvl);
}
//monster loses hp
public void attack(Character target)
{
if (target == null)
{
System.out.println("There is nothing to attack.");
return;
}
if (!target.canBeAttacked()) //to prevent npc getting attacked
{
System.out.println("You cannot attack " + target.getName() + ". They are not hostile.");
return;
}
if (!target.isAlive())
{
System.out.println(target.getName() + " is already defeated.");
return;
}
int dmg = getPlayerDamage();
int newHP = Math.max(0, target.getHealth() - dmg);
target.setHealth(newHP);
System.out.println("You attacked " + target.getName() + " for " + dmg + " damage.");
if (newHP <= 0)
{
System.out.println(target.getMonsterDies());
}
}
//player loses hp
public void takeDamage(int amount)
{
// delegate to the detailed version with no attacker
takeDamage(amount, null);
}
// detailed version – can also show monster stats
public void takeDamage(int amount, Character attacker)
{
playerHP = Math.max(0, playerHP - amount);
System.out.println("You took " + amount + " damage!");
System.out.println();
// show player status (like checkStatus)
System.out.println("~Player stats~");
System.out.println("HP: " + playerHP + "/" + playerMaxHP);
System.out.println("Attack: " + playerDamage);
System.out.println("Detection: " + detectionLvl);
System.out.println("Current Room: " + currentRoomID);
System.out.println();
// if we know who hit us, show their stats too
if (attacker != null)
{
System.out.println("~Monster stats~");
System.out.println("Monster: " + attacker.getName());
System.out.println("HP: " + attacker.getHealth());
System.out.println("ATK: " + attacker.getDamage());
System.out.println();
}
}
public void flee() // need flee <N|E|S|W>
{
if (previousRoomID == null)
{
System.out.println("There is nowhere you can flee.\n");
return;
}
System.out.println("You flee back to room " + previousRoomID + ".");
System.out.println();
currentRoomID = previousRoomID;
}
public void checkStatus()
{
System.out.println("~Player stats~");
System.out.println("HP: " + playerHP);
System.out.println("Attack: " + playerDamage);
System.out.println("Detection: " + detectionLvl);
System.out.println("Current Room: " + currentRoomID);
System.out.println();
}
public void saveGame(String fileName, Map <String, Room> gameMap)
{
try(PrintWriter writer = new PrintWriter(new FileWriter(fileName)))
{
writer.println("Player:");
writer.println(currentRoomID);
writer.println(detectionLvl);
writer.println(playerHP);
writer.println(playerDamage);
writer.println("Inventory:");
//items in inventory
for(Item item : playerInventory)
{
writer.println(item.getItemID());
}
writer.println("End Inventory");
writer.println("Equipped Items:");
for(Item item : equippedItems)
{
writer.println(item.getItemID());
}
writer.println("End Equipped");
writer.println("CheckpointRooms:");
for (String roomID : visitedCheckpointRooms)
{
writer.println(roomID);
}
writer.println("End CheckpointRooms");
writer.println("Rooms:");
for(Room room : gameMap.values())
{
StringBuilder sb = new StringBuilder();
sb.append(room.getRoomID()).append(",");
sb.append(room.isVisited()).append(",");
for(Character character : room.getCharacterList())
{
sb.append(character.getiD()).append("|");
}
sb.append(",");
for (Item item : room.getItems())
{
sb.append(item.getItemID()).append("|");
}
writer.println(sb.toString());
}
writer.println("End Room");
System.out.println("Game saved. File saved under name: " + fileName);
System.out.println();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void loadGame(String fileName, ArrayList<Character> characters,
ArrayList<Item> items, Map <String, Room> gameMap)
{
try(BufferedReader reader = new BufferedReader(new FileReader(fileName)))
{
String line;
reader.readLine();
currentRoomID = reader.readLine();
detectionLvl = Double.parseDouble(reader.readLine());
playerHP = Integer.parseInt(reader.readLine());
playerDamage = Integer.parseInt(reader.readLine());
reader.readLine();
playerInventory.clear();
while (!(line = reader.readLine()).equals("End Inventory"))
{
for (Item item : items)
{
if(item.getItemID().equals(line))
{
playerInventory.add(item);
break;
}
}
}
reader.readLine();
equippedItems.clear();
while (!(line = reader.readLine()).equals("End Equipped"))
{
for (Item item : items)
{
if(item.getItemID().equals(line))
{
equippedItems.add(item);
break;
}
}
}
reader.readLine();
visitedCheckpointRooms.clear();
while (!(line = reader.readLine()).equals("End CheckpointRooms"))
{
visitedCheckpointRooms.add(line.trim());
}
reader.readLine();
while (!(line = reader.readLine()).equals("End Room"))
{
String[] parts = line.split(",", -1);
String roomID = parts[0];
boolean visited = Boolean.parseBoolean(parts[1]);
Room room = gameMap.get(roomID);
if (room == null) continue;
room.setVisited(visited);
room.getCharacterList().clear();
if (!parts[2].isEmpty())
{
String[] charIDs = parts[2].split("\\|");
for (String iD : charIDs)
{
for (Character character : characters)
{
if (character.getiD().equals(iD))
{
room.setCharacter(character);
break;
}
}
}
}
room.getItems().clear();
if (!parts[3].isEmpty())
{
String[] itemIDs = parts[3].split("\\|");
for (String iD : itemIDs)
{
for (Item item : items)
{
if (item.getItemID().equals(iD))
{
room.addItem(item);
break;
}
}
}
}
}
System.out.println("~Loading Game...~");
System.out.println("Game has loaded!");
System.out.println();
currentRoom = gameMap.get(currentRoomID);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void readMap()
{
String floorID = currentRoom.getRoomFloorID();
String map = switch(floorID) {
case "F1" -> "F1.jpg";
case "F2" -> "F2.jpg";
case "F3" -> "F3.jpg";
case "F4" -> "F4.jpg";
default -> null;
};
if (map == null) {
System.out.println("The file name does not exist. "
+ "Try a different file name.\n");
return;
}
// Load image
ImageIcon mapIcon = new ImageIcon(map);
// Create a frame to display the map
JFrame frame = new JFrame("Game Map");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // only close this window
frame.setSize(800, 600); // adjust size as needed
// Add the image to a label
JLabel label = new JLabel(mapIcon);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
frame.add(label);
frame.setLocationRelativeTo(null); // center on screen
frame.setVisible(true);
}
public void startPuzzle()
{
// TODO Auto-generated method stub
// This can be used to manually trigger a puzzle if needed
}
public void move(String direction, Map<String, Room> gameMap) {
direction = direction.toLowerCase();
if (inPuzzleMode) {
System.out.println("You are currently solving a puzzle and cannot leave this room.\n");
return;
}
// Check if the current room has an exit in that direction
if (!currentRoom.getExits().containsKey(direction)) {
System.out.println("You can't go that way.\n");
return;
}
String nextRoomID = currentRoom.getExits().get(direction);
if (nextRoomID == null || nextRoomID.equals("-1")) {
System.out.println("You can't go that way.\n");
return;
}
Room nextRoom = gameMap.get(nextRoomID);
if (nextRoom == null) {
System.out.println("That room does not exist.\n");
return;
}
if (nextRoom.isLocked())
{
if (!nextRoom.unlockRoom(this))
{
System.out.println("The door is locked.");
return;
}
}
// Move player
currentRoom = nextRoom;
currentRoomID = nextRoomID;
// Mark the room as visited
if (!currentRoom.isVisited()) {
currentRoom.setVisited(true);
}
if (currentRoom.isCheckpoint()) {
if (!visitedCheckpointRooms.contains(currentRoomID)) {
playerHP = playerMaxHP;
visitedCheckpointRooms.add(currentRoomID);
System.out.println("Checkpoint reached! HP refilled to max.");
}
saveCheckpoint(gameMap);
}
// Display room info
System.out.println(currentRoom.getRoomName());
System.out.println("Exits: " + currentRoom.getExits().keySet());
System.out.println();
// Automatically start puzzle if the room has one
// NOTE: adjust method names if your Room/Puzzle classes are different
if (currentRoom.hasPuzzle()) {
currentRoom.getPuzzle().startPuzzle(this);
}
}
private void saveCheckpoint(Map<String, Room> gameMap)
{
try
{
String fileName = "checkpoint.txt"; //always overwrite
saveGame(fileName, gameMap);
System.out.println("Checkpoint saved at " + currentRoom.getRoomName());
System.out.println();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public boolean hasItem(String requiredKeyID) {
for (Item item : playerInventory) {
if (item.getItemID().equals(requiredKeyID)) return true;
}
return false;
}
public ArrayList<Item> getInventory()
{
return playerInventory;
}
}