林旭祥 2 روز پیش
والد
کامیت
4b0c05e809
1فایلهای تغییر یافته به همراه41 افزوده شده و 51 حذف شده
  1. 41 51
      src/views/game/basketball.vue

+ 41 - 51
src/views/game/basketball.vue

@@ -22,9 +22,31 @@ const data = reactive<any>({
   againTimer: null,//定时状态
   wsState: false,//WS状态
   clientObj: {},//浏览器对象
+  myThrow: 0,//0放下1举投
 });
 
-const { bodyposeData, bodyposeState, parameter, deviceInfo, againNum, againTimer, wsState, clientObj } = toRefs(data);
+const { bodyposeData, bodyposeState, parameter, deviceInfo, againNum, againTimer, wsState, clientObj, myThrow } = toRefs(data);
+
+/**
+ * 输出犯规
+ */
+watch(
+  () => myThrow.value,
+  (newData,oldData) => {
+    console.log("ppp",oldData,newData)
+    if (newData == 1) {
+      console.log("投篮")
+      handleMouseDown();
+      setTimeout(()=>{
+        handleMouseUp();
+      },100)
+      
+    }else{
+      
+    }
+  },
+  { immediate: true }
+);
 
 /**
  * 初始化
@@ -46,7 +68,7 @@ const getInit = async () => {
   }
   speckText("正在姿态识别");
   bodyposeWs((e: any) => {
-    console.log("bodyposeWS", e)
+    //console.log("bodyposeWS", e)
     if (e?.wksid) {
       wsState.value = true;
       //获取设备信息
@@ -111,7 +133,7 @@ const getInit = async () => {
       for (let i = 0; i < arr.length; i += 3) {
         result.push(arr.slice(i, i + 2));
       }
-      console.log("result", result)
+      //console.log("result", result)
       bodyposeData.value = result;
       getCanvas(result);
     }
@@ -154,10 +176,17 @@ const getCloseBodypose = () => {
 
 
 const getCanvas = (data) => {
-  const analyzer = new TrendAnalyzer();
-  if (analyzer.addPoint(data[10][0],data[10][1])) {
-    handleMouseDown();
+  let leftA = data[6][1];
+  let rightA = data[5][1];
+  let leftB = data[10][1];
+  let rightB = data[9][1];
+
+  if (leftB > leftA || rightB > rightA) {
+    myThrow.value = 1;
+  } else {
+    myThrow.value = 0;
   }
+
 };
 
 // 游戏主类的响应式状态
@@ -239,7 +268,7 @@ class Ball {
     this.solid = false;
     this.z = 1;
   }
-
+  
   setAngle(angle) {
     this.angle = angle;
     this.vx = this.speed * Math.cos(this.angle * Math.PI / 180);
@@ -676,62 +705,23 @@ const initGame = async () => {
   gameState.animationFrameId = requestAnimationFrame(gameLoop);
 };
 
-class TrendAnalyzer {
-  constructor(minPoints = 5, threshold = 0) {
-    this.points = [];
-    this.minPoints = minPoints; // 判断趋势所需的最小点数
-    this.threshold = threshold; // 定义最小上升幅度
-  }
-
-  addPoint(x, y) {
-    this.points.push({ x, y });
-    // 保持数组长度适中,仅保留分析所需的点
-    if (this.points.length > this.minPoints + 1) {
-      this.points.shift();
-    }
-    return this.isIncreasing();
-  }
 
-  isIncreasing() {
-    if (this.points.length < this.minPoints) {
-      return false;
-    }
-
-    let increasingCount = 0;
-    for (let i = 1; i < this.points.length; i++) {
-      // 计算斜率:(y2 - y1) / (x2 - x1)
-      const slope = (this.points[i].y - this.points[i - 1].y) /
-        (this.points[i].x - this.points[i - 1].x);
-
-      // 若斜率大于阈值,则视为上升
-      if (slope > this.threshold) {
-        increasingCount++;
-      }
-    }
-
-    // 要求至少有 minPoints-1 个连续上升的线段
-    return increasingCount >= this.points.length - 1;
-  }
-}
 
 // 生命周期钩子
 onMounted(() => {
   initGame();
-  // setInterval(() => {
-  //   handleMouseDown()
-  // }, 500)
 });
 
-onBeforeUnmount(() => {
-  if (gameState.animationFrameId) {
-    cancelAnimationFrame(gameState.animationFrameId);
-  }
-  window.removeEventListener('resize', resizeToWindow);
+onBeforeMount(() => {
   getInit();
 });
 
 onBeforeUnmount(() => {
   closeWS();
+  if (gameState.animationFrameId) {
+    cancelAnimationFrame(gameState.animationFrameId);
+  }
+  window.removeEventListener('resize', resizeToWindow);
 });
 </script>