Skip to content

Commit

Permalink
fix hits playing forever when closing the hits view while still playi…
Browse files Browse the repository at this point in the history
…ng a hit (closes #10)
  • Loading branch information
Timtam committed Aug 1, 2024
1 parent 507c54b commit b9b9fd5
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions client/src/pages/game/hit-player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import Button from "react-bootstrap/Button"
import { useTranslation } from "react-i18next"
import { Events, Sfx } from "../../events"

interface HitPlayerTimers {
sfxTimer: ReturnType<typeof setTimeout> | null
stopTimer: ReturnType<typeof setTimeout> | null
}

export type HitPlayerProps = {
src: string
duration: number
Expand All @@ -31,14 +36,17 @@ export const HitPlayer = forwardRef<HitPlayerRef, HitPlayerProps>(
) {
let player = useRef<Howl | null>(null)
let [playing, setPlaying] = useState(false)
let timer = useRef<ReturnType<typeof setTimeout> | null>(null)
let timers = useRef<HitPlayerTimers>({
sfxTimer: null,
stopTimer: null,
} satisfies HitPlayerTimers)
let { t } = useTranslation()
let [volume] = useLocalStorage("musicVolume", "1.0")
let [sfxVolume] = useLocalStorage("sfxVolume", "1.0")

const play = () => {
if (timer.current) {
clearTimeout(timer.current)
if (timers.current.stopTimer) {
clearTimeout(timers.current.stopTimer)
}
player.current?.stop()
let plr = new Howl({
Expand All @@ -52,13 +60,16 @@ export const HitPlayer = forwardRef<HitPlayerRef, HitPlayerProps>(
})
player.current = plr
if (parseFloat(sfxVolume) > 0) {
setTimeout(() => plr.play(), 250)
timers.current.sfxTimer = setTimeout(() => {
plr.play()
timers.current.sfxTimer = null
}, 250)
EventManager.publish(Events.playSfx, { sfx: Sfx.playHit })
} else {
plr.play()
}
if (duration > 0)
timer.current = setTimeout(() => {
timers.current.stopTimer = setTimeout(() => {
setPlaying(false)
}, duration * 1000)
if (onPlay !== undefined) onPlay()
Expand Down Expand Up @@ -93,9 +104,13 @@ export const HitPlayer = forwardRef<HitPlayerRef, HitPlayerProps>(
if (playing === true) {
play()
} else {
if (timer.current) {
clearTimeout(timer.current)
timer.current = null
if (timers.current.stopTimer) {
clearTimeout(timers.current.stopTimer)
timers.current.stopTimer = null
}
if (timers.current.sfxTimer) {
clearTimeout(timers.current.sfxTimer)
timers.current.sfxTimer = null
}
player.current?.pause()
if (
Expand All @@ -115,9 +130,13 @@ export const HitPlayer = forwardRef<HitPlayerRef, HitPlayerProps>(

useEffect(() => {
return () => {
if (timer.current) {
clearTimeout(timer.current)
timer.current = null
if (timers.current.stopTimer) {
clearTimeout(timers.current.stopTimer)
timers.current.stopTimer = null
}
if (timers.current.sfxTimer) {
clearTimeout(timers.current.sfxTimer)
timers.current.sfxTimer = null
}
player.current?.pause()
player.current = null
Expand Down

0 comments on commit b9b9fd5

Please sign in to comment.