92 lines
3.2 KiB
Java
92 lines
3.2 KiB
Java
package com.github.dtschust.zork.objects;
|
|
|
|
|
|
import com.github.dtschust.zork.ZorkGame;
|
|
import com.github.dtschust.zork.ZorkTrigger;
|
|
import com.github.dtschust.zork.types.ObjectCollector;
|
|
import com.github.dtschust.zork.types.ZorkDirection;
|
|
|
|
import java.util.*;
|
|
|
|
/* Room*/
|
|
public class ZorkRoom extends ZorkObject implements ObjectCollector {
|
|
private final String type;
|
|
private final Map<ZorkDirection, String> border;
|
|
private final Set<String> container;
|
|
private final Set<String> item;
|
|
private final Set<String> creature;
|
|
|
|
public ZorkRoom(final String name,
|
|
final String description,
|
|
final String type,
|
|
final String status,
|
|
final Collection<ZorkTrigger> triggers,
|
|
final Map<ZorkDirection, String> borders,
|
|
final Collection<String> containers,
|
|
final Collection<String> items,
|
|
final Collection<String> creatures) {
|
|
super(name, description, status, triggers);
|
|
this.type = type;
|
|
this.border = new EnumMap<>(borders);
|
|
this.container = new HashSet<>(containers);
|
|
this.item = new HashSet<>(items);
|
|
this.creature = new HashSet<>(creatures);
|
|
}
|
|
|
|
public boolean isExit() {
|
|
return "exit".equals(type);
|
|
}
|
|
|
|
public void removeBorderingRoom(String roomName) {
|
|
for (final Map.Entry<ZorkDirection, String> d : this.border.entrySet()) {
|
|
if (d.getValue().equals(roomName)) {
|
|
this.border.remove(d.getKey());
|
|
}
|
|
}
|
|
}
|
|
|
|
public Optional<String> getBorderingRoom(ZorkDirection border) {
|
|
return Optional.ofNullable(this.border.get(border));
|
|
}
|
|
|
|
@Override
|
|
public boolean evaluateTriggers(ZorkGame game, String input) {
|
|
final boolean items = ZorkTrigger.evaluateTriggersFor(item.stream(), game, input);
|
|
final boolean creatures = ZorkTrigger.evaluateTriggersFor(creature.stream(), game, input);
|
|
final boolean containers = ZorkTrigger.evaluateTriggersFor(container.stream(), game, input);
|
|
return super.evaluateTriggers(game, input) || items || creatures || containers;
|
|
}
|
|
|
|
public Set<String> getContainer() {
|
|
return container;
|
|
}
|
|
|
|
public boolean containsItem(final String item) {
|
|
return this.item.contains(item);
|
|
}
|
|
|
|
@Override
|
|
public void addObject(ZorkObject object) {
|
|
if (object instanceof ZorkContainer) {
|
|
container.add(object.getName());
|
|
} else if (object instanceof ZorkItem) {
|
|
item.add(object.getName());
|
|
} else if (object instanceof ZorkCreature) {
|
|
creature.add(object.getName());
|
|
} else {
|
|
throw new UnsupportedOperationException("room cannot store " + object.getClass().getSimpleName() + " objects");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void removeObject(ZorkObject object) {
|
|
if (object instanceof ZorkContainer) {
|
|
container.remove(object.getName());
|
|
} else if (object instanceof ZorkItem) {
|
|
item.remove(object.getName());
|
|
} else if (object instanceof ZorkCreature) {
|
|
creature.remove(object.getName());
|
|
}
|
|
}
|
|
}
|