-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHero.java
More file actions
345 lines (293 loc) · 9.35 KB
/
Copy pathHero.java
File metadata and controls
345 lines (293 loc) · 9.35 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import java.util.ArrayList;
import java.util.Scanner;
/**
* Extends from Role
* Adding opponent, mana, strength, dexterity, agility
* Hero can also hold weapons, armors, potions, spells
* Hero has coins and need exp to level up
*/
public class Hero extends Role {
// attributes
protected int opponent;
protected int mana;
protected int strength;
protected int dexterity;
protected int agility;
protected int exp;
protected int coins;
// inventories
protected Inventory inventory;
// equipments
protected Weapon equippedWeapon; // in single hand or double hand, can be extended in the feature
protected Armor equippedArmor;
public Hero() {
super();
mana = 0;
strength = 0;
dexterity = 0;
agility = 0;
exp = 0;
coins = 0;
inventory = new Inventory();
}
public Hero(String name) {
super(name);
inventory = new Inventory();
}
public Hero(String name, int level, int hp) {
super(name, level, hp);
inventory = new Inventory();
}
public Hero(String name, int level, int hp, int mana, int strength, int dexterity, int agility, int exp, int coins) {
super(name, level, hp);
this.mana = mana;
this.strength = strength;
this.dexterity = dexterity;
this.agility = agility;
this.exp = exp;
this.coins = coins;
inventory = new Inventory();
}
public void equipWeapon(int idx) {
equippedWeapon = inventory.getWeapon(idx);
System.out.println("Hero " + name +" is equipped with weapon: " + inventory.getWeapon(idx).getName());
}
public void equipArmor(int idx) {
equippedArmor = inventory.getArmor(idx);
System.out.println("Hero " + name +" is equipped with armor: " + inventory.getArmor(idx).getName());
}
public void usePotion(int idx) {
Potion potion = inventory.getPotion(idx);
System.out.println("Hero " + name +" has used potion: " + potion.getName());
// inventory.getPotion(idx).usePotion(this);
int amount = potion.getIncreaseAmount();
String[] attrs = potion.getEffectedAttribute().split("/");
for (String attr : attrs) {
switch (attr) {
case "Health":
hp += amount;
break;
case "Mana":
mana += amount;
break;
case "Strength":
strength += amount;
break;
case "Dexterity":
dexterity += amount;
break;
case "Agility":
agility += amount;
break;
}
}
inventory.potions.remove(idx);
}
public void useSpell(int idx) {
// todo
if (canUseSpell(idx)) {
this.mana = this.mana - inventory.getSpell(idx).getManaCost();
} else {
System.out.println("Can't use the spell, hero doesn't have enough mana!");
}
}
public boolean canUseSpell(int idx) {
if (mana >= inventory.getSpell(idx).getManaCost()) {
return true;
}
return false;
}
public void showInventory() {
System.out.println("Hero " + name + "'s inventory is: ");
inventory.showInventory();
}
@Override
public String toString() {
String status = isDead()? "Dead" : "Alive";
return "Hero{" +
" Name: '" + name +
" Status: " + status +
" HP: " + hp +
" Level: " + level +
" Opponent: '" + opponent +
" Mana: " + mana +
" Strength: " + strength +
" Dexterity: " + dexterity +
" Agility: " + agility +
" Exp: " + exp +
" Coins: " + coins +
" Inventory: " + inventory +
" EquippedWeapon: " + equippedWeapon +
" EquippedArmor: " + equippedArmor +
" }";
}
public void setAttributes(String fileSetting) {
level = 1;
hp = level * 100;
String[] attributes = fileSetting.split("\\s+");
name = attributes[0];
mana = Integer.parseInt(attributes[1]);
strength = Integer.parseInt(attributes[2]);
agility = Integer.parseInt(attributes[3]);
dexterity = Integer.parseInt(attributes[4]);
coins = Integer.parseInt(attributes[5]);
exp = Integer.parseInt(attributes[6]);
inventory = new Inventory();
equippedWeapon = new Weapon();
equippedArmor = new Armor();
}
public boolean isLevelUp() {
if (exp >= level * 10) {
return true;
}
return false;
}
public void levelUp() {
level++;
// update all related attributes
hp = level * 100;
mana = (int) (mana *= 1.1);
}
public void changeWeapon(int idx) {
inventory.showInventory();
System.out.println("Please input the index of the weapon you want to equip:");
Scanner sc = new Scanner(System.in);
ArrayList<Weapon> weapons = inventory.getWeapons();
String weaponIndex;
while (true) {
weaponIndex = sc.nextLine();
int index = Integer.parseInt(weaponIndex);
if (weaponIndex.matches("\\d+")) {
if (index > 0 && index < weapons.size()) {
equippedWeapon = weapons.get(index - 1);
System.out.println("Hero " + name + " changes weapon to: " + equippedWeapon.getName());
break;
} else {
System.out.println("Please choose a weapon!");
}
} else {
System.out.println("Please choose a weapon!");
}
}
}
public void changeArmor(int idx) {
inventory.showInventory();
System.out.println("Please input the index of the armor you want to equip:");
Scanner sc = new Scanner(System.in);
ArrayList<Armor> armors = inventory.getArmors();
String armorIndex;
while (true) {
armorIndex = sc.nextLine();
int index = Integer.parseInt(armorIndex);
if (armorIndex.matches("\\d+")) {
if (index > 0 && index < armors.size()) {
equippedArmor = armors.get(index - 1);
System.out.println("Hero " + name + " changes armor to: " + equippedArmor.getName());
break;
} else {
System.out.println("Please choose an armor!");
}
} else {
System.out.println("Please choose an armor!");
}
}
}
// revive with half hp
public void revive(){
hp = level * 50;
}
// get reward
public void getReward(int coins, int exp) {
this.coins += coins;
this.exp += exp;
if (this.exp > this.level * 10) {
// should we keep the exp? or set it to 0
this.levelUp();
}
}
public int getWeaponDamage() {
double damage = (strength + equippedWeapon.getDamage()) * 0.05;
return (int) damage;
}
public int getSpellDamage(Spell spell) {
double damage = (spell.getDamage() * (dexterity / 10000 + 1));
return (int) damage;
}
// does hero have attributes of dodge and defense?
public int getDefense() {
return (int) (equippedArmor.getDamageReduce() * 0.1);
}
public void takeDamage(int damage) {
hp -= (damage - getDefense());
}
public int getDodge() {
return (int) (agility * 0.002);
}
public Hero createHero(String attributes) {
Hero hero = new Hero();
hero.setAttributes(attributes);
return hero;
}
// getter
public int getOpponent() {
return opponent;
}
public int getMana() {
return mana;
}
public int getStrength() {
return strength;
}
public int getDexterity() {
return dexterity;
}
public int getAgility() {
return agility;
}
public int getExp() {
return exp;
}
public int getCoins() {
return coins;
}
public Inventory getInventory() {
return inventory;
}
public Weapon getEquippedWeapon() {
return equippedWeapon;
}
public Armor getEquippedArmor() {
return equippedArmor;
}
// setter
public void setOpponent(int opponent) {
this.opponent = opponent;
}
public void setMana(int mana) {
this.mana = mana;
}
public void setStrength(int strength) {
this.strength = strength;
}
public void setDexterity(int dexterity) {
this.dexterity = dexterity;
}
public void setAgility(int agility) {
this.agility = agility;
}
public void setExp(int exp) {
this.exp = exp;
}
public void setCoins(int coins) {
this.coins = coins;
}
public void setInventory(Inventory inventory) {
this.inventory = inventory;
}
public void setEquippedWeapon(Weapon equippedWeapon) {
this.equippedWeapon = equippedWeapon;
}
public void setEquippedArmor(Armor equippedArmor) {
this.equippedArmor = equippedArmor;
}
}