feat : add sounds
This commit is contained in:
35
src/lib/sounds.ts
Normal file
35
src/lib/sounds.ts
Normal 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();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user