-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
62 lines (57 loc) · 1.73 KB
/
Player.java
File metadata and controls
62 lines (57 loc) · 1.73 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
import java.util.ArrayList;
import java.util.List;
public class Player extends InstanceEntity {
private List<Item> inventory;
// Constructors
public Player(InstanceEntity instanceEntity) {
this(new ArrayList<>(), instanceEntity);
}
public Player(Entity entity) {
super(entity);
this.inventory = new ArrayList<>();
}
public Player(List<Item> inventory, InstanceEntity instanceEntity) {
super(instanceEntity.getCurrentHealth(), instanceEntity.getSelectedAttacks(), instanceEntity);
this.inventory = inventory;
}
/* Adds the specified item to the inventory.
* If the item already exists, adds to its count.
* Returns 0 if added, 1 if count changed. */
public int addItem(Item newItem) {
for (Item item : inventory)
if (item.getName().equals(newItem.getName())) {
item.changeCount(newItem.getCount());
// Debug print
// System.out.println("Count changed: " + item.getName() + " " + item.getCount());
return 1;
}
inventory.add(newItem);
// Debug print
//System.out.println("Item added: " + newItem.getName() + " " + newItem.getCount());
return 0;
}
/* Adds the specified item to the inventory.
* If the item already exists, increment its count.
* Returns 0 if added, 1 if count incremented. */
public int addItem(Usable usable) {
for (Item item : inventory)
if (item.getName().equals(usable.getName())) {
item.increment();
return 1;
}
inventory.add(new Item(usable));
return 0;
}
// Getters/Setters
public List<Item> getInventory() {
return inventory;
}
public void setInventory(List<Item> inventory) {
this.inventory = inventory;
}
// toString
@Override
public String toString() {
return "Player [inventory=" + inventory + "instance entity=" + super.toString() + "]";
}
}