2022-11-16 15:18:00 +00:00
|
|
|
package com.github.dtschust.zork;
|
|
|
|
|
2022-11-19 07:48:21 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
2022-11-16 15:18:00 +00:00
|
|
|
/* 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-16 15:18:00 +00:00
|
|
|
public boolean evaluate(Zork zork) {
|
|
|
|
/*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")) {
|
2022-11-19 07:48:21 +00:00
|
|
|
return evaluateCondition(zork.game.Inventory, object);
|
2022-11-16 15:18:00 +00:00
|
|
|
} else {
|
|
|
|
/* is it a room?*/
|
2022-11-16 16:18:19 +00:00
|
|
|
ZorkRoom roomObject = zork.game.Rooms.get(owner);
|
2022-11-16 15:18:00 +00:00
|
|
|
if (roomObject != null) {
|
2022-11-19 07:48:21 +00:00
|
|
|
return evaluateCondition(roomObject.item, object);
|
2022-11-16 15:18:00 +00:00
|
|
|
}
|
|
|
|
/* is it a container?*/
|
|
|
|
else {
|
2022-11-16 16:18:19 +00:00
|
|
|
ZorkContainer containerObject = zork.game.Containers.get(owner);
|
2022-11-16 15:18:00 +00:00
|
|
|
if (containerObject != null) {
|
2022-11-19 07:48:21 +00:00
|
|
|
return evaluateCondition(containerObject.item, object);
|
2022-11-16 15:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-19 07:48:21 +00:00
|
|
|
|
|
|
|
private boolean evaluateCondition(Set items, String object){
|
|
|
|
if(has.equals("yes")) return items.contains(object);
|
|
|
|
else if(has.equals("no")) return !items.contains(object);
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-16 15:18:00 +00:00
|
|
|
}
|