UEGridMap.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 HomeMergeSellCom from "../fgui/Home/Home/HomeMergeSellCom";
  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_MERGE_RSP, this.OnHcMergeRsp);
  77. this.initEvent(GridEvent.HC_FIGHT_OVER, this.OnHcFightOver);
  78. this.initEvent(GridEvent.HC_CELL_SELECT, this.OnHcCellSelect);
  79. }
  80. public GetCubeLayer() {
  81. return this.cubeLayer;
  82. }
  83. /** 监听格子选中 */
  84. private OnHcCellSelect(idx: number) {
  85. if (!idx) {
  86. this.cellSelect.active = false;
  87. } else {
  88. this.cellSelect.active = true;
  89. this.cellSelect.setPosition(GameDataCenter.gridMap.GetPosByIdx(idx));
  90. }
  91. }
  92. /** 加载地图数据 */
  93. private LoadMapData(data: ResHcInfo) {
  94. this.cellMap = {};
  95. for (let i = 0; i < GridConstant.COL; i++) {
  96. let rowCells = [];
  97. for (let j = 0; j < GridConstant.ROW; j++) {
  98. let idx = (i + 1) * 10 + (j + 1);
  99. let cellData = data.list[idx];
  100. if (cellData) {
  101. let cell = this.CreateCell(i, j);
  102. rowCells.push(cell);
  103. cell.Init({
  104. x: i,
  105. y: j,
  106. idx: idx,
  107. unlock: cellData.unlock
  108. });
  109. if (cellData.type > 0) {
  110. cell.CreateCube(cellData);
  111. }
  112. this.cellMap[idx] = cell;
  113. }
  114. }
  115. }
  116. }
  117. private OnHcFightOver(data: { gzid: number, list: { [gzid: string]: HcInfoGeziInfo } }) {
  118. let cell = this.cellMap[data.gzid];
  119. cell.ClearCube();
  120. for (let key in data.list) {
  121. let geziData = data.list[key];
  122. if (geziData.type != 0) {
  123. let curCell = this.cellMap[Number(key)];
  124. if (curCell.IsLock()) {
  125. curCell.ChangeLockState(geziData.unlock);
  126. }
  127. let mergeCube = curCell.CreateCube(geziData);
  128. mergeCube.Init({
  129. type: geziData.type,
  130. id: geziData.correlationId,
  131. idx: Number(key),
  132. });
  133. }
  134. }
  135. }
  136. private OnHcMergeRsp(data: { cell: UECell, cube: HcInfoGeziInfo }) {
  137. let mergeCube = data.cell.CreateCube(data.cube);
  138. mergeCube.Init({
  139. type: data.cube.type,
  140. id: data.cube.correlationId,
  141. idx: data.cell.GetIdx(),
  142. });
  143. //播放经验爆炸飞行动画
  144. }
  145. /** 创建格子 */
  146. private CreateCell(i: number, j: number) {
  147. let cell = cc.instantiate(this.cellPrefab).getComponent(UECell);
  148. this.cellLayer.addChild(cell.node);
  149. cell.node.width = GridConstant.CELL_WIDTH;
  150. cell.node.height = GridConstant.CELL_WIDTH;
  151. let pos = GameDataCenter.gridMap.GetPosByVec(i, j);
  152. cell.node.setPosition(pos);
  153. return cell;
  154. }
  155. private OnTouchStart(event: cc.Event.EventTouch): void {
  156. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  157. const cell = this.GetCellByPos(touchPos);
  158. if (cell) {
  159. if (this.selectedCell) {
  160. // this.selectedCell.SetSelect(false);
  161. EventMng.emit(GridEvent.HC_CELL_SELECT, 0);
  162. }
  163. if (!cell.IsEmpty() && !cell.IsLock()) {
  164. if (cell.CanDrag()) {
  165. this.dragStartPos = touchPos;
  166. }
  167. if (this.selectedCell != cell) {
  168. this.clickCnt = 1;
  169. }
  170. this.selectedCell = cell;
  171. // cell.SetSelect(true);
  172. EventMng.emit(GridEvent.HC_CELL_SELECT, cell.GetIdx());
  173. EventMng.emit(GridEvent.HC_MERGE_SELL, { isShow: true, item: cell.GetCube().GetCubeData() });
  174. } else {
  175. this.selectedCell = null;
  176. this.clickCnt = 0;
  177. EventMng.emit(GridEvent.HC_MERGE_SELL, { isShow: false });
  178. }
  179. } else {
  180. this.clickCnt = 0;
  181. }
  182. }
  183. private OnTouchMove(event: cc.Event.EventTouch): void {
  184. if (!this.selectedCell || !this.dragStartPos) return;
  185. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  186. const distance = touchPos.sub(this.dragStartPos).mag();
  187. // 只有当移动距离超过阈值时才开始拖动
  188. if (!this.isDragging && distance >= DRAG_THRESHOLD) {
  189. this.isDragging = true;
  190. this.selectedCell.GetCube().StartDrag();
  191. // this.selectedCell.SetSelect(false);
  192. EventMng.emit(GridEvent.HC_CELL_SELECT, 0);
  193. }
  194. if (this.isDragging) {
  195. // 计算移动差值并应用到cube节点
  196. const deltaPos = touchPos.sub(this.dragStartPos);
  197. const cube = this.selectedCell.GetCube();
  198. const originalPos = cube.node.getPosition();
  199. cube.node.setPosition(originalPos.x + deltaPos.x, originalPos.y + deltaPos.y);
  200. this.dragStartPos = touchPos;
  201. const targetCell = this.GetCellByPos(touchPos);
  202. // this.lastMoveCell?.SetSelect(false);
  203. EventMng.emit(GridEvent.HC_CELL_SELECT, 0);
  204. this.lastMoveCell = targetCell;
  205. if (targetCell && !targetCell.IsLock()) {
  206. // targetCell.SetSelect(true);
  207. EventMng.emit(GridEvent.HC_CELL_SELECT, targetCell.GetIdx());
  208. }
  209. if (targetCell && targetCell != this.selectedCell) {
  210. if (!targetCell.IsEmpty() && GameDataCenter.gridMap.CellCanPut(this.selectedCell, targetCell)) {
  211. this.ueMergeTip.node.setPosition(targetCell.node.getPosition());
  212. this.ueMergeTip.Init({
  213. idx: targetCell.GetIdx(),
  214. ueCube1: this.selectedCell.GetCube(),
  215. ueCube2: targetCell.GetCube(),
  216. dir: targetCell.GetIdx() % 10 > 4 ? 2 : 1
  217. });
  218. } else {
  219. this.ueMergeTip.Hide();
  220. }
  221. } else {
  222. this.ueMergeTip.Hide();
  223. }
  224. }
  225. }
  226. private OnTouchEnd(event: cc.Event.EventTouch): void {
  227. this.dragStartPos = null;
  228. this.ueMergeTip.Hide();
  229. if (!this.selectedCell) return;
  230. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  231. const targetCell = this.GetCellByPos(touchPos);
  232. if (!this.isDragging && this.selectedCell === targetCell) {
  233. //二次点击同一个格子
  234. if (!targetCell.IsEmpty()) {
  235. targetCell.GetCube().PlayJellyAnim();
  236. if (this.clickCnt >= 2) {
  237. console.log("二次点击同一个格子");
  238. targetCell.GetCube().TriggerClick();
  239. } else {
  240. this.clickCnt++;
  241. }
  242. }
  243. } else {
  244. if (!this.isDragging) return;
  245. if (targetCell && targetCell != this.selectedCell && !targetCell.IsLock()) {
  246. GameDataCenter.gridMap.TryMergeItems(this.selectedCell, targetCell);
  247. // this.selectedCell.SetSelect(false);
  248. // targetCell.SetSelect(true);
  249. EventMng.emit(GridEvent.HC_CELL_SELECT, targetCell.GetIdx());
  250. } else {
  251. this.CheckSell();
  252. }
  253. this.isDragging = false;
  254. this.selectedCell = targetCell;
  255. }
  256. }
  257. /** 检测物品是否拖动到售卖区域售卖 */
  258. private CheckSell() {
  259. let sellSize = cc.size(200, 200);
  260. let sellBtn = HomeMergeSellCom.instance.GetSellBtn();
  261. let sellPos = uiCommon.transPos(sellBtn, this.cubeLayer);
  262. // 创建以sellPos为中心点的矩形
  263. let sellRect = cc.rect(
  264. sellPos.x - sellSize.width / 2, // 左边界 = 中心点x - 宽度/2
  265. sellPos.y - sellSize.height / 2, // 下边界 = 中心点y - 高度/2
  266. sellSize.width,
  267. sellSize.height
  268. );
  269. let cubePos = this.selectedCell.GetCube().node.getPosition();
  270. if (sellRect.contains(cubePos)) {
  271. GameDataCenter.gridMap.sendHcSell(this.selectedCell.GetIdx());
  272. } else {
  273. this.selectedCell.GetCube().BackToOriginalPos(true);
  274. }
  275. }
  276. /** 根据像素坐标获取格子 */
  277. private GetCellByPos(pos: cc.Vec2): UECell | null {
  278. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  279. const startY = (GridConstant.COL * GridConstant.CELL_WIDTH) / 2;
  280. const row = Math.floor((pos.x - startX) / GridConstant.CELL_WIDTH);
  281. const col = Math.floor((startY - pos.y) / GridConstant.CELL_WIDTH);
  282. if (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) {
  283. let idx = (col + 1) * 10 + (row + 1);
  284. return this.cellMap[idx];
  285. }
  286. return null;
  287. }
  288. /** 发射出新的物品 */
  289. private TriggerEmitter(data: { idx: number, item: HcInfoGeziInfo, targetIdx: number }) {
  290. let targetCell = this.cellMap[data.targetIdx];
  291. let startPos = GameDataCenter.gridMap.TranIdxToPos(data.idx);
  292. let mergeCube = targetCell.CreateCube(data.item);
  293. mergeCube.Init({
  294. type: data.item.type,
  295. id: data.item.correlationId,
  296. idx: data.idx,
  297. });
  298. mergeCube.SetZIndex(1000);
  299. let targetPos = GameDataCenter.gridMap.TranIdxToPos(data.targetIdx);
  300. // 计算起点和终点
  301. const startWorldPos = GameDataCenter.gridMap.GetPosByVec(startPos.y, startPos.x);
  302. mergeCube.node.setPosition(startWorldPos.x, startWorldPos.y);
  303. const endWorldPos = GameDataCenter.gridMap.GetPosByVec(targetPos.y, targetPos.x);
  304. // 计算方向向量和总距离
  305. const moveVec = cc.v2(endWorldPos.x - startWorldPos.x, endWorldPos.y - startWorldPos.y);
  306. const totalDistance = moveVec.mag();
  307. const normalizedDir = moveVec.normalize();
  308. // 根据距离动态计算动画时间(距离越远,时间越长)
  309. const baseDuration = 0.5; // 基础动画时间
  310. const distanceFactor = totalDistance / 300; // 假设300是标准距离
  311. const duration = Math.min(baseDuration + distanceFactor * 0.3, 1.2); // 限制最大时间为1.2秒
  312. // 根据距离动态计算最大高度
  313. const baseHeight = 200; // 基础高度
  314. const maxHeight = Math.min(baseHeight + totalDistance * 0.5, 600); // 根据距离增加高度,但限制最大值
  315. // 计算最后弹跳的起点(在终点前20%距离处)
  316. const bounceStartPos = cc.v3(
  317. endWorldPos.x - normalizedDir.x * (totalDistance * 0.2),
  318. endWorldPos.y - normalizedDir.y * (totalDistance * 0.2),
  319. 0
  320. );
  321. // 创建第一段抛物线(抛向空中然后落到弹跳起点)
  322. const bezier1 = [];
  323. const midPoint1 = cc.v3(
  324. startWorldPos.x + moveVec.x * 0.35, // 调整为0.35,让第一段抛物线更自然
  325. Math.max(startWorldPos.y, bounceStartPos.y) + maxHeight,
  326. 0
  327. );
  328. bezier1.push(cc.v2(startWorldPos.x, startWorldPos.y));
  329. bezier1.push(cc.v2(midPoint1.x, midPoint1.y));
  330. bezier1.push(cc.v2(bounceStartPos.x, bounceStartPos.y));
  331. // 创建第二段弹跳(第一次弹跳)
  332. const bezier2 = [];
  333. const bounceHeight = totalDistance * 0.2; // 增加弹跳高度为总距离的20%,让弹跳更明显
  334. // 计算第一次弹跳的最高点和落点
  335. const firstBounceTop = cc.v2(
  336. bounceStartPos.x + (endWorldPos.x - bounceStartPos.x) * 0.3, // 调整水平位置,让弹跳更自然
  337. endWorldPos.y + bounceHeight
  338. );
  339. // 计算第二段距离(从bounceStartPos到终点的距离)
  340. const secondDistance = totalDistance * 0.2;
  341. const firstBounceEnd = cc.v2(
  342. bounceStartPos.x + normalizedDir.x * (secondDistance * 0.6), // 增加到0.6,让第二次弹跳的起点更靠近终点
  343. bounceStartPos.y + normalizedDir.y * (secondDistance * 0.6)
  344. );
  345. bezier2.push(cc.v2(bounceStartPos.x, bounceStartPos.y));
  346. bezier2.push(firstBounceTop);
  347. bezier2.push(firstBounceEnd);
  348. // 创建第三段弹跳(最后一次弹跳到终点)
  349. const bezier3 = [];
  350. const secondBounceTop = cc.v2(
  351. firstBounceEnd.x + (endWorldPos.x - firstBounceEnd.x) * 0.5,
  352. endWorldPos.y + bounceHeight * 0.4 // 降低为40%,让最后的弹跳更轻盈
  353. );
  354. bezier3.push(firstBounceEnd);
  355. bezier3.push(secondBounceTop);
  356. bezier3.push(cc.v2(endWorldPos.x, endWorldPos.y));
  357. // 创建动作序列
  358. const bezierAction1 = cc.bezierTo(duration * 0.65, bezier1); // 缩短第一段时间
  359. const bezierAction2 = cc.bezierTo(0.2, bezier2);
  360. const bezierAction3 = cc.bezierTo(0.15, bezier3);
  361. const seq = cc.sequence(
  362. bezierAction1,
  363. bezierAction2,
  364. bezierAction3,
  365. cc.callFunc(() => {
  366. mergeCube.SetZIndex(data.targetIdx);
  367. })
  368. );
  369. // 执行动画
  370. mergeCube.node.runAction(seq);
  371. }
  372. }