-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
110 lines (96 loc) · 2.77 KB
/
Player.java
File metadata and controls
110 lines (96 loc) · 2.77 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
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Scanner;
/**
* A class to represent the player.
*
* @author Thomas Vakili
* @version 2013.12.14
*/
public class Player extends Character {
private int maxWeight;
private Inventory inventory;
public Player(Room room) {
super("Placeholder", room); // A name should be passed
inventory = new Inventory();
maxWeight = 100; // This should maybe be passed as a parameter?
}
/**
* Picks up an item.
*
* @param item item to pick up
* @return true if player can carry more, otherwise false
*/
public boolean pickUp(Item item) {
if (inventory.getWeight() + item.getWeight() <= maxWeight) {
return inventory.addItem(item);
} else {
return false;
}
}
/**
* Drop an item in the current room.
*
* @param item item to drop.
*/
public void dropItem(Item item) {
inventory.removeItem(item);
getRoom().addItem(item);
}
/**
* Look around.
*
* @return a String describing where you are, what exits are available
* and that let's any NPCs state their mind.
*/
public String look() {
return getRoom().getLongDescription();
}
public boolean teleport() {
boolean success = false;
if (getRoom() instanceof Teleporter) {
Teleporter teleporter = (Teleporter) getRoom();
setRoom(teleporter.getDestination());
success = true;
}
return success;
}
/**
* Prompts the player for an action in a fight.
*
* @param enemy the enemy the player faces
* @return the action chosen by the player
*/
@Override
public Action getAction(Character enemy) {
Action.ActionVal action = null;
Scanner input = new Scanner(System.in);
// Print available commands
System.out.print("Available commands:");
for (Action.ActionVal command : Action.ActionVal.values()) {
System.out.print(" " + command);
}
while (action == null) {
System.out.print("\nEnter action: ");
// Get first word entered
try {
String command = input.nextLine().split(" ")[0];
action = Action.ActionVal.valueOf(command.toUpperCase());
} catch(Exception e) {
System.out.println("Invalid action!");
}
}
return new Action(action, this, enemy);
}
/**
* Kills the player, ending so that the game can end.
*/
@Override
public void die() {
super.die();
System.out.println("You are dead.");
}
public Inventory getInventory() {
return inventory;
}
}