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

127 lines
4.3 KiB
Java

package com.github.dtschust.zork;
import com.github.dtschust.zork.objects.*;
import com.github.dtschust.zork.types.ObjectCollector;
import com.github.dtschust.zork.types.ZorkMapByName;
import java.io.PrintStream;
import java.util.*;
import java.util.stream.Stream;
public class ZorkGame {
public final PrintStream stream;
public final Set<String> inventory = new HashSet<>();
private final ZorkMapByName<ZorkRoom> rooms;
private final ZorkMapByName<ZorkItem> items;
private final ZorkMapByName<ZorkContainer> containers;
private final ZorkMapByName<ZorkCreature> creatures;
private boolean running = false;
private String currentRoom;
public ZorkGame(final Collection<ZorkRoom> rooms,
final Collection<ZorkItem> items,
final Collection<ZorkContainer> containers,
final Collection<ZorkCreature> creatures,
final PrintStream stream) {
this.stream = stream;
this.rooms = new ZorkMapByName<>(rooms);
this.items = new ZorkMapByName<>(items);
this.containers = new ZorkMapByName<>(containers);
this.creatures = new ZorkMapByName<>(creatures);
}
public ZorkRoom getCurrentRoom() {
return rooms.get(currentRoom).orElseThrow(() ->
new IllegalStateException("current room not found: " + currentRoom));
}
public boolean changeRoom(String newRoom) {
if (rooms.containsName(newRoom)) {
currentRoom = newRoom;
running = true;
return true;
}
return false;
}
public boolean isRunning() {
return running;
}
public void setGameOver() {
running = false;
}
public void removeFromBorders(final ZorkRoom room) {
for (final ZorkRoom bordering : rooms.values()) {
bordering.removeBorderingRoom(room.getName());
}
rooms.put(room);
}
public void updateObjectStatus(final String objectName, final String status) {
for (final ZorkMapByName<? extends ZorkObject> map : List.of(containers, rooms, items, creatures)) {
final Optional<? extends ZorkObject> o = map.get(objectName);
if (o.isPresent()) {
o.get().updateStatus(status);
break;
}
}
}
public Optional<? extends ZorkObject> getObject(final String objectName) {
return Stream.of(containers, rooms, items, creatures)
.map(m -> m.get(objectName))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
}
public void addObjectToCollection(final ZorkObject object, final String destinationName) {
for (final ZorkMapByName<? extends ObjectCollector> map : List.of(containers, rooms)) {
final Optional<? extends ObjectCollector> o = map.get(destinationName);
if (o.isPresent()) {
o.get().addObject(object);
return;
}
}
throw new UnsupportedOperationException("destination " + destinationName + " not a room or container");
}
public void removeObjectFromCollections(final ZorkObject object) {
for (final ZorkMapByName<? extends ObjectCollector> map : List.of(containers, rooms)) {
for (ObjectCollector v : map.values()) {
v.removeObject(object);
}
}
}
public Optional<ZorkRoom> getRoom(final String roomName) {
return this.rooms.get(roomName);
}
public Optional<ZorkCreature> getCreature(final String creatureName) {
return this.creatures.get(creatureName);
}
public Optional<ZorkItem> getItem(final String itemName) {
return this.items.get(itemName);
}
public Optional<ZorkContainer> getContainer(final String containerName) {
return this.containers.get(containerName);
}
public boolean evaluateTriggers(final String currentCommand) {
final boolean currentRoom1 = getCurrentRoom().evaluateTriggers(this, currentCommand);
final boolean itemsInInventory = ZorkTrigger.evaluateTriggersFor(inventory.stream(), this, currentCommand);
return currentRoom1 || itemsInInventory;
}
}