UEGridMap.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import UEBase from "../../frameWork/compment/UEBase";
  2. import { GridConstant } from "./GridConstant";
  3. import UECell from "./UECell";
  4. const { ccclass, property } = cc._decorator;
  5. @ccclass
  6. export default class UEGridMap extends UEBase {
  7. static readonly BundleKey: string = "gridMap";
  8. static readonly PrefabUrl: string = "UEGridMap";
  9. static readonly CLS: string = "UEGridMap";
  10. @property(cc.Prefab)
  11. cellPrefab: cc.Prefab = null!;
  12. @property(cc.Node)
  13. gridCenter: cc.Node = null;
  14. @property(cc.Node)
  15. gridLayer: cc.Node = null;
  16. gridMap: UECell[][] = [];
  17. private isDragging: boolean = false;
  18. private dragStartPos: cc.Vec2 = cc.v2(0, 0);
  19. private selectedCell: UECell = null!;
  20. Init() {
  21. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  22. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  23. this.gridLayer.setContentSize(GridConstant.CELL_WIDTH * GridConstant.ROW, GridConstant.CELL_HEIGHT * GridConstant.COL);
  24. let idx = 0;
  25. for (let i = 0; i < GridConstant.COL; i++) {
  26. let row = [];
  27. for (let j = 0; j < GridConstant.ROW; j++) {
  28. let cell = cc.instantiate(this.cellPrefab).getComponent(UECell);
  29. this.gridLayer.addChild(cell.node);
  30. cell.node.width = GridConstant.CELL_WIDTH;
  31. cell.node.height = GridConstant.CELL_HEIGHT;
  32. cell.node.setPosition(cc.v2(
  33. startX + j * GridConstant.CELL_WIDTH + GridConstant.CELL_WIDTH / 2,
  34. startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2
  35. ));
  36. row.push(cell);
  37. cell.Init(idx);
  38. idx++;
  39. }
  40. this.gridMap.push(row);
  41. }
  42. this.initEvent();
  43. }
  44. initEvent(): void {
  45. this.gridLayer.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
  46. this.gridLayer.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  47. this.gridLayer.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  48. this.gridLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  49. }
  50. private onTouchStart(event: cc.Event.EventTouch): void {
  51. const touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
  52. const cell = this.getCellByPos(touchPos);
  53. if (cell && cell.hasItem()) {
  54. this.isDragging = true;
  55. this.dragStartPos = touchPos;
  56. this.selectedCell = cell;
  57. cell.startDrag();
  58. }
  59. }
  60. private onTouchMove(event: cc.Event.EventTouch): void {
  61. if (!this.isDragging || !this.selectedCell) return;
  62. const touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
  63. this.selectedCell.updateDragPosition(cc.v3(touchPos.x, touchPos.y));
  64. }
  65. private onTouchEnd(event: cc.Event.EventTouch): void {
  66. if (!this.isDragging || !this.selectedCell) return;
  67. const touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
  68. const targetCell = this.getCellByPos(touchPos);
  69. if (targetCell && targetCell !== this.selectedCell) {
  70. this.tryMergeItems(this.selectedCell, targetCell);
  71. }
  72. this.selectedCell.endDrag();
  73. this.isDragging = false;
  74. this.selectedCell = null;
  75. }
  76. private getCellByPos(pos: cc.Vec2): UECell | null {
  77. const row = Math.floor(-pos.y / GridConstant.CELL_HEIGHT);
  78. const col = Math.floor(pos.x / GridConstant.CELL_WIDTH);
  79. if (row >= 0 && row < GridConstant.ROW && col >= 0 && col < GridConstant.COL) {
  80. return this.gridMap[row][col];
  81. }
  82. return null;
  83. }
  84. private tryMergeItems(fromCell: UECell, toCell: UECell): void {
  85. if (fromCell.canMergeWith(toCell)) {
  86. toCell.mergeFrom(fromCell);
  87. }
  88. }
  89. }