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

49 lines
1.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.repl.ActionDispatcher;
import com.github.dtschust.zork.types.ZorkCreature;
import java.util.List;
/**
* Attempt an attack, do you feel lucky?
*/
public class AttackAction extends Action {
@Override
public boolean matchesInput(List<String> arguments) {
return arguments.get(0).equals("attack");
}
@Override
public int getMinimumArgCount() {
return 4;
}
@Override
public void run(ZorkGame game, List<String> arguments) {
final String tempString = arguments.get(1);
final String weapon = arguments.get(3);
if (game.getCurrentRoom().creature.contains(tempString)) {
ZorkCreature tempCreature = (ZorkCreature) game.get("creature", tempString);
if (tempCreature != null && game.inventory.contains(weapon)) {
if (tempCreature.attack(game, weapon)) {
System.out.println("You assault the " + tempString + " with the " + weapon + ".");
for (String print : tempCreature.print) {
System.out.println(print);
}
final ActionDispatcher effectsDispatcher = new ActionDispatcher(game);
for (final String action : tempCreature.action) {
effectsDispatcher.dispatch(action);
}
return;
}
}
}
System.out.println("Error");
}
}