-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation.java
More file actions
191 lines (152 loc) · 3.99 KB
/
Location.java
File metadata and controls
191 lines (152 loc) · 3.99 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
package myclasses;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
/**
* A location in the virtual world. The location contains views in different directions.
* The location may also contain portable items.
*
* @author mukeswe brian
* @contact b.mukeswe@sms.ed.ac.uk
*
*/
public class Location {
private String locationName;
// A record of possible views at the location
private HashMap<String, View> locationViews = new HashMap<>();
// A record of the items contained at the location
private List<Item> contents = new ArrayList<>();
// A record of possible directions at that location
private List<String> directions = new ArrayList<>();
/**
* Constructor for the Location class
*
* @param locationName, the name of the location
*/
public Location(String locationName) {
this.locationName = locationName;
init();
}
/**
* Initialise the location
*/
private void init() {
setDirections(); // specify the possible directions at the location
setViews(); // Specify the views corresponding to each direction
setItems(); // Specify the items contained in the location
}
/**
* Specify the views corresponding to each direction
*/
private void setViews() {
for (String direction : directions) {
locationViews.put(direction, new View(locationName, direction));
}
}
/**
* Specify the items contained in the location
*/
private void setItems() {
// Retrieve item names from the configuration class
String filenames = WorldConfig.getItemImages(locationName);
if (filenames.equals("none") == false) {
for (String filename: filenames.split(",")) {
contents.add(new Item(filename.trim()));
}
}
}
/**
* specify the possible directions at the location
*/
private void setDirections() {
directions.add("north");
directions.add("south");
directions.add("east");
directions.add("west");
}
/**
* Retrieve the name of the location
*
* @return a string specifying the name of the location
*/
public String getLocationName() {
return locationName;
}
/**
* Retrieve the view that corresponds to what one sees
* when facing in a specific direction at the location
*
* @param direction of interest
*
* @return a View object
*/
public View getView(String direction) {
if (locationViews.isEmpty() == false) {
return locationViews.get(direction);
}
else {
return null;
}
}
/**
* Retrieve all the portable items that are currently at the location
*
* @return an iterator of items
*/
public Iterator<Item> getContents(){
if (contents.isEmpty() == false) {
return contents.iterator();
}
else {
return null;
}
}
/**
* Retrieve a single item from the location
*
* @param itemName, the name of the item to be retrieved
*
* @return an ITem object
*/
public Item getItem(String itemName) {
Item targetItem = null;
if (contents.isEmpty() == false) {
// Identify the requested item
for (Item item : contents) {
if (item.getFileName().equals(itemName)) {
targetItem = item;
}
}
}
return targetItem;
}
/**
* Remove a portable item from the location
*
* @param itemName, the name of the item to be removed
*/
public void removeItem(String itemName) {
Item toRemove = null;
if (contents.isEmpty() == false) {
// Identify the item to be removed
for (Item item : contents) {
if (item.getFileName().equals(itemName)) {
toRemove = item;
}
}
// Remove the item
if (toRemove != null) {
contents.remove(toRemove);
}
}
}
/**
* Add a new portable item to the location
*
* @param item, the item to be added to the location
*/
public void addItem(Item item) {
contents.add(item);
}
}