102 lines
3.0 KiB
Java
102 lines
3.0 KiB
Java
package com.github.dtschust.zork;
|
|
|
|
import com.github.dtschust.zork.Zork.Type;
|
|
import com.github.dtschust.zork.types.*;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
public class ZorkGame {
|
|
|
|
public final Set<String> inventory = new HashSet<>();
|
|
private final ZorkMap<ZorkRoom> rooms = new ZorkMap<>();
|
|
private final ZorkMap<ZorkItem> items = new ZorkMap<>();
|
|
private final ZorkMap<ZorkContainer> containers = new ZorkMap<>();
|
|
private final ZorkMap<ZorkCreature> creatures = new ZorkMap<>();
|
|
private final HashMap<String, Type> objectLookup = new HashMap<>();
|
|
private boolean running = false;
|
|
private String currentRoom;
|
|
|
|
public ZorkRoom getCurrentRoom() {
|
|
return rooms.get(currentRoom);
|
|
}
|
|
|
|
public boolean changeRoom(String newRoom) {
|
|
if (rooms.containsKey(newRoom)) {
|
|
currentRoom = newRoom;
|
|
running = true;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean isRunning() {
|
|
return running;
|
|
}
|
|
|
|
public void setGameOver() {
|
|
running = false;
|
|
}
|
|
|
|
public Type getTypeFromLookup(String object) {
|
|
return objectLookup.get(object);
|
|
}
|
|
|
|
public void addObjectThroughLookup(Type type, ZorkObject object) {
|
|
putInMapGivenType(type, object);
|
|
objectLookup.put(object.getName(), type);
|
|
}
|
|
|
|
public <T extends ZorkObject> ZorkMap<T> getListThroughLookup(Class<T> cast, String name) {
|
|
return values(cast, objectLookup.get(name));
|
|
}
|
|
|
|
public <T extends ZorkObject> ZorkMap<T> values(Class<T> cast, Type type) {
|
|
return (ZorkMap<T>) getMapFromType(type);
|
|
}
|
|
|
|
public ZorkObject get(Type type, String key) {
|
|
return getMapFromType(type).get(key);
|
|
}
|
|
|
|
public void put(Type type, ZorkObject object) {
|
|
putInMapGivenType(type, object);
|
|
}
|
|
|
|
|
|
private ZorkMap<? extends ZorkObject> getMapFromType(Type type) {
|
|
switch (type) {
|
|
case ROOM:
|
|
return rooms;
|
|
case CONTAINER:
|
|
return containers;
|
|
case CREATURE:
|
|
return creatures;
|
|
case ITEM:
|
|
return items;
|
|
default:
|
|
throw new IllegalStateException("Unexpected value: " + type);
|
|
}
|
|
}
|
|
|
|
private void putInMapGivenType(Type type, ZorkObject object) {
|
|
switch (type) {
|
|
case ROOM:
|
|
rooms.put((ZorkRoom) object);
|
|
break;
|
|
case CONTAINER:
|
|
containers.put((ZorkContainer) object);
|
|
break;
|
|
case CREATURE:
|
|
creatures.put((ZorkCreature) object);
|
|
break;
|
|
case ITEM:
|
|
items.put((ZorkItem) object);
|
|
break;
|
|
default:
|
|
throw new IllegalStateException("Unexpected value: " + type);
|
|
}
|
|
}
|
|
}
|