Browse Source

增加锤子功能,点击锤子消除随机一部分方块

mojunshou 8 months ago
parent
commit
c31ba22d3c

File diff suppressed because it is too large
+ 749 - 246
assets/bundle/gui/eliminate/eliminate.prefab


+ 4 - 4
assets/bundle/gui/eliminate/texture/icon_6.png.meta

@@ -50,10 +50,10 @@
         "height": 135,
         "rawWidth": 162,
         "rawHeight": 135,
-        "borderTop": 36,
-        "borderBottom": 45,
-        "borderLeft": 47,
-        "borderRight": 63,
+        "borderTop": 23,
+        "borderBottom": 36,
+        "borderLeft": 34,
+        "borderRight": 33,
         "packable": true,
         "pixelsToUnit": 100,
         "pivotX": 0.5,

+ 153 - 1
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 14:54:40
+ * @LastEditTime: 2025-03-17 15:32:35
  * @Description: 
  */
 import { _decorator, Color, EventTouch, instantiate, JsonAsset, Node, Prefab, Sprite, UITransform, Vec3, Widget, tween } from "cc";
@@ -76,6 +76,9 @@ export class EliminateViewComp extends CCComp {
     @property({ type: Prefab, displayName: "红包预制体" })
     private redPacketPrefab: Prefab = null!;
 
+    @property({ type: Prefab, displayName: "锤子预制体" })
+    private hammerPrefab: Prefab = null!;
+
     //游戏状态
     game_status: number = GameStatus.None;
     max_row: number = 0;
@@ -162,6 +165,9 @@ export class EliminateViewComp extends CCComp {
 
 
         }); // 然后再执行 loadPrefabsAsset
+
+        // 初始化锤子节点
+        this.initHammerNode();
     }
 
     /** 视图对象通过 ecs.Entity.remove(GameViewComp) 删除组件是触发组件处理自定义释放逻辑 */
@@ -1700,4 +1706,150 @@ export class EliminateViewComp extends CCComp {
             }, i * 0.1);
         }
     }
+
+
+    //======================拓展功能==锤子玩法==
+    private btn_hammer(): void {
+        if (
+            this.game_status !== GameStatus.Start ||
+            this.editingFlag ||
+            this.isAutoMode
+        ) return
+
+        // 随机选择一片区域的方块进行清除
+        this.performRandomHammerSmash();
+    }
+
+    // 添加初始化锤子节点的方法
+    private initHammerNode(): void {
+        if (!this.hammerNode) {
+            console.warn('锤子节点未找到');
+            return;
+        }
+
+        // 默认隐藏锤子节点
+        this.hammerNode.active = false;
+    }
+
+    // 实现随机锤子敲击效果 - 清除一片区域
+    private performRandomHammerSmash(): void {
+        // 收集所有已放置的方块
+        const placedBlocks: GridData[] = [];
+
+        for (let row = 0; row < this.max_row; row++) {
+            for (let col = 0; col < this.max_col; col++) {
+                const gridData = this.gridList[row][col];
+                if (gridData.status === GridStatus.Used && gridData.gridNode) {
+                    placedBlocks.push(gridData);
+                }
+            }
+        }
+
+        // 如果没有已放置的方块,直接返回
+        if (placedBlocks.length === 0) {
+            console.log('没有可清除的方块');
+            return;
+        }
+
+        // 随机选择一个方块作为中心点
+        const randomIndex = randomRangeInt(0, placedBlocks.length);
+        const centerBlock = placedBlocks[randomIndex];
+
+        if (!centerBlock.gridNode) return;
+
+        // 获取中心方块的位置
+        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;
+
+                // 检查是否在网格范围内
+                if (targetRow >= 0 && targetRow < this.max_row &&
+                    targetCol >= 0 && targetCol < this.max_col) {
+
+                    const targetBlock = this.gridList[targetRow][targetCol];
+
+                    // 只处理已使用的格子
+                    if (targetBlock.status === GridStatus.Used && targetBlock.gridNode) {
+                        blocksToSmash.push(targetBlock);
+                    }
+                }
+            }
+        }
+
+        // 如果没有可清除的方块,直接返回
+        if (blocksToSmash.length === 0) {
+            console.log('区域内没有可清除的方块');
+            return;
+        }
+
+        // 显示锤子并设置在中心方块上方
+        if (this.hammerNode) {
+            this.hammerNode.active = true;
+            const hammerPosition = centerPosition.clone();
+            hammerPosition.y += 50; // 设置在方块上方一定距离
+            this.hammerNode.setWorldPosition(hammerPosition);
+
+            // 播放锤子敲击动画,并清除区域内所有方块
+            this.playHammerSmashAnimation(blocksToSmash);
+        }
+    }
+
+    // 添加锤子敲击动画,并清除目标区域内所有方块
+    private playHammerSmashAnimation(blocksToSmash: GridData[]): void {
+        if (!this.hammerNode) return;
+
+        // 创建敲击动画
+        tween(this.hammerNode)
+            .to(0.1, { angle: -30 })  // 锤子抬起
+            .to(0.1, { angle: 30 })   // 锤子落下
+            .call(() => {
+                // 添加更强的震动效果
+                tween(this.node)
+                    .to(0.05, { position: new Vec3(8, 0, 0) })
+                    .to(0.05, { position: new Vec3(-8, 0, 0) })
+                    .to(0.05, { position: new Vec3(5, 5, 0) })
+                    .to(0.05, { position: new Vec3(-5, -5, 0) })
+                    .to(0.05, { position: new Vec3(0, 0, 0) })
+                    .start();
+
+                // 播放敲击音效(如果有的话)
+                // this.audioManager.playHammerSmash();
+
+                // 依次执行方块消除动画,让消除有先后顺序
+                blocksToSmash.forEach((targetBlock, index) => {
+                    if (targetBlock.gridNode?.children[0]) {
+                        this.scheduleOnce(() => {
+                            tween(targetBlock.gridNode!.children[0])
+                                .to(0.2, { scale: new Vec3(0.5, 0.5) })
+                                .call(() => {
+                                    // 更新格子状态
+                                    targetBlock.status = GridStatus.NotUse;
+                                    targetBlock.gridColorKey = null;
+                                    this.generateGrid(targetBlock);
+                                })
+                                .start();
+                        }, index * 0.05); // 添加少量延迟,创造波纹式消除效果
+                    }
+                });
+
+                // 延迟一段时间后隐藏锤子
+                this.scheduleOnce(() => {
+                    if (this.hammerNode) {
+                        this.hammerNode.active = false;
+                    }
+                }, 0.5); // 增加时间以完成全部消除动画
+            })
+            .start();
+    }
+
 }