UEGridMap.ts 14 KB

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