clean up
This commit is contained in:
91
src/js/core/gameActions.js
Normal file
91
src/js/core/gameActions.js
Normal file
@@ -0,0 +1,91 @@
|
||||
// gameActions.js - Core game action functions
|
||||
import * as config from '../config.js';
|
||||
import * as state from './state.js';
|
||||
import * as ui from '../ui/ui.js';
|
||||
import * as timer from './timer.js';
|
||||
import audioManager from '../ui/audio.js';
|
||||
|
||||
// --- Core Game Actions ---
|
||||
|
||||
// Declare handleGameOver at the top level to avoid referencing before definition
|
||||
export function handleGameOver() {
|
||||
state.setGameState(config.GAME_STATES.OVER);
|
||||
audioManager.play('gameOver');
|
||||
timer.stopTimer(); // Ensure timer is stopped
|
||||
ui.updateGameButton();
|
||||
ui.renderPlayers(); // Update to show final state
|
||||
}
|
||||
|
||||
export function startGame() {
|
||||
if (state.getPlayers().length < 2) {
|
||||
alert('You need at least 2 players to start.');
|
||||
return;
|
||||
}
|
||||
if (state.getGameState() === config.GAME_STATES.SETUP || state.getGameState() === config.GAME_STATES.PAUSED) {
|
||||
state.setGameState(config.GAME_STATES.RUNNING);
|
||||
audioManager.play('gameStart');
|
||||
timer.startTimer();
|
||||
ui.updateGameButton();
|
||||
ui.renderPlayers(); // Ensure active timer styling is applied
|
||||
}
|
||||
}
|
||||
|
||||
export function pauseGame() {
|
||||
if (state.getGameState() === config.GAME_STATES.RUNNING) {
|
||||
state.setGameState(config.GAME_STATES.PAUSED);
|
||||
audioManager.play('gamePause');
|
||||
timer.stopTimer();
|
||||
ui.updateGameButton();
|
||||
ui.renderPlayers(); // Ensure active timer styling is removed
|
||||
}
|
||||
}
|
||||
|
||||
export function resumeGame() {
|
||||
if (state.getGameState() === config.GAME_STATES.PAUSED) {
|
||||
// Check if there's actually a player with time left
|
||||
if (state.findNextPlayerWithTime() === -1) {
|
||||
console.log("Cannot resume, no players have time left.");
|
||||
// Optionally set state to OVER here
|
||||
handleGameOver();
|
||||
return;
|
||||
}
|
||||
state.setGameState(config.GAME_STATES.RUNNING);
|
||||
audioManager.play('gameResume');
|
||||
timer.startTimer();
|
||||
ui.updateGameButton();
|
||||
ui.renderPlayers(); // Ensure active timer styling is applied
|
||||
}
|
||||
}
|
||||
|
||||
export function togglePauseResume() {
|
||||
const currentGameState = state.getGameState();
|
||||
if (currentGameState === config.GAME_STATES.RUNNING) {
|
||||
pauseGame();
|
||||
} else if (currentGameState === config.GAME_STATES.PAUSED) {
|
||||
resumeGame();
|
||||
} else if (currentGameState === config.GAME_STATES.SETUP) {
|
||||
startGame();
|
||||
} else if (currentGameState === config.GAME_STATES.OVER) {
|
||||
resetGame(); // Or just go back to setup? Let's reset.
|
||||
startGame();
|
||||
}
|
||||
}
|
||||
|
||||
export function resetGame() {
|
||||
timer.stopTimer(); // Stop timer if running/paused
|
||||
state.resetPlayersTime();
|
||||
state.setGameState(config.GAME_STATES.SETUP);
|
||||
state.setCurrentPlayerIndex(0); // Go back to first player
|
||||
audioManager.play('buttonClick'); // Or a specific reset sound?
|
||||
ui.updateGameButton();
|
||||
ui.renderPlayers();
|
||||
}
|
||||
|
||||
export function fullResetApp() {
|
||||
timer.stopTimer();
|
||||
state.resetToDefaults();
|
||||
audioManager.play('gameOver'); // Use game over sound for full reset
|
||||
ui.hideResetModal();
|
||||
ui.updateGameButton();
|
||||
ui.renderPlayers();
|
||||
}
|
||||
Reference in New Issue
Block a user