Merge branch 'feature-points-book' into development

This commit is contained in:
kron
2025-08-05 11:56:51 +08:00
52 changed files with 2042 additions and 72 deletions

View File

@@ -286,3 +286,155 @@ export const isGameEnded = async (battleId) => {
}
return !isGaming;
};
// 获取元素尺寸和位置信息
export const getElementRect = () => {
return new Promise((resolve) => {
const query = uni.createSelectorQuery();
query
.select(".container")
.boundingClientRect((rect) => {
resolve(rect);
})
.exec();
});
};
const calcNormalBowTarget = (x, y, targetWidth, targetHeight) => {
// 计算靶心坐标(靶纸中心)
const centerX = targetWidth / 2;
const centerY = targetHeight / 2;
// 计算点击点到靶心的距离
const deltaX = x - centerX;
const deltaY = y - centerY;
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
// 计算靶纸半径(取宽高中较小值的一半)
// const targetRadius = Math.min(targetWidth, targetHeight) / 2;
const targetRadius = targetWidth / 2;
// 如果距离超过靶纸半径,则脱靶
if (distance > targetRadius) return 0;
// 计算相对距离0-1之间
const relativeDistance = distance / targetRadius;
// 全环靶有10个环每个环占半径的10%
// 从外到内1环到10环
// 距离越近靶心,环数越高
if (relativeDistance <= 0.05) return "X";
if (relativeDistance <= 0.1) return 10;
if (relativeDistance <= 0.2) return 9;
if (relativeDistance <= 0.3) return 8;
if (relativeDistance <= 0.4) return 7;
if (relativeDistance <= 0.5) return 6;
if (relativeDistance <= 0.6) return 5;
if (relativeDistance <= 0.7) return 4;
if (relativeDistance <= 0.8) return 3;
if (relativeDistance <= 0.9) return 2;
if (relativeDistance <= 1.0) return 1;
return 0; // 脱靶
};
const calcHalfBowTarget = (x, y, side, noX = false) => {
// 计算靶心坐标(靶纸中心)
const centerX = side / 2;
const centerY = side / 2;
// 计算点击点到靶心的距离
const deltaX = x - centerX;
const deltaY = y - centerY;
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
// 计算靶纸半径(取宽高中较小值的一半)
const targetRadius = side / 2;
// 如果距离超过靶纸半径,则脱靶
if (distance > targetRadius) return 0;
// 计算相对距离0-1之间
const relativeDistance = distance / targetRadius;
if (relativeDistance <= 0.1) return noX ? 10 : "X";
if (relativeDistance <= 0.2) return noX ? 9 : 10;
if (relativeDistance <= 0.4) return 9;
if (relativeDistance <= 0.6) return 8;
if (relativeDistance <= 0.8) return 7;
if (relativeDistance <= 1.0) return 6;
return 0; // 脱靶
};
export const calcTripleBowTarget = (
x,
y,
targetWidth,
targetHeight,
noX = false
) => {
const side = targetWidth * 0.325;
if (x / targetWidth >= 0.337 && x / targetWidth <= 0.663) {
if (y / targetHeight <= 0.325) {
return calcHalfBowTarget(x - targetWidth * 0.337, y, side, noX);
} else if (y / targetHeight >= 0.335 && y / targetHeight <= 0.662) {
return calcHalfBowTarget(
x - targetWidth * 0.337,
y - targetHeight * 0.335,
side,
noX
);
} else if (y / targetHeight >= 0.674) {
return calcHalfBowTarget(
x - targetWidth * 0.337,
y - targetHeight * 0.674,
side,
noX
);
}
}
return 0;
};
export const calcPinBowTarget = (
x,
y,
targetWidth,
targetHeight,
noX = false
) => {
const side = targetWidth * 0.48;
if (
x / targetWidth >= 0.26 &&
x / targetWidth <= 0.74 &&
y / targetHeight <= 0.48
) {
return calcHalfBowTarget(x - targetWidth * 0.26, y, side, noX);
} else if (x / targetHeight <= 0.48 && y / targetHeight >= 0.456) {
return calcHalfBowTarget(x, y - targetHeight * 0.456, side, noX);
} else if (x / targetHeight >= 0.52 && y / targetHeight >= 0.456) {
return calcHalfBowTarget(
x - targetWidth * 0.52,
y - targetHeight * 0.456,
side,
noX
);
}
return 0;
};
export const calcRing = (bowtargetId, x, y, targetWidth, targetHeight) => {
if (bowtargetId < 4) {
return calcNormalBowTarget(x, y, targetWidth, targetHeight);
} else if (bowtargetId < 7) {
return calcHalfBowTarget(x, y, targetWidth);
} else if (bowtargetId === 7) {
return calcTripleBowTarget(x, y, targetWidth, targetHeight);
} else if (bowtargetId === 8) {
return calcPinBowTarget(x, y, targetWidth, targetHeight);
} else if (bowtargetId === 9) {
return calcTripleBowTarget(x, y, targetWidth, targetHeight, true);
} else if (bowtargetId === 10) {
return calcPinBowTarget(x, y, targetWidth, targetHeight, true);
}
return 0;
};