forked from brandr/Roguelike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonster.java
More file actions
229 lines (173 loc) · 5.07 KB
/
Monster.java
File metadata and controls
229 lines (173 loc) · 5.07 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
public class Monster {
public final int MAXINVENTORYSIZE=30; //maximum inventory size
public final int INVENTORYSLOTS=6; //number of places to wear equipment
public Monster(){
name = null;
hitPoints[0]=0;
hitPoints[1]=0;
baseDamage = 0;
}
public Monster(String name){
this.name = name;
hitPoints[0]=0;
hitPoints[1]=0;
baseDamage = 0;
}
//toString methods
public String showHitPoints(){
return hitPoints[0]+"/"+hitPoints[1];
}
public String showEquipment(){
boolean noEquipment=true;
String retVal="Equipped items:"+"\n";
for(int i=0;i<INVENTORYSLOTS;i++){
if(equippedItems[i]!=null){ //if an item is equipped in the slot
noEquipment=false;
String slotName="";
switch(i){ //maybe this switch statement should have its own method
case(0):
slotName="Head";
break;
case(1):
slotName="Chest";
break;
case(2):
slotName="Left Hand";
break;
case(3):
slotName="Right Hand";
break;
case(4):
slotName="Pants";
break;
case(5):
slotName="Boots";
break;
default:
break;
}
retVal+=(slotName+": "+equippedItems[i].toString()+"\n");
}
}
if(noEquipment){
retVal+="none";
}
return retVal;
}
//movement methods
//public void Move(direction)
//damage-related functions
public int determineDamage(){ //will be more complicated later
int retVal = baseDamage;
if(equippedItems[3]!=null){
retVal+=equippedItems[3].getPower(1);
}
return retVal;
}
public void attack(Monster target){ //attacks a target
int damage = determineDamage();
target.takeDamage(damage);
}
public void takeDamage(int damage){ //takes damage
for(int i = 0;i<INVENTORYSLOTS;i++){
if(equippedItems[i]!=null){
damage-=equippedItems[i].getPower(0);
}
}
if(damage<0){ //if there is more than enough armor to mitigate the damage, no damage is dealt.
damage = 0;
}
hitPoints[0]-=damage;
if(hitPoints[0]<=0){
die();
}
}
public void restoreHealth(int health){ //restores health
hitPoints[0]+=health;
if(hitPoints[0]>maxHitPoints()){
fillHitPoints();
}
}
public void die(){ //develop this more (drop items, can no longer perform actions, object gets deleted, more specific message appears, etc.)
System.out.println(name+" died.");
}
//hit point getters and setters (Note: currently allows negative numbers. Change this if it causes problems.
public int[] getHitPoints() {
return hitPoints;
}
public int maxHitPoints() {
return hitPoints[1];
}
public int currentHp(){
return hitPoints[0];
}
public void setHitPoints(int hitPoints) { //only meant for initially creating a monster with full health.
this.hitPoints[0] = hitPoints;
this.hitPoints[1] = hitPoints;
}
public void fillHitPoints(){ //restore to full HP
hitPoints[0]=hitPoints[1];
}
public void setMaxHitPoints(int newMax){
hitPoints[1]=newMax;
}
//damage getters and setters
public int getBaseDamage() {
return baseDamage;
}
public void setBaseDamage(int baseDamage) {
this.baseDamage = baseDamage;
}
//Item related functions
//TODO: obtainItem(try to add an item to inventory, but only succeed if there is room.)
public void useItem(int index){
if(inventory.getItem(index)!=null){
inventory.getItem(index).use(this);
if(inventory.getItem(index).consumable==true){
inventory.removeItem(index);
}
}
}
public void equipItem(Equipment item){ //TODO: make it so program can stop player from equipping non-equipment items.
int index = item.wornIndex;
if(item.equipped==false){ //case for equipping an item
item.equip();
if(equippedItems[index]==null){ //checks to see if an item is already worn there
equippedItems[index]=item;
}
else{ //if an item is already worn there, do this
unequipItem(item.wornIndex); //this step unequips the current item and equips the chosen item in its place.
if(item.wornIndex==3 && item.twoHanded){ //if the item is a two-handed weapon, unequip the left-hand item.
unequipItem(2);
}
else if(item.wornIndex==2 && equippedItems[3].twoHanded){ //if equipping a left-hand item while holding a two-handed weapon, unequip the weapon.
unequipItem(2);
equippedItems[2]=item;
unequipItem(3);
}
equippedItems[index]=item;
}
}
else{ //case for unequipping an item
unequipItem(item.wornIndex);
}
}
public void unequipItem(int index){ //unequip an item
if(equippedItems[index]!=null){
equippedItems[index].equip();
equippedItems[index]=null;
}
}
//name getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected String name;
protected int[] hitPoints=new int[2]; //two-int array, with first as current and second and maximum
protected int baseDamage;
protected Inventory inventory = new Inventory();
protected Equipment[] equippedItems = new Equipment[INVENTORYSLOTS];
}