-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.java
More file actions
129 lines (111 loc) · 3.03 KB
/
Inventory.java
File metadata and controls
129 lines (111 loc) · 3.03 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
import java.util.*;
/** Contains a collection of items currently stored on a Unit
* It also keeps track of which are held in a "backpack" and which are in use
* If the unit is dual-wielding (player, monsters don't do this), then only
* the bonuses and stats from the current weapon in use are counted
* The advantage from dual wielding stems from being able to switch which weapon
* one is using instantly
*/
public class Inventory {
public static int MAX_MASS = 25;
Vector<Item> items;
Vector<Item> backpack; //inactive items
Inventory() {
items = new Vector<Item>();
backpack = new Vector<Item>();
}
/** Returns whether there is space for an item */
public boolean spaceFor(Item i) {
return getMass() + i.getMass() <= MAX_MASS;
}
/** Attempts to add an item. Returns false if the item adding failed (too heavy) */
public boolean addItem(Item i) {
if(spaceFor(i)) {
//Figure out whether to add to items or backpack (add to items by default)
if(slotUse(i.getSlot()))
backpack.add(i);
else
items.add(i);
return true;
} else
return false;
}
/** Determines whether an item is already weilded in this slot */
public boolean slotUse(String slot) {
for(Item i : items) {
if(i.getSlot().equals(slot))
return true;
}
return false;
}
/** Gets the thing in the given slot */
public Item getSlot(String slot) {
for(Item i : items) {
if(i.getSlot().equals(slot))
return i;
}
return null;
}
/** Equips the given item (it must be in the backpack) */
public void equipItem(Item i) {
if(backpack.contains(i)) {
backpack.remove(i);
if(getSlot(i.getSlot()) != null) {
unequipItem(getSlot(i.getSlot()));
}
items.add(i);
}
}
/** Unequips the given item (it must be equipped [obviously]) */
public void unequipItem(Item i) {
if(items.contains(i)) {
items.remove(i);
backpack.add(i);
}
}
/** Gets the effect of the active weapon */
public Effect getEffect() {
if(slotUse("weapon"))
return getSlot("weapon").getEffect();
return null;
}
public int getMass() {
int val = 0;
for(Item i : items) val += i.getMass();
for(Item i : backpack) val += i.getMass();
return val;
}
public int getRange() {
int val = 0;
for(Item i : items) val += i.getRange();
return val;
}
public int getAttack() {
int val = 0;
for(Item i : items) val += i.getAttack();
return val;
}
public int getASpeed() {
int val = 0;
for(Item i : items) val += i.getASpeed();
return val;
}
public int getDamage() {
int val = 0;
for(Item i : items) val += i.getDamage();
return val;
}
public int getDefense() {
int val = 0;
for(Item i : items) val += i.getDefense();
return val;
}
public int getSpeed() {
int val = 0;
for(Item i : items) val += i.getSpeed();
return val;
}
public int getSpeedMassDebuff() {
return 30 * getMass();
}
}