Sfoglia il codice sorgente

优化锤子敲击动画逻辑

mojunshou 8 mesi fa
parent
commit
13c03f452e
1 ha cambiato i file con 52 aggiunte e 22 eliminazioni
  1. 52 22
      assets/script/game/eliminate/view/EliminateViewComp.ts

+ 52 - 22
assets/script/game/eliminate/view/EliminateViewComp.ts

@@ -2,7 +2,7 @@
  * @Author: mojunshou 1637302775@qq.com
  * @Date: 2025-03-11 18:05:45
  * @LastEditors: mojunshou 1637302775@qq.com
- * @LastEditTime: 2025-03-17 15:32:35
+ * @LastEditTime: 2025-03-17 16:01:55
  * @Description: 
  */
 import { _decorator, Color, EventTouch, instantiate, JsonAsset, Node, Prefab, Sprite, UITransform, Vec3, Widget, tween } from "cc";
@@ -1731,7 +1731,7 @@ export class EliminateViewComp extends CCComp {
         this.hammerNode.active = false;
     }
 
-    // 实现随机锤子敲击效果 - 清除一片区域
+    // 实现随机锤子敲击效果 - 清除周围少量方块
     private performRandomHammerSmash(): void {
         // 收集所有已放置的方块
         const placedBlocks: GridData[] = [];
@@ -1760,35 +1760,65 @@ export class EliminateViewComp extends CCComp {
         // 获取中心方块的位置
         const centerPosition = centerBlock.gridNode.getWorldPosition();
 
-        // 清除区域大小 (范围1-3,随机确定)
-        const clearRange = randomRangeInt(1, 5);
-
-        // 查找该区域内的所有方块
+        // 要清除的方块列表
         const blocksToSmash: GridData[] = [];
 
-        // 根据行列范围查找区域内方块
-        for (let rowOffset = -clearRange; rowOffset <= clearRange; rowOffset++) {
-            for (let colOffset = -clearRange; colOffset <= clearRange; colOffset++) {
-                const targetRow = centerBlock.row + rowOffset;
-                const targetCol = centerBlock.column + colOffset;
+        // 首先添加中心块
+        blocksToSmash.push(centerBlock);
+
+        // 确定要消除的总方块数量 (3-6个方块)
+        const totalBlockCount = randomRangeInt(3, 7);
+
+        // 设置最大曼哈顿距离,限制只选择锤子附近的方块
+        const MAX_DISTANCE = 2; // 最多选择周围2格内的方块
+
+        // 收集锤子周围符合距离要求的方块
+        const nearbyBlocks: GridData[] = [];
+
+        for (let row = Math.max(0, centerBlock.row - MAX_DISTANCE);
+            row <= Math.min(this.max_row - 1, centerBlock.row + MAX_DISTANCE);
+            row++) {
+
+            for (let col = Math.max(0, centerBlock.column - MAX_DISTANCE);
+                col <= Math.min(this.max_col - 1, centerBlock.column + MAX_DISTANCE);
+                col++) {
 
-                // 检查是否在网格范围内
-                if (targetRow >= 0 && targetRow < this.max_row &&
-                    targetCol >= 0 && targetCol < this.max_col) {
+                // 跳过中心块自身
+                if (row === centerBlock.row && col === centerBlock.column) continue;
 
-                    const targetBlock = this.gridList[targetRow][targetCol];
+                const currentBlock = this.gridList[row][col];
 
-                    // 只处理已使用的格子
-                    if (targetBlock.status === GridStatus.Used && targetBlock.gridNode) {
-                        blocksToSmash.push(targetBlock);
+                // 只考虑已放置的方块
+                if (currentBlock.status === GridStatus.Used && currentBlock.gridNode) {
+                    // 计算与中心块的曼哈顿距离
+                    const distance = Math.abs(row - centerBlock.row) + Math.abs(col - centerBlock.column);
+
+                    // 只添加在指定距离内的方块
+                    if (distance <= MAX_DISTANCE) {
+                        nearbyBlocks.push(currentBlock);
                     }
                 }
             }
         }
 
-        // 如果没有可清除的方块,直接返回
-        if (blocksToSmash.length === 0) {
-            console.log('区域内没有可清除的方块');
+        // 如果周围没有足够的方块,就用现有的所有方块
+        const extraBlocksToSelect = Math.min(totalBlockCount - 1, nearbyBlocks.length);
+
+        // 随机选择周围的方块,但保持锤子范围内的聚集效果
+        for (let i = 0; i < extraBlocksToSelect; i++) {
+            if (nearbyBlocks.length === 0) break;
+
+            // 随机选择一个附近的方块
+            const selectedIndex = randomRangeInt(0, nearbyBlocks.length);
+            blocksToSmash.push(nearbyBlocks[selectedIndex]);
+
+            // 从列表中移除已选方块,避免重复选择
+            nearbyBlocks.splice(selectedIndex, 1);
+        }
+
+        // 如果没有足够的方块可清除,直接返回
+        if (blocksToSmash.length < 2) {
+            console.log('附近没有足够的方块可清除');
             return;
         }
 
@@ -1799,7 +1829,7 @@ export class EliminateViewComp extends CCComp {
             hammerPosition.y += 50; // 设置在方块上方一定距离
             this.hammerNode.setWorldPosition(hammerPosition);
 
-            // 播放锤子敲击动画,并清除区域内所有方块
+            // 播放锤子敲击动画,并清除选择的方块
             this.playHammerSmashAnimation(blocksToSmash);
         }
     }