UEGridMap.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. /** 加载地图数据 */
  72. private LoadMapData(data: ResHcInfo) {
  73. this.cellMap = {};
  74. for (let i = 0; i < GridConstant.COL; i++) {
  75. let rowCells = [];
  76. let rowCubes = [];
  77. for (let j = 0; j < GridConstant.ROW; j++) {
  78. let idx = (i + 1) * 10 + (j + 1);
  79. let cellData = data.list[idx];
  80. if (cellData) {
  81. let cell = this.CreateCell(i, j);
  82. rowCells.push(cell);
  83. let cube = null;
  84. if (cellData.type > 0) {
  85. cube = this.CreateCube(i, j);
  86. rowCubes.push(cube);
  87. cube.Init({
  88. type: cellData.type,
  89. id: cellData.correlationId,
  90. zIndex: idx
  91. }, idx)
  92. }
  93. cell.Init({
  94. idx: idx,
  95. ueCube: cube,
  96. unlock: cellData.unlock
  97. });
  98. this.cellMap[idx] = cell;
  99. }
  100. }
  101. }
  102. }
  103. private OnHcFightOver(data: { gzid: number, list: { [gzid: string]: HcInfoGeziInfo } }) {
  104. let cell = this.cellMap[data.gzid];
  105. cell.ClearCube();
  106. for (let key in data.list) {
  107. let geziData = data.list[key];
  108. if (geziData.type != 0) {
  109. let curCell = this.cellMap[Number(key)];
  110. if (curCell.IsLock()) {
  111. curCell.ChangeLockState(geziData.unlock);
  112. }
  113. let vec = GameDataCenter.gridMap.TranIdxToPos(Number(key));
  114. let mergeCube = this.CreateCube(vec.y, vec.x);
  115. mergeCube.Init({
  116. type: geziData.type,
  117. id: geziData.correlationId,
  118. idx: Number(key),
  119. });
  120. curCell.SetCube(mergeCube);
  121. }
  122. }
  123. }
  124. private OnHcMergeRsp(data: { cell: UECell, cube: HcInfoGeziInfo }) {
  125. let vec = GameDataCenter.gridMap.TranIdxToPos(data.cell.GetZIndex());
  126. let mergeCube = this.CreateCube(vec.y, vec.x);
  127. mergeCube.Init({
  128. type: data.cube.type,
  129. id: data.cube.correlationId,
  130. idx: data.cell.GetZIndex(),
  131. });
  132. data.cell.SetCube(mergeCube);
  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. /** 创建棋子 */
  146. private CreateCube(i: number, j: number) {
  147. let cube = cc.instantiate(this.cubePrefab).getComponent(UECube);
  148. this.cubeLayer.addChild(cube.node);
  149. cube.node.width = GridConstant.CELL_WIDTH;
  150. cube.node.height = GridConstant.CELL_WIDTH;
  151. let pos = GameDataCenter.gridMap.GetPosByVec(i, j);
  152. cube.node.setPosition(pos);
  153. return cube;
  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.GetZIndex());
  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.GetZIndex());
  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.GetZIndex(),
  214. ueCube1: this.selectedCell.GetCube(),
  215. ueCube2: targetCell.GetCube(),
  216. dir: targetCell.GetZIndex() % 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.GetZIndex());
  250. } else {
  251. this.CheckSell();
  252. this.selectedCell.GetCube().BackToOriginalPos(true);
  253. }
  254. this.isDragging = false;
  255. this.selectedCell = targetCell;
  256. }
  257. }
  258. /** 检测物品是否拖动到售卖区域售卖 */
  259. private CheckSell() {
  260. let sellPos = cc.v2(0, 0);
  261. let sellSize = cc.size(GridConstant.CELL_WIDTH, GridConstant.CELL_WIDTH);
  262. let sellRect = cc.rect(sellPos.x, sellPos.y, sellSize.width, sellSize.height);
  263. let cubePos = this.selectedCell.GetCube().node.getPosition();
  264. // if (cc.rectContainsPoint(sellRect, cubePos)) {
  265. // this.selectedCell.GetCube().PlayJellyAnim();
  266. // }
  267. }
  268. /** 根据像素坐标获取格子 */
  269. private GetCellByPos(pos: cc.Vec2): UECell | null {
  270. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  271. const startY = (GridConstant.COL * GridConstant.CELL_WIDTH) / 2;
  272. const row = Math.floor((pos.x - startX) / GridConstant.CELL_WIDTH);
  273. const col = Math.floor((startY - pos.y) / GridConstant.CELL_WIDTH);
  274. if (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) {
  275. let idx = (col + 1) * 10 + (row + 1);
  276. return this.cellMap[idx];
  277. }
  278. return null;
  279. }
  280. /** 发射出新的物品 */
  281. private TriggerEmitter(data: { idx: number, item: HcInfoGeziInfo, targetIdx: number }) {
  282. let targetCell = this.cellMap[data.targetIdx];
  283. let startPos = GameDataCenter.gridMap.TranIdxToPos(data.idx);
  284. let mergeCube = this.CreateCube(startPos.y, startPos.x);
  285. mergeCube.Init({
  286. type: data.item.type,
  287. id: data.item.correlationId,
  288. idx: data.idx,
  289. });
  290. targetCell.SetCube(mergeCube);
  291. mergeCube.SetZIndex(1000);
  292. let targetPos = GameDataCenter.gridMap.TranIdxToPos(data.targetIdx);
  293. // 计算起点和终点
  294. const startWorldPos = mergeCube.node.getPosition();
  295. const endWorldPos = GameDataCenter.gridMap.GetPosByVec(targetPos.y, targetPos.x);
  296. // 计算方向向量和总距离
  297. const moveVec = cc.v2(endWorldPos.x - startWorldPos.x, endWorldPos.y - startWorldPos.y);
  298. const totalDistance = moveVec.mag();
  299. const normalizedDir = moveVec.normalize();
  300. // 根据距离动态计算动画时间(距离越远,时间越长)
  301. const baseDuration = 0.5; // 基础动画时间
  302. const distanceFactor = totalDistance / 300; // 假设300是标准距离
  303. const duration = Math.min(baseDuration + distanceFactor * 0.3, 1.2); // 限制最大时间为1.2秒
  304. // 设置最大高度(向上弹跳的高度)
  305. const maxHeight = 400;
  306. // 计算最后弹跳的起点(在终点前20%距离处)
  307. const bounceStartPos = cc.v3(
  308. endWorldPos.x - normalizedDir.x * (totalDistance * 0.2),
  309. endWorldPos.y - normalizedDir.y * (totalDistance * 0.2),
  310. 0
  311. );
  312. // 创建第一段抛物线(抛向空中然后落到弹跳起点)
  313. const bezier1 = [];
  314. const midPoint1 = cc.v3(
  315. startWorldPos.x + moveVec.x * 0.3,
  316. Math.max(startWorldPos.y, bounceStartPos.y) + maxHeight,
  317. 0
  318. );
  319. bezier1.push(cc.v2(startWorldPos.x, startWorldPos.y));
  320. bezier1.push(cc.v2(midPoint1.x, midPoint1.y));
  321. bezier1.push(cc.v2(bounceStartPos.x, bounceStartPos.y));
  322. // 创建第二段弹跳(第一次弹跳)
  323. const bezier2 = [];
  324. const bounceHeight = totalDistance * 0.15; // 弹跳高度设为总距离的15%
  325. // 计算第一次弹跳的最高点和落点
  326. const firstBounceTop = cc.v2(
  327. (bounceStartPos.x + endWorldPos.x) / 2, // 水平位置在中间
  328. endWorldPos.y + bounceHeight // 垂直高度为弹跳高度
  329. );
  330. // 计算第二段距离(从bounceStartPos到终点的距离)
  331. const secondDistance = totalDistance * 0.2; // 最后20%的距离
  332. const firstBounceEnd = cc.v2(
  333. bounceStartPos.x + normalizedDir.x * (secondDistance * 0.5), // 前进一半
  334. bounceStartPos.y + normalizedDir.y * (secondDistance * 0.5)
  335. );
  336. bezier2.push(cc.v2(bounceStartPos.x, bounceStartPos.y)); // 起跳点
  337. bezier2.push(firstBounceTop); // 最高点
  338. bezier2.push(firstBounceEnd); // 第一次落点
  339. // 创建第三段弹跳(最后一次弹跳到终点)
  340. const bezier3 = [];
  341. const secondBounceTop = cc.v2(
  342. (firstBounceEnd.x + endWorldPos.x) / 2,
  343. endWorldPos.y + bounceHeight * 0.6 // 第二次弹跳高度为第一次的60%
  344. );
  345. bezier3.push(firstBounceEnd); // 起跳点
  346. bezier3.push(secondBounceTop); // 最高点
  347. bezier3.push(cc.v2(endWorldPos.x, endWorldPos.y)); // 终点
  348. // 创建动作序列
  349. const bezierAction1 = cc.bezierTo(duration * 0.7, bezier1);
  350. const bezierAction2 = cc.bezierTo(duration * 0.18, bezier2);
  351. const bezierAction3 = cc.bezierTo(duration * 0.12, bezier3);
  352. const seq = cc.sequence(
  353. bezierAction1,
  354. bezierAction2,
  355. bezierAction3,
  356. cc.callFunc(() => {
  357. mergeCube.SetZIndex(data.targetIdx);
  358. })
  359. );
  360. // 执行动画
  361. mergeCube.node.runAction(seq);
  362. }
  363. }