feat : add sounds

This commit is contained in:
GuillaumeSD
2024-03-20 04:57:37 +01:00
parent 32fc196e74
commit 9e497cb24b
15 changed files with 74 additions and 26 deletions

35
src/lib/sounds.ts Normal file
View File

@@ -0,0 +1,35 @@
import { Move } from "chess.js";
import { getWhoIsCheckmated, isCheck } from "./chess";
const playSound = async (url: string) => {
const audio = new Audio(url);
await audio.play();
};
export const playCaptureSound = () => playSound("/sounds/capture.webm");
export const playCastleSound = () => playSound("/sounds/castle.webm");
export const playGameEndSound = () => playSound("/sounds/game-end.webm");
export const playGameStartSound = () => playSound("/sounds/game-start.webm");
export const playIllegalMoveSound = () =>
playSound("/sounds/illegal-move.webm");
export const playMoveCheckSound = () => playSound("/sounds/move-check.webm");
export const playMoveSound = () => playSound("/sounds/move.webm");
export const playPromoteSound = () => playSound("/sounds/promote.webm");
export const playSoundFromMove = async (move: Move | null) => {
if (!move) {
playIllegalMoveSound();
} else if (getWhoIsCheckmated(move.after)) {
playGameEndSound();
} else if (isCheck(move.after)) {
playMoveCheckSound();
} else if (move.promotion) {
playPromoteSound();
} else if (move.captured) {
playCaptureSound();
} else if (move.flags.includes("k") || move.flags.includes("q")) {
playCastleSound();
} else {
playMoveSound();
}
};