-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInventory.java
248 lines (216 loc) · 7.22 KB
/
Inventory.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
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import ansi_terminal.Terminal;
/** Creates an inventory object that stores the items of the player and also keeps track of which items are equipped
*
*/
public class Inventory {
// the actual list of items
private ArrayList<Item> items;
// which item is equipped, if any
private Item equippedArmor;
private Item equippedWeapon;
private Item equippedOther;
// the max weight limit for the player here
private int maxWeight;
public Inventory(int maxWeight) {
items = new ArrayList<>();
this.maxWeight = maxWeight;
}
// returns true on success, false when full
public boolean add(Item item) {
if ((item.getWeight() + totalWeight()) > maxWeight) {
return false;
} else {
items.add(item);
return true;
}
}
// this method not only adds the item, but equips it into the correct slot
// it is used for setting up the player's starting gear
public void addAndEquip(Item item) {
items.add(item);
if (item.getType() == ItemType.Weapon) {
equippedWeapon = item;
} else if (item.getType() == ItemType.Armor) {
equippedArmor = item;
} else if (item.getType() == ItemType.Other) {
equippedOther = item;
}
}
// get the equipped weapon and armor
public Item getEquippedWeapon() {
return equippedWeapon;
}
public Item getEquippedArmor() {
return equippedArmor;
}
public Item getEquippedOther(){
return equippedOther;
}
// returns the total weight of all items stored
public int totalWeight() {
int total = 0;
for (Item i : items) {
total += i.getWeight();
}
return total;
}
// print all of the items in the list, that match the given type (can be null)
// returns the number of items matching they type
private int print(ItemType filter) {
// clear the terminal so we print over all else
Terminal.clear();
// print a heading row
// the numbers and junk are to make it print in nice columns
System.out.printf("%-4s %-40s %-8s %-8s %-8s\n\r", "No.", "Name", "Weight", "Value", "Strength");
Terminal.reset();
// print each item out
int num = 0;
for (Item i : items) {
if (filter == null || i.getType() == filter) {
System.out.printf("%-4d %-40s %-8s %-8s %-8s", num + 1, i.getName(), i.getWeight(), i.getValue(), i.getStrength());
// tell them if this thing is equipped
if (i == equippedArmor) {
System.out.print(" (equipped armor)");
} else if (i == equippedWeapon) {
System.out.print(" (equipped weapon)");
} else if (i == equippedOther) {
System.out.print(" (equipped other)");
}
System.out.print("\n\r");
num++;
}
}
return num;
}
// stay here until the user is ready to go back
public void pressAnyKey() {
System.out.printf("\n\rPress any key to return...\n\r");
Terminal.getKey();
}
// print all of the items in the list
public void print() {
print(null);
pressAnyKey();
}
// drop an item from the inventory, return what was dropped
public Item drop() {
Item toDrop = pickItem(null);
if (toDrop != null) {
// if we're dropping our equipped stuff, remove those too!
if (equippedWeapon == toDrop) {
equippedWeapon = null;
} else if (equippedArmor == toDrop) {
equippedArmor = null;
}
items.remove(toDrop);
}
if (toDrop != null) {
System.out.print("You dropped the " + toDrop.getName() + "...\n\r");
} else {
System.out.print("You dropped nothing...\n\r");
}
pressAnyKey();
return toDrop;
}
// equip something
private Item equip(ItemType type) {
Item thing = pickItem(type);
if (thing != null) {
System.out.print("You equipped the " + thing.getName() + "\n\r");
} else {
System.out.print("You equipped nothing...\n\r");
}
pressAnyKey();
return thing;
}
// equip a weapon
public void equipWeapon() {
equippedWeapon = equip(ItemType.Weapon);
}
// equip a piece of armor
public void equipArmor() {
equippedArmor = equip(ItemType.Armor);
}
//equip a piece of other
public void equipOther(){
equippedOther = equip(ItemType.Other);
}
// a method which allows users to choose an item
// this is private - only called by drop and equip
private Item pickItem(ItemType filter) {
// print all the matching items
int options = print(filter);
if (options == 0) {
System.out.print("You have no appropriate items!\n\r");
return null;
}
// give them a cancel option as well
System.out.print((options + 1) + " None\n\r");
// get their choice, only allowing ints in the correct range
int choice;
do {
String entry = Terminal.getLine("Select an item: ");
try {
choice = Integer.parseInt(entry) - 1;
} catch (NumberFormatException e) {
choice = -1;
}
} while (choice < 0 || choice > options);
// go through and skip items until we reach this one
int realIndex = 0;
for (Item i : items) {
if (filter == null || i.getType() == filter) {
if (choice == 0) {
break;
}
choice--;
}
realIndex++;
}
// return the thing they chose
if (realIndex < 0 || realIndex >= items.size()) {
return null;
} else {
return items.get(realIndex);
}
}
public ArrayList<Item> getItems() {
return items;
}
/** Writes each item in the inventory, including the equipped weapon, armor, and other obtained items to the
* save file
*
* @param out the printwriter used to write data to a file
*/
public void save(PrintWriter out) {
equippedWeapon.save(out);
equippedArmor.save(out);
for (Item item : items) {
if (item != equippedWeapon && item != equippedArmor) {
item.save(out);
}
}
out.println(".");
}
/** A constructor used for reading in the inventory, including the equipped weapon, armor, and other obtained items
* from the save file
*
* @param in the scanner used to read in data from the file
*/
public Inventory(Scanner in) {
items = new ArrayList<>();
maxWeight = 80;
Item weapon = new Item(in);
addAndEquip(weapon);
Item armor = new Item(in);
addAndEquip(armor);
while (!in.hasNext(".")) {
Item item = new Item(in);
items.add(item);
}
in.nextLine();
}
}