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

45 lines
1.4 KiB
Java
Raw Normal View History

package com.github.dtschust.zork;
2022-11-19 07:48:21 +00:00
import java.util.Set;
/* 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(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);
} else {
/* is it a room?*/
2022-11-16 16:18:19 +00:00
ZorkRoom roomObject = zork.game.Rooms.get(owner);
if (roomObject != null) {
2022-11-19 07:48:21 +00:00
return evaluateCondition(roomObject.item, object);
}
/* is it a container?*/
else {
2022-11-16 16:18:19 +00:00
ZorkContainer containerObject = zork.game.Containers.get(owner);
if (containerObject != null) {
2022-11-19 07:48:21 +00:00
return evaluateCondition(containerObject.item, object);
}
}
}
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;
}
}