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/types/ZorkCreature.java

52 lines
1.8 KiB
Java
Raw Normal View History

2022-11-19 09:46:28 +00:00
package com.github.dtschust.zork.types;
import com.github.dtschust.zork.ZorkCondition;
import com.github.dtschust.zork.parser.ZorkGame;
2022-11-21 16:10:08 +00:00
import java.util.*;
/* 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-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) {
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);
}
/* 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);
}
}