Skip to content

Commit 8c65a12

Browse files
authored
Merge pull request #33 from ProLoser/feature/piece-sound-vibration
2 parents 6be85dc + 72232bd commit 8c65a12

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/Board/Piece.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useCallback, forwardRef, type DragEventHandler } from "react";
2+
import { playCheckerSound } from '../../Utils';
23
import black from './images/piece-black-2.png';
34
import white from './images/piece-white-2.png';
45
import './Piece.css'
@@ -13,6 +14,8 @@ type PieceProps = {
1314

1415
const Piece = forwardRef<HTMLImageElement, PieceProps>(({ color, position, onSelect }, ref) => {
1516
const onDragStart: DragEventHandler = useCallback((event) => {
17+
playCheckerSound();
18+
navigator.vibrate?.(10);
1619
if (onSelect) onSelect(null)
1720
switch (position) {
1821
case undefined: // Home

src/Board/Point.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useCallback, useRef, useState, type DragEventHandler } from "react";
2+
import { playCheckerSound } from '../../Utils';
23
import Piece from './Piece'
34

45
type PointProps = {
@@ -14,6 +15,8 @@ export default function Point({ pieces, move, position, onSelect, selected }: Po
1415
const pieceRef = useRef<HTMLImageElement>(null);
1516
const onDragOver: DragEventHandler = useCallback((event) => { event.preventDefault(); }, [])
1617
const onDrop: DragEventHandler = useCallback((event) => {
18+
playCheckerSound();
19+
navigator.vibrate?.(10);
1720
event.preventDefault();
1821
onSelect(null)
1922
let from = event.dataTransfer?.getData("text")!

src/Utils.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,21 @@ export function calculate(state: GameType, from: number | "white" | "black", to:
109109
nextGame.status = 'moved'
110110
}
111111
return { state: nextGame, moveLabel };
112-
}
112+
}
113+
114+
export const playCheckerSound = () => {
115+
const mp3Files = [
116+
'public/capture.mp3',
117+
'public/castle.mp3',
118+
'public/move-check.mp3',
119+
'public/move-self.mp3',
120+
'public/notify.mp3',
121+
'public/promote.mp3',
122+
];
123+
124+
const randomIndex = Math.floor(Math.random() * mp3Files.length);
125+
const randomMp3 = mp3Files[randomIndex];
126+
127+
const audio = new Audio(randomMp3);
128+
audio.play();
129+
};

0 commit comments

Comments
 (0)