Browse Source

改成不用点击就出现刀

林旭祥 2 days ago
parent
commit
fd46c3084b
1 changed files with 39 additions and 32 deletions
  1. 39 32
      src/views/game/fruit.vue

+ 39 - 32
src/views/game/fruit.vue

@@ -547,11 +547,12 @@ class Blade {
   constructor(envConfig) {
     this.env = envConfig;
     this.game = envConfig.scene;
-    this.points = [];
+    this.points = []; // 记录鼠标轨迹点
     this.graphics = null;
-    this.POINTLIFETIME = 200;
+    this.POINTLIFETIME = 200; // 轨迹点的生命周期(毫秒)
     this.allowBlade = false;
-    this.lastPoint = null;
+    this.lastPoint = null; // 上一个记录的点
+    this.moveThreshold = 5; // 鼠标移动超过这个距离才记录新点(避免密集冗余)
 
     this.init();
   }
@@ -561,53 +562,59 @@ class Blade {
       x: 0,
       y: 0
     });
+    // 监听鼠标移动事件(可选,也可在update中处理)
+    this.game.input.on('pointermove', (pointer) => {
+      if (this.allowBlade) {
+        this.handleMouseMove(pointer);
+      }
+    });
+  }
+
+  // 处理鼠标移动:记录轨迹点
+  handleMouseMove(pointer) {
+    const point = {
+      x: pointer.x,
+      y: pointer.y,
+      time: Date.now()
+    };
+
+    if (!this.lastPoint) {
+      // 首次记录点
+      this.lastPoint = point;
+      this.points.push(point);
+    } else {
+      // 计算与上一个点的距离,超过阈值才记录新点
+      const dis = Math.hypot(
+        point.x - this.lastPoint.x,
+        point.y - this.lastPoint.y
+      );
+      if (dis > this.moveThreshold) {
+        this.lastPoint = point;
+        this.points.push(point);
+      }
+    }
   }
 
   update() {
     if (this.allowBlade) {
       this.graphics.clear();
 
-      // 清理过期点
+      // 清理过期的轨迹(超过生命周期的点)
       const now = Date.now();
       this.points = this.points.filter(point => now - point.time < this.POINTLIFETIME);
 
-      if (this.game.input.activePointer.isDown) {
-        const point = {
-          x: this.game.input.activePointer.x,
-          y: this.game.input.activePointer.y,
-          time: Date.now()
-        };
-
-        if (!this.lastPoint) {
-          this.lastPoint = point;
-          this.points.push(point);
-        } else {
-          const dis = Math.hypot(
-            point.x - this.lastPoint.x,
-            point.y - this.lastPoint.y
-          );
-
-          if (dis > Math.sqrt(300)) { // 相当于距离平方>300
-            this.lastPoint = point;
-            this.points.push(point);
-          }
-        }
-      }
-
+      // 生成刀光图形(基于现有轨迹点)
       if (this.points.length > 0) {
         const bladePoints = mathTool.generateBlade(this.points);
-
         if (bladePoints.length > 0) {
           this.graphics.fillStyle(0xffffff, 0.8);
           this.graphics.beginPath();
           this.graphics.moveTo(bladePoints[0].x, bladePoints[0].y);
-
           bladePoints.forEach((point, i) => {
             if (i > 0) {
               this.graphics.lineTo(point.x, point.y);
             }
           });
-
           this.graphics.closePath();
           this.graphics.fill();
         }
@@ -615,10 +622,10 @@ class Blade {
     }
   }
 
+  // 碰撞检测:去掉“鼠标按下”的限制,只要有轨迹就检测
   checkCollide(sprite, onCollide) {
-    if (this.allowBlade && this.game.input.activePointer.isDown && this.points.length > 2) {
+    if (this.allowBlade && this.points.length > 2) { // 仅保留轨迹点数量的判断
       const bounds = sprite.getBounds();
-
       for (const point of this.points) {
         if (Phaser.Geom.Rectangle.Contains(bounds, point.x, point.y)) {
           onCollide();