feat: wire TutorialGuide into the game loop; show on New Game only

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-01 21:53:27 +02:00
parent e27a12e4fe
commit c315d3f4b7
3 changed files with 80 additions and 3 deletions

View File

@@ -21,6 +21,9 @@ import thb.jeanluc.adventure.command.impl.UseCommand;
import thb.jeanluc.adventure.game.Game;
import thb.jeanluc.adventure.game.GameContext;
import thb.jeanluc.adventure.game.GameSession;
import thb.jeanluc.adventure.game.TutorialGuide;
import thb.jeanluc.adventure.loader.TutorialLoader;
import thb.jeanluc.adventure.model.Tutorial;
import thb.jeanluc.adventure.io.ConsoleIO;
import thb.jeanluc.adventure.io.GameIO;
import thb.jeanluc.adventure.io.text.Banner;
@@ -70,12 +73,12 @@ public final class App {
boolean running = true;
while (running) {
switch (MainMenu.show(io)) {
case NEW_GAME -> play(io, saves, newSession(io));
case NEW_GAME -> play(io, saves, newSession(io), true);
case LOAD -> {
SaveSlotInfo slot = MainMenu.pickSlot(io, saves);
if (slot != null) {
try {
play(io, saves, saves.load(slot.slug()));
play(io, saves, saves.load(slot.slug()), false);
} catch (SaveException e) {
io.write("Could not load: " + e.getUserMessage());
}
@@ -96,7 +99,7 @@ public final class App {
}
/** Builds the registry, runs one game to completion, autosaving to its slot. */
private static void play(GameIO io, SaveService saves, GameSession session) {
private static void play(GameIO io, SaveService saves, GameSession session, boolean withTutorial) {
GameContext ctx = new GameContext(session, io);
ctx.setSaveCallback(() -> {
try {
@@ -128,6 +131,13 @@ public final class App {
registry.register(menu, "menu", "quit", "exit");
registry.register(new HelpCommand(registry), "help", "?");
if (withTutorial) {
Tutorial tutorial = new TutorialLoader().load();
if (!tutorial.isEmpty()) {
game.setTutorialGuide(new TutorialGuide(tutorial, registry));
}
}
io.print(Banner.welcome(session.getWorld().getTitle()));
io.write(session.getWorld().getWelcomeMessage());
new LookCommand().execute(ctx, List.of());

View File

@@ -32,12 +32,23 @@ public class Game {
/** Loop flag. Flipped to false by {@link #stop()}. */
private boolean running = true;
/** Optional onboarding guide; null in normal/loaded play. */
private TutorialGuide tutorialGuide;
/** Wires the interactive tutorial (New Game only). */
public void setTutorialGuide(TutorialGuide tutorialGuide) {
this.tutorialGuide = tutorialGuide;
}
/**
* Runs the loop until {@link #stop()} is called or input is exhausted.
*/
public void run() {
publishHud();
maybeEnd();
if (tutorialGuide != null) {
tutorialGuide.begin(ctx);
}
while (running) {
String input = ctx.getIo().readLine();
if (input == null) {
@@ -47,6 +58,12 @@ public class Game {
if (parsed.verb().isEmpty()) {
continue;
}
if (tutorialGuide != null && tutorialGuide.isActive()
&& parsed.verb().equals("skip")) {
tutorialGuide.skip(ctx);
publishHud();
continue;
}
Optional<Command> cmd = registry.find(parsed.verb());
if (cmd.isEmpty()) {
ctx.getIo().write("I don't understand '" + parsed.verb() + "'. Type 'help'.");
@@ -56,6 +73,9 @@ public class Game {
if (ctx.getSession().getTurn() % 10 == 0) {
ctx.save();
}
if (tutorialGuide != null) {
tutorialGuide.onCommand(ctx, parsed);
}
}
publishHud();
maybeEnd();