2022-11-19 09:46:28 +00:00
|
|
|
package com.github.dtschust.zork.types;
|
|
|
|
|
|
|
|
import com.github.dtschust.zork.ZorkCondition;
|
2022-11-21 15:36:38 +00:00
|
|
|
import com.github.dtschust.zork.parser.ZorkGame;
|
2022-11-16 15:18:00 +00:00
|
|
|
|
2022-11-21 16:10:08 +00:00
|
|
|
import java.util.*;
|
2022-11-16 15:18:00 +00:00
|
|
|
|
|
|
|
/* Creature*/
|
2022-11-21 16:10:08 +00:00
|
|
|
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;
|
2022-11-16 15:18:00 +00:00
|
|
|
|
2022-11-21 16:10:08 +00:00
|
|
|
public ZorkCreature(final String name,
|
|
|
|
final String description,
|
|
|
|
final Collection<String> vulnerabilities,
|
|
|
|
final Collection<ZorkCondition> conditions,
|
|
|
|
final Collection<String> prints,
|
|
|
|
final Collection<String> actions) {
|
2022-11-20 13:11:58 +00:00
|
|
|
super(name, description);
|
2022-11-21 16:10:08 +00:00
|
|
|
this.vulnerabilities = new HashSet<>(vulnerabilities);
|
|
|
|
this.conditions = new ArrayList<>(conditions);
|
|
|
|
this.print = new ArrayList<>(prints);
|
|
|
|
this.action = new ArrayList<>(actions);
|
2022-11-16 15:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Evaluate the success of an attack*/
|
2022-11-21 16:10:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2022-11-16 15:18:00 +00:00
|
|
|
}
|
|
|
|
}
|