/
playSound.ts
61 lines (55 loc) · 1.21 KB
/
playSound.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-disable @typescript-eslint/no-explicit-any */
import store from '@/store'
import loadSnd from 'audio-loader'
import playSnd from 'audio-play'
type SndName = 'btn' | 'catch' | 'jump' | 'gameover' | 'shot' | 'down' | 'lvup' | 'miss' | 'warn'
const sndNames: SndName[] = [
'btn',
'catch',
'jump',
'gameover',
'shot',
'down',
'lvup',
'miss',
'warn'
]
const snds: Partial<{ [key in SndName]: AudioBuffer }> = {}
const load = (name: SndName) => {
loadSnd(`/snd/${name}.mp3`).then(a => {
snds[name] = a
})
}
sndNames.forEach(name => load(name))
const isPlayAllowed = () => {
return store.state.system.initialTapped
}
const playSound = (name: SndName) => {
if (!isPlayAllowed()) {
return
}
const audio = snds[name]
if (!audio) {
// console.warn(`No sound for: ${name}`)
return
}
return playSnd(audio, {}, () => {
//
})
}
export const repeatSound = (name: SndName, times: number) => {
if (!isPlayAllowed()) {
return
}
const audio = snds[name]
if (!audio || times <= 0) {
// console.warn(`No sound for: ${name}`)
return
}
return playSnd(audio, {}, () => {
if (times >= 2) {
repeatSound(name, times - 1)
}
})
}
export default playSound