UEGridMap.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import UEBase from "../../frameWork/compment/UEBase";
  2. import { GridConstant } from "./GridConstant";
  3. import UECell from "./UECell";
  4. const { ccclass, property } = cc._decorator;
  5. const DRAG_THRESHOLD: number = 10; // 拖动判定阈值(像素)
  6. @ccclass
  7. export default class UEGridMap extends UEBase {
  8. static readonly BundleKey: string = "gridMap";
  9. static readonly PrefabUrl: string = "UEGridMap";
  10. static readonly CLS: string = "UEGridMap";
  11. @property(cc.Prefab)
  12. cellPrefab: cc.Prefab = null!;
  13. @property(cc.Node)
  14. gridCenter: cc.Node = null;
  15. @property(cc.Node)
  16. gridLayer: cc.Node = null;
  17. gridMap: UECell[][] = [];
  18. private isDragging: boolean = false;
  19. private dragStartPos: cc.Vec2 = cc.v2(0, 0);
  20. private selectedCell: UECell = null!;
  21. private clickCnt: number = 0;
  22. Init() {
  23. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  24. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  25. this.gridLayer.setContentSize(GridConstant.CELL_WIDTH * GridConstant.ROW, GridConstant.CELL_HEIGHT * GridConstant.COL);
  26. let idx = 0;
  27. for (let i = 0; i < GridConstant.COL; i++) {
  28. let row = [];
  29. for (let j = 0; j < GridConstant.ROW; j++) {
  30. let cell = cc.instantiate(this.cellPrefab).getComponent(UECell);
  31. this.gridLayer.addChild(cell.node);
  32. cell.node.width = GridConstant.CELL_WIDTH;
  33. cell.node.height = GridConstant.CELL_HEIGHT;
  34. cell.node.setPosition(cc.v2(
  35. startX + j * GridConstant.CELL_WIDTH + GridConstant.CELL_WIDTH / 2,
  36. startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2
  37. ));
  38. row.push(cell);
  39. cell.Init({
  40. id: idx, type: 1,
  41. zIndex: idx
  42. }, idx);
  43. idx++;
  44. }
  45. this.gridMap.push(row);
  46. }
  47. this.InitEvent();
  48. }
  49. InitEvent(): void {
  50. this.gridLayer.on(cc.Node.EventType.TOUCH_START, this.OnTouchStart, this);
  51. this.gridLayer.on(cc.Node.EventType.TOUCH_MOVE, this.OnTouchMove, this);
  52. this.gridLayer.on(cc.Node.EventType.TOUCH_END, this.OnTouchEnd, this);
  53. this.gridLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.OnTouchEnd, this);
  54. }
  55. private OnTouchStart(event: cc.Event.EventTouch): void {
  56. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  57. const cell = this.GetCellByPos(touchPos);
  58. if (cell && cell.CanDrag()) {
  59. this.dragStartPos = touchPos;
  60. if (this.selectedCell && this.selectedCell != cell) {
  61. this.selectedCell.SetSelect(false);
  62. this.clickCnt = 1;
  63. } else if (!this.selectedCell) {
  64. this.clickCnt = 1;
  65. }
  66. this.selectedCell = cell;
  67. cell.SetSelect(true);
  68. }
  69. }
  70. private OnTouchMove(event: cc.Event.EventTouch): void {
  71. if (!this.selectedCell) return;
  72. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  73. const distance = touchPos.sub(this.dragStartPos).mag();
  74. // 只有当移动距离超过阈值时才开始拖动
  75. if (!this.isDragging && distance >= DRAG_THRESHOLD) {
  76. this.isDragging = true;
  77. this.selectedCell.StartDrag();
  78. this.selectedCell.SetSelect(false);
  79. }
  80. if (this.isDragging) {
  81. this.selectedCell.UpdateDragPosition(cc.v3(touchPos.x, touchPos.y));
  82. }
  83. }
  84. private OnTouchEnd(event: cc.Event.EventTouch): void {
  85. if (!this.selectedCell) return;
  86. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  87. const targetCell = this.GetCellByPos(touchPos);
  88. if (!this.isDragging && this.selectedCell === targetCell) {
  89. //二次点击同一个格子
  90. targetCell.PlayJellyAnim();
  91. if (this.clickCnt >= 2) {
  92. console.log("二次点击同一个格子");
  93. } else {
  94. this.clickCnt++;
  95. }
  96. } else {
  97. if (!this.isDragging) return;
  98. if (targetCell) {
  99. this.TryMergeItems(this.selectedCell, targetCell);
  100. this.selectedCell.SetSelect(false);
  101. targetCell.SetSelect(true);
  102. }
  103. this.selectedCell.EndDrag();
  104. this.isDragging = false;
  105. this.selectedCell = targetCell;
  106. }
  107. }
  108. private GetCellByPos(pos: cc.Vec2): UECell | null {
  109. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  110. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  111. const row = Math.floor((pos.x - startX) / GridConstant.CELL_WIDTH);
  112. const col = Math.floor((startY - pos.y) / GridConstant.CELL_HEIGHT);
  113. if (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) {
  114. return this.gridMap[col][row];
  115. }
  116. return null;
  117. }
  118. /** 尝试合成 */
  119. private TryMergeItems(fromCell: UECell, toCell: UECell): void {
  120. if (fromCell.cellData.type == toCell.cellData.type &&
  121. fromCell.cellData.id != toCell.cellData.id &&
  122. fromCell.cellData.level == toCell.cellData.level) {
  123. toCell.PlayMergeAnim();
  124. }
  125. }
  126. }