Skip to content

Commit d412998

Browse files
Christopher Straussh908714124
Christopher Strauss
authored andcommitted
sound on and off
1 parent efd04f3 commit d412998

File tree

5 files changed

+84
-16
lines changed

5 files changed

+84
-16
lines changed

src/main/client/package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"dependencies": {
1414
"@stomp/stompjs": "^7.0.0",
15+
"howler": "^2.2.4",
1516
"immer": "^10.1.1",
1617
"react": "^18.2.0",
1718
"react-click-away-listener": "^2.2.3",

src/main/client/public/stone1.wav

218 KB
Binary file not shown.

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

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import {
2424
useAuthStore,
2525
useGameStore,
26+
useMuteStore,
2627
} from "../../store.js"
2728
import {
2829
useLayoutStore,
@@ -37,6 +38,16 @@ import {
3738
import {
3839
GamePanel,
3940
} from "./GamePanel.jsx"
41+
import {
42+
Howl,
43+
} from "howler"
44+
import {
45+
FaVolumeMute,
46+
FaVolumeUp,
47+
} from "react-icons/fa"
48+
import {
49+
IconContext,
50+
} from "react-icons"
4051

4152
export const Game = () => {
4253
let [cursor_x, setCursor_x] = useState(-1)
@@ -65,6 +76,14 @@ export const Game = () => {
6576
let sidebarWidth = useLayoutStore(state => state.sidebarWidth.game)
6677
let vw = useLayoutStore(state => state.vw)
6778
let dragging = useLayoutStore(state => state.dragging)
79+
let muted = useMuteStore(state => state.muted)
80+
let setMuteState = useMuteStore((state) => state.setMuted)
81+
let sound = useMemo(() => new Howl({
82+
src: ["/app/stone1.wav"],
83+
onloaderror: function (id, error) {
84+
throw new Error(id + ": " + error)
85+
}
86+
}),[])
6887

6988
let context = useMemo(() => {
7089
let dim = board.length
@@ -184,11 +203,22 @@ export const Game = () => {
184203
if (!isSelfPlay()) { // myColor is 0 in self play
185204
addMove({...move, color: myColor})
186205
}
206+
if (!muted) {
207+
sound.play();
208+
}
187209
stompClient.publish({
188210
destination: "/app/game/move",
189211
body: JSON.stringify(move),
190212
})
191-
}, [context, currentPlayer, currentColor, auth, board, stompClient, counting, forbidden_x, forbidden_y, gameHasEnded, movesLength, addMove, isSelfPlay, myColor])
213+
}, [context, currentPlayer, currentColor, auth, board, stompClient, counting, forbidden_x, forbidden_y, gameHasEnded, movesLength, addMove, isSelfPlay, myColor, muted])
214+
215+
let onMuteClick = useCallback(() => {
216+
if (muted) {
217+
setMuteState(false)
218+
} else {
219+
setMuteState(true)
220+
}
221+
}, [setMuteState, muted])
192222

193223
useEffect(() => {
194224
if (!board.length) {
@@ -256,22 +286,39 @@ export const Game = () => {
256286
}
257287

258288
return (
259-
<div
260-
style={{width: (vw - sidebarWidth) + "px"}}
261-
className="h-full">
262-
<div className="grid h-full">
263-
<canvas className="place-self-center" ref={canvasRef}
264-
onMouseLeave={() => {
265-
setCursor_x(-1)
266-
setCursor_y(-1)
267-
}}
268-
onMouseMove={onMouseMove}
269-
onClick={onClick}
270-
width={context.width} height={context.width}>
271-
</canvas>
289+
<div
290+
style={{ width: (vw - sidebarWidth) + "px" }}
291+
className="h-full">
292+
<div className="grid h-full">
293+
<canvas className="place-self-center" ref={canvasRef}
294+
onMouseLeave={() => {
295+
setCursor_x(-1)
296+
setCursor_y(-1)
297+
}}
298+
onMouseMove={onMouseMove}
299+
onClick={onClick}
300+
width={context.width} height={context.width}>
301+
</canvas>
302+
</div>
303+
<div
304+
style={{right: (sidebarWidth + 12) + "px"}}
305+
className="absolute bottom-4">
306+
<button onClick={onMuteClick}>
307+
<IconContext.Provider value={{
308+
size: "1.5em",
309+
className: "pl-[4px]",
310+
}}>
311+
{muted && (
312+
<FaVolumeMute />
313+
)}
314+
{!muted && (
315+
<FaVolumeUp />
316+
)}
317+
</IconContext.Provider>
318+
</button>
319+
</div>
320+
<GamePanel />
272321
</div>
273-
<GamePanel />
274-
</div>
275322
)
276323
}
277324

src/main/client/src/store.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ export const useAuthStore = create(
5252
),
5353
)
5454

55+
export const useMuteStore = create(
56+
persist(
57+
(set) => ({
58+
muted: false,
59+
setMuted: (mute) =>
60+
set(() => ({
61+
muted: mute
62+
}))
63+
}),
64+
{ name: "mute-storage" }
65+
)
66+
)
67+
5568
export const useGameStore = create((set, get) => ({
5669
id: "",
5770
moves: [],

0 commit comments

Comments
 (0)