-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbilities.java
More file actions
197 lines (149 loc) · 5.29 KB
/
Abilities.java
File metadata and controls
197 lines (149 loc) · 5.29 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
package myclasses;
import java.util.HashMap;
/**
* A class that implements the avatar's abilites including navigation and
* manipulation of portable items in the virtual world.
*
* @author Brian Mukeswe
* @contact b.mukeswe@sms.ed.ac.uk
*/
public class Abilities {
// Maps to store navigation logic
private static HashMap<String, String> leftLogic = new HashMap<>();
private static HashMap<String, String> rightLogic = new HashMap<>();
/**
* Initialise the navigation logic maps
*/
private static void init(){
// Specify the logic for turning left
leftLogic.put("north", "west");
leftLogic.put("west", "south");
leftLogic.put("south", "east");
leftLogic.put("east", "north");
// Specify the logic for turning right
rightLogic.put("north", "east");
rightLogic.put("east", "south");
rightLogic.put("south", "west");
rightLogic.put("west", "north");
}
/**
* Identify the appropriate actions to take based on the received command
*
*/
public static void perform(Command command, Avatar avatar) {
init();
switch (command.getCommandWord()) {
case DROP_ITEM:
dropItem(command, avatar);
break;
case MOVE_FORWARD:
// Check that the command contains a parameter
if (command.getParameters() != null) {
moveForward(command, avatar);
}
else {// The command contains no parameter
moveForward(avatar);
}
break;
case PICKUP_ITEM:
pickupItem(command, avatar);
break;
case TURN_LEFT:
turnLeft(command, avatar);
break;
case TURN_RIGHT:
turnRight(command, avatar);
break;
case UNKNOWN:
break;
default:
break;
}
}
/**
* Make the avatar turn right
*/
private static void turnRight(Command command, Avatar avatar) {
// Identify the new direction after turning
String newDirection = rightLogic.get(avatar.getCurrentDirection());
// Make the avatar face the new direction
avatar.setCurrentDirection(newDirection);
}
/**
* Make the avatar turn left
*/
private static void turnLeft(Command command, Avatar avatar) {
// Identify the new direction after turning
String newDirection = leftLogic.get(avatar.getCurrentDirection());
// Make the avatar face the new direction
avatar.setCurrentDirection(newDirection);
}
/**
* Make the avatar pick up an item from the current location
*/
private static void pickupItem(Command command, Avatar avatar) {
// Check that the command has a parameter
if (command.getParameters() != null) {
if (command.getParameters().hasNext() == true) {
// Identify the item to be picked up from the avatar's current location
Item item = avatar.getCurrentLocation().getItem(command.getParameters().next());
// Pick up the item
avatar.addCartItem(item);
avatar.getCurrentLocation().removeItem(item.getFileName());
}
}
}
/**
* Make the avatar move forward through a specified exit
*/
private static void moveForward(Command command, Avatar avatar) {
// Check that the command has a parameter
if (command.getParameters() != null) {
if (command.getParameters().hasNext() == true){
// Identify the destination
String newLocationName = command.getParameters().next();
// Check if the avatar has previously visited the destination location
if (avatar.getVisitedLocation(newLocationName) != null) {
// Load the destination location from the record of previously visited locations
avatar.setCurrentLocation(avatar.getVisitedLocation(newLocationName));
}
else { // The avatar is visiting the destination for the first time
// Load the destination from the World class
avatar.setCurrentLocation(World.getLocation(newLocationName));
}
}
}
}
/**
* Make the avatar move forward through the first available exit. This method is
* used when the user does not explicitly specify the exit to be used.
*/
private static void moveForward(Avatar avatar) {
// Identify the destination
String newLocationName = avatar.getCurrentLocation().getView(avatar.getCurrentDirection()).getExits().next();
// Check if the avatar has previously visited the destination location
if (avatar.getVisitedLocation(newLocationName) != null) {
// Load the destination location from the record of previously visited locations
avatar.setCurrentLocation(avatar.getVisitedLocation(newLocationName));
}
else {// The avatar is visiting the destination for the first time
// Load the destination from the World class
avatar.setCurrentLocation(World.getLocation(newLocationName));
}
}
/**
* Make the avatar drop off an item at the current location
*/
private static void dropItem(Command command, Avatar avatar) {
// Check that the command has a parameter
if (command.getParameters() != null) {
if (command.getParameters().hasNext() == true){
// Get the name of the item to be dropped
String itemName = command.getParameters().next();
// Drop off the specified item in the current location
avatar.getCurrentLocation().addItem(avatar.getCartItem(itemName));
avatar.removeCartItem(itemName);
}
}
}
}