-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.java
More file actions
303 lines (261 loc) · 8.53 KB
/
Copy pathCharacter.java
File metadata and controls
303 lines (261 loc) · 8.53 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
import java.util.*;
import java.io.*;
//this class creates a character in RokaScape, or uploads an existing save file of a character
public class Character {
// character name
private String charName;
private Area loc;
// stats
private int maxHp;
private int hp;
private int atk;
private int str;
private int def;
private int gp;
private int xp;
private int lvl;
// inventory
private Item weapon;
private Map<Item, Integer> inv;
// CHARACTER CONSTRUCTOR (NEW CHARACTER)
public Character() {
Scanner console = new Scanner(System.in);
this.lvl = 1;
this.xp = 0;
this.maxHp = 10;
this.hp = 10;
this.atk = 1;
this.str = 1;
this.def = 1;
this.weapon = null;
this.inv = new TreeMap<>();
}
// CHARACTER CONSTRUCTOR (OLD CHARACTER)
public Character(String fileName) throws FileNotFoundException {
File charFile = new File(fileName);
Scanner scan = new Scanner(charFile);
this.charName = scan.nextLine();
this.lvl = Integer.parseInt(scan.nextLine());
this.xp = Integer.parseInt(scan.nextLine());
this.maxHp = Integer.parseInt(scan.nextLine());
this.hp = maxHp;
this.atk = Integer.parseInt(scan.nextLine());
this.str = Integer.parseInt(scan.nextLine());
this.def = Integer.parseInt(scan.nextLine());
this.gp = Integer.parseInt(scan.nextLine());
this.loc = Area.getAreaByName(scan.nextLine());
this.weapon = Item.getItemByName(scan.nextLine());
this.inv = new TreeMap<>();
// this.cook = Integer.parseInt(scan.nextLine());
// this.wc = Integer.parseInt(scan.nextLine());
// this.fish = Integer.parseInt(scan.nextLine());
// this.mine = Integer.parseInt(scan.nextLine());
scan.nextLine();
while (scan.hasNextLine()) {
String itemName = scan.nextLine();
if (itemName.equals("END INVENTORY")) {
break;
}
Item item = Item.getItemByName(itemName);
int count = Integer.parseInt(scan.nextLine());
this.addToInv(item, count);
}
}
// SAVE METHOD FOR LOADING OLD CHARACTER
public void save(String fileName) throws FileNotFoundException {
PrintStream out = new PrintStream(new File(fileName));
out.println(charName);
out.println(lvl);
out.println(xp);
out.println(maxHp);
out.println(atk);
out.println(str);
out.println(def);
out.println(gp);
out.println(loc.toString());
out.println(weapon);
out.println("Inventory:");
// INVENTORY ITEMS
for (Item key : inv.keySet()) {
out.println(key);
out.println(inv.get(key));
}
out.println("END INVENTORY");
// out.println(cook);
// out.println(wc);
// out.println(fish);
// out.println(mine);
out.close();
}
// SETS CHARACTERS NAME
public void setName(String name) {
this.charName = name;
}
// END CREATION OF CHARACTER
// ADDS ITEM TO CHARACTER INVENTORY
public void addToInv(Item item, int count) {
if (this.inv.get(item) == null) {
this.inv.put(item, count);
} else {
this.inv.put(item, this.inv.get(item) + count);
}
}
// CHECKS INVENTORY FOR FOOD
// - true: inventory contains food
// - false: does not contain food
public boolean containsFood() {
for (Item item : inv.keySet()) {
if (item.isFood()) {
return true;
}
}
return false;
}
// CHECKS INVENTORY FOR WEAPONS
// - true: inventory contains weapons
// - false: does not contain weapons
public boolean containsWeapon() {
for (Item item : inv.keySet()) {
if (item.isWeapon()) {
return true;
}
}
return false;
}
// CHARACTER GAINS XP (BATTLE SEQUENCE)
public void gainXp(int amount) {
this.xp += amount;
System.out.println("- You have gained " + amount + " xp.");
if (this.xp > this.lvl * 100) {
System.out.println("You are now level " + this.lvl + ". You have healed to full"
+ " and your stats have slightly improved.");
this.lvl++;
this.xp = 0;
this.atk++;
this.def++;
this.str++;
this.maxHp += 5;
this.hp = this.maxHp;
}
}
// CHARACTER DAMAGED (BATTLE SEQUENCE)
public void heroHit(int dmg) {
// losing hp
if (dmg >= 0) {
if ((this.hp - dmg) > 0) {
this.hp = this.hp - dmg;
} else {
this.hp = 0;
}
} else {
if ((this.hp - dmg) > this.maxHp) {
this.hp = this.maxHp;
}
}
}
// CHARACTER DIES, LOSES HALF OF GOLD
public void characterDeath() {
this.gp = this.gp / 2;
this.hp = maxHp;
}
// KILLING MONSTER, GAINING THEIR HP IN GOLD
public void monsterKill(int hp) {
System.out.println("You have gained " + hp + " gp.");
this.gp += hp;
}
// EQUIPPING A WEAPON
public void equipItem(String newWeapon) {
if (this.weapon != null) {
System.out.println("You currently have " + this.weapon + " equipped.");
}
for (Item key : inv.keySet()) {
if (this.weapon != null) {
// break
if (key.equals(Item.getItemByName(newWeapon))) {
if (key.isWeapon() && !(key.toString().equals(this.weapon.toString()))) {
if (this.weapon != null) {
this.atk -= this.weapon.getAtk();
this.str -= this.weapon.getStr();
}
System.out.println(key + " equipped.");
this.weapon = key;
this.atk += this.weapon.getAtk();
this.str += this.weapon.getStr();
break;
} else if (key.isWeapon() && key.toString().equals(this.weapon.toString())) {
System.out.println("You already have that weapon equipped!");
} else {
System.out.println("That is not a weapon!");
break;
}
}
} else if (key.isWeapon() && key.equals(Item.getItemByName(newWeapon))) {
System.out.println(key + " equipped.");
this.weapon = key;
this.atk += this.weapon.getAtk();
this.str += this.weapon.getStr();
break;
}
}
}
public void unequipItem() {
this.atk -= this.weapon.getAtk();
this.str -= this.weapon.getStr();
this.weapon = null;
System.out.println("Weapon unequipped.");
}
// USING A PIECE OF FOOD/ITEM
public void lowerItemValue(Item key, int amount) {
int currentCount = inv.get(key);
inv.put(key, currentCount - amount);
if (inv.get(key) == 0) {
inv.remove(key);
}
}
public void gainGP(int amount) {
this.gp = this.gp + amount;
}
public void loseGP(int amount) {
this.gp = this.gp - amount;
}
public void setHp(int hp) {
this.hp = hp;
}
// GETTER METHODS
public String getName() {
return this.charName;
}
public Area getLoc() {
return this.loc;
}
public void setLoc(Area loc) {
this.loc = loc;
}
public int getHp() {
return this.hp;
}
public int getAtk() {
return this.atk;
}
public int getStr() {
return this.str;
}
public int getDef() {
return this.def;
}
public int getMaxHp() {
return this.maxHp;
}
public int getGP() {
return this.gp;
}
public Item getWep() {
return this.weapon;
}
public Map<Item, Integer> getInv() {
return this.inv;
}
public String toString() {
return this.charName;
}
}