浏览代码

第四步拖拽成功

181404010226 1 月之前
父节点
当前提交
092b30ec7a

+ 8 - 0
assets/scripts/CombatSystem/BlockManager.ts

@@ -1705,6 +1705,8 @@ export class BlockManager extends Component {
 
                 // 清除拖拽标记
                 (block as any)['isDragging'] = false;
+                // 在教程保护场景下,确保位置标记为 grid(避免后续逻辑误判)
+                this.blockLocations.set(block, 'grid');
 
                 // 启用根节点碰撞体
                 const rootCol = block.getComponent(Collider2D);
@@ -1728,6 +1730,12 @@ export class BlockManager extends Component {
                     // 安静失败,不影响主流程
                 }
             }
+            // 循环处理完毕后,广播全局强制结束拖拽事件,清理拖拽上下文
+            try {
+                EventBus.getInstance().emit(GameEvents.FORCE_END_ALL_DRAGS);
+            } catch (e) {
+                // 安静失败,不影响主流程
+            }
         } catch (e) {
             console.warn('[BlockManager] 教程保护:恢复方块碰撞与拖拽状态失败:', e);
         }

+ 34 - 0
assets/scripts/CombatSystem/BlockSelection/GameBlockSelection.ts

@@ -394,6 +394,8 @@ export class GameBlockSelection extends Component {
         // 监听敌人数量更新与新波次开始,精确在第二波结束时解禁按钮
         eventBus.on(GameEvents.ENEMY_UPDATE_COUNT, this.updateGuideButtonStates, this);
         eventBus.on(GameEvents.ENEMY_START_WAVE, this.updateGuideButtonStates, this);
+        // 监听:全局强制结束所有拖拽(用于教程自动放置期间收敛拖拽状态)
+        eventBus.on(GameEvents.FORCE_END_ALL_DRAGS, this.onForceEndAllDrags, this);
     }
     
     // 处理重置方块选择事件
@@ -1578,6 +1580,38 @@ export class GameBlockSelection extends Component {
         eventBus.off(GameEvents.ENEMY_UPDATE_COUNT, this.updateGuideButtonStates, this);
         eventBus.off(GameEvents.ENEMY_START_WAVE, this.updateGuideButtonStates, this);
         eventBus.off(GameEvents.SETUP_BLOCK_DRAG_EVENTS, this.onSetupBlockDragEventsEvent, this);
+        eventBus.off(GameEvents.FORCE_END_ALL_DRAGS, this.onForceEndAllDrags, this);
+    }
+
+    // 强制结束当前拖拽:用于教程自动放置发生时,立即清空拖拽上下文
+    private onForceEndAllDrags() {
+        if (!this.currentDragBlock || !this.currentDragBlock.isValid) {
+            return;
+        }
+        try {
+            // 清除拖拽标记并恢复碰撞
+            (this.currentDragBlock as any)['isDragging'] = false;
+            const rootCol = this.currentDragBlock.getComponent(Collider2D);
+            if (rootCol && !rootCol.enabled) rootCol.enabled = true;
+            const b1 = this.currentDragBlock.getChildByName('B1');
+            if (b1) {
+                const b1Col = b1.getComponent(Collider2D);
+                if (b1Col && !b1Col.enabled) b1Col.enabled = true;
+            }
+
+            // 根据方块原始位置决定是否需要回到kuang容器
+            const startLocation = this.blockManager.blockLocations.get(this.currentDragBlock);
+            if (startLocation && startLocation !== 'grid') {
+                this.returnBlockToKuang(startLocation);
+            }
+
+            // 统一抛出拖拽结束事件,维持各模块状态一致
+            EventBus.getInstance().emit(GameEvents.BLOCK_DRAG_END, { block: this.currentDragBlock });
+        } catch (e) {
+            console.warn('[GameBlockSelection] 强制结束拖拽失败:', e);
+        } finally {
+            this.currentDragBlock = null;
+        }
     }
     
     /**

+ 2 - 0
assets/scripts/Core/EventBus.ts

@@ -100,6 +100,8 @@ export enum GameEvents {
     // 方块拖拽事件
     BLOCK_DRAG_START = 'BLOCK_DRAG_START',
     BLOCK_DRAG_END = 'BLOCK_DRAG_END',
+    // 全局强制结束所有拖拽(例如教程自动放置期间)
+    FORCE_END_ALL_DRAGS = 'FORCE_END_ALL_DRAGS',
 
     // 方块选择事件
     BLOCK_SELECTION_CONFIRMED = 'BLOCK_SELECTION_CONFIRMED',