UEGridMap.ts 16 KB

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