|
|
@@ -40,6 +40,8 @@ export class BlockManager extends Component {
|
|
|
// 网格偏移修正
|
|
|
private readonly GRID_OFFSET_X = 0;
|
|
|
private readonly GRID_OFFSET_Y = 0;
|
|
|
+ // 添加一个新属性来保存方块的原始占用格子
|
|
|
+ private tempRemovedOccupiedGrids: { block: Node, occupiedGrids: { row: number, col: number }[] }[] = [];
|
|
|
|
|
|
start() {
|
|
|
// 如果没有指定GridContainer,尝试找到它
|
|
|
@@ -212,15 +214,18 @@ export class BlockManager extends Component {
|
|
|
// 将块置于顶层
|
|
|
block.setSiblingIndex(this.node.children.length - 1);
|
|
|
|
|
|
- // 如果方块已经放置在网格中,移除占用状态
|
|
|
- this.removeBlockFromGrid(block);
|
|
|
+ // 临时保存方块占用的网格,而不是直接移除
|
|
|
+ this.tempStoreBlockOccupiedGrids(block);
|
|
|
|
|
|
// 更新并打印网格占用情况
|
|
|
this.printGridOccupation();
|
|
|
|
|
|
// 输出方块B1节点(根节点)的世界坐标
|
|
|
- const blockWorldPos = block.parent.getComponent(UITransform).convertToWorldSpaceAR(block.position);
|
|
|
- console.log(`拖拽开始 - 方块 ${block.name} B1节点(根节点)世界坐标: (${blockWorldPos.x}, ${blockWorldPos.y})`);
|
|
|
+ const b1Node = block.name === 'B1' ? block : block.getChildByName('B1');
|
|
|
+ if (b1Node) {
|
|
|
+ const blockWorldPos = b1Node.parent.getComponent(UITransform).convertToWorldSpaceAR(b1Node.position);
|
|
|
+ console.log(`拖拽开始 - 方块 ${block.name} B1节点世界坐标: (${blockWorldPos.x}, ${blockWorldPos.y})`);
|
|
|
+ }
|
|
|
}, this);
|
|
|
|
|
|
// 添加触摸移动事件
|
|
|
@@ -243,16 +248,25 @@ export class BlockManager extends Component {
|
|
|
// 添加触摸结束事件
|
|
|
block.on(Node.EventType.TOUCH_END, (event: EventTouch) => {
|
|
|
if (this.currentDragBlock) {
|
|
|
- // 输出拖拽结束时方块B1节点(根节点)的世界坐标
|
|
|
- const blockWorldPos = this.currentDragBlock.parent.getComponent(UITransform).convertToWorldSpaceAR(this.currentDragBlock.position);
|
|
|
- console.log(`拖拽结束 - 方块 ${this.currentDragBlock.name} B1节点(根节点)世界坐标: (${blockWorldPos.x}, ${blockWorldPos.y})`);
|
|
|
+ // 输出拖拽结束时方块B1节点的世界坐标
|
|
|
+ const b1Node = this.currentDragBlock.name === 'B1' ? this.currentDragBlock : this.currentDragBlock.getChildByName('B1');
|
|
|
+ if (b1Node) {
|
|
|
+ const blockWorldPos = b1Node.parent.getComponent(UITransform).convertToWorldSpaceAR(b1Node.position);
|
|
|
+ console.log(`拖拽结束 - 方块 ${this.currentDragBlock.name} B1节点世界坐标: (${blockWorldPos.x}, ${blockWorldPos.y})`);
|
|
|
+ }
|
|
|
|
|
|
// 尝试将方块放置到网格中
|
|
|
if (!this.tryPlaceBlockToGrid(this.currentDragBlock)) {
|
|
|
// 放置失败,返回原位
|
|
|
this.currentDragBlock.position = this.blockStartPos.clone();
|
|
|
console.log('方块放置失败,返回原位');
|
|
|
+
|
|
|
+ // 恢复方块原来的占用状态
|
|
|
+ this.restoreBlockOccupiedGrids(this.currentDragBlock);
|
|
|
} else {
|
|
|
+ // 放置成功,清除临时保存的占用状态
|
|
|
+ this.clearTempStoredOccupiedGrids(this.currentDragBlock);
|
|
|
+
|
|
|
// 更新并打印网格占用情况
|
|
|
this.printGridOccupation();
|
|
|
}
|
|
|
@@ -265,6 +279,10 @@ export class BlockManager extends Component {
|
|
|
if (this.currentDragBlock) {
|
|
|
// 触摸取消,返回原位
|
|
|
this.currentDragBlock.position = this.blockStartPos.clone();
|
|
|
+
|
|
|
+ // 恢复方块原来的占用状态
|
|
|
+ this.restoreBlockOccupiedGrids(this.currentDragBlock);
|
|
|
+
|
|
|
this.currentDragBlock = null;
|
|
|
}
|
|
|
}, this);
|
|
|
@@ -544,6 +562,69 @@ export class BlockManager extends Component {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 临时保存方块占用的网格
|
|
|
+ tempStoreBlockOccupiedGrids(block: Node) {
|
|
|
+ // 获取方块占用的网格
|
|
|
+ const occupiedGrids = block['occupiedGrids'];
|
|
|
+ if (!occupiedGrids || occupiedGrids.length === 0) return;
|
|
|
+
|
|
|
+ console.log(`临时保存占用: 方块=${block.name}, 占用网格数=${occupiedGrids.length}`);
|
|
|
+
|
|
|
+ // 保存到临时数组
|
|
|
+ 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];
|
|
|
+ console.log(`恢复占用: 方块=${block.name}, 占用网格数=${savedItem.occupiedGrids.length}`);
|
|
|
+
|
|
|
+ // 恢复占用标记
|
|
|
+ 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);
|
|
|
+
|
|
|
+ // 更新并打印网格占用情况
|
|
|
+ this.printGridOccupation();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清除临时保存的占用状态
|
|
|
+ clearTempStoredOccupiedGrids(block: Node) {
|
|
|
+ // 查找临时保存的占用状态
|
|
|
+ const index = this.tempRemovedOccupiedGrids.findIndex(item => item.block === block);
|
|
|
+ if (index === -1) return;
|
|
|
+
|
|
|
+ // 从临时数组中移除
|
|
|
+ this.tempRemovedOccupiedGrids.splice(index, 1);
|
|
|
+ }
|
|
|
+
|
|
|
// 移除方块占用的格子
|
|
|
removeBlockFromGrid(block: Node) {
|
|
|
// 获取方块占用的网格
|