Pārlūkot izejas kodu

解决网格刷新问题

181404010226 4 mēneši atpakaļ
vecāks
revīzija
ab85d3433d

+ 0 - 12
assets/resources/data/excel/关卡配置/~$关卡配置表_完整版_更新_v2.xlsx.meta

@@ -1,12 +0,0 @@
-{
-  "ver": "1.0.0",
-  "importer": "*",
-  "imported": true,
-  "uuid": "2579fc27-3acf-437a-befb-0771c5203678",
-  "files": [
-    ".json",
-    ".xlsx"
-  ],
-  "subMetas": {},
-  "userData": {}
-}

+ 1 - 61
assets/scripts/CombatSystem/BlockManager.ts

@@ -546,52 +546,6 @@ export class BlockManager extends Component {
     
 
     
-
-    
-
-    
-
-    
-
-    
-    // 临时保存方块占用的网格
-    tempStoreBlockOccupiedGrids(block: Node) {
-        const occupiedGrids = block['occupiedGrids'];
-        if (!occupiedGrids || occupiedGrids.length === 0) return;
-        
-        this.tempRemovedOccupiedGrids.push({
-            block: block,
-            occupiedGrids: [...occupiedGrids]
-        });
-        
-        for (const grid of occupiedGrids) {
-            if (grid.row >= 0 && grid.row < this.GRID_ROWS && 
-                grid.col >= 0 && grid.col < this.GRID_COLS) {
-                this.gridOccupationMap[grid.row][grid.col] = 0;
-            }
-        }
-        
-        block['occupiedGrids'] = [];
-    }
-    
-    // 恢复方块原来的占用状态
-    restoreBlockOccupiedGrids(block: Node) {
-        const index = this.tempRemovedOccupiedGrids.findIndex(item => item.block === block);
-        if (index === -1) return;
-        
-        const savedItem = this.tempRemovedOccupiedGrids[index];
-        
-        for (const grid of savedItem.occupiedGrids) {
-            if (grid.row >= 0 && grid.row < this.GRID_ROWS && 
-                grid.col >= 0 && grid.col < this.GRID_COLS) {
-                this.gridOccupationMap[grid.row][grid.col] = 1;
-            }
-        }
-        
-        block['occupiedGrids'] = [...savedItem.occupiedGrids];
-        this.tempRemovedOccupiedGrids.splice(index, 1);
-    }
-    
     // 清除临时保存的占用状态
     clearTempStoredOccupiedGrids(block: Node) {
         const index = this.tempRemovedOccupiedGrids.findIndex(item => item.block === block);
@@ -1003,7 +957,6 @@ export class BlockManager extends Component {
                 this.gridOccupationMap[row][col] = 0;
             }
         }
-        
         // 清理临时存储的占用状态
         this.tempRemovedOccupiedGrids = [];
     }
@@ -1718,19 +1671,6 @@ export class BlockManager extends Component {
         // 生成新的方块
         this.generateRandomBlocksInKuang();
         
-        // 恢复已放置方块的占用信息
-        for (const item of placedBlocksOccupation) {
-            if (item.block && item.block.isValid) {
-                item.block['occupiedGrids'] = [...item.occupiedGrids];
-                // 重新标记网格占用状态
-                for (const grid of item.occupiedGrids) {
-                    if (grid.row >= 0 && grid.row < this.GRID_ROWS && 
-                        grid.col >= 0 && grid.col < this.GRID_COLS) {
-                        this.gridOccupationMap[grid.row][grid.col] = 1;
-                    }
-                }
-            }
-        }
     }
 
     // 检查是否有已放置的方块
@@ -1935,7 +1875,7 @@ export class BlockManager extends Component {
     
     // 输出格子占用情况矩阵
     public printGridOccupationMatrix() {
-        console.log('[BlockManager] 格子占用情况矩阵 (6行11列):');
+        console.log('[BlockManager] 格子占用情况de矩阵 (6行11列):');
         for (let row = 0; row < this.GRID_ROWS; row++) {
             let rowStr = '';
             for (let col = 0; col < this.GRID_COLS; col++) {

+ 30 - 33
assets/scripts/CombatSystem/BlockSelection/GameBlockSelection.ts

@@ -501,18 +501,39 @@ export class GameBlockSelection extends Component {
         console.log('[GameBlockSelection] 刷新方块占用情况');
         
         if (this.blockManager) {
-            // 不重置网格占用状态,只重新计算已放置方块的占用情况
-            // 这样可以保留refreshBlocks中恢复的占用信息
+             this.blockManager.resetGridOccupation();
             
             // 重新计算所有已放置方块的占用情况
             const placedBlocksContainer = find('Canvas/GameLevelUI/PlacedBlocks');
             if (placedBlocksContainer) {
                 for (let i = 0; i < placedBlocksContainer.children.length; i++) {
                     const block = placedBlocksContainer.children[i];
+                    console.log(`[GameBlockSelection] 刷新方块占用情况,方块${i}`, block);
+                    
+                    // 按照BlockManager.tryPlaceBlockToGrid的正确方法获取位置
+                    let b1Node = block;
+                    if (block.name !== 'B1') {
+                        b1Node = block.getChildByName('B1');
+                        if (!b1Node) {
+                            console.warn(`[GameBlockSelection] 方块 ${block.name} 没有B1子节点`);
+                            continue;
+                        }
+                    }
+                    
+                    // 获取B1节点的世界坐标,然后转换为网格本地坐标
+                    const b1WorldPos = b1Node.parent.getComponent(UITransform).convertToWorldSpaceAR(b1Node.position);
+                    const gridPos = this.blockManager.gridContainer.getComponent(UITransform).convertToNodeSpaceAR(b1WorldPos);
+                    
+                    console.log(`[GameBlockSelection] B1世界坐标:`, b1WorldPos);
+                    console.log(`[GameBlockSelection] 网格本地坐标:`, gridPos);
+                    
                     // 重新标记方块占用的网格位置
-                    const gridNode = this.blockManager.findNearestGridNode(block.worldPosition);
+                    const gridNode = this.blockManager.findNearestGridNode(gridPos);
                     if (gridNode) {
                         this.blockManager.markOccupiedPositions(block, gridNode);
+                        console.log(`[GameBlockSelection] 方块 ${block.name} 重新标记到网格 ${gridNode.name}`);
+                    } else {
+                        console.warn(`[GameBlockSelection] 方块 ${block.name} 未找到对应的网格节点`);
                     }
                 }
             }
@@ -521,6 +542,8 @@ export class GameBlockSelection extends Component {
         } else {
             console.warn('[GameBlockSelection] BlockManager未找到,无法刷新占用情况');
         }
+        
+        this.blockManager.printGridOccupationMatrix();
     }
 
     // 重置方块选择状态
@@ -538,10 +561,7 @@ export class GameBlockSelection extends Component {
         // 清理所有方块标签
         BlockTag.clearAllTags();
         console.log('[GameBlockSelection] 方块标签已清理');
-        
-        // 刷新方块占用情况
-        this.refreshGridOccupation();
-        
+    
         // 更新金币显示
         this.updateCoinDisplay();
         console.log('[GameBlockSelection] 金币显示已更新');
@@ -593,7 +613,6 @@ export class GameBlockSelection extends Component {
             this.currentDragBlock['startLocation'] = this.blockManager.blockLocations.get(block);
             
             block.setSiblingIndex(block.parent.children.length - 1);
-            this.blockManager.tempStoreBlockOccupiedGrids(block);
             // 拖拽开始时禁用碰撞体
             const collider = block.getComponent(Collider2D);
             if (collider) collider.enabled = false;
@@ -669,6 +688,7 @@ export class GameBlockSelection extends Component {
     
     // 处理方块放下
     private async handleBlockDrop(event: EventTouch) {
+        console.log("事件:处理方块放下");
         try {
             const touchPos = event.getLocation();
             const startLocation = this.currentDragBlock['startLocation'];
@@ -677,25 +697,13 @@ export class GameBlockSelection extends Component {
                 // 检查是否有标签,只有有标签的方块才能放回kuang
                 if (BlockTag.hasTag(this.currentDragBlock)) {
                     this.returnBlockToKuang(startLocation);
-                    // 返回kuang后输出格子占用情况
-                    this.blockManager.printGridOccupationMatrix();
-                    // 刷新方块占用情况
-                    this.refreshGridOccupation();
                 } else {
                     // 没有标签的方块不能放回kuang,返回原位置
                     console.log(`[GameBlockSelection] 方块 ${this.currentDragBlock.name} 没有标签,不能放回kuang区域`);
                     this.returnBlockToOriginalPosition();
-                    // 返回原位置后输出格子占用情况
-                    this.blockManager.printGridOccupationMatrix();
-                    // 刷新方块占用情况
-                    this.refreshGridOccupation();
                 }
             } else if (this.blockManager.tryPlaceBlockToGrid(this.currentDragBlock)) {
                 await this.handleSuccessfulPlacement(startLocation);
-                // 放置成功后输出格子占用情况
-                this.blockManager.printGridOccupationMatrix();
-                // 刷新方块占用情况
-                this.refreshGridOccupation();
             } else {
                 console.log(`[GameBlockSelection] 方块 ${this.currentDragBlock.name} 放置到网格失败`);
                 console.log(`[GameBlockSelection] 方块标签状态:`, BlockTag.hasTag(this.currentDragBlock));
@@ -709,17 +717,9 @@ export class GameBlockSelection extends Component {
                         this.blockManager.deductPlayerCoins(price);
                     }
                     // 当前拖拽块已被销毁(合并时),无需复位
-                    // 合成成功后输出格子占用情况
-                    this.blockManager.printGridOccupationMatrix();
-                    // 刷新方块占用情况
-                    this.refreshGridOccupation();
                 } else {
                     console.log(`[GameBlockSelection] 方块 ${this.currentDragBlock.name} 合成也失败,返回原位置`);
                     this.returnBlockToOriginalPosition();
-                    // 放置失败后输出格子占用情况
-                    this.blockManager.printGridOccupationMatrix();
-                    // 刷新方块占用情况
-                    this.refreshGridOccupation();
                 }
             }
         } catch (error) {
@@ -728,6 +728,8 @@ export class GameBlockSelection extends Component {
             this.returnBlockToOriginalPosition();
             throw error; // 重新抛出错误,让上层处理
         }
+        // 刷新方块占用情况
+        this.refreshGridOccupation();
     }
     
     // 检查是否在kuang区域内
@@ -760,7 +762,6 @@ export class GameBlockSelection extends Component {
             this.currentDragBlock.position = originalPos.clone();
         }
         
-        this.blockManager.restoreBlockOccupiedGrids(this.currentDragBlock);
         this.blockManager.blockLocations.set(this.currentDragBlock, 'kuang');
         this.blockManager.showPriceLabel(this.currentDragBlock);
         
@@ -865,7 +866,6 @@ export class GameBlockSelection extends Component {
             this.currentDragBlock.position = this.blockStartPos.clone();
         }
         
-        this.blockManager.restoreBlockOccupiedGrids(this.currentDragBlock);
         this.blockManager.showPriceLabel(this.currentDragBlock);
         
         const dbNode = this.currentDragBlock['dbNode'];
@@ -873,9 +873,6 @@ export class GameBlockSelection extends Component {
             dbNode.active = true;
             this.currentDragBlock.emit(Node.EventType.TRANSFORM_CHANGED);
         }
-        
-        // 刷新方块占用情况
-        this.refreshGridOccupation();
     }
 
     // === 调试绘制相关方法 ===