UEGridMap.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import Gamecfg from "../../common/gameCfg";
  2. import { xlsChapterLayout } from "../../common/xlsConfig";
  3. import UEBase from "../../frameWork/compment/UEBase";
  4. import { GridConstant } from "./GridConstant";
  5. import UECell, { I_CellData } from "./UECell";
  6. import UECube from "./UECube";
  7. const { ccclass, property } = cc._decorator;
  8. const DRAG_THRESHOLD: number = 10; // 拖动判定阈值(像素)
  9. @ccclass
  10. export default class UEGridMap extends UEBase {
  11. static readonly BundleKey: string = "gridMap";
  12. static readonly PrefabUrl: string = "UEGridMap";
  13. static readonly CLS: string = "UEGridMap";
  14. @property(cc.Prefab)
  15. cellPrefab: cc.Prefab = null!;
  16. @property(cc.Prefab)
  17. cubePrefab: cc.Prefab = null!;
  18. @property(cc.Node)
  19. gridLayer: cc.Node = null;
  20. @property(cc.Node)
  21. cellLayer: cc.Node = null;
  22. @property(cc.Node)
  23. cubeLayer: cc.Node = null;
  24. cellMap: { [gid: number]: UECell } = {};
  25. private isDragging: boolean = false;
  26. private dragStartPos: cc.Vec2 = cc.v2(0, 0);
  27. private selectedCell: UECell = null!;
  28. private clickCnt: number = 0;
  29. Init() {
  30. this.gridLayer.setContentSize(GridConstant.CELL_WIDTH * GridConstant.ROW, GridConstant.CELL_HEIGHT * GridConstant.COL);
  31. this.LoadMapData();
  32. this.InitEvent();
  33. }
  34. InitEvent(): void {
  35. this.gridLayer.on(cc.Node.EventType.TOUCH_START, this.OnTouchStart, this);
  36. this.gridLayer.on(cc.Node.EventType.TOUCH_MOVE, this.OnTouchMove, this);
  37. this.gridLayer.on(cc.Node.EventType.TOUCH_END, this.OnTouchEnd, this);
  38. this.gridLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.OnTouchEnd, this);
  39. }
  40. /** 加载地图数据 */
  41. private LoadMapData() {
  42. let chapterInfoCfg = Gamecfg.chapterInfo.getItem("1");
  43. let layoutCfg = Gamecfg.chapterLayoutList.getItemList("1");
  44. let layoutGridMap: { [grid: number]: xlsChapterLayout } = {};
  45. layoutCfg.forEach(element => {
  46. layoutGridMap[element.grid] = element;
  47. });
  48. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  49. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  50. this.cellMap = {};
  51. for (let i = 0; i < GridConstant.COL; i++) {
  52. let rowCells = [];
  53. let rowCubes = [];
  54. for (let j = 0; j < GridConstant.ROW; j++) {
  55. let idx = (i + 1) * 10 + (j + 1);
  56. if (chapterInfoCfg.grid.indexOf(idx) != -1) {
  57. let cell = this.CreateCell(i, j);
  58. rowCells.push(cell);
  59. let cellCfg: xlsChapterLayout = layoutGridMap[idx];
  60. let cube = null;
  61. if (cellCfg) {
  62. cube = cc.instantiate(this.cubePrefab).getComponent(UECube);
  63. rowCubes.push(cube);
  64. this.cubeLayer.addChild(cube.node);
  65. cube.node.width = GridConstant.CELL_WIDTH;
  66. cube.node.height = GridConstant.CELL_HEIGHT;
  67. cube.node.setPosition(cc.v2(
  68. startX + j * GridConstant.CELL_WIDTH + GridConstant.CELL_WIDTH / 2,
  69. startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2
  70. ));
  71. cube.Init({
  72. type: cellCfg.type,
  73. id: cellCfg.correlationId,
  74. zIndex: idx,
  75. unlock: cellCfg.unlock
  76. }, idx)
  77. }
  78. cell.Init({
  79. zIndex: idx,
  80. ueCube: cube
  81. });
  82. this.cellMap[idx] = cell;
  83. }
  84. }
  85. }
  86. }
  87. /** 创建格子 */
  88. private CreateCell(i: number, j: number) {
  89. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  90. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  91. let cell = cc.instantiate(this.cellPrefab).getComponent(UECell);
  92. this.cellLayer.addChild(cell.node);
  93. cell.node.width = GridConstant.CELL_WIDTH;
  94. cell.node.height = GridConstant.CELL_HEIGHT;
  95. cell.node.setPosition(cc.v2(
  96. startX + j * GridConstant.CELL_WIDTH + GridConstant.CELL_WIDTH / 2,
  97. startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2
  98. ));
  99. return cell;
  100. }
  101. private OnTouchStart(event: cc.Event.EventTouch): void {
  102. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  103. const cell = this.GetCellByPos(touchPos);
  104. if (cell && cell.CanDrag()) {
  105. this.dragStartPos = touchPos;
  106. if (this.selectedCell && this.selectedCell != cell) {
  107. this.selectedCell.SetSelect(false);
  108. this.clickCnt = 1;
  109. } else if (!this.selectedCell) {
  110. this.clickCnt = 1;
  111. }
  112. this.selectedCell = cell;
  113. cell.SetSelect(true);
  114. }
  115. }
  116. private OnTouchMove(event: cc.Event.EventTouch): void {
  117. if (!this.selectedCell) return;
  118. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  119. const distance = touchPos.sub(this.dragStartPos).mag();
  120. // 只有当移动距离超过阈值时才开始拖动
  121. if (!this.isDragging && distance >= DRAG_THRESHOLD) {
  122. this.isDragging = true;
  123. this.selectedCell.GetCube().StartDrag();
  124. this.selectedCell.SetSelect(false);
  125. }
  126. if (this.isDragging) {
  127. // 计算移动差值并应用到cube节点
  128. const deltaPos = touchPos.sub(this.dragStartPos);
  129. const cube = this.selectedCell.GetCube();
  130. const originalPos = cube.node.getPosition();
  131. cube.node.setPosition(originalPos.x + deltaPos.x, originalPos.y + deltaPos.y);
  132. this.dragStartPos = touchPos;
  133. }
  134. }
  135. private OnTouchEnd(event: cc.Event.EventTouch): void {
  136. if (!this.selectedCell) return;
  137. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  138. const targetCell = this.GetCellByPos(touchPos);
  139. if (!this.isDragging && this.selectedCell === targetCell) {
  140. //二次点击同一个格子
  141. if (!targetCell.IsEmpty()) {
  142. targetCell.GetCube().PlayJellyAnim();
  143. if (this.clickCnt >= 2) {
  144. console.log("二次点击同一个格子");
  145. } else {
  146. this.clickCnt++;
  147. }
  148. }
  149. } else {
  150. if (!this.isDragging) return;
  151. if (targetCell) {
  152. this.TryMergeItems(this.selectedCell, targetCell);
  153. this.selectedCell.SetSelect(false);
  154. targetCell.SetSelect(true);
  155. } else {
  156. this.selectedCell.GetCube().BackToOriginalPos(true);
  157. }
  158. this.isDragging = false;
  159. this.selectedCell = targetCell;
  160. }
  161. }
  162. private GetCellByPos(pos: cc.Vec2): UECell | null {
  163. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  164. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  165. const row = Math.floor((pos.x - startX) / GridConstant.CELL_WIDTH);
  166. const col = Math.floor((startY - pos.y) / GridConstant.CELL_HEIGHT);
  167. if (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) {
  168. let idx = (col + 1) * 10 + (row + 1);
  169. return this.cellMap[idx];
  170. }
  171. return null;
  172. }
  173. /** 尝试合成 */
  174. private TryMergeItems(fromCell: UECell, toCell: UECell): void {
  175. if (toCell.IsEmpty()) {
  176. //格子上没有物品
  177. fromCell.MoveCubeToCell(toCell);
  178. toCell.SetCube(fromCell.GetCube());
  179. fromCell.SetCube(null);
  180. } else if (this.CanMergeItems(fromCell, toCell)) {
  181. toCell.GetCube().PlayMergeAnim();
  182. } else {
  183. this.SwitchCell(fromCell, toCell);
  184. }
  185. }
  186. private CanMergeItems(fromCell: UECell, toCell: UECell): boolean {
  187. if (fromCell.GetCube().getCubeData().type == toCell.GetCube().getCubeData().type
  188. && fromCell.GetCube().getCubeData().id != fromCell.GetCube().getCubeData().id
  189. ) {
  190. return true;
  191. }
  192. return false;
  193. }
  194. private SwitchCell(fromCell: UECell, toCell: UECell): void {
  195. //交换格子
  196. let fromCube = fromCell.GetCube();
  197. let toCube = toCell.GetCube();
  198. fromCell.SetCube(toCube);
  199. toCell.SetCube(fromCube);
  200. toCube.MoveToNewPos(cc.v3(fromCell.node.x, fromCell.node.y));
  201. fromCube.MoveToNewPos(cc.v3(toCell.node.x, toCell.node.y), 0.05);
  202. }
  203. /** 抛出新的物品 */
  204. private ThrowNewItem() {
  205. }
  206. }