UEGridMap.ts 15 KB

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