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/AttackAction.java

39 lines
1.1 KiB
Java

package com.github.dtschust.zork.repl.actions;
import com.github.dtschust.zork.ZorkGame;
import com.github.dtschust.zork.objects.ZorkCreature;
import com.github.dtschust.zork.repl.Action;
import java.util.List;
import java.util.Optional;
/**
* Attempt an attack, do you feel lucky?
*/
public class AttackAction implements Action {
@Override
public boolean matchesInput(final List<String> arguments) {
return arguments.get(0).equals("attack");
}
@Override
public int getMinimumArgCount() {
return 4;
}
@Override
public boolean run(final ZorkGame game, final List<String> arguments) {
final String what = arguments.get(1);
final String weapon = arguments.get(3);
final Optional<ZorkCreature> tempCreature = game.getCreature(what);
if (tempCreature.isPresent() && game.inventory.contains(weapon) &&
tempCreature.get().isAttackSuccessful(game, weapon)) {
game.stream.println("You assault the " + what + " with the " + weapon + ".");
tempCreature.get().printAndExecuteActions(game);
return true;
}
return false;
}
}