UEGridMap.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import Gamecfg from "../../common/gameCfg";
  2. import { gameMethod } from "../../common/gameMethod";
  3. import { xlsChapterLayout } from "../../common/xlsConfig";
  4. import UEBase from "../../frameWork/compment/UEBase";
  5. import { uiCommon } from "../../utils/UICommon";
  6. import { GridConstant } from "./GridConstant";
  7. import { GridEvent } from "./GridEvent";
  8. import UECell, { I_CellData } from "./UECell";
  9. import UECube from "./UECube";
  10. const { ccclass, property } = cc._decorator;
  11. const DRAG_THRESHOLD: number = 10; // 拖动判定阈值(像素)
  12. @ccclass
  13. export default class UEGridMap extends UEBase {
  14. static readonly BundleKey: string = "gridMap";
  15. static readonly PrefabUrl: string = "UEGridMap";
  16. static readonly CLS: string = "UEGridMap";
  17. @property(cc.Prefab)
  18. cellPrefab: cc.Prefab = null!;
  19. @property(cc.Prefab)
  20. cubePrefab: cc.Prefab = null!;
  21. @property(cc.Node)
  22. gridLayer: cc.Node = null;
  23. @property(cc.Node)
  24. cellLayer: cc.Node = null;
  25. @property(cc.Node)
  26. cubeLayer: cc.Node = null;
  27. cellMap: { [gid: number]: UECell } = {};
  28. private isDragging: boolean = false;
  29. private dragStartPos: cc.Vec2 = cc.v2(0, 0);
  30. private selectedCell: UECell = null!;
  31. private clickCnt: number = 0;
  32. Init() {
  33. this.gridLayer.setContentSize(GridConstant.CELL_WIDTH * GridConstant.ROW, GridConstant.CELL_HEIGHT * GridConstant.COL);
  34. this.LoadMapData();
  35. this.InitEvent();
  36. }
  37. InitEvent(): void {
  38. this.gridLayer.on(cc.Node.EventType.TOUCH_START, this.OnTouchStart, this);
  39. this.gridLayer.on(cc.Node.EventType.TOUCH_MOVE, this.OnTouchMove, this);
  40. this.gridLayer.on(cc.Node.EventType.TOUCH_END, this.OnTouchEnd, this);
  41. this.gridLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.OnTouchEnd, this);
  42. this.initEvent(GridEvent.TRIGGER_EMITTER, this.TriggerEmitter);
  43. }
  44. /** 加载地图数据 */
  45. private LoadMapData() {
  46. let chapterInfoCfg = Gamecfg.chapterInfo.getItem("1");
  47. let layoutCfg = Gamecfg.chapterLayoutList.getItemList("1");
  48. let layoutGridMap: { [grid: number]: xlsChapterLayout } = {};
  49. layoutCfg.forEach(element => {
  50. layoutGridMap[element.grid] = element;
  51. });
  52. this.cellMap = {};
  53. for (let i = 0; i < GridConstant.COL; i++) {
  54. let rowCells = [];
  55. let rowCubes = [];
  56. for (let j = 0; j < GridConstant.ROW; j++) {
  57. let idx = (i + 1) * 10 + (j + 1);
  58. if (chapterInfoCfg.grid.indexOf(idx) != -1) {
  59. let cell = this.CreateCell(i, j);
  60. rowCells.push(cell);
  61. let cellCfg: xlsChapterLayout = layoutGridMap[idx];
  62. let cube = null;
  63. if (cellCfg) {
  64. cube = this.CreateCube(i, j);
  65. rowCubes.push(cube);
  66. cube.Init({
  67. type: cellCfg.type,
  68. id: cellCfg.correlationId,
  69. zIndex: idx,
  70. unlock: cellCfg.unlock
  71. }, idx)
  72. }
  73. cell.Init({
  74. zIndex: idx,
  75. ueCube: cube
  76. });
  77. this.cellMap[idx] = cell;
  78. }
  79. }
  80. }
  81. }
  82. /** 根据索引获取实际像素坐标 */
  83. private GetPosByIdx(i: number, j: number): cc.Vec3 {
  84. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  85. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  86. return cc.v3(
  87. startX + j * GridConstant.CELL_WIDTH + GridConstant.CELL_WIDTH / 2,
  88. startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2
  89. )
  90. }
  91. /** 创建格子 */
  92. private CreateCell(i: number, j: number) {
  93. let cell = cc.instantiate(this.cellPrefab).getComponent(UECell);
  94. this.cellLayer.addChild(cell.node);
  95. cell.node.width = GridConstant.CELL_WIDTH;
  96. cell.node.height = GridConstant.CELL_HEIGHT;
  97. let pos = this.GetPosByIdx(i, j);
  98. cell.node.setPosition(pos);
  99. return cell;
  100. }
  101. /** 创建棋子 */
  102. private CreateCube(i: number, j: number) {
  103. let cube = cc.instantiate(this.cubePrefab).getComponent(UECube);
  104. this.cubeLayer.addChild(cube.node);
  105. cube.node.width = GridConstant.CELL_WIDTH;
  106. cube.node.height = GridConstant.CELL_HEIGHT;
  107. let pos = this.GetPosByIdx(i, j);
  108. cube.node.setPosition(pos);
  109. return cube;
  110. }
  111. private OnTouchStart(event: cc.Event.EventTouch): void {
  112. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  113. const cell = this.GetCellByPos(touchPos);
  114. if (cell && cell.CanDrag()) {
  115. this.dragStartPos = touchPos;
  116. if (this.selectedCell && this.selectedCell != cell) {
  117. this.selectedCell.SetSelect(false);
  118. this.clickCnt = 1;
  119. } else if (!this.selectedCell) {
  120. this.clickCnt = 1;
  121. }
  122. this.selectedCell = cell;
  123. cell.SetSelect(true);
  124. }
  125. }
  126. private OnTouchMove(event: cc.Event.EventTouch): void {
  127. if (!this.selectedCell || !this.dragStartPos) return;
  128. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  129. const distance = touchPos.sub(this.dragStartPos).mag();
  130. // 只有当移动距离超过阈值时才开始拖动
  131. if (!this.isDragging && distance >= DRAG_THRESHOLD) {
  132. this.isDragging = true;
  133. this.selectedCell.GetCube().StartDrag();
  134. this.selectedCell.SetSelect(false);
  135. }
  136. if (this.isDragging) {
  137. // 计算移动差值并应用到cube节点
  138. const deltaPos = touchPos.sub(this.dragStartPos);
  139. const cube = this.selectedCell.GetCube();
  140. const originalPos = cube.node.getPosition();
  141. cube.node.setPosition(originalPos.x + deltaPos.x, originalPos.y + deltaPos.y);
  142. this.dragStartPos = touchPos;
  143. }
  144. }
  145. private OnTouchEnd(event: cc.Event.EventTouch): void {
  146. this.dragStartPos = null;
  147. if (!this.selectedCell) return;
  148. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  149. const targetCell = this.GetCellByPos(touchPos);
  150. if (!this.isDragging && this.selectedCell === targetCell) {
  151. //二次点击同一个格子
  152. if (!targetCell.IsEmpty()) {
  153. targetCell.GetCube().PlayJellyAnim();
  154. if (this.clickCnt >= 2) {
  155. console.log("二次点击同一个格子");
  156. targetCell.GetCube().TriggerClick();
  157. } else {
  158. this.clickCnt++;
  159. }
  160. }
  161. } else {
  162. if (!this.isDragging) return;
  163. if (targetCell && targetCell != this.selectedCell) {
  164. this.TryMergeItems(this.selectedCell, targetCell);
  165. this.selectedCell.SetSelect(false);
  166. targetCell.SetSelect(true);
  167. } else {
  168. this.selectedCell.GetCube().BackToOriginalPos(true);
  169. }
  170. this.isDragging = false;
  171. this.selectedCell = targetCell;
  172. }
  173. }
  174. /** 根据像素坐标获取格子 */
  175. private GetCellByPos(pos: cc.Vec2): UECell | null {
  176. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  177. const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
  178. const row = Math.floor((pos.x - startX) / GridConstant.CELL_WIDTH);
  179. const col = Math.floor((startY - pos.y) / GridConstant.CELL_HEIGHT);
  180. if (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) {
  181. let idx = (col + 1) * 10 + (row + 1);
  182. return this.cellMap[idx];
  183. }
  184. return null;
  185. }
  186. /** 尝试合成 */
  187. private TryMergeItems(fromCell: UECell, toCell: UECell): void {
  188. if (toCell.IsEmpty()) {
  189. //格子上没有物品
  190. fromCell.MoveCubeToCell(toCell);
  191. toCell.SetCube(fromCell.GetCube());
  192. fromCell.SetCube(null);
  193. } else if (this.CanMergeItems(fromCell, toCell)) {
  194. toCell.GetCube().PlayMergeAnim();
  195. fromCell.ClearCube();
  196. toCell.ClearCube();
  197. let vec = this.TranIdxToPos(toCell.GetZIndex());
  198. let mergeCube = this.CreateCube(vec.y, vec.x);
  199. mergeCube.Init({
  200. type: 3,
  201. id: 10022,
  202. zIndex: toCell.GetZIndex(),
  203. unlock: 0
  204. });
  205. toCell.SetCube(mergeCube);
  206. } else {
  207. this.SwitchCell(fromCell, toCell);
  208. }
  209. }
  210. private TranIdxToPos(idx: number): cc.Vec2 {
  211. let row = idx % 10;
  212. let col = Math.floor(idx / 10);
  213. return cc.v2(row - 1, col - 1);
  214. }
  215. private CanMergeItems(fromCell: UECell, toCell: UECell): boolean {
  216. if (fromCell.GetCube().GetCubeData().type == toCell.GetCube().GetCubeData().type) {
  217. return true;
  218. }
  219. return false;
  220. }
  221. private SwitchCell(fromCell: UECell, toCell: UECell): void {
  222. //交换格子
  223. let fromCube = fromCell.GetCube();
  224. let toCube = toCell.GetCube();
  225. fromCell.SetCube(toCube);
  226. toCell.SetCube(fromCube);
  227. toCube.MoveToNewPos(cc.v3(fromCell.node.x, fromCell.node.y));
  228. fromCube.MoveToNewPos(cc.v3(toCell.node.x, toCell.node.y), 0.05);
  229. }
  230. private GetEmptyCell(): UECell {
  231. for (const key in this.cellMap) {
  232. const element = this.cellMap[key];
  233. if (element.IsEmpty()) {
  234. return element;
  235. }
  236. }
  237. return null;
  238. }
  239. /** 发射出新的物品 */
  240. private TriggerEmitter(idx: number, itemId: number, targetIdx: number) {
  241. let emptyCell = this.GetEmptyCell();
  242. if (!emptyCell) return;
  243. targetIdx = emptyCell.GetZIndex();
  244. let startPos = this.TranIdxToPos(idx);
  245. let mergeCube = this.CreateCube(startPos.y, startPos.x);
  246. mergeCube.Init({
  247. type: 3,
  248. id: itemId,
  249. zIndex: idx,
  250. unlock: 0
  251. });
  252. emptyCell.SetCube(mergeCube);
  253. mergeCube.SetZIndex(1000);
  254. let targetPos = this.TranIdxToPos(targetIdx);
  255. // 设置动画时间
  256. const duration = 0.8;
  257. // 设置最大高度(向上弹跳的高度)
  258. const maxHeight = 400;
  259. // 计算起点和终点
  260. const startWorldPos = mergeCube.node.getPosition();
  261. const endWorldPos = this.GetPosByIdx(targetPos.y, targetPos.x);
  262. // 计算方向向量和总距离
  263. const moveVec = cc.v2(endWorldPos.x - startWorldPos.x, endWorldPos.y - startWorldPos.y);
  264. const totalDistance = moveVec.mag();
  265. const normalizedDir = moveVec.normalize();
  266. // 计算最后弹跳的起点(在终点前100像素)
  267. const bounceStartPos = cc.v3(
  268. endWorldPos.x - normalizedDir.x * 100,
  269. endWorldPos.y - normalizedDir.y * 100,
  270. 0
  271. );
  272. // 创建第一段抛物线(抛向空中然后落到弹跳起点)
  273. const bezier1 = [];
  274. const midPoint1 = cc.v3(
  275. startWorldPos.x + moveVec.x * 0.3,
  276. Math.max(startWorldPos.y, bounceStartPos.y) + maxHeight,
  277. 0
  278. );
  279. bezier1.push(cc.v2(startWorldPos.x, startWorldPos.y));
  280. bezier1.push(cc.v2(midPoint1.x, midPoint1.y));
  281. bezier1.push(cc.v2(bounceStartPos.x, bounceStartPos.y));
  282. // 创建最后的弹跳到终点
  283. const bezier2 = [];
  284. const bounceHeight = 80; // 弹跳高度
  285. // 计算弹跳的最高点(在起跳点和终点之间)
  286. const bounceTopPoint = cc.v2(
  287. (bounceStartPos.x + endWorldPos.x) / 2, // 水平位置在中间
  288. endWorldPos.y + bounceHeight // 垂直高度为弹跳高度
  289. );
  290. // const bounceSecondPos = cc.v3(
  291. // endWorldPos.x - normalizedDir.x * 100,
  292. // endWorldPos.y - normalizedDir.y * 100,
  293. // 0
  294. // );
  295. // 最后一次弹跳到终点
  296. bezier2.push(cc.v2(bounceStartPos.x, bounceStartPos.y)); // 起跳点
  297. bezier2.push(bounceTopPoint); // 最高点
  298. bezier2.push(cc.v2(endWorldPos.x, endWorldPos.y)); // 终点
  299. // 创建动作序列
  300. const bezierAction1 = cc.bezierTo(duration * 0.8, bezier1);
  301. const bezierAction2 = cc.bezierTo(duration * 0.2, bezier2);
  302. // 组合所有动作
  303. const seq = cc.sequence(
  304. bezierAction1,
  305. bezierAction2,
  306. cc.callFunc(() => {
  307. mergeCube.SetZIndex(targetIdx);
  308. })
  309. );
  310. // 执行动画
  311. mergeCube.node.runAction(seq);
  312. }
  313. }