UEGridMap.ts 16 KB

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