Files
shoot-miniprograms/src/components/ShootProgress.vue

272 lines
6.7 KiB
Vue
Raw Normal View History

2025-05-10 16:57:36 +08:00
<script setup>
import { ref, watch, onMounted, onUnmounted } from "vue";
2025-07-14 16:37:56 +08:00
import audioManager from "@/audioManager";
import { MESSAGETYPES } from "@/constants";
2025-07-15 17:10:41 +08:00
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
2025-05-10 16:57:36 +08:00
const props = defineProps({
2025-07-19 16:16:53 +08:00
show: {
type: Boolean,
default: true,
},
2025-05-29 23:45:44 +08:00
start: {
type: Boolean,
default: false,
},
2025-05-10 16:57:36 +08:00
tips: {
type: String,
default: "",
},
total: {
type: Number,
2025-07-16 17:55:11 +08:00
default: 120,
2025-05-10 16:57:36 +08:00
},
2025-06-05 21:32:51 +08:00
seq: {
type: Number,
default: 0,
},
2025-07-14 22:39:53 +08:00
currentRound: {
type: Number,
2025-07-16 17:55:11 +08:00
default: 0,
2025-07-14 22:39:53 +08:00
},
battleId: {
type: String,
default: "",
},
2025-07-15 17:10:41 +08:00
melee: {
type: Boolean,
default: false,
},
2025-05-10 16:57:36 +08:00
});
2025-05-29 23:45:44 +08:00
2025-06-11 23:57:17 +08:00
const barColor = ref("#fed847");
2025-05-29 23:45:44 +08:00
const remain = ref(props.total);
2025-06-05 21:32:51 +08:00
const timer = ref(null);
2025-06-28 20:51:50 +08:00
const sound = ref(true);
2025-07-14 16:37:56 +08:00
const currentSound = ref("");
2025-07-14 22:39:53 +08:00
const currentRound = ref(props.currentRound);
const currentRoundEnded = ref(!props.battleId);
2025-07-15 18:24:18 +08:00
const ended = ref(false);
2025-07-16 17:55:11 +08:00
const halfTime = ref(false);
2025-06-05 21:32:51 +08:00
2025-06-11 23:57:17 +08:00
watch(
() => props.tips,
(newVal) => {
2025-07-14 16:37:56 +08:00
let key = "";
2025-07-14 22:39:53 +08:00
if (newVal.includes("红队")) key = "请红方射击";
if (newVal.includes("蓝队")) key = "请蓝方射击";
2025-07-14 16:37:56 +08:00
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);
}
}
2025-07-14 22:39:53 +08:00
}
);
watch(
() => props.tips,
(newVal) => {
let key = "";
if (newVal.includes("红队")) barColor.value = "#FF6060";
if (newVal.includes("蓝队")) barColor.value = "#5FADFF";
},
{
immediate: true,
2025-06-11 23:57:17 +08:00
}
);
2025-06-05 21:32:51 +08:00
watch(
2025-07-21 10:40:43 +08:00
() => [props.seq],
2025-06-05 21:32:51 +08:00
() => {
if (timer.value) clearInterval(timer.value);
remain.value = props.total;
timer.value = setInterval(() => {
2025-07-14 16:37:56 +08:00
if (remain.value > 0) remain.value--;
2025-06-05 21:32:51 +08:00
}, 1000);
}
);
2025-05-29 23:45:44 +08:00
watch(
() => props.start,
2025-07-13 14:57:16 +08:00
(newVal) => {
2025-07-16 12:09:27 +08:00
if (timer.value) clearInterval(timer.value);
2025-07-13 14:57:16 +08:00
if (newVal) {
2025-07-05 14:52:41 +08:00
remain.value = props.total;
2025-07-11 00:47:34 +08:00
timer.value = setInterval(() => {
2025-07-14 16:37:56 +08:00
if (remain.value > 0) remain.value--;
2025-07-11 00:47:34 +08:00
}, 1000);
2025-05-10 16:57:36 +08:00
}
2025-06-25 00:09:53 +08:00
},
{
immediate: true,
2025-05-29 23:45:44 +08:00
}
);
2025-06-19 01:55:40 +08:00
const updateRemain = (value) => {
2025-07-13 14:57:16 +08:00
if (timer.value) clearInterval(timer.value);
2025-07-23 11:18:47 +08:00
remain.value = Math.round(value);
2025-07-16 12:09:27 +08:00
if (remain.value > 0) {
timer.value = setInterval(() => {
if (remain.value > 0) remain.value--;
}, 1000);
}
};
2025-07-14 16:37:56 +08:00
const updateSound = () => {
sound.value = !sound.value;
if (!sound.value) audioManager.stop(currentSound.value);
};
async function onReceiveMessage(messages = []) {
2025-07-15 18:24:18 +08:00
if (!sound.value || ended.value) return;
2025-07-14 16:37:56 +08:00
messages.forEach((msg) => {
if (
2025-07-14 22:39:53 +08:00
(props.battleId && msg.constructor === MESSAGETYPES.ShootResult) ||
(!props.battleId && msg.constructor === MESSAGETYPES.ShootSyncMeArrowID)
2025-07-14 16:37:56 +08:00
) {
2025-07-15 17:10:41 +08:00
if (props.melee && msg.userId !== user.value.id) return;
2025-07-16 17:55:11 +08:00
if (!halfTime.value && msg.target) {
2025-07-14 16:37:56 +08:00
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) {
2025-07-16 17:55:11 +08:00
halfTime.value = false;
2025-07-16 15:32:49 +08:00
audioManager.play("比赛开始");
2025-07-14 16:37:56 +08:00
} else if (msg.constructor === MESSAGETYPES.CurrentRoundEnded) {
currentRoundEnded.value = true;
} else if (msg.constructor === MESSAGETYPES.HalfTimeOver) {
2025-07-16 17:55:11 +08:00
halfTime.value = true;
2025-07-14 16:37:56 +08:00
audioManager.play("中场休息");
} else if (msg.constructor === MESSAGETYPES.MatchOver) {
audioManager.play("比赛结束");
} else if (msg.constructor === MESSAGETYPES.FinalShoot) {
audioManager.play("决金箭轮");
2025-07-15 18:24:18 +08:00
} else if (msg.constructor === MESSAGETYPES.ShootSyncMePracticeID) {
ended.value = true;
} else if (msg.constructor === MESSAGETYPES.MatchOver) {
ended.value = true;
2025-07-14 16:37:56 +08:00
}
});
}
const playSound = (key) => {
currentSound.value = key;
audioManager.play(key);
};
onMounted(() => {
uni.$on("update-ramain", updateRemain);
2025-07-14 16:37:56 +08:00
uni.$on("socket-inbox", onReceiveMessage);
uni.$on("play-sound", playSound);
});
2025-06-19 01:55:40 +08:00
onUnmounted(() => {
uni.$off("update-ramain", updateRemain);
2025-07-14 16:37:56 +08:00
uni.$off("socket-inbox", onReceiveMessage);
uni.$off("play-sound", playSound);
2025-06-19 01:55:40 +08:00
if (timer.value) clearInterval(timer.value);
});
2025-05-10 16:57:36 +08:00
</script>
<template>
2025-07-19 16:16:53 +08:00
<view class="container" :style="{ display: show ? 'block' : 'none' }">
2025-05-10 16:57:36 +08:00
<view>
<image src="../static/shooter.png" mode="widthFix" />
2025-07-13 21:06:48 +08:00
<text>{{ start && remain === 0 ? "时间到!" : tips }}</text>
2025-07-14 16:37:56 +08:00
<button hover-class="none" @click="updateSound">
2025-06-28 20:51:50 +08:00
<image
:src="`../static/sound${sound ? '' : '-off'}-yellow.png`"
mode="widthFix"
/>
2025-05-10 16:57:36 +08:00
</button>
</view>
<view>
2025-05-16 15:56:54 +08:00
<view
:style="{
2025-06-19 01:55:40 +08:00
width: `${(remain / total) * 100}%`,
2025-05-16 15:56:54 +08:00
backgroundColor: barColor,
right: tips.includes('红队') ? 0 : 'unset',
}"
/>
2025-05-10 16:57:36 +08:00
<text>剩余{{ remain }}</text>
</view>
</view>
</template>
<style scoped>
.container {
width: 100vw;
}
.container > view:first-child {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 15px;
z-index: 1;
2025-06-17 12:50:59 +08:00
transform: translateX(-10px);
2025-05-10 16:57:36 +08:00
}
.container > view:first-child > image:first-child {
2025-07-23 14:31:21 +08:00
width: 22vw;
2025-06-18 21:30:54 +08:00
transform: translateX(10px);
2025-05-10 16:57:36 +08:00
}
2025-07-10 15:34:00 +08:00
.container > view:first-child > text {
color: #fed847;
font-size: 18px;
2025-07-23 14:31:21 +08:00
transform: translateY(-10px) translateX(-10px);
2025-07-10 15:34:00 +08:00
}
2025-07-17 16:14:30 +08:00
.container > view:first-child > button:last-child {
overflow: visible;
}
2025-05-10 16:57:36 +08:00
.container > view:first-child > button:last-child > image {
2025-07-10 15:34:00 +08:00
width: 40px;
transform: translateX(10px) translateY(-10px);
2025-05-10 16:57:36 +08:00
}
.container > view:last-child {
2025-07-23 14:31:21 +08:00
display: flex;
justify-content: center;
align-items: center;
2025-05-10 16:57:36 +08:00
z-index: -1;
width: clac(100% - 30px);
margin: 0 15px;
text-align: center;
background-color: #ffffff80;
border-radius: 20px;
2025-07-23 14:31:21 +08:00
margin-top: -16px;
2025-05-10 16:57:36 +08:00
font-size: 12px;
2025-07-23 14:31:21 +08:00
height: 15px;
line-height: 15px;
2025-05-10 16:57:36 +08:00
position: relative;
overflow: hidden;
}
.container > view:last-child > view {
position: absolute;
2025-07-23 14:31:21 +08:00
height: 15px;
border-radius: 15px;
2025-05-10 16:57:36 +08:00
z-index: -1;
2025-06-19 01:55:40 +08:00
transition: all 1s linear;
2025-05-10 16:57:36 +08:00
}
.container > view:last-child > text {
2025-07-23 14:31:21 +08:00
font-size: 10px;
line-height: 15px;
2025-05-10 16:57:36 +08:00
z-index: 1;
2025-06-21 22:22:19 +08:00
color: #000;
2025-05-10 16:57:36 +08:00
}
</style>