UEGridMap.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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({
  38. id: idx, type: 1,
  39. zIndex: idx
  40. }, idx);
  41. idx++;
  42. }
  43. this.gridMap.push(row);
  44. }
  45. this.InitEvent();
  46. }
  47. InitEvent(): void {
  48. this.gridLayer.on(cc.Node.EventType.TOUCH_START, this.OnTouchStart, this);
  49. this.gridLayer.on(cc.Node.EventType.TOUCH_MOVE, this.OnTouchMove, this);
  50. this.gridLayer.on(cc.Node.EventType.TOUCH_END, this.OnTouchEnd, this);
  51. this.gridLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.OnTouchEnd, this);
  52. }
  53. private OnTouchStart(event: cc.Event.EventTouch): void {
  54. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  55. const cell = this.GetCellByPos(touchPos);
  56. if (cell && cell.CanDrag()) {
  57. this.isDragging = true;
  58. this.dragStartPos = touchPos;
  59. this.selectedCell = cell;
  60. cell.StartDrag();
  61. }
  62. }
  63. private OnTouchMove(event: cc.Event.EventTouch): void {
  64. if (!this.isDragging || !this.selectedCell) return;
  65. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  66. this.selectedCell.UpdateDragPosition(cc.v3(touchPos.x, touchPos.y));
  67. }
  68. private OnTouchEnd(event: cc.Event.EventTouch): void {
  69. if (!this.isDragging || !this.selectedCell) return;
  70. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  71. const targetCell = this.GetCellByPos(touchPos);
  72. if (targetCell && targetCell !== this.selectedCell) {
  73. this.TryMergeItems(this.selectedCell, targetCell);
  74. }
  75. this.selectedCell.EndDrag();
  76. this.isDragging = false;
  77. this.selectedCell = null;
  78. }
  79. private GetCellByPos(pos: cc.Vec2): UECell | null {
  80. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  81. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  82. const row = Math.floor((pos.x - startX) / GridConstant.CELL_WIDTH);
  83. const col = Math.floor((startY - pos.y) / GridConstant.CELL_HEIGHT);
  84. if (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) {
  85. return this.gridMap[col][row];
  86. }
  87. return null;
  88. }
  89. private TryMergeItems(fromCell: UECell, toCell: UECell): void {
  90. }
  91. }