UEGridMap.ts 15 KB

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