添加声音
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<script setup>
|
||||
import { ref, watch, onMounted, onUnmounted } from "vue";
|
||||
import audioManager from "@/audioManager";
|
||||
import { MESSAGETYPES } from "@/constants";
|
||||
const props = defineProps({
|
||||
start: {
|
||||
type: Boolean,
|
||||
@@ -17,22 +19,44 @@ const props = defineProps({
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
onTimeIsUp: {
|
||||
type: Function,
|
||||
default: () => {},
|
||||
},
|
||||
});
|
||||
|
||||
const barColor = ref("#fed847");
|
||||
const remain = ref(props.total);
|
||||
const timer = ref(null);
|
||||
const sound = ref(true);
|
||||
const currentSound = ref("");
|
||||
const currentRound = ref(0);
|
||||
const currentRoundEnded = ref(true);
|
||||
|
||||
watch(
|
||||
() => props.tips,
|
||||
(newVal) => {
|
||||
if (newVal.includes("红队")) barColor.value = "#FF6060";
|
||||
if (newVal.includes("蓝队")) barColor.value = "#5FADFF";
|
||||
let key = "";
|
||||
if (newVal.includes("红队")) {
|
||||
barColor.value = "#FF6060";
|
||||
key = "请红方射击";
|
||||
}
|
||||
if (newVal.includes("蓝队")) {
|
||||
barColor.value = "#5FADFF";
|
||||
key = "请蓝方射击";
|
||||
}
|
||||
if (key && sound.value) {
|
||||
if (currentRoundEnded.value) {
|
||||
currentRound.value += 1;
|
||||
currentRoundEnded.value = false;
|
||||
if (currentRound.value === 1) audioManager.play("第一轮");
|
||||
if (currentRound.value === 2) audioManager.play("第二轮");
|
||||
if (currentRound.value === 3) audioManager.play("第三轮");
|
||||
if (currentRound.value === 4) audioManager.play("第四轮");
|
||||
if (currentRound.value === 5) audioManager.play("第五轮");
|
||||
setTimeout(() => {
|
||||
audioManager.play(key);
|
||||
}, 1000);
|
||||
} else {
|
||||
audioManager.play(key);
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
immediate: true,
|
||||
@@ -45,11 +69,7 @@ watch(
|
||||
if (timer.value) clearInterval(timer.value);
|
||||
remain.value = props.total;
|
||||
timer.value = setInterval(() => {
|
||||
if (remain.value > 0) {
|
||||
remain.value--;
|
||||
} else {
|
||||
props.onTimeIsUp();
|
||||
}
|
||||
if (remain.value > 0) remain.value--;
|
||||
}, 1000);
|
||||
}
|
||||
);
|
||||
@@ -62,15 +82,9 @@ watch(
|
||||
}
|
||||
if (newVal) {
|
||||
remain.value = props.total;
|
||||
// setTimeout(() => {
|
||||
timer.value = setInterval(() => {
|
||||
if (remain.value > 0) {
|
||||
remain.value--;
|
||||
} else {
|
||||
props.onTimeIsUp();
|
||||
}
|
||||
if (remain.value > 0) remain.value--;
|
||||
}, 1000);
|
||||
// }, 3000);
|
||||
} else {
|
||||
if (timer.value) clearInterval(timer.value);
|
||||
}
|
||||
@@ -84,20 +98,59 @@ const updateRemain = (value) => {
|
||||
if (timer.value) clearInterval(timer.value);
|
||||
remain.value = Math.floor(value);
|
||||
timer.value = setInterval(() => {
|
||||
if (remain.value > 0) {
|
||||
remain.value--;
|
||||
} else {
|
||||
props.onTimeIsUp();
|
||||
}
|
||||
if (remain.value > 0) remain.value--;
|
||||
}, 1000);
|
||||
};
|
||||
|
||||
const updateSound = () => {
|
||||
sound.value = !sound.value;
|
||||
if (!sound.value) audioManager.stop(currentSound.value);
|
||||
};
|
||||
|
||||
async function onReceiveMessage(messages = []) {
|
||||
if (!sound.value) return;
|
||||
messages.forEach((msg) => {
|
||||
if (
|
||||
msg.constructor === MESSAGETYPES.ShootSyncMeArrowID ||
|
||||
msg.constructor === MESSAGETYPES.ShootResult
|
||||
) {
|
||||
if (msg.target) {
|
||||
currentSound.value = msg.target.ring
|
||||
? `${msg.target.ring}环`
|
||||
: "未上靶";
|
||||
audioManager.play(currentSound.value);
|
||||
}
|
||||
} else if (msg.constructor === MESSAGETYPES.AllReady) {
|
||||
audioManager.play("比赛开始");
|
||||
} else if (msg.constructor === MESSAGETYPES.MeleeAllReady) {
|
||||
audioManager.play("比赛开始");
|
||||
} else if (msg.constructor === MESSAGETYPES.CurrentRoundEnded) {
|
||||
currentRoundEnded.value = true;
|
||||
} else if (msg.constructor === MESSAGETYPES.HalfTimeOver) {
|
||||
audioManager.play("中场休息");
|
||||
} else if (msg.constructor === MESSAGETYPES.MatchOver) {
|
||||
audioManager.play("比赛结束");
|
||||
} else if (msg.constructor === MESSAGETYPES.FinalShoot) {
|
||||
audioManager.play("决金箭轮");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const playSound = (key) => {
|
||||
currentSound.value = key;
|
||||
audioManager.play(key);
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
uni.$on("update-ramain", updateRemain);
|
||||
uni.$on("socket-inbox", onReceiveMessage);
|
||||
uni.$on("play-sound", playSound);
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
uni.$off("update-ramain", updateRemain);
|
||||
uni.$off("socket-inbox", onReceiveMessage);
|
||||
uni.$off("play-sound", playSound);
|
||||
if (timer.value) clearInterval(timer.value);
|
||||
});
|
||||
</script>
|
||||
@@ -107,7 +160,7 @@ onUnmounted(() => {
|
||||
<view>
|
||||
<image src="../static/shooter.png" mode="widthFix" />
|
||||
<text>{{ start && remain === 0 ? "时间到!" : tips }}</text>
|
||||
<button hover-class="none" @click="() => (sound = !sound)">
|
||||
<button hover-class="none" @click="updateSound">
|
||||
<image
|
||||
:src="`../static/sound${sound ? '' : '-off'}-yellow.png`"
|
||||
mode="widthFix"
|
||||
|
||||
Reference in New Issue
Block a user