124 lines
4.2 KiB
Java
124 lines
4.2 KiB
Java
package de.umarumg;
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import de.umarumg.models.Decision;
|
|
import de.umarumg.models.Scene;
|
|
import de.umarumg.models.TextSpeed;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import java.io.FileInputStream;
|
|
import java.io.InputStream;
|
|
import java.util.Arrays;
|
|
import java.util.LinkedList;
|
|
import java.util.Map;
|
|
import java.util.Scanner;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.logging.Logger;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class Main {
|
|
|
|
public static Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
|
|
public static Scanner scanner = new Scanner(System.in);
|
|
|
|
private static Map<String, Scene> story;
|
|
private static TextSpeed textSpeed = TextSpeed.NORMAL;
|
|
private static final String finalSpeeds = Arrays.stream(TextSpeed.values())
|
|
.map(speed -> StringUtils.capitalize(speed.name().toLowerCase()))
|
|
.collect(Collectors.joining(", "));
|
|
|
|
private static String currentScene = "start";
|
|
private static String userName = "Player";
|
|
|
|
public static void main(String[] args) {
|
|
if (args.length > 0) {
|
|
loadDialog(args[0], false);
|
|
} else {
|
|
loadDialog("dialog.json", true);
|
|
}
|
|
|
|
System.out.println("How fast should the text scroll?: " + finalSpeeds);
|
|
System.out.print("> ");
|
|
String data = scanner.nextLine().toLowerCase();
|
|
|
|
if (data.startsWith("s")) {
|
|
textSpeed = TextSpeed.SLOW;
|
|
} else if (data.startsWith("n")) {
|
|
textSpeed = TextSpeed.NORMAL;
|
|
} else if (data.startsWith("f")) {
|
|
textSpeed = TextSpeed.FAST;
|
|
}
|
|
|
|
System.out.println("What is your Name?");
|
|
System.out.print("> ");
|
|
String name = scanner.nextLine();
|
|
if(!name.isEmpty()) {
|
|
userName = name.strip().split(" ")[0];
|
|
}
|
|
|
|
run(currentScene);
|
|
}
|
|
|
|
public static void loadDialog(String fileName, boolean defaultFile) {
|
|
try {
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
InputStream inputStream = defaultFile ? Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName) : new FileInputStream(fileName);
|
|
story = mapper.readValue(inputStream, new TypeReference<>() {});
|
|
} catch (Exception ex) {
|
|
logger.severe("Error reading the internal dialog File. Exiting now!");
|
|
System.exit(1);
|
|
}
|
|
}
|
|
|
|
public static void writeText(String text) {
|
|
text = text.replace("%user%", userName);
|
|
try {
|
|
for (char c : text.toCharArray()) {
|
|
System.out.print(c);
|
|
TimeUnit.MILLISECONDS.sleep(textSpeed.getSpeed());
|
|
}
|
|
} catch (Exception ex) {
|
|
logger.severe("Error while writing text to screen. Exiting now!");
|
|
System.exit(1);
|
|
}
|
|
}
|
|
|
|
public static void run(String key) {
|
|
if (story != null) {
|
|
Scene scene = story.get(key);
|
|
for (int i = 0; i < scene.getText().size(); i++) {
|
|
writeText(scene.getText().get(i));
|
|
if (i == scene.getText().size() - 1) {
|
|
System.out.print("\n");
|
|
break;
|
|
}
|
|
scanner.nextLine();
|
|
}
|
|
|
|
LinkedList<Decision> decisions = scene.getDecisions();
|
|
|
|
if (!decisions.isEmpty()) {
|
|
int choice = 0;
|
|
for (int i = 0; i < decisions.size(); i++) {
|
|
writeText(i+1 + ": " + decisions.get(i).getText() + "\n");
|
|
}
|
|
|
|
while (choice == 0) {
|
|
System.out.print("> ");
|
|
if (scanner.hasNextInt()) {
|
|
choice = Integer.parseInt(scanner.nextLine());
|
|
if (choice < 0 || choice > decisions.size()) {
|
|
choice = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
currentScene = decisions.get(choice-1).getScene();
|
|
run(currentScene);
|
|
} else {
|
|
System.out.println("End of Story!");
|
|
}
|
|
}
|
|
}
|
|
} |