From 430bd77937d878af327d69e2e954302b0ee1a987 Mon Sep 17 00:00:00 2001 From: Pihkaal Date: Fri, 30 Jan 2026 19:42:46 +0100 Subject: [PATCH] feat(settings/options/2048): compute and save score --- .../BottomScreen/Menus/Options/GbaMode.vue | 50 +++++++++++++++--- .../bottom-screen/options/2048/frame.webp | Bin 0 -> 188 bytes 2 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 public/nds/images/settings/bottom-screen/options/2048/frame.webp diff --git a/app/components/Settings/BottomScreen/Menus/Options/GbaMode.vue b/app/components/Settings/BottomScreen/Menus/Options/GbaMode.vue index ef05510..e206881 100644 --- a/app/components/Settings/BottomScreen/Menus/Options/GbaMode.vue +++ b/app/components/Settings/BottomScreen/Menus/Options/GbaMode.vue @@ -59,16 +59,29 @@ const BOARD_X = 64; const BOARD_Y = 32; const BOARD_SIZE = 4; +const SCORE_X = 195; +const SCORE_Y = 36; +const HIGH_SCORE_Y = 68; + const emptyBoard = () => Array.from({ length: BOARD_SIZE }, () => Array.from({ length: BOARD_SIZE }, () => 0), ); -const savedBoard = useLocalStorage("nds-2048-board", emptyBoard()); -const board = savedBoard.value.map((r) => [...r]); +const savedState = useLocalStorage("nds-2048", { + board: emptyBoard(), + score: 0, + highScore: 0, +}); +const board = savedState.value.board.map((r) => [...r]); +let score = savedState.value.score; -const saveBoard = () => { - savedBoard.value = board.map((r) => [...r]); +const saveState = () => { + savedState.value.board = board.map((r) => [...r]); + savedState.value.score = score; + if (score > savedState.value.highScore) { + savedState.value.highScore = score; + } }; type VisualTile = { value: number; x: number; y: number; scale: number }; @@ -115,6 +128,8 @@ const animateSpawnAll = () => { onRender((ctx) => { assets.images.home.topScreen.background.draw(ctx, 0, 0); ctx.textBaseline = "top"; + + ctx.save(); ctx.translate(BOARD_X, BOARD_Y); assets.images.settings.bottomScreen.options._2048.frame.draw(ctx, -3, -3); @@ -158,6 +173,22 @@ onRender((ctx) => { ); ctx.restore(); } + ctx.restore(); + + ctx.font = "7px NDS7"; + ctx.fillStyle = "#010101"; + + // score + ctx.fillText("Score:", SCORE_X, SCORE_Y); + ctx.fillText(score.toString(), SCORE_X, SCORE_Y + 16); + + // high score + ctx.fillText("High:", SCORE_X, HIGH_SCORE_Y); + ctx.fillText( + savedState.value.highScore.toString(), + SCORE_X, + HIGH_SCORE_Y + 16, + ); }); const getCell = (row: number, col: number) => board[row]![col]!; @@ -209,8 +240,10 @@ const merge = (rowDir: number, colDir: number) => { getCell(row, col) !== 0 && getCell(row, col) === getCell(nextRow, nextCol) ) { - setCell(row, col, getCell(row, col) * 2); + const merged = getCell(row, col) * 2; + setCell(row, col, merged); setCell(nextRow, nextCol, 0); + score += merged; } } } @@ -237,9 +270,10 @@ const resetBoard = () => { } } + score = 0; spawnTile(); spawnTile(); - saveBoard(); + saveState(); buildTilesFromBoard(); animateSpawnAll(); }; @@ -380,7 +414,7 @@ const slide = (rowDir: number, colDir: number) => { } const spawned = spawnTile(); - saveBoard(); + saveState(); if (isDead()) { buildTilesFromBoard(); @@ -440,7 +474,7 @@ useKeyDown((key) => { switch (key) { // TODO: remove this, testing only case "n": - savedBoard.value = [ + savedState.value.board = [ [0, 0, 2, 4], [8, 16, 32, 64], [128, 256, 512, 1024], diff --git a/public/nds/images/settings/bottom-screen/options/2048/frame.webp b/public/nds/images/settings/bottom-screen/options/2048/frame.webp new file mode 100644 index 0000000000000000000000000000000000000000..7a5c6f196019e30c35b3ec4b4c6fb23b25813a9a GIT binary patch literal 188 zcmWIYbaUInz`zjh>J$(bVBxbI$kyL-KtaIYVMSx%FZHj=4J#dCEX6AaEdEaZC3xUW zJwwAJk4