-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
142 lines (123 loc) · 4.25 KB
/
Player.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
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Set;
import java.awt.Point;
import java.util.Iterator;
import java.util.Random;
/**
* Player class - will be responsible for handling
* actions of that like a real world player.
*
* @author Jamie Dixon
* @version v0.01
*/
public class Player extends Char
{
//private HashMap<String, Item> inventory; // collection of items
private Random generator = new Random();
/**
* Constructor for objects of class Player
*
* Sets the players hitpoints
* and prepares an inventory array
*/
public Player()
{
this.hp = 150; // players health points
this.maxHp = 150; // players health points
this.ap = 12;
//inventory = new HashMap<String, Item>(); // new empty hashmap for inventory
}
/**
* Move - player moves to a new location
*
* @since v0.02
* @params String direction
* @return true if the move was successful otherwise false
*/
public boolean move(String direction)
{
if(hp < 1){
return false;
}
// Try to leave current room.
Location nextLocation = getCurrentPosition().getExit(direction);
if (nextLocation == null) {
return false;
}else if (currentPosition.getLocationType() == LocationType.TRAP){
return false;
} else {
setCurrentPosition(nextLocation);
}
this.hp = hp - 1;
return true;
}
public boolean freeHostage(String hostageName) throws CharacterException
{
if(getCurrentPosition().hasNpc(hostageName)){
if(hasItem("key")){
if(getCurrentPosition().getNPC(hostageName).getCharType() == CharacterType.PRINCESS){
getCurrentPosition().removeNPC(hostageName);
removeItem("key");
}else{
throw new CharacterException("Unable to free "+hostageName);
}
}else{
throw new CharacterException("Find a key!");
}
}else{
throw new CharacterException("NPC Doesnt Exist!");
}
return true;
}
public boolean useItem(String itemName) throws InventoryException
{
if(!this.hasItem(itemName)){
throw new InventoryException("You do not have that item to use");
}else{
switch(itemName){
case "ether":
this.hp = this.hp + getItem(itemName).getHealthPoints();
if(this.hp > maxHp){
this.hp = maxHp;
}
break;
case "coin":
double chance = generator.nextDouble();
//System.out.println(chance);
if(!this.getCurrentPosition().isLocked()){
throw new InventoryException("No guards to bribe");
}
if(chance < 0.5){
throw new InventoryException("Guard would not be bribed");
}
this.getCurrentPosition().unlockExits();
break;
default:
throw new InventoryException("You cannot find a way to use this item here");
}
}
this.removeItem(itemName);
return true;
}
public boolean attack(String opponentName) throws CharacterException
{
if(this.hp < 1){
throw new CharacterException("No health remaining to attack");
}
int damagePoints = this.getCurrentPosition().getNPC(opponentName).getDamagePoints();
try{
this.getCurrentPosition().getNPC(opponentName).defend(this);
}catch(Exception e){
this.getCurrentPosition().removeNPC(opponentName);
throw new CharacterException(opponentName+" killed!");
//System.out.println(e);
}
this.hp = this.hp - damagePoints;
return true;
}
public boolean isLockedIn()
{
return this.getCurrentPosition().isLocked();
}
}