UNPKG

@dcl/ecs

Version:
27 lines (26 loc) 1 kB
import { AudioSource } from '../generated/index.gen'; export function defineAudioSourceComponent(engine) { const theComponent = AudioSource(engine); return { ...theComponent, playSound(entity, src, resetCursor = true) { // Get the mutable to modify const audioSource = theComponent.getMutableOrNull(entity); if (!audioSource) return false; audioSource.audioClipUrl = src; audioSource.playing = true; audioSource.currentTime = resetCursor ? 0 : audioSource.currentTime; return true; }, stopSound(entity, resetCursor = true) { // Get the mutable to modify const audioSource = theComponent.getMutableOrNull(entity); if (!audioSource) return false; audioSource.playing = false; audioSource.currentTime = resetCursor ? 0 : audioSource.currentTime; return true; } }; }