林旭祥 3 nedēļas atpakaļ
vecāks
revīzija
5368aa7ae3
1 mainītis faili ar 108 papildinājumiem un 27 dzēšanām
  1. 108 27
      src/views/game/fruit.vue

+ 108 - 27
src/views/game/fruit.vue

@@ -202,7 +202,9 @@ const getCanvas = () => {
   });
   // console.log("原始坐标:", originalPoints);
   // console.log("缩放后坐标:", postData);
-  externalMethod(postData[10][0] - offset.x, postData[10][1] - offset.y)
+  //externalMethod(postData[10][0] - offset.x, postData[10][1] - offset.y)
+  externalRightHandMethod(postData[10][0] - offset.x, postData[10][1] - offset.y)
+  externalLeftHandMethod(postData[9][0] - offset.x, postData[9][1] - offset.y)
   //绘制头部
   const point1 = { x: postData[4][0], y: postData[4][1] };
   const point2 = { x: postData[3][0], y: postData[3][1] };
@@ -829,7 +831,7 @@ class Fruit {
 
 // 刀身类
 class Blade {
-  constructor(envConfig) {
+  constructor(envConfig, handType) {// 新增handType区分左右手
     this.env = envConfig;
     this.game = envConfig.scene;
     this.points = []; // 记录鼠标轨迹点
@@ -838,7 +840,8 @@ class Blade {
     this.allowBlade = false;
     this.lastPoint = null; // 上一个记录的点
     this.moveThreshold = 5; // 鼠标移动超过这个距离才记录新点(避免密集冗余)
-
+    this.handType = handType; // 存储"left"或"right"
+    this.color = handType === 'left' ? 0x00FFFF : 0xFFFF00; // 左手蓝色,右手黄色(示例)
     this.init();
   }
 
@@ -847,6 +850,8 @@ class Blade {
       x: 0,
       y: 0
     });
+        // 根据手型设置绘图层级(可选)
+    this.graphics.setDepth(this.handType === 'left' ? 1001 : 1002);
     // 监听鼠标移动事件(可选,也可在update中处理)
     this.game.input.on('pointermove', (pointer) => {
       //console.log("11111", pointer)
@@ -858,6 +863,7 @@ class Blade {
 
   // 处理鼠标移动:记录轨迹点
   handleMouseMove(pointer) {
+    if (!this.allowBlade) return;
     const point = {
       x: pointer.x,
       y: pointer.y,
@@ -882,28 +888,20 @@ class Blade {
   }
 
   update() {
-    if (this.allowBlade) {
-      this.graphics.clear();
-
-      // 清理过期的轨迹点(超过生命周期的点)
-      const now = Date.now();
-      this.points = this.points.filter(point => now - point.time < this.POINTLIFETIME);
-
-      // 生成刀光图形(基于现有轨迹点)
-      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();
-        }
+    if (!this.allowBlade) return;
+    this.graphics.clear();
+    const now = Date.now();
+    this.points = this.points.filter(point => now - point.time < this.POINTLIFETIME);
+
+    if (this.points.length > 0) {
+      const bladePoints = mathTool.generateBlade(this.points);
+      if (bladePoints.length > 0) {
+        this.graphics.fillStyle(this.color, 0.8); // 使用当前刀光的颜色
+        this.graphics.beginPath();
+        this.graphics.moveTo(bladePoints[0].x, bladePoints[0].y);
+        bladePoints.forEach((point, i) => i > 0 && this.graphics.lineTo(point.x, point.y));
+        this.graphics.closePath();
+        this.graphics.fill();
       }
     }
   }
@@ -988,7 +986,6 @@ class PreloadScene extends Phaser.Scene {
     this.load.image('basaha-2', 'static/images/fruit/basaha-2.png');
     this.load.image('best', 'static/images/fruit/best.png');
     this.load.image('bomb', 'static/images/fruit/bomb.png');
-    this.load.image('dojo', 'static/images/fruit/dojo.png');
     this.load.image('game-over', 'static/images/fruit/game-over.png');
     this.load.image('home-desc', 'static/images/fruit/home-desc.png');
     this.load.image('home-mask', 'static/images/fruit/home-mask.png');
@@ -1039,6 +1036,8 @@ class MainScene extends Phaser.Scene {
     this.start = false;
     this.sandiaRotateSpeed = 0.9;
     this.newGameRotateSpeed = -0.3;
+        this.leftBlade = null; // 左手刀光
+    this.rightBlade = null; // 右手刀光
   }
 
   create() {
@@ -1058,6 +1057,9 @@ class MainScene extends Phaser.Scene {
 
     // 开始动画
     this.homeGroupAnim();
+    // 初始化左右手刀光
+    this.leftBlade = new Blade({ scene: this }, 'left');
+    this.rightBlade = new Blade({ scene: this }, 'right');
   }
 
   update() {
@@ -1080,6 +1082,26 @@ class MainScene extends Phaser.Scene {
         }
       );
     }
+
+        // 分别更新左右手刀光
+    this.leftBlade.update();
+    this.rightBlade.update();
+
+    // 分别检测左右手刀光与水果的碰撞
+    if (this.sandia && this.sandia.getSprite() && !this.start) {
+      // 右手碰撞检测
+      this.rightBlade.checkCollide(
+        this.sandia.getSprite(),
+        () => this.startGame()
+      );
+      // 左手碰撞检测(可选:允许左手也能启动游戏)
+      this.leftBlade.checkCollide(
+        this.sandia.getSprite(),
+        () => this.startGame()
+      );
+    }
+
+
   }
 
   homeGroupAnim() {
@@ -1150,6 +1172,10 @@ class MainScene extends Phaser.Scene {
 
   allowBlade() {
     this.blade.enable();
+
+        // 同时启用左右手刀光
+    this.leftBlade.enable();
+    this.rightBlade.enable();
   }
 
   startGame() {
@@ -1227,6 +1253,8 @@ class PlayScene extends Phaser.Scene {
     this.xx = null;
     this.xxx = null;
     this.gravity = 200;
+        this.leftBlade = null;
+    this.rightBlade = null;
   }
 
   create() {
@@ -1262,6 +1290,12 @@ class PlayScene extends Phaser.Scene {
 
     // 调用初始化方法,而不是直接开始生成水果
     this.initGame();
+
+        // 初始化左右手刀光
+    this.leftBlade = new Blade({ scene: this }, 'left');
+    this.rightBlade = new Blade({ scene: this }, 'right');
+    this.leftBlade.enable();
+    this.rightBlade.enable();
   }
 
   initGame() {
@@ -1329,6 +1363,10 @@ class PlayScene extends Phaser.Scene {
     // 更新刀光
     this.blade.update();
 
+    // 分别更新左右手刀光
+    this.leftBlade.update();
+    this.rightBlade.update();
+
     // 检查碰撞
     if (!this.bombExplode) {
       this.fruits.forEach((fruit, i) => {
@@ -1344,11 +1382,32 @@ class PlayScene extends Phaser.Scene {
               }
             }
           );
+
+        // 右手碰撞
+        this.rightBlade.checkCollide(
+          fruit.getSprite(),
+          () => this.handleCollision(fruit, i)
+        );
+        // 左手碰撞
+        this.leftBlade.checkCollide(
+          fruit.getSprite(),
+          () => this.handleCollision(fruit, i)
+        );
         }
       });
     }
-  }
 
+
+  }
+  // 统一处理碰撞逻辑(提取重复代码)
+  handleCollision(fruit, index) {
+    if (fruit.isFruit) {
+      this.onKill(fruit);
+      this.fruits.splice(index, 1);
+    } else {
+      this.onBomb(fruit);
+    }
+  }
   scoreAnim() {
     this.scoreImage = this.add.image(-100, 8, 'score');
     this.scoreImage.setOrigin(0, 0);
@@ -1704,6 +1763,28 @@ const externalMethod = (autoX, autoY) => {
   targetScene.blade.handleMouseMove(mockPointer);
 };
 
+// 新增:区分左右手的输入方法
+const externalLeftHandMethod = (x, y) => {
+  const game = gameRef.value;
+  if (!game) return;
+  const currentScene = getActiveScene();
+  const targetScene = game.scene.getScene(currentScene);
+  if (targetScene && targetScene.leftBlade && targetScene.leftBlade.allowBlade) {
+    targetScene.leftBlade.handleMouseMove({ x, y });
+  }
+};
+
+const externalRightHandMethod = (x, y) => {
+  const game = gameRef.value;
+  if (!game) return;
+  const currentScene = getActiveScene();
+  const targetScene = game.scene.getScene(currentScene);
+  if (targetScene && targetScene.rightBlade && targetScene.rightBlade.allowBlade) {
+    targetScene.rightBlade.handleMouseMove({ x, y });
+  }
+};
+
+
 const getActiveScene = () => {
   const game = gameRef.value; // 获取游戏实例
   if (!game) return null;