-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToggleLightCommand.java
More file actions
64 lines (58 loc) · 2.65 KB
/
ToggleLightCommand.java
File metadata and controls
64 lines (58 loc) · 2.65 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
package bork;
/**
* Handles command to turn a light source on or off
* @author Woodruff
*/
class ToggleLightCommand extends Command {
private String lightSourceName;
private String toggleState;
/**
* Constructor for the ToggleLightCommand with the input of which light source and with state to turn it to
* @param lightSourceName the name of the light source item being toggled
* @param toggleState the state it is being toggled to
*/
ToggleLightCommand(String lightSourceName, String toggleState) {
this.lightSourceName = lightSourceName;
this.toggleState = toggleState;
}
/**
* Returns the String indicating the change in state of the light source
* @return String the message reflecting the change or lack of change in the light sources state
*/
@Override
public String execute() {
Item itemReferredTo = null;
try {
itemReferredTo = GameState.instance().getItemInVicinityNamed(lightSourceName);
} catch (Item.NoItemException e) {
return "There's no " + lightSourceName + " here.";
}
if (!(itemReferredTo instanceof LightSource)){
return "The " + lightSourceName + " is not a light source.";
}
LightSource lsReferredTo = (LightSource)itemReferredTo;
if (toggleState.equals("on")) {
if (lsReferredTo.isLit()) {
return "The " + lightSourceName + " is already on.";
} else if (lsReferredTo.getFuel() > 0) {
lsReferredTo.toggleLight();
String msg = "The " + lightSourceName + " casts light around you.";
if (!GameState.instance().getAdventurersCurrentRoom().isLit()) {
msg += "\n" + GameState.instance().getAdventurersCurrentRoom().describe();
}
return msg;
} else {
return "The " + lightSourceName + " has no fuel.";
}
} else if (toggleState.equals("off")) {
if (!lsReferredTo.isLit()) {
return "The " + lightSourceName + " is already off.";
} else {
lsReferredTo.toggleLight();
return "The " + lightSourceName + " stops lighting up the room.";
}
} else {
return "You can not " + toggleState + " the " + lightSourceName + ".";
}
}
}