This repository has been archived on 2022-12-21. You can view files and clone it, but cannot push or open issues or pull requests.
sdm03/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java

63 lines
2.0 KiB
Java

package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.Zork.Type;
import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.repl.Action;
import com.github.dtschust.zork.types.HasSetOfCollectable;
import com.github.dtschust.zork.types.ZorkObject;
import com.github.dtschust.zork.types.ZorkRoom;
import java.util.List;
import java.util.Set;
import static com.github.dtschust.zork.Zork.Type.*;
/**
* Delete: figure out what object it is and delete it accordingly. Rooms are especially tricky
*/
public class DeleteAction implements Action {
private static void deleteElementFromSpace(ZorkGame game, Type space, Type element, String object) {
for (ZorkObject tempObject : game.values(ZorkObject.class, space)) {
Set<String> set = ((HasSetOfCollectable) tempObject).getSetFromType(element);
if (set.contains(object)) {
set.remove(object);
game.put(space, tempObject);
}
}
}
@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);
switch (game.getTypeFromLookup(object)) {
case ROOM:
for (final ZorkRoom tempRoom : game.values(ZorkRoom.class, ROOM)) {
tempRoom.removeBorderingRoom(object);
game.put(ROOM, tempRoom);
}
break;
case ITEM:
deleteElementFromSpace(game, ROOM, ITEM, object);
deleteElementFromSpace(game, CONTAINER, ITEM, object);
break;
case CONTAINER:
deleteElementFromSpace(game, ROOM, CONTAINER, object);
break;
case CREATURE:
deleteElementFromSpace(game, ROOM, CREATURE, object);
break;
}
}
}