52 lines
1.8 KiB
Java
52 lines
1.8 KiB
Java
package com.github.dtschust.zork.types;
|
|
|
|
import com.github.dtschust.zork.ZorkCondition;
|
|
import com.github.dtschust.zork.parser.ZorkGame;
|
|
|
|
import java.util.*;
|
|
|
|
/* Creature*/
|
|
public class ZorkCreature extends ZorkObject implements HasPrintsAndActions {
|
|
private final Set<String> vulnerabilities;
|
|
private final List<ZorkCondition> conditions;
|
|
private final List<String> print;
|
|
private final List<String> action;
|
|
|
|
public ZorkCreature(final String name,
|
|
final String description,
|
|
final Collection<String> vulnerabilities,
|
|
final Collection<ZorkCondition> conditions,
|
|
final Collection<String> prints,
|
|
final Collection<String> actions) {
|
|
super(name, description);
|
|
this.vulnerabilities = new HashSet<>(vulnerabilities);
|
|
this.conditions = new ArrayList<>(conditions);
|
|
this.print = new ArrayList<>(prints);
|
|
this.action = new ArrayList<>(actions);
|
|
}
|
|
|
|
/* Evaluate the success of an attack*/
|
|
|
|
/**
|
|
* Given a game instance and a weapon, returns whether the attack is successful, i.e. if the creature is vulnerable
|
|
* to the weapon and all conditions for a successful attack are satisfied
|
|
*
|
|
* @param game the game
|
|
* @param weapon the weapon
|
|
* @return true if the attack is successful
|
|
*/
|
|
public boolean isAttackSuccessful(final ZorkGame game, final String weapon) {
|
|
return vulnerabilities.contains(weapon) && conditions.stream().allMatch(c -> c.evaluate(game));
|
|
}
|
|
|
|
@Override
|
|
public List<String> getPrints() {
|
|
return Collections.unmodifiableList(print);
|
|
}
|
|
|
|
@Override
|
|
public List<String> getActions() {
|
|
return Collections.unmodifiableList(action);
|
|
}
|
|
}
|