-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovementCommand.java
More file actions
38 lines (34 loc) · 1.04 KB
/
MovementCommand.java
File metadata and controls
38 lines (34 loc) · 1.04 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
package bork;
/**
* Handles the movement command.
*
* @author Dr. Zeitz
*/
class MovementCommand extends Command {
private String dir;
/**
* Constructs a new directional Command object.
*
* @param dir the direction the player desires to travel in
*/
MovementCommand(String dir) {
this.dir = dir;
}
/**
* Checks to see if the player can move in the requested direction, updates
* the GameState accordingly, and returns a message.
*
* @return a description of the next room or a
* notification that the user cannot go there
*/
public String execute() {
Room currentRoom = GameState.instance().getAdventurersCurrentRoom();
Room nextRoom = currentRoom.leaveBy(dir);
if (nextRoom != null) { // could try/catch here.
GameState.instance().setAdventurersCurrentRoom(nextRoom);
return "\n" + nextRoom.describe() + "\n";
} else {
return "You can't go " + dir + ".\n";
}
}
}