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/ZorkConditionHas.java

31 lines
1.0 KiB
Java
Raw Normal View History

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 16:42:56 +00:00
public ZorkConditionHas(String has, String object, String owner) {
super(object);
this.has = has;
this.owner = owner;
}
@Override
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
if (owner.equals("inventory")) {
return evaluateCondition(game.inventory.contains(object));
} 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-19 07:48:21 +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;
}
}