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
2022-11-22 16:46:23 +01:00

41 lines
1.3 KiB
Java

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