2022-11-22 12:43:15 +00:00
|
|
|
package com.github.dtschust.zork.parser;
|
|
|
|
|
|
|
|
import com.github.dtschust.zork.ZorkGame;
|
2022-11-22 14:12:55 +00:00
|
|
|
import com.github.dtschust.zork.objects.*;
|
2022-11-22 12:43:15 +00:00
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
2022-11-22 14:12:55 +00:00
|
|
|
import static com.github.dtschust.zork.objects.ZorkObjectTypes.*;
|
2022-11-22 12:43:15 +00:00
|
|
|
|
|
|
|
public abstract class ZorkParser {
|
2022-11-22 14:12:55 +00:00
|
|
|
private final Map<ZorkObjectTypes, PropertyParseStrategy<? extends ZorkObject>> strategies;
|
2022-11-22 12:43:15 +00:00
|
|
|
|
|
|
|
public ZorkParser(final PropertyParseStrategy<ZorkCreature> creatureStrategy,
|
|
|
|
final PropertyParseStrategy<ZorkContainer> containerStrategy,
|
|
|
|
final PropertyParseStrategy<ZorkItem> itemStrategy,
|
|
|
|
final PropertyParseStrategy<ZorkRoom> roomStrategy) {
|
|
|
|
this.strategies = Map.ofEntries(
|
|
|
|
Map.entry(CREATURE, creatureStrategy),
|
|
|
|
Map.entry(CONTAINER, containerStrategy),
|
|
|
|
Map.entry(ITEM, itemStrategy),
|
|
|
|
Map.entry(ROOM, roomStrategy)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract Property getRootProperty(final String filename);
|
|
|
|
|
|
|
|
public ZorkGame parse(final String filename) {
|
|
|
|
ZorkGame data = new ZorkGame();
|
|
|
|
|
|
|
|
final Property rootElement = getRootProperty(filename);
|
|
|
|
|
|
|
|
// Every single first generation child is a room, container, creature, or item. So load them in
|
|
|
|
for (final Property element : rootElement.subProperties()) {
|
|
|
|
final String name = element.name();
|
2022-11-22 14:12:55 +00:00
|
|
|
final ZorkObjectTypes t = ZorkObjectTypes.fromPropertyName(name)
|
2022-11-22 12:43:15 +00:00
|
|
|
.orElseThrow(() -> new IllegalStateException("Unexpected value: " + name));
|
|
|
|
final ZorkObject built = strategies.get(t).parse(element);
|
|
|
|
data.addObjectThroughLookup(t, built);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|