update: 修复飞行特效
This commit is contained in:
+108
-32
@@ -63,6 +63,10 @@ const props = defineProps({
|
||||
type: Number,
|
||||
default: 5,
|
||||
},
|
||||
stableShotEffect: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
});
|
||||
|
||||
const pMode = ref(true);
|
||||
@@ -73,12 +77,14 @@ const dirTimer = ref(null);
|
||||
const angle = ref(null);
|
||||
const circleColor = ref("");
|
||||
const shotEffect = ref(null);
|
||||
const pendingShotEffect = ref(null);
|
||||
const hiddenRedLatestKey = ref("");
|
||||
const hiddenBlueLatestKey = ref("");
|
||||
const targetShaking = ref(false);
|
||||
const targetRect = ref({ left: 0, top: 0, width: 0, height: 0 });
|
||||
const shakeTimer = ref(null);
|
||||
const instance = getCurrentInstance();
|
||||
let shotEffectRequestGeneration = 0;
|
||||
const ROUND_TIP_OFFSET_Y = -32;
|
||||
const EXPERIENCE_TIP_OFFSET_Y = -68;
|
||||
|
||||
@@ -135,7 +141,7 @@ function showShotTip(team, shot) {
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
function triggerShotEffect(team, shot, index) {
|
||||
function triggerShotEffect(team, shot, index, viewportMode = false) {
|
||||
const key = buildShotEffectKey(team, shot, index);
|
||||
|
||||
if (shotEffect.value?.team === "red") hiddenRedLatestKey.value = "";
|
||||
@@ -149,7 +155,29 @@ function triggerShotEffect(team, shot, index) {
|
||||
hiddenBlueLatestKey.value = key;
|
||||
}
|
||||
|
||||
shotEffect.value = { key, team, shot };
|
||||
shotEffect.value = { key, team, shot, viewportMode };
|
||||
}
|
||||
|
||||
async function prepareShotEffect(team, shot, index) {
|
||||
const requestGeneration = ++shotEffectRequestGeneration;
|
||||
const key = buildShotEffectKey(team, shot, index);
|
||||
pendingShotEffect.value = { generation: requestGeneration, team, key };
|
||||
clearTipTimer();
|
||||
if (team === "red") latestOne.value = null;
|
||||
if (team === "blue") bluelatestOne.value = null;
|
||||
|
||||
const viewportMode = props.stableShotEffect
|
||||
? await updateTargetRect()
|
||||
: false;
|
||||
|
||||
if (requestGeneration !== shotEffectRequestGeneration) {
|
||||
if (pendingShotEffect.value?.generation === requestGeneration) {
|
||||
pendingShotEffect.value = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
pendingShotEffect.value = null;
|
||||
triggerShotEffect(team, shot, index, viewportMode);
|
||||
}
|
||||
|
||||
function completeShotEffect(key) {
|
||||
@@ -178,44 +206,67 @@ function shakeTarget() {
|
||||
});
|
||||
}
|
||||
|
||||
function updateTargetRect() {
|
||||
nextTick(() => {
|
||||
const query = instance?.proxy
|
||||
? uni.createSelectorQuery().in(instance.proxy)
|
||||
: uni.createSelectorQuery();
|
||||
function hasValidTargetRect(rect = targetRect.value) {
|
||||
return (
|
||||
Number.isFinite(Number(rect?.left)) &&
|
||||
Number.isFinite(Number(rect?.top)) &&
|
||||
Number(rect?.width) > 0 &&
|
||||
Number(rect?.height) > 0
|
||||
);
|
||||
}
|
||||
|
||||
query
|
||||
.select(".target")
|
||||
.boundingClientRect((rect) => {
|
||||
const left = Number(rect?.left);
|
||||
const top = Number(rect?.top);
|
||||
const width = Number(rect?.width);
|
||||
const height = Number(rect?.height);
|
||||
if (
|
||||
!Number.isFinite(left) ||
|
||||
!Number.isFinite(top) ||
|
||||
!Number.isFinite(width) ||
|
||||
!Number.isFinite(height)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if (width <= 0 || height <= 0) return;
|
||||
targetRect.value = { left, top, width, height };
|
||||
})
|
||||
.exec();
|
||||
async function updateTargetRect() {
|
||||
await nextTick();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
let settled = false;
|
||||
const finish = (rect) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
|
||||
const isValid = hasValidTargetRect(rect);
|
||||
if (isValid) {
|
||||
targetRect.value = {
|
||||
left: Number(rect.left),
|
||||
top: Number(rect.top),
|
||||
width: Number(rect.width),
|
||||
height: Number(rect.height),
|
||||
};
|
||||
}
|
||||
resolve(isValid);
|
||||
};
|
||||
|
||||
try {
|
||||
const query = instance?.proxy
|
||||
? uni.createSelectorQuery().in(instance.proxy)
|
||||
: uni.createSelectorQuery();
|
||||
|
||||
query
|
||||
.select(".target")
|
||||
.boundingClientRect()
|
||||
.exec((result) => finish(Array.isArray(result) ? result[0] : null));
|
||||
} catch {
|
||||
finish(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function handleWindowResize() {
|
||||
updateTargetRect();
|
||||
void updateTargetRect();
|
||||
}
|
||||
|
||||
function shouldHideRedHit(index) {
|
||||
return !!hiddenRedLatestKey.value && index === props.scores.length - 1;
|
||||
return (
|
||||
(!!hiddenRedLatestKey.value || pendingShotEffect.value?.team === "red") &&
|
||||
index === props.scores.length - 1
|
||||
);
|
||||
}
|
||||
|
||||
function shouldHideBlueHit(index) {
|
||||
return !!hiddenBlueLatestKey.value && index === props.blueScores.length - 1;
|
||||
return (
|
||||
(!!hiddenBlueLatestKey.value || pendingShotEffect.value?.team === "blue") &&
|
||||
index === props.blueScores.length - 1
|
||||
);
|
||||
}
|
||||
|
||||
watch(
|
||||
@@ -224,14 +275,18 @@ watch(
|
||||
if (newLen === oldLen + 1) {
|
||||
const latestShot = props.scores[newLen - 1];
|
||||
if (shouldPlayShotEffect(latestShot)) {
|
||||
triggerShotEffect("red", latestShot, newLen - 1);
|
||||
void prepareShotEffect("red", latestShot, newLen - 1);
|
||||
} else {
|
||||
shotEffectRequestGeneration += 1;
|
||||
pendingShotEffect.value = null;
|
||||
showShotTip("red", latestShot);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (newLen < oldLen) {
|
||||
shotEffectRequestGeneration += 1;
|
||||
pendingShotEffect.value = null;
|
||||
latestOne.value = null;
|
||||
hiddenRedLatestKey.value = "";
|
||||
if (shotEffect.value?.team === "red") shotEffect.value = null;
|
||||
@@ -245,14 +300,18 @@ watch(
|
||||
if (newLen === oldLen + 1) {
|
||||
const latestShot = props.blueScores[newLen - 1];
|
||||
if (shouldPlayShotEffect(latestShot)) {
|
||||
triggerShotEffect("blue", latestShot, newLen - 1);
|
||||
void prepareShotEffect("blue", latestShot, newLen - 1);
|
||||
} else {
|
||||
shotEffectRequestGeneration += 1;
|
||||
pendingShotEffect.value = null;
|
||||
showShotTip("blue", latestShot);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (newLen < oldLen) {
|
||||
shotEffectRequestGeneration += 1;
|
||||
pendingShotEffect.value = null;
|
||||
bluelatestOne.value = null;
|
||||
hiddenBlueLatestKey.value = "";
|
||||
if (shotEffect.value?.team === "blue") shotEffect.value = null;
|
||||
@@ -411,11 +470,13 @@ async function onReceiveMessage(message) {
|
||||
|
||||
onMounted(() => {
|
||||
uni.$on("socket-inbox", onReceiveMessage);
|
||||
updateTargetRect();
|
||||
void updateTargetRect();
|
||||
if (uni.onWindowResize) uni.onWindowResize(handleWindowResize);
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
shotEffectRequestGeneration += 1;
|
||||
pendingShotEffect.value = null;
|
||||
if (timer.value) {
|
||||
clearTimeout(timer.value);
|
||||
timer.value = null;
|
||||
@@ -521,6 +582,7 @@ onBeforeUnmount(() => {
|
||||
</view>
|
||||
</block>
|
||||
<BowShotEffect
|
||||
v-if="!shotEffect || !shotEffect.viewportMode"
|
||||
:shot="shotEffect && shotEffect.shot"
|
||||
:playKey="shotEffect ? shotEffect.key : ''"
|
||||
:targetRadius="safeTargetRadius"
|
||||
@@ -534,6 +596,20 @@ onBeforeUnmount(() => {
|
||||
/>
|
||||
<image src="https://static.shelingxingqiu.com/shootmini/static/bow-target.png" mode="widthFix" />
|
||||
</view>
|
||||
<BowShotEffect
|
||||
v-if="shotEffect && shotEffect.viewportMode"
|
||||
:shot="shotEffect.shot"
|
||||
:playKey="shotEffect.key"
|
||||
:targetRadius="safeTargetRadius"
|
||||
:targetLeft="targetRect.left"
|
||||
:targetTop="targetRect.top"
|
||||
:targetWidth="targetRect.width"
|
||||
:targetHeight="targetRect.height"
|
||||
:hitOffsetPx="currentHitRadiusPx"
|
||||
:viewportMode="true"
|
||||
@impact="shakeTarget"
|
||||
@complete="completeShotEffect"
|
||||
/>
|
||||
<view class="footer">
|
||||
<PointSwitcher
|
||||
:onChange="(val) => (pMode = val)"
|
||||
|
||||
Reference in New Issue
Block a user