import {ref, watch,computed} from "vue"; import {gooseGrowthTypeList} from "./constantConfig.js"; import {onHide} from "@dcloudio/uni-app" const audioList = { 0: [ '/static/mp3/dan/01.MP3', '/static/mp3/dan/02.MP3', '/static/mp3/dan/03.MP3', ], 1: [ '/static/mp3/xiaoe/01.MP3', '/static/mp3/xiaoe/02.MP3', '/static/mp3/xiaoe/03.MP3', '/static/mp3/xiaoe/04.MP3', ], 2: [ '/static/mp3/zhonge/01.MP3', '/static/mp3/zhonge/02.MP3', '/static/mp3/zhonge/03.MP3', '/static/mp3/zhonge/04.MP3', ], 3: [ '/static/mp3/dae/01.MP3', '/static/mp3/dae/02.MP3', '/static/mp3/dae/03.MP3', '/static/mp3/dae/04.MP3', ], } const imageList = { 0: ['/static/images/game/game-img1.gif', '/static/images/game/game-eat-img1.gif'], 1: ['/static/images/game/game-img2.gif', '/static/images/game/game-eat-img2.gif'], 2: ['/static/images/game/game-img3.gif', '/static/images/game/game-eat-img3.gif'], 3: ['/static/images/game/game-img4.gif', '/static/images/game/game-eat-img4.gif'], } export function useAudio (grouthType) { const defaultGrouthType = ref(''); // 成长状态历史 const audioIndex = ref(0); // 播放序列 const innerAudioContext = uni.createInnerAudioContext(); innerAudioContext.autoplay = false; innerAudioContext.src = audioList[grouthType.value][0]; innerAudioContext.onPlay(() => { // 播放 uni.$emit('play-audio'); }); innerAudioContext.onError((res) => { console.log(res.errMsg); console.log(res.errCode); }); innerAudioContext.onEnded(() => { // 播放结束 uni.$emit('play-audio-ended') // 计算播放音频序列 const list = audioList[grouthType.value]; if (audioIndex.value >= list.length-1) { audioIndex.value = 0; } else { audioIndex.value = audioIndex.value+1; } // 更新音频源 changeSource(list[audioIndex.value]); }) function changeSource(url) { innerAudioContext.src= url; innerAudioContext.currentTime = 0; } onHide(() => { innerAudioContext.stop(); }) // 展开食物停止播放 uni.$on('food-select-open', () => { innerAudioContext.stop(); audioIndex.value = 0; }) watch(grouthType, () => { if (defaultGrouthType.value != grouthType.value) { const list = audioList[grouthType.value]; // 当鹅成长状态变更时重置播放序列 audioIndex.value = 0; defaultGrouthType.value = grouthType.value; changeSource(audioList[grouthType.value][0]); } }) return { innerAudioContext, } } export function useHuDong(props) { const imgUrl = ref(''); function getDefaultImg() { const active = gooseGrowthTypeList.find(item => item.id == props.growthType); return active ? active.imgUrl: '' } // 初始化 function init() { imgUrl.value = getDefaultImg(); } function doTouch(myAudio) { myAudio.play(); } // 播放开始切换动画 uni.$on('play-audio', () => { imgUrl.value = imageList[props.growthType][1] }) // 播放结束恢复默认 uni.$on('play-audio-ended', () => { imgUrl.value = imageList[props.growthType][0] }) // 展开食物恢复默认 uni.$on('food-select-open', () => { imgUrl.value = imageList[props.growthType][0] }) return { init, doTouch, imgUrl } }