-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.java
More file actions
136 lines (98 loc) · 3.27 KB
/
Inventory.java
File metadata and controls
136 lines (98 loc) · 3.27 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
package roguelike.Inventory;
import com.sun.glass.ui.Screen;
import roguelike.entities.Creature;
import roguelike.entities.Player;
import roguelike.world.World;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
public class Inventory {
String letters;
Creature creature;
World world;
private Item[] items;
public Item[] getItems() { return items; }
public Item get(int i) { return items[i]; }
public Inventory(int max){
items = new Item[max];
}
public void add(Item item){
for (int i = 0; i < items.length; i++){
if (items[i] == null){
items[i] = item;
break;
}
}
}
public void remove(Item item){
for (int i = 0; i < items.length; i++){
if (items[i] == item){
items[i] = null;
return;
}
}
}
public boolean isFull(){
int size = 0;
for (int i = 0; i < items.length; i++){
if (items[i] != null)
size++;
}
return size == items.length;
}
private Inventory inventory = new Inventory(20);;
public Inventory inventory() { return inventory; }
public void pickup(){
Item item = world.item(int x, int y, int z);
if (inventory.isFull() || item == null){
doAction("grab at the ground");
} else {
doAction("pickup a %s", item.name());
world.remove(x, y, z);
inventory.add(item);
}
}
public void drop(Item item){
doAction("drop a " + item.name());
inventory.remove(item);
world.addAtEmptySpace(item, x, y, z);
}
public void addAtEmptySpace(Item item, int x, int y, int z){
if (item == null)
return;
List<Point> points = new ArrayList<Point>();
List<Point> checked = new ArrayList<Point>();
points.add(new Point(x, y, z));
while (!points.isEmpty()){
Point p = points.remove(0);
checked.add(p);
if (!tile(p.x, p.y, p.z).isGround())
continue;
if (items[p.x][p.y][p.z] == null){
items[p.x][p.y][p.z] = item;
Creature c = this.creature(p.x, p.y, p.z);
if (c != null)
c.notify("A %s lands between your feet.", item.name());
return;
} else {
List<Point> neighbors = p.neighbors8();
neighbors.removeAll(checked);
points.addAll(neighbors);
}
}
}
private ArrayList<String> getList() {
ArrayList<String> lines = new ArrayList<String>();
Item[] inventory = player.inventory().getItems();
for (int i = 0; i < inventory.length; i++){
Item item = inventory[i];
if (item == null || !isAcceptable(item))
continue;
String line = letters.charAt(i) + " - " + item.glyph() + " " + item.name();
lines.add(line);
}
return lines;
}
protected boolean isAcceptable(Item item) {
return true;
}
}