31 lines
1.0 KiB
Java
31 lines
1.0 KiB
Java
package com.github.dtschust.zork;
|
|
|
|
/* Has conditions*/
|
|
public class ZorkConditionHas extends ZorkCondition {
|
|
private final String has;
|
|
private final String owner;
|
|
|
|
public ZorkConditionHas(String has, String object, String owner) {
|
|
super(object);
|
|
this.has = has;
|
|
this.owner = owner;
|
|
}
|
|
|
|
@Override
|
|
public boolean evaluate(ZorkGame game) {
|
|
// Inventory is a special case as it isn't the name of any object in the game, check for it specifically
|
|
if (owner.equals("inventory")) {
|
|
return evaluateCondition(game.inventory.contains(object));
|
|
} else {
|
|
return game.getRoom(owner).map(r -> evaluateCondition(r.containsItem(object))).orElseGet(() ->
|
|
game.getContainer(owner).map(c -> evaluateCondition(c.containsItem(object))).orElse(false));
|
|
}
|
|
}
|
|
|
|
private boolean evaluateCondition(boolean contained) {
|
|
if (has.equals("yes")) return contained;
|
|
else if (has.equals("no")) return !contained;
|
|
return false;
|
|
}
|
|
}
|