UEGridMap.ts 15 KB

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