Fix: separa startNewGame da initGame, flusso corretto per salvataggio e ripristino
This commit is contained in:
1 parent
a389640ea0
commit
805bc6e1c7
1 file changed
+37
-7
@@ -240,6 +240,31 @@
|
||||
localStorage.removeItem(STORAGE_KEY_STATE);
|
||||
}
|
||||
|
||||
function startNewGame() {
|
||||
// Genera sempre una nuova partita, ignorando stato salvato
|
||||
clearGameState();
|
||||
const size = parseInt(gridSizeSelect.value);
|
||||
state.size = size;
|
||||
state.errors = state.maxErrors;
|
||||
state.catsPlaced = 0;
|
||||
state.totalCats = size;
|
||||
state.gameOver = false;
|
||||
state.won = false;
|
||||
state.hints = 3;
|
||||
state.levelStartTime = Date.now();
|
||||
state.levelTime = 0;
|
||||
|
||||
const puzzle = generatePuzzle(size);
|
||||
state.grid = puzzle.grid;
|
||||
state.solution = puzzle.solution;
|
||||
|
||||
updateUI();
|
||||
renderGrid();
|
||||
hideModal();
|
||||
startTimer();
|
||||
saveGameState();
|
||||
}
|
||||
|
||||
function initGame() {
|
||||
// Prima prova a caricare stato salvato
|
||||
const savedState = loadGameState();
|
||||
@@ -418,7 +443,7 @@
|
||||
state.gameOver = true;
|
||||
clearGameState();
|
||||
showModal('Game Over!', 'Hai esaurito tutti i tentativi. Riprova!', 'Ricomincia', () => {
|
||||
initGame();
|
||||
startNewGame();
|
||||
});
|
||||
}
|
||||
return;
|
||||
@@ -441,7 +466,7 @@
|
||||
showModal('Vittoria!',
|
||||
`Hai completato il livello ${state.level - 1}!\nTempo: ${formatTime(state.levelTime)}\nPunteggio: ${state.score}`,
|
||||
'Livello Successivo', () => {
|
||||
initGame();
|
||||
startNewGame(); // Nuovo livello, mantiene il nome
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -483,10 +508,13 @@
|
||||
|
||||
if (state.catsPlaced === state.totalCats) {
|
||||
state.won = true;
|
||||
stopTimer();
|
||||
state.score += state.errors * 10 + state.hints * 5;
|
||||
saveLevelRecord();
|
||||
clearGameState();
|
||||
state.level++;
|
||||
showModal('Vittoria!', 'Hai completato il livello! Punteggio: ' + state.score, 'Livello Successivo', () => {
|
||||
initGame();
|
||||
startNewGame();
|
||||
});
|
||||
}
|
||||
return;
|
||||
@@ -641,7 +669,8 @@
|
||||
state.score = 0;
|
||||
state.hints = 3;
|
||||
state.sessionRecords = [];
|
||||
clearGameState(); // Cancella stato salvato per nuova partita
|
||||
clearGameState();
|
||||
state.playerName = '';
|
||||
showNameInput();
|
||||
});
|
||||
|
||||
@@ -676,8 +705,7 @@
|
||||
playerNameDisplay.textContent = name;
|
||||
}
|
||||
hideNameInput();
|
||||
initGame();
|
||||
saveGameState(); // Salva subito dopo aver inserito il nome
|
||||
startNewGame(); // Nuova partita, non initGame che carica stato salvato
|
||||
}
|
||||
});
|
||||
|
||||
@@ -689,10 +717,12 @@
|
||||
|
||||
// Init - controlla se c'è una partita salvata
|
||||
const savedState = loadGameState();
|
||||
if (savedState && savedState.playerName && !savedState.gameOver && !savedState.won) {
|
||||
if (savedState && savedState.playerName && savedState.grid && savedState.grid.length > 0 && !savedState.gameOver && !savedState.won) {
|
||||
// Ripristina partita in corso, salta input nome
|
||||
initGame();
|
||||
} else {
|
||||
// Prima partita o stato invalido
|
||||
clearGameState();
|
||||
showNameInput();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user