UEGridMap.ts 14 KB

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