73 lines
2.6 KiB
Java
73 lines
2.6 KiB
Java
package com.github.dtschust.zork.repl.actions;
|
|
|
|
import com.github.dtschust.zork.parser.ZorkGame;
|
|
import com.github.dtschust.zork.repl.Action;
|
|
import com.github.dtschust.zork.types.ZorkContainer;
|
|
import com.github.dtschust.zork.types.ZorkRoom;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Delete: figure out what object it is and delete it accordingly. Rooms are especially tricky
|
|
*/
|
|
public class DeleteAction extends Action {
|
|
@Override
|
|
public boolean matchesInput(List<String> arguments) {
|
|
return arguments.get(0).equals("Delete");
|
|
}
|
|
|
|
@Override
|
|
public int getMinimumArgCount() {
|
|
return 2;
|
|
}
|
|
|
|
@Override
|
|
public void run(ZorkGame game, List<String> arguments) {
|
|
final String object = arguments.get(1);
|
|
|
|
String objectType = game.getTypeFromLookup(object);
|
|
switch (objectType) {
|
|
case "room":
|
|
for (ZorkRoom tempRoom : (Iterable<ZorkRoom>) game.values("room")) {
|
|
for (String key : tempRoom.border.keySet()) {
|
|
if (tempRoom.border.get(key).equals(object)) {
|
|
tempRoom.border.remove(key);
|
|
}
|
|
}
|
|
game.put("room", tempRoom);
|
|
}
|
|
break;
|
|
case "item":
|
|
for (ZorkRoom tempRoom : (Iterable<ZorkRoom>) game.values("room")) {
|
|
if (tempRoom.item.contains(object)) {
|
|
tempRoom.item.remove(object);
|
|
game.put("room", tempRoom);
|
|
}
|
|
}
|
|
for (ZorkContainer tempContainer : (Iterable<ZorkContainer>) game.values("container")) {
|
|
if (tempContainer.item.contains(object)) {
|
|
tempContainer.item.remove(object);
|
|
game.put("container", tempContainer);
|
|
}
|
|
}
|
|
break;
|
|
case "container":
|
|
for (ZorkRoom tempRoom : (Iterable<ZorkRoom>) game.values("room")) {
|
|
if (tempRoom.container.contains(object)) {
|
|
tempRoom.container.remove(object);
|
|
game.put("room", tempRoom);
|
|
}
|
|
}
|
|
break;
|
|
case "creature":
|
|
for (ZorkRoom tempRoom : (Iterable<ZorkRoom>) game.values("room")) {
|
|
if (tempRoom.creature.contains(object)) {
|
|
tempRoom.creature.remove(object);
|
|
game.put("room", tempRoom);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|