Add win condition UI and zero-G grid level
This commit is contained in:
40
ui.js
40
ui.js
@@ -14,11 +14,18 @@
|
||||
const restartBtn = document.getElementById("restart-btn");
|
||||
const pauseBtn = document.getElementById("pause-btn");
|
||||
const pauseOverlay = document.getElementById("pause-overlay");
|
||||
const goalLabelEl = document.getElementById("goal-label");
|
||||
const goalProgressEl = document.getElementById("goal-progress");
|
||||
const winEl = document.getElementById("win-overlay");
|
||||
const winMessageEl = document.getElementById("win-message");
|
||||
const winNextBtn = document.getElementById("win-next");
|
||||
const winRestartBtn = document.getElementById("win-restart");
|
||||
|
||||
const handlers = {
|
||||
onPauseToggle: null,
|
||||
onRestart: null,
|
||||
onSceneChange: null,
|
||||
onWinNext: null,
|
||||
};
|
||||
|
||||
if (pauseBtn) {
|
||||
@@ -39,6 +46,18 @@
|
||||
});
|
||||
}
|
||||
|
||||
if (winNextBtn) {
|
||||
winNextBtn.addEventListener("click", () => {
|
||||
if (handlers.onWinNext) handlers.onWinNext();
|
||||
});
|
||||
}
|
||||
|
||||
if (winRestartBtn) {
|
||||
winRestartBtn.addEventListener("click", () => {
|
||||
if (handlers.onRestart) handlers.onRestart();
|
||||
});
|
||||
}
|
||||
|
||||
window.addEventListener("keydown", (e) => {
|
||||
if (e.key === "Escape" && handlers.onPauseToggle) {
|
||||
handlers.onPauseToggle();
|
||||
@@ -78,6 +97,24 @@
|
||||
}
|
||||
};
|
||||
|
||||
const setGoal = ({ label, progress }) => {
|
||||
if (goalLabelEl) goalLabelEl.textContent = label || "—";
|
||||
if (goalProgressEl)
|
||||
goalProgressEl.style.width = `${Math.max(
|
||||
0,
|
||||
Math.min(100, progress ?? 0),
|
||||
)}%`;
|
||||
};
|
||||
|
||||
const showWin = (message) => {
|
||||
if (winMessageEl) winMessageEl.textContent = message || "You win!";
|
||||
if (winEl) winEl.classList.add("visible");
|
||||
};
|
||||
|
||||
const hideWin = () => {
|
||||
if (winEl) winEl.classList.remove("visible");
|
||||
};
|
||||
|
||||
const showGameOver = (score) => {
|
||||
if (finalScoreEl) finalScoreEl.textContent = score;
|
||||
if (gameOverEl) gameOverEl.classList.add("visible");
|
||||
@@ -146,9 +183,12 @@
|
||||
setPauseState,
|
||||
showGameOver,
|
||||
hideGameOver,
|
||||
showWin,
|
||||
hideWin,
|
||||
setSceneOptions,
|
||||
setSceneSelection,
|
||||
setHandlers,
|
||||
setGoal,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user