2022-11-16 15:18:00 +00:00
|
|
|
package com.github.dtschust.zork;
|
|
|
|
|
|
|
|
/* Has conditions*/
|
2022-11-16 16:18:19 +00:00
|
|
|
public class ZorkConditionHas extends ZorkCondition {
|
2022-11-16 16:42:56 +00:00
|
|
|
private final String has;
|
|
|
|
private final String owner;
|
2022-11-16 15:18:00 +00:00
|
|
|
|
2022-11-16 16:42:56 +00:00
|
|
|
public ZorkConditionHas(String has, String object, String owner) {
|
|
|
|
super(object);
|
|
|
|
this.has = has;
|
|
|
|
this.owner = owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-11-21 15:36:38 +00:00
|
|
|
public boolean evaluate(ZorkGame game) {
|
2022-11-22 17:31:06 +00:00
|
|
|
// Inventory is a special case as it isn't the name of any object in the game, check for it specifically
|
2022-11-16 15:18:00 +00:00
|
|
|
if (owner.equals("inventory")) {
|
2022-11-21 15:36:38 +00:00
|
|
|
return evaluateCondition(game.inventory.contains(object));
|
2022-11-16 15:18:00 +00:00
|
|
|
} else {
|
2022-11-22 17:31:06 +00:00
|
|
|
return game.getRoom(owner).map(r -> evaluateCondition(r.containsItem(object))).orElseGet(() ->
|
|
|
|
game.getContainer(owner).map(c -> evaluateCondition(c.containsItem(object))).orElse(false));
|
2022-11-16 15:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-19 07:48:21 +00:00
|
|
|
|
2022-11-21 15:36:38 +00:00
|
|
|
private boolean evaluateCondition(boolean contained) {
|
|
|
|
if (has.equals("yes")) return contained;
|
|
|
|
else if (has.equals("no")) return !contained;
|
2022-11-19 07:48:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-16 15:18:00 +00:00
|
|
|
}
|