-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNPC.java
202 lines (176 loc) · 5.09 KB
/
NPC.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
package bork;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
/**
* Constructs new NPC's and holds the means to change their state.
*
* @author Christopher Ringham
*/
public class NPC
{
/**
* A subclass of Exception that handles exceptions that result when looking
* for an NPC in a bork file when there is none.
* @author Brian Burns
*/
static class NoNpcException extends Exception {}
static String INVENTORY_STARTER = "Inventory: ";
private String name;
private String desc;
private int health;
private int score;
private Room currentRoom;
private ArrayList<Item> inventory;
/**
* Constructs an NPC with a name, description, health, score, and current
* room from the file attached to the given Scanner.
*
* @param s the Scanner reading the hydration file
* @throws bork.NPC.NoNPCException
* @throws bork.Dungeon.IllegalDungeonFormatException
*/
public NPC(Scanner s, Dungeon d
) throws NoNpcException, Dungeon.IllegalDungeonFormatException
{
inventory = new ArrayList<Item>();
health = 100;
desc = "";
name = s.nextLine();
if (name.equals(Dungeon.TOP_LEVEL_DELIM)) {
throw new NoNpcException();
}
score = Integer.valueOf(s.nextLine());
currentRoom = d.getRoom(s.nextLine());
String lineOfDesc = s.nextLine();
while (!lineOfDesc.equals(Dungeon.SECOND_LEVEL_DELIM) &&
!lineOfDesc.equals(Dungeon.TOP_LEVEL_DELIM)) {
if (lineOfDesc.startsWith(INVENTORY_STARTER)) {
String itemsList = lineOfDesc.substring(INVENTORY_STARTER.length());
String[] itemNames = itemsList.split(",");
for (String itemName : itemNames) {
try {
add(d.getItem(itemName));
}
catch (Item.NoItemException e) {
throw new Dungeon.IllegalDungeonFormatException(
"No such item '" + itemName + "'");
}
}
}
else {
desc += lineOfDesc + "\n";
}
lineOfDesc = s.nextLine();
}
currentRoom.addNPC(this);
d.add(this);
}
/**
* Gets the name of this NPC.
*
* @return the name of this NPC
*/
public String getName()
{
return this.name;
}
/**
* Gets the description of this NPC.
*
* @return the description of this NPC
*/
public String describe()
{
return this.desc;
}
/**
* Gets the current health of this NPC.
*
* @return the current health of this NPC
*/
public int getHealth()
{
return this.health;
}
/**
* Gets the current score of this NPC.
*
* @return the current score of this NPC
*/
public int getScore()
{
return this.score;
}
public Weapon getWeapon() throws Weapon.NoWeaponException
{
for(Item item : this.inventory)
{
if(item instanceof Weapon)
{
return (Weapon)item;
}
}
throw new Weapon.NoWeaponException();
}
/**
* Gets the Room this NPC is currently in.
*
* @return the Room this NPC is currently in
*/
public Room getCurrentRoom()
{
return this.currentRoom;
}
public void setHealth(int health)
{
this.health = health;
}
/**
* Takes hit points away from this NPC's health.
*
* @param damage the number of hit points to subtract from this NPC's health
*/
public void wound(int damage)
{
this.health -= damage;
//if(this.health <= 0) {
// die();
//}
}
/**
* Kills a NPC and removes them from the dungeon
*/
void die(){
/// for(Item item : inventory){
/// remove(item);
/// this.currentRoom.add(item);
/// }
Iterator<Item> iter = inventory.iterator();
while (iter.hasNext()) {
Item item = iter.next();
this.currentRoom.add(item);
iter.remove();
}
this.currentRoom.removeNPC(this);
GameState.instance().getDungeon().removeNPC(this.name);
}
/**
* Adds the given item to this NPC's inventory.
*
* @param item the item to add to this NPC's inventory
*/
public void add(Item item)
{
inventory.add(item);
}
/**
* Removes the given item from this NPC's inventory.
*
* @param item the item to remove from this NPC's inventory
*/
public void remove(Item item)
{
inventory.remove(item);
}
}