import UEBase from "../../frameWork/compment/UEBase"; import { GridConstant } from "./GridConstant"; import UECell from "./UECell"; const { ccclass, property } = cc._decorator; @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!; 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(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.node.convertToNodeSpaceAR(event.getLocation()); const cell = this.getCellByPos(touchPos); if (cell && cell.hasItem()) { this.isDragging = true; this.dragStartPos = touchPos; this.selectedCell = cell; cell.startDrag(); } } private onTouchMove(event: cc.Event.EventTouch): void { if (!this.isDragging || !this.selectedCell) return; const touchPos = this.node.convertToNodeSpaceAR(event.getLocation()); this.selectedCell.updateDragPosition(cc.v3(touchPos.x, touchPos.y)); } private onTouchEnd(event: cc.Event.EventTouch): void { if (!this.isDragging || !this.selectedCell) return; const touchPos = this.node.convertToNodeSpaceAR(event.getLocation()); const targetCell = this.getCellByPos(touchPos); if (targetCell && targetCell !== this.selectedCell) { this.tryMergeItems(this.selectedCell, targetCell); } this.selectedCell.endDrag(); this.isDragging = false; this.selectedCell = null; } private getCellByPos(pos: cc.Vec2): UECell | null { const row = Math.floor(-pos.y / GridConstant.CELL_HEIGHT); const col = Math.floor(pos.x / GridConstant.CELL_WIDTH); if (row >= 0 && row < GridConstant.ROW && col >= 0 && col < GridConstant.COL) { return this.gridMap[row][col]; } return null; } private tryMergeItems(fromCell: UECell, toCell: UECell): void { if (fromCell.canMergeWith(toCell)) { toCell.mergeFrom(fromCell); } } }