-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvatar.java
More file actions
202 lines (165 loc) · 4.61 KB
/
Avatar.java
File metadata and controls
202 lines (165 loc) · 4.61 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
package myclasses;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import myclasses.Abilities;
/**
* An avatar that is able to move through locations in the virtual world and to carry
* portable items from one location to another using a cart. The avatar's abilities
* correspond to specific user commands.
*
* @author Brian Mukeswe
* @contact b.mukeswe@sms.ed.ac.uk
*
*/
public class Avatar {
// A variable to keep track of the avatar's location in the world
private Location currentLocation;
// A variable to keep track of the avatar's direction in the world
private String currentDirection;
// A variable to keep track of the items that have been collected
private List<Item> cart = new ArrayList<>();
// A variable to keep track of the locations that have been visited
private HashMap<String, Location> visitedLocations = new HashMap<>();
/**
* Constructor for the Avatar class
*
* @param startLocation, the location where the avatar enters the virtual world
*/
public Avatar(Location startLocation) {
// Put the avatar in the specified start location
currentLocation = startLocation;
init();
}
/**
* Initialise the avatar
*/
private void init() {
// Specify the direction where the newly initialised avatar will face
currentDirection = "south";
}
/**
* Retrieve the avatar's current location
*
* @return a Location object corresponding to the avatar's current location
*/
public Location getCurrentLocation() {
return currentLocation;
}
/**
* Move the avatar to a new location
*
* @param location the Location where the avatar should move
*/
public void setCurrentLocation(Location location) {
updateVisitedLocations();
currentLocation = location;
}
/**
* Retrieve the direction where the avatar currently is facing
*
* @return a String specifying the avatar's current direction
*/
public String getCurrentDirection() {
return currentDirection;
}
/**
* Make the avatar face a new direction
*
* @param a String specifying where the avatar should face
*/
public void setCurrentDirection(String direction) {
currentDirection = direction;
}
/**
* Retrieve all the portable items that the avatar is carrying
*
* @return an iterator of Items
*/
public Iterator<Item> getCartItems(){
if (cart.isEmpty() == false) {
return cart.iterator();
}
else {
return null;
}
}
/**
* Add a new item to the avatar's cart
*
* @param the Item to be added to the avatar's cart
*/
public void addCartItem(Item item) {
cart.add(item);
}
/**
* Remove an item from the avatar's cart
*
* @param itemName a String specifying the name of the item to be removed
*/
public void removeCartItem(String itemName) {
Item toRemove = null;
if (cart.isEmpty() == false) {
// Identify the item to be removed
for (Item item : cart) {
if (item.getFileName().equals(itemName)) {
toRemove = item;
}
}
// Remove the identified item
if (toRemove != null) {
cart.remove(toRemove);
}
}
}
/**
* Handle a command from the controller
*
* @param command a Command corresponding to a specific action to be performed by the avatar.
*/
public void perform(Command command) {
// Use the abilities to perform the appropriate action
Abilities.perform(command, this);
}
/**
* Retrieve a single item from the avatar's cart
*
* @param itemName, the name of the item to be retrieved
*
* @return an Item object
*/
public Item getCartItem(String itemName) {
Item targetItem = null;
if (cart.isEmpty() == false) {
// Find the requested item
for (Item item : cart) {
if (item.getFileName().equals(itemName)) {
targetItem = item;
}
}
}
return targetItem;
}
/**
* Update the record of visited locations
*/
private void updateVisitedLocations() {
visitedLocations.put(currentLocation.getLocationName(), currentLocation);
}
/**
* Retrieve a location from the record of visited locations
*
* @param locationName, the name of the location to be retrieved
*
* @return a Location object
*/
public Location getVisitedLocation(String locationName) {
if (visitedLocations.containsKey(locationName) == true) {
return visitedLocations.get(locationName);
}
else {
return null;
}
}
}