import UEBase from "../../frameWork/compment/UEBase"; import { GridConstant } from "./GridConstant"; import UECell from "./UECell"; const { ccclass, property } = cc._decorator; const DRAG_THRESHOLD: number = 10; // 拖动判定阈值(像素) @ccclass export default class UEGridMap extends UEBase { static readonly BundleKey: string = "gridMap"; static readonly PrefabUrl: string = "UEGridMap"; static readonly CLS: string = "UEGridMap"; @property(cc.Prefab) cellPrefab: cc.Prefab = null!; @property(cc.Node) gridCenter: cc.Node = null; @property(cc.Node) gridLayer: cc.Node = null; gridMap: UECell[][] = []; private isDragging: boolean = false; private dragStartPos: cc.Vec2 = cc.v2(0, 0); private selectedCell: UECell = null!; private clickCnt: number = 0; Init() { const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2; const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2; this.gridLayer.setContentSize(GridConstant.CELL_WIDTH * GridConstant.ROW, GridConstant.CELL_HEIGHT * GridConstant.COL); let idx = 0; for (let i = 0; i < GridConstant.COL; i++) { let row = []; for (let j = 0; j < GridConstant.ROW; j++) { let cell = cc.instantiate(this.cellPrefab).getComponent(UECell); this.gridLayer.addChild(cell.node); cell.node.width = GridConstant.CELL_WIDTH; cell.node.height = GridConstant.CELL_HEIGHT; cell.node.setPosition(cc.v2( startX + j * GridConstant.CELL_WIDTH + GridConstant.CELL_WIDTH / 2, startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2 )); row.push(cell); cell.Init({ id: idx, type: 1, zIndex: idx }, idx); idx++; } this.gridMap.push(row); } this.InitEvent(); } InitEvent(): void { this.gridLayer.on(cc.Node.EventType.TOUCH_START, this.OnTouchStart, this); this.gridLayer.on(cc.Node.EventType.TOUCH_MOVE, this.OnTouchMove, this); this.gridLayer.on(cc.Node.EventType.TOUCH_END, this.OnTouchEnd, this); this.gridLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.OnTouchEnd, this); } private OnTouchStart(event: cc.Event.EventTouch): void { const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation()); const cell = this.GetCellByPos(touchPos); if (cell && cell.CanDrag()) { this.dragStartPos = touchPos; if (this.selectedCell && this.selectedCell != cell) { this.selectedCell.SetSelect(false); this.clickCnt = 1; } else if (!this.selectedCell) { this.clickCnt = 1; } this.selectedCell = cell; cell.SetSelect(true); } } private OnTouchMove(event: cc.Event.EventTouch): void { if (!this.selectedCell) return; const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation()); const distance = touchPos.sub(this.dragStartPos).mag(); // 只有当移动距离超过阈值时才开始拖动 if (!this.isDragging && distance >= DRAG_THRESHOLD) { this.isDragging = true; this.selectedCell.StartDrag(); this.selectedCell.SetSelect(false); } if (this.isDragging) { this.selectedCell.UpdateDragPosition(cc.v3(touchPos.x, touchPos.y)); } } private OnTouchEnd(event: cc.Event.EventTouch): void { if (!this.selectedCell) return; const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation()); const targetCell = this.GetCellByPos(touchPos); if (!this.isDragging && this.selectedCell === targetCell) { //二次点击同一个格子 targetCell.PlayJellyAnim(); if (this.clickCnt >= 2) { console.log("二次点击同一个格子"); } else { this.clickCnt++; } } else { if (!this.isDragging) return; if (targetCell) { this.TryMergeItems(this.selectedCell, targetCell); this.selectedCell.SetSelect(false); targetCell.SetSelect(true); } this.selectedCell.EndDrag(); this.isDragging = false; this.selectedCell = targetCell; } } private GetCellByPos(pos: cc.Vec2): UECell | null { const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2; const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2; const row = Math.floor((pos.x - startX) / GridConstant.CELL_WIDTH); const col = Math.floor((startY - pos.y) / GridConstant.CELL_HEIGHT); if (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) { return this.gridMap[col][row]; } return null; } /** 尝试合成 */ private TryMergeItems(fromCell: UECell, toCell: UECell): void { if (fromCell.cellData.type == toCell.cellData.type && fromCell.cellData.id != toCell.cellData.id && fromCell.cellData.level == toCell.cellData.level) { toCell.PlayMergeAnim(); } } }