UEGridMap.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. this.cellMap = {};
  49. for (let i = 0; i < GridConstant.COL; i++) {
  50. let rowCells = [];
  51. let rowCubes = [];
  52. for (let j = 0; j < GridConstant.ROW; j++) {
  53. let idx = (i + 1) * 10 + (j + 1);
  54. if (chapterInfoCfg.grid.indexOf(idx) != -1) {
  55. let cell = this.CreateCell(i, j);
  56. rowCells.push(cell);
  57. let cellCfg: xlsChapterLayout = layoutGridMap[idx];
  58. let cube = null;
  59. if (cellCfg) {
  60. cube = this.CreateCube(i, j);
  61. rowCubes.push(cube);
  62. cube.Init({
  63. type: cellCfg.type,
  64. id: cellCfg.correlationId,
  65. zIndex: idx,
  66. unlock: cellCfg.unlock
  67. }, idx)
  68. }
  69. cell.Init({
  70. zIndex: idx,
  71. ueCube: cube
  72. });
  73. this.cellMap[idx] = cell;
  74. }
  75. }
  76. }
  77. }
  78. /** 创建格子 */
  79. private CreateCell(i: number, j: number) {
  80. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  81. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  82. let cell = cc.instantiate(this.cellPrefab).getComponent(UECell);
  83. this.cellLayer.addChild(cell.node);
  84. cell.node.width = GridConstant.CELL_WIDTH;
  85. cell.node.height = GridConstant.CELL_HEIGHT;
  86. cell.node.setPosition(cc.v2(
  87. startX + j * GridConstant.CELL_WIDTH + GridConstant.CELL_WIDTH / 2,
  88. startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2
  89. ));
  90. return cell;
  91. }
  92. /** 创建棋子 */
  93. private CreateCube(i: number, j: number) {
  94. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  95. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  96. let cube = cc.instantiate(this.cubePrefab).getComponent(UECube);
  97. this.cubeLayer.addChild(cube.node);
  98. cube.node.width = GridConstant.CELL_WIDTH;
  99. cube.node.height = GridConstant.CELL_HEIGHT;
  100. cube.node.setPosition(cc.v2(
  101. startX + j * GridConstant.CELL_WIDTH + GridConstant.CELL_WIDTH / 2,
  102. startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2
  103. ));
  104. return cube;
  105. }
  106. private OnTouchStart(event: cc.Event.EventTouch): void {
  107. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  108. const cell = this.GetCellByPos(touchPos);
  109. if (cell && cell.CanDrag()) {
  110. this.dragStartPos = touchPos;
  111. if (this.selectedCell && this.selectedCell != cell) {
  112. this.selectedCell.SetSelect(false);
  113. this.clickCnt = 1;
  114. } else if (!this.selectedCell) {
  115. this.clickCnt = 1;
  116. }
  117. this.selectedCell = cell;
  118. cell.SetSelect(true);
  119. }
  120. }
  121. private OnTouchMove(event: cc.Event.EventTouch): void {
  122. if (!this.selectedCell) return;
  123. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  124. const distance = touchPos.sub(this.dragStartPos).mag();
  125. // 只有当移动距离超过阈值时才开始拖动
  126. if (!this.isDragging && distance >= DRAG_THRESHOLD) {
  127. this.isDragging = true;
  128. this.selectedCell.GetCube().StartDrag();
  129. this.selectedCell.SetSelect(false);
  130. }
  131. if (this.isDragging) {
  132. // 计算移动差值并应用到cube节点
  133. const deltaPos = touchPos.sub(this.dragStartPos);
  134. const cube = this.selectedCell.GetCube();
  135. const originalPos = cube.node.getPosition();
  136. cube.node.setPosition(originalPos.x + deltaPos.x, originalPos.y + deltaPos.y);
  137. this.dragStartPos = touchPos;
  138. }
  139. }
  140. private OnTouchEnd(event: cc.Event.EventTouch): void {
  141. this.dragStartPos = null;
  142. if (!this.selectedCell) return;
  143. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  144. const targetCell = this.GetCellByPos(touchPos);
  145. if (!this.isDragging && this.selectedCell === targetCell) {
  146. //二次点击同一个格子
  147. if (!targetCell.IsEmpty()) {
  148. targetCell.GetCube().PlayJellyAnim();
  149. if (this.clickCnt >= 2) {
  150. console.log("二次点击同一个格子");
  151. } else {
  152. this.clickCnt++;
  153. }
  154. }
  155. } else {
  156. if (!this.isDragging) return;
  157. if (targetCell && targetCell != this.selectedCell) {
  158. this.TryMergeItems(this.selectedCell, targetCell);
  159. this.selectedCell.SetSelect(false);
  160. targetCell.SetSelect(true);
  161. } else {
  162. this.selectedCell.GetCube().BackToOriginalPos(true);
  163. }
  164. this.isDragging = false;
  165. this.selectedCell = targetCell;
  166. }
  167. }
  168. private GetCellByPos(pos: cc.Vec2): UECell | null {
  169. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  170. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  171. const row = Math.floor((pos.x - startX) / GridConstant.CELL_WIDTH);
  172. const col = Math.floor((startY - pos.y) / GridConstant.CELL_HEIGHT);
  173. if (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) {
  174. let idx = (col + 1) * 10 + (row + 1);
  175. return this.cellMap[idx];
  176. }
  177. return null;
  178. }
  179. /** 尝试合成 */
  180. private TryMergeItems(fromCell: UECell, toCell: UECell): void {
  181. if (toCell.IsEmpty()) {
  182. //格子上没有物品
  183. fromCell.MoveCubeToCell(toCell);
  184. toCell.SetCube(fromCell.GetCube());
  185. fromCell.SetCube(null);
  186. } else if (this.CanMergeItems(fromCell, toCell)) {
  187. toCell.GetCube().PlayMergeAnim();
  188. fromCell.ClearCube();
  189. toCell.ClearCube();
  190. let vec = this.TranIdxToPos(toCell.GetZIndex());
  191. let mergeCube = this.CreateCube(vec.y, vec.x);
  192. mergeCube.Init({
  193. type: 3,
  194. id: 10022,
  195. zIndex: toCell.GetZIndex(),
  196. unlock: 0
  197. });
  198. toCell.SetCube(mergeCube);
  199. } else {
  200. this.SwitchCell(fromCell, toCell);
  201. }
  202. }
  203. private TranIdxToPos(idx: number): cc.Vec2 {
  204. let row = idx % 10;
  205. let col = Math.floor(idx / 10);
  206. return cc.v2(row - 1, col - 1);
  207. }
  208. private CanMergeItems(fromCell: UECell, toCell: UECell): boolean {
  209. if (fromCell.GetCube().getCubeData().type == toCell.GetCube().getCubeData().type) {
  210. return true;
  211. }
  212. return false;
  213. }
  214. private SwitchCell(fromCell: UECell, toCell: UECell): void {
  215. //交换格子
  216. let fromCube = fromCell.GetCube();
  217. let toCube = toCell.GetCube();
  218. fromCell.SetCube(toCube);
  219. toCell.SetCube(fromCube);
  220. toCube.MoveToNewPos(cc.v3(fromCell.node.x, fromCell.node.y));
  221. fromCube.MoveToNewPos(cc.v3(toCell.node.x, toCell.node.y), 0.05);
  222. }
  223. /** 抛出新的物品 */
  224. private ThrowNewItem() {
  225. }
  226. }