UEGridMap.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. zIndex: 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. zIndex: 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. zIndex: 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. }
  162. if (!cell.IsEmpty() && !cell.IsLock()) {
  163. if (cell.CanDrag()) {
  164. this.dragStartPos = touchPos;
  165. }
  166. if (this.selectedCell != cell) {
  167. this.clickCnt = 1;
  168. }
  169. this.selectedCell = cell;
  170. cell.SetSelect(true);
  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. }
  191. if (this.isDragging) {
  192. // 计算移动差值并应用到cube节点
  193. const deltaPos = touchPos.sub(this.dragStartPos);
  194. const cube = this.selectedCell.GetCube();
  195. const originalPos = cube.node.getPosition();
  196. cube.node.setPosition(originalPos.x + deltaPos.x, originalPos.y + deltaPos.y);
  197. this.dragStartPos = touchPos;
  198. const targetCell = this.GetCellByPos(touchPos);
  199. this.lastMoveCell?.SetSelect(false);
  200. this.lastMoveCell = targetCell;
  201. if (targetCell && !targetCell.IsLock()) {
  202. targetCell?.SetSelect(true);
  203. }
  204. if (targetCell && targetCell != this.selectedCell) {
  205. if (!targetCell.IsEmpty() && GameDataCenter.gridMap.CellCanPut(this.selectedCell, targetCell)) {
  206. this.ueMergeTip.node.setPosition(targetCell.node.getPosition());
  207. this.ueMergeTip.Init({
  208. idx: targetCell.GetZIndex(),
  209. ueCube1: this.selectedCell.GetCube(),
  210. ueCube2: targetCell.GetCube(),
  211. dir: targetCell.GetZIndex() % 10 > 4 ? 2 : 1
  212. });
  213. } else {
  214. this.ueMergeTip.Hide();
  215. }
  216. } else {
  217. this.ueMergeTip.Hide();
  218. }
  219. }
  220. }
  221. private OnTouchEnd(event: cc.Event.EventTouch): void {
  222. this.dragStartPos = null;
  223. this.ueMergeTip.Hide();
  224. if (!this.selectedCell) return;
  225. const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
  226. const targetCell = this.GetCellByPos(touchPos);
  227. if (!this.isDragging && this.selectedCell === targetCell) {
  228. //二次点击同一个格子
  229. if (!targetCell.IsEmpty()) {
  230. targetCell.GetCube().PlayJellyAnim();
  231. if (this.clickCnt >= 2) {
  232. console.log("二次点击同一个格子");
  233. targetCell.GetCube().TriggerClick();
  234. } else {
  235. this.clickCnt++;
  236. }
  237. }
  238. } else {
  239. if (!this.isDragging) return;
  240. if (targetCell && targetCell != this.selectedCell && !targetCell.IsLock()) {
  241. GameDataCenter.gridMap.TryMergeItems(this.selectedCell, targetCell);
  242. this.selectedCell.SetSelect(false);
  243. targetCell.SetSelect(true);
  244. } else {
  245. this.CheckSell();
  246. this.selectedCell.GetCube().BackToOriginalPos(true);
  247. }
  248. this.isDragging = false;
  249. this.selectedCell = targetCell;
  250. }
  251. }
  252. /** 检测物品是否拖动到售卖区域售卖 */
  253. private CheckSell() {
  254. let sellPos = cc.v2(0, 0);
  255. let sellSize = cc.size(GridConstant.CELL_WIDTH, GridConstant.CELL_WIDTH);
  256. let sellRect = cc.rect(sellPos.x, sellPos.y, sellSize.width, sellSize.height);
  257. let cubePos = this.selectedCell.GetCube().node.getPosition();
  258. // if (cc.rectContainsPoint(sellRect, cubePos)) {
  259. // this.selectedCell.GetCube().PlayJellyAnim();
  260. // }
  261. }
  262. /** 根据像素坐标获取格子 */
  263. private GetCellByPos(pos: cc.Vec2): UECell | null {
  264. const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
  265. const startY = (GridConstant.COL * GridConstant.CELL_WIDTH) / 2;
  266. const row = Math.floor((pos.x - startX) / GridConstant.CELL_WIDTH);
  267. const col = Math.floor((startY - pos.y) / GridConstant.CELL_WIDTH);
  268. if (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) {
  269. let idx = (col + 1) * 10 + (row + 1);
  270. return this.cellMap[idx];
  271. }
  272. return null;
  273. }
  274. /** 发射出新的物品 */
  275. private TriggerEmitter(data: { idx: number, item: HcInfoGeziInfo, targetIdx: number }) {
  276. let targetCell = this.cellMap[data.targetIdx];
  277. let startPos = GameDataCenter.gridMap.TranIdxToPos(data.idx);
  278. let mergeCube = this.CreateCube(startPos.y, startPos.x);
  279. mergeCube.Init({
  280. type: data.item.type,
  281. id: data.item.correlationId,
  282. zIndex: data.idx,
  283. });
  284. targetCell.SetCube(mergeCube);
  285. mergeCube.SetZIndex(1000);
  286. let targetPos = GameDataCenter.gridMap.TranIdxToPos(data.targetIdx);
  287. // 计算起点和终点
  288. const startWorldPos = mergeCube.node.getPosition();
  289. const endWorldPos = GameDataCenter.gridMap.GetPosByVec(targetPos.y, targetPos.x);
  290. // 计算方向向量和总距离
  291. const moveVec = cc.v2(endWorldPos.x - startWorldPos.x, endWorldPos.y - startWorldPos.y);
  292. const totalDistance = moveVec.mag();
  293. const normalizedDir = moveVec.normalize();
  294. // 根据距离动态计算动画时间(距离越远,时间越长)
  295. const baseDuration = 0.5; // 基础动画时间
  296. const distanceFactor = totalDistance / 300; // 假设300是标准距离
  297. const duration = Math.min(baseDuration + distanceFactor * 0.3, 1.2); // 限制最大时间为1.2秒
  298. // 设置最大高度(向上弹跳的高度)
  299. const maxHeight = 400;
  300. // 计算最后弹跳的起点(在终点前20%距离处)
  301. const bounceStartPos = cc.v3(
  302. endWorldPos.x - normalizedDir.x * (totalDistance * 0.2),
  303. endWorldPos.y - normalizedDir.y * (totalDistance * 0.2),
  304. 0
  305. );
  306. // 创建第一段抛物线(抛向空中然后落到弹跳起点)
  307. const bezier1 = [];
  308. const midPoint1 = cc.v3(
  309. startWorldPos.x + moveVec.x * 0.3,
  310. Math.max(startWorldPos.y, bounceStartPos.y) + maxHeight,
  311. 0
  312. );
  313. bezier1.push(cc.v2(startWorldPos.x, startWorldPos.y));
  314. bezier1.push(cc.v2(midPoint1.x, midPoint1.y));
  315. bezier1.push(cc.v2(bounceStartPos.x, bounceStartPos.y));
  316. // 创建第二段弹跳(第一次弹跳)
  317. const bezier2 = [];
  318. const bounceHeight = totalDistance * 0.15; // 弹跳高度设为总距离的15%
  319. // 计算第一次弹跳的最高点和落点
  320. const firstBounceTop = cc.v2(
  321. (bounceStartPos.x + endWorldPos.x) / 2, // 水平位置在中间
  322. endWorldPos.y + bounceHeight // 垂直高度为弹跳高度
  323. );
  324. // 计算第二段距离(从bounceStartPos到终点的距离)
  325. const secondDistance = totalDistance * 0.2; // 最后20%的距离
  326. const firstBounceEnd = cc.v2(
  327. bounceStartPos.x + normalizedDir.x * (secondDistance * 0.5), // 前进一半
  328. bounceStartPos.y + normalizedDir.y * (secondDistance * 0.5)
  329. );
  330. bezier2.push(cc.v2(bounceStartPos.x, bounceStartPos.y)); // 起跳点
  331. bezier2.push(firstBounceTop); // 最高点
  332. bezier2.push(firstBounceEnd); // 第一次落点
  333. // 创建第三段弹跳(最后一次弹跳到终点)
  334. const bezier3 = [];
  335. const secondBounceTop = cc.v2(
  336. (firstBounceEnd.x + endWorldPos.x) / 2,
  337. endWorldPos.y + bounceHeight * 0.6 // 第二次弹跳高度为第一次的60%
  338. );
  339. bezier3.push(firstBounceEnd); // 起跳点
  340. bezier3.push(secondBounceTop); // 最高点
  341. bezier3.push(cc.v2(endWorldPos.x, endWorldPos.y)); // 终点
  342. // 创建动作序列
  343. const bezierAction1 = cc.bezierTo(duration * 0.7, bezier1);
  344. const bezierAction2 = cc.bezierTo(duration * 0.18, bezier2);
  345. const bezierAction3 = cc.bezierTo(duration * 0.12, bezier3);
  346. const seq = cc.sequence(
  347. bezierAction1,
  348. bezierAction2,
  349. bezierAction3,
  350. cc.callFunc(() => {
  351. mergeCube.SetZIndex(data.targetIdx);
  352. })
  353. );
  354. // 执行动画
  355. mergeCube.node.runAction(seq);
  356. }
  357. }