This commit is contained in:
43
client/app/composables/use-sfx.ts
Normal file
43
client/app/composables/use-sfx.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { createSharedComposable } from '@vueuse/core'
|
||||
import { Howl, Howler } from 'howler'
|
||||
|
||||
const CONNECTION_SOUNDS = Object.keys(import.meta.glob('@/../public/sfx/connection/*.ogg')).map(path => path.replace('../public', ''))
|
||||
|
||||
console.log('CONNECTION_SOUNDS', CONNECTION_SOUNDS)
|
||||
|
||||
function hashStringToNumber(str: string, cap: number): number {
|
||||
let hash = 0
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
hash = (hash * 31 + str.charCodeAt(i)) | 0
|
||||
}
|
||||
return Math.abs(hash) % cap
|
||||
}
|
||||
|
||||
export const useSfx = createSharedComposable(() => {
|
||||
async function play(src: string, volume = 0.2): Promise<void> {
|
||||
Howler.stop()
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const howl = new Howl({
|
||||
src,
|
||||
autoplay: true,
|
||||
loop: false,
|
||||
volume,
|
||||
})
|
||||
|
||||
howl.on('end', () => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function playRandomConnectionSound(seed: string) {
|
||||
await play('/sfx/on_trans.ogg', 0.03)
|
||||
await play(CONNECTION_SOUNDS[hashStringToNumber(seed, CONNECTION_SOUNDS.length + 1)]!, 0.1)
|
||||
}
|
||||
|
||||
return {
|
||||
play,
|
||||
playRandomConnectionSound,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user