diff --git a/src/app.ts b/src/app.ts index 4d95d68..dd1ff54 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,8 +3,8 @@ import { companions } from "./companion/characters"; import { getGreeting } from "./companion/companion"; import { renderInlineMarkdown } from "./utils/markdown"; import { initRewardScreen, startPreGeneration } from "./forest/reward"; -import { initForestScreen, renderForestScene } from "./forest/scene"; -import { getProgress, openDB, saveProgress } from "./storage/db"; +import { initForestScreen, renderForestScene, SLOTS_PER_PAGE } from "./forest/scene"; +import { getForestElements, getProgress, openDB, saveProgress } from "./storage/db"; import type { ScreenName } from "./types"; import { initParentScreen, showParentCodePrompt } from "./ui/parent"; import { initLessonScreen, initWelcomeScreen } from "./ui/screens"; @@ -119,11 +119,19 @@ async function handleLessonComplete( ); }, async () => { - // "Zurück zum Wald" -- show forest with new element animated + // "Zurück zum Wald" -- navigate to page containing the new element + const elements = await getForestElements(db); + const newPage = Math.max(0, Math.ceil(elements.length / SLOTS_PER_PAGE) - 1); + + // Update progress with new page before initForestScreen reads it + const latestProgress = await getProgress(db); + if (latestProgress) { + latestProgress.currentForestPage = newPage; + await saveProgress(db, latestProgress); + } + await initForestScreen(db, startLessonFromForest); - const prog = await getProgress(db); - const page = prog?.currentForestPage ?? 0; - await renderForestScene(db, page, elementId); + await renderForestScene(db, newPage, elementId); showScreen("forest"); }, ); diff --git a/src/forest/reward.ts b/src/forest/reward.ts index 12425fc..7012936 100644 --- a/src/forest/reward.ts +++ b/src/forest/reward.ts @@ -23,6 +23,7 @@ import type { CompanionType, ForestElement } from "../types"; import { playRewardSound } from "../audio/sounds"; import { speakText } from "../audio/speech"; import { renderInlineMarkdown } from "../utils/markdown"; +import { SLOTS_PER_PAGE } from "./scene"; // --- Element types for random selection --- @@ -176,7 +177,7 @@ export async function initRewardScreen( // 4. Determine grid position for new element const existing = await getForestElements(db); - const slotIndex = existing.length % 12; + const slotIndex = existing.length % SLOTS_PER_PAGE; const col = slotIndex % 4; const row = Math.floor(slotIndex / 4); // Add small random offset for natural feel