Skip to content

Commit b390570

Browse files
committed
countdown with sound
1 parent ecee6c7 commit b390570

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/main/client/src/feature/game/Board.jsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,33 @@ export function Board({
6565
let [forbidden_x, forbidden_y] = gameState.forbidden
6666
let dragging = useLayoutStore(state => state.dragging)
6767
let muted = useMuteStore(state => state.muted)
68-
let howler = useRef()
68+
let howler = useRef({})
6969
let end = gameHasEnded(gameState)
7070
let showMoveNumbers = ctrlKeyDown && (isKibitz(gameState, auth) || end)
71+
let howlerActive = useRef(false)
7172

72-
let playClickSound = useCallback(() => {
73+
let playSound = useCallback(({file, volume}) => {
74+
if (!howlerActive.current) {
75+
return
76+
}
7377
if (muted) {
7478
return
7579
}
76-
if (!howler.current) {
77-
howler.current = new Howl({
78-
src: [base + "/stone1.wav"],
79-
onloaderror: (id, error) => {
80-
throw new Error(id + ": " + error)
81-
},
80+
if (!howler.current[file]) {
81+
howler.current[file] = new Howl({
82+
src: [base + "/" + file + ".wav"],
83+
volume: volume,
8284
})
8385
}
84-
howler.current.play()
86+
return howler.current[file].play()
8587
}, [howler, muted])
8688

89+
useEffect(() => {
90+
if (!end && !counting && timeRemaining >= 1 && timeRemaining <= 9) {
91+
playSound({file: "" + timeRemaining, volume: 1})
92+
}
93+
}, [playSound, timeRemaining, end, counting])
94+
8795
useEffect(() => {
8896
let onKeyDown = (e) => {
8997
let activeElement = window.document.activeElement
@@ -160,6 +168,7 @@ export function Board({
160168
}, [context, setCursor_x, setCursor_y, board.length, dragging])
161169

162170
let onClick = useCallback(() => {
171+
howlerActive.current = true
163172
if (!context.canvasRef.current) {
164173
return
165174
}
@@ -206,12 +215,12 @@ export function Board({
206215
}))
207216
}
208217
resetCountdown()
209-
playClickSound()
218+
playSound({file: "stone1", volume: 0.04})
210219
stompClient.publish({
211220
destination: "/app/game/move",
212221
body: JSON.stringify(move),
213222
})
214-
}, [context, gameState, setGameState, auth, board, stompClient, counting, forbidden_x, forbidden_y, myColor, playClickSound, isCursorInBounds, showMoveNumbers, resetCountdown, end])
223+
}, [context, gameState, setGameState, auth, board, stompClient, counting, forbidden_x, forbidden_y, myColor, playSound, isCursorInBounds, showMoveNumbers, resetCountdown, end])
215224

216225
useEffect(() => {
217226
if (!context.canvasRef.current) {

src/main/client/src/feature/game/Game.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function Game() {
8787
destination: "/app/game/move",
8888
body: JSON.stringify({ x: -1, y: -1 }),
8989
})
90-
}, 100)
90+
}, Math.trunc(Math.random() * 200))
9191
}
9292
}, 1000)
9393
}, [setTimeRemaining, timesetting, stompClient])

0 commit comments

Comments
 (0)