UEGridMap.ts 15 KB

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