update:优化精准射箭语音

This commit is contained in:
2026-08-20 17:57:22 +08:00
parent 6363dda204
commit 962596e510
6 changed files with 132 additions and 20 deletions
+44 -2
View File
@@ -6,11 +6,16 @@ import Avatar from "@/components/Avatar.vue";
import audioManager from "@/audioManager";
import { simulShootAPI } from "@/apis";
import { MESSAGETYPESV2 } from "@/constants";
import { getDistanceCheckAudioKey, getDistanceCheckText } from "@/util";
import {
getDistanceCheckAudioKey,
getDistanceCheckText,
getShootValidation,
} from "@/util";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user, device } = storeToRefs(store);
const emit = defineEmits(["passed"]);
const props = defineProps({
guide: {
type: Boolean,
@@ -28,6 +33,10 @@ const props = defineProps({
type: [Number, String],
default: "",
},
autoStart: {
type: Boolean,
default: false,
},
});
const arrow = ref({});
const distance = ref(0);
@@ -35,6 +44,29 @@ const statusText = ref("");
const showsimul = ref(false);
const count = ref(props.count);
const timer = ref(null);
const autoStartPending = ref(false);
const autoStartTriggered = ref(false);
let autoStartTimer = null;
const DISTANCE_PASSED_AUDIO_KEY = "站距合格,靶纸正确";
const AUTO_START_TIMEOUT_MS = 6000;
const clearAutoStartTimer = () => {
if (!autoStartTimer) return;
clearTimeout(autoStartTimer);
autoStartTimer = null;
};
const triggerAutoStart = () => {
if (!autoStartPending.value || autoStartTriggered.value) return;
clearAutoStartTimer();
autoStartPending.value = false;
autoStartTriggered.value = true;
emit("passed");
};
const onAudioEnded = (key) => {
if (key === DISTANCE_PASSED_AUDIO_KEY) triggerAutoStart();
};
const updateTimer = (value) => {
count.value = Math.round(value);
@@ -48,21 +80,31 @@ onMounted(() => {
}, 1000);
}
uni.$on("update-timer", updateTimer);
uni.$on("audioEnded", onAudioEnded);
});
onBeforeUnmount(() => {
if (timer.value) clearInterval(timer.value);
clearAutoStartTimer();
uni.$off("update-timer", updateTimer);
uni.$off("audioEnded", onAudioEnded);
});
async function onReceiveMessage(msg) {
if (Array.isArray(msg)) return;
if (msg.type === MESSAGETYPESV2.TestDistance) {
if (autoStartPending.value || autoStartTriggered.value) return;
const rawDistance = Number(msg.shootData?.distance ?? msg.shootData?.dst);
distance.value = Number.isFinite(rawDistance)
? Number((rawDistance / 100).toFixed(2))
: 0;
statusText.value = getDistanceCheckText(msg.shootData);
audioManager.play(getDistanceCheckAudioKey(msg.shootData));
const audioKey = getDistanceCheckAudioKey(msg.shootData);
const validation = getShootValidation(msg.shootData);
if (props.autoStart && validation.distanceOk && validation.targetOk) {
autoStartPending.value = true;
autoStartTimer = setTimeout(triggerAutoStart, AUTO_START_TIMEOUT_MS);
}
audioManager.play(audioKey);
}
}