layout_Buttom.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import { _decorator, Button, Component, Node, tween, sys, Sprite, Color, instantiate, Label, v3 } from 'cc';
  2. import ch_audio from 'db://assets/ch/audio/audio';
  3. import { BlockLink } from '../../game/link/block/BlockLink';
  4. import { Block, BlockState } from '../../game/link/block/Block';
  5. import { UI_Main } from '../main/UI_Main';
  6. import { MySdk, sdkMe } from '../../game/MySdk';
  7. import { audioManager } from '../../Audio/AudioManager';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('layout_Buttom')
  10. export class layout_Buttom extends Component {
  11. @property(Button)
  12. sx_btn: Button
  13. @property(Button)
  14. cz_btn: Button
  15. @property(Button)
  16. xc_btn: Button
  17. @property(Button)
  18. js_btn: Button
  19. // 按钮状态标记
  20. private isSxBtnBusy = false;
  21. private isCzBtnBusy = false;
  22. private isXcBtnBusy = false;
  23. private isJsBtnBusy = false;
  24. private globalLock = false;
  25. // 记录正在执行的动画数量
  26. private activeAnimations = 0;
  27. // 标记是否有等待执行的刷新操作
  28. private hasPendingShuffle = false;
  29. private highlightedBlocks: Node[] = [];
  30. blockLink: BlockLink | null = null;
  31. protected start() {
  32. this.blockLink = BlockLink.inti;
  33. //刷新
  34. this.sx_btn.node.on(Button.EventType.CLICK, async () => {
  35. this.huifuhighlight(this.highlightedBlocks)
  36. this.guanbibtn(!1)
  37. if (this.isSxBtnBusy) return;
  38. audioManager.playOneShot('sound/click_Btn');
  39. this.isSxBtnBusy = true; // 标记为忙碌
  40. try {
  41. const ret = await sdkMe.playRewardAd('刷新');
  42. if (ret) {
  43. // 如果有动画正在执行,标记等待状态
  44. if (this.activeAnimations > 0) {
  45. this.hasPendingShuffle = true;
  46. } else {
  47. // 否则立即执行刷新
  48. await this.asyncShuffleBlocks();
  49. }
  50. this.guanbibtn(!0)
  51. }
  52. else {
  53. this.guanbibtn(!0)
  54. }
  55. } finally {
  56. this.guanbibtn(!0)
  57. // 无论操作成功与否,都重置状态
  58. this.isSxBtnBusy = false;
  59. }
  60. })
  61. //查找
  62. this.cz_btn.node.on(Button.EventType.CLICK, async () => {
  63. this.guanbibtn(!1)
  64. if (this.isCzBtnBusy) return;
  65. audioManager.playOneShot('sound/click_Btn');
  66. this.isCzBtnBusy = true; // 标记为忙碌
  67. try {
  68. const ret = await sdkMe.playRewardAd('查找')
  69. if (ret) {
  70. this.findConnectableBlocks();
  71. } else {
  72. this.guanbibtn(!0)
  73. }
  74. } finally {
  75. this.guanbibtn(!0)
  76. // 无论操作成功与否,都重置状态
  77. this.isCzBtnBusy = false;
  78. }
  79. })
  80. //消除
  81. this.xc_btn.node.on(Button.EventType.CLICK, async () => {
  82. this.guanbibtn(!1)
  83. if (this.globalLock || this.isXcBtnBusy) return;
  84. this.globalLock = true;
  85. this.isXcBtnBusy = true;
  86. try {
  87. audioManager.playOneShot('sound/click_Btn');
  88. const ret = await sdkMe.playRewardAd('消除');
  89. if (ret) {
  90. await this.asyncEliminateConnectableBlocks();
  91. } else {
  92. this.guanbibtn(!0)
  93. }
  94. } finally {
  95. setTimeout(() => {
  96. this.guanbibtn(!0)
  97. this.isXcBtnBusy = false;
  98. this.globalLock = false;
  99. }, 500);
  100. }
  101. })
  102. //加时
  103. this.js_btn.node.on(Button.EventType.CLICK, async () => {
  104. // 检查按钮状态,忙则不执行
  105. if (this.isJsBtnBusy) return;
  106. audioManager.playOneShot('sound/click_Btn');
  107. this.isJsBtnBusy = true; // 标记为忙碌
  108. try {
  109. const ret = await sdkMe.playRewardAd('加时')
  110. if (ret) {
  111. const role = UI_Main.lt.role;
  112. if (role) {
  113. UI_Main.lt.title.addTime(50);
  114. await role.reviveMove(); // 复活移动
  115. }
  116. }
  117. } finally {
  118. // 无论操作成功与否,都重置状态
  119. this.isJsBtnBusy = false;
  120. }
  121. })
  122. }
  123. //刷新
  124. private async asyncShuffleBlocks(): Promise<boolean> {
  125. BlockLink.inti.isMoving = true;
  126. //取消消除前选中状态
  127. this.resetAllBlocksAnimation1()
  128. console.log('开始洗牌');
  129. return new Promise<boolean>((resolve) => {
  130. const blockLink = BlockLink.inti;
  131. const allBlocks = blockLink.node.children.filter(child => child.getComponent(Block));
  132. if (allBlocks.length < 2) {
  133. resolve(false);
  134. return;
  135. }
  136. // 洗牌算法
  137. const shuffledBlocks = this.fisherYatesShuffle([...allBlocks]);
  138. // 清空原始数组
  139. BlockLink.blockArry = new Array(blockLink.mapConfig.rows)
  140. .fill(null)
  141. .map(() => new Array(blockLink.mapConfig.cols).fill(null));
  142. // 记录原始位置用于动画
  143. const originalPositions = allBlocks.map(block => block.getPosition().clone());
  144. // 记录需要执行的动画总数
  145. this.activeAnimations = allBlocks.length;
  146. // 遍历所有方块并播放移动动画
  147. allBlocks.forEach((block, index) => {
  148. const targetBlock = shuffledBlocks[index];
  149. const targetPosition = targetBlock.getPosition();
  150. tween(block)
  151. .to(0.1, { position: targetPosition }, { easing: 'sineOut' })
  152. .call(() => {
  153. // 动画完成后更新数组
  154. const { i, j } = blockLink.getRowCol(block.position);
  155. BlockLink.blockArry[i][j] = block;
  156. // 减少活跃动画计数
  157. this.activeAnimations--;
  158. // 如果所有动画都完成且有等待的刷新操作,则执行
  159. if (this.activeAnimations === 0 && this.hasPendingShuffle) {
  160. this.hasPendingShuffle = false;
  161. this.asyncShuffleBlocks();
  162. }
  163. // 如果是最后一个动画完成,解决Promise
  164. if (this.activeAnimations === 0) {
  165. BlockLink.inti.isMoving = false;
  166. resolve(true);
  167. }
  168. })
  169. .start();
  170. });
  171. });
  172. }
  173. // 洗牌方法
  174. private fisherYatesShuffle(array: Node[]): Node[] {
  175. for (let i = array.length - 1; i > 0; i--) {
  176. const j = Math.floor(Math.random() * (i + 1));
  177. [array[i], array[j]] = [array[j], array[i]];
  178. }
  179. return array;
  180. }
  181. //查找
  182. findConnectableBlocks() {
  183. //取消消除前选中状态
  184. //this.resetAllBlocksAnimation1()
  185. const blockLink = BlockLink.inti;
  186. const allBlocks = blockLink.node.children.filter(child => child.getComponent(Block));
  187. let found = false;
  188. for (let i = 0; i < allBlocks.length; i++) {
  189. const blockA = allBlocks[i];
  190. const blockAComp = blockA.getComponent(Block);
  191. for (let j = i + 1; j < allBlocks.length; j++) {
  192. const blockB = allBlocks[j];
  193. const blockBComp = blockB.getComponent(Block);
  194. // 增加判断方块类型是否相同
  195. if (blockAComp && blockBComp && blockAComp.isMatch(blockBComp)) {
  196. if (blockLink.isConnected(blockA, blockB, true)) {
  197. found = true;
  198. this.highlightedBlocks = [blockA, blockB]
  199. this.highlight(blockAComp, blockBComp)
  200. // this.tipNofind()
  201. return; // 找到一对就停止查找
  202. }
  203. }
  204. }
  205. }
  206. if (!found) {
  207. this.tipNofind()
  208. }
  209. }
  210. //恢复
  211. huifuhighlight(node: Node[]) {
  212. const node1 = node[0];
  213. const node2 = node[1];
  214. if (!node1 || !node1.isValid) return;
  215. node1.getComponent(Sprite).color = new Color(255, 255, 255);
  216. node2.getComponent(Sprite).color = new Color(255, 255, 255);
  217. }
  218. tipNofind() {
  219. const tipPrefab = UI_Main.lt.tip;
  220. const tip = instantiate(tipPrefab);
  221. tip.setPosition(0, 0, 0);
  222. tip.parent = this.node;
  223. tween(tip)
  224. .to(0.5, { scale: v3(1.2, 1.2, 1) })
  225. .to(0.5, { scale: v3(0, 0, 1) })
  226. .call(() => {
  227. if (tip && tip.isValid) {
  228. tip.destroy();
  229. }
  230. })
  231. .start();
  232. }
  233. //异步消除方法,等待消除动画完成
  234. private async asyncEliminateConnectableBlocks() {
  235. //取消消除前选中状态
  236. this.resetAllBlocksAnimation1()
  237. // 创建一个Promise来等待消除完成
  238. return new Promise<void>((resolve) => {
  239. const blockLink = BlockLink.inti;
  240. const allBlocks = blockLink.node.children.filter(child => child.getComponent(Block));
  241. let found = false;
  242. for (let i = 0; i < allBlocks.length; i++) {
  243. const blockA = allBlocks[i];
  244. const blockAComp = blockA.getComponent(Block);
  245. for (let j = i + 1; j < allBlocks.length; j++) {
  246. const blockB = allBlocks[j];
  247. const blockBComp = blockB.getComponent(Block);
  248. if (blockAComp && blockBComp && blockAComp.isMatch(blockBComp)) {
  249. if (blockLink.isConnected(blockA, blockB)) {
  250. found = true;
  251. // 执行匹配成功的消除逻辑
  252. if (blockAComp.handleMatchSuccess instanceof Function) {
  253. // 执行消除并等待完成
  254. blockAComp.handleMatchSuccess(blockBComp);
  255. // 等待一小段时间,确保动画开始
  256. setTimeout(() => {
  257. setTimeout(resolve, 500); // 等待 0.5 秒后执行 resolve
  258. }, 100); //
  259. } else {
  260. // 如果没有动画,直接完成
  261. resolve();
  262. }
  263. return;
  264. }
  265. }
  266. }
  267. }
  268. if (!found) {
  269. this.tipNofind();
  270. }
  271. // 如果没有找到可消除的方块,直接完成
  272. resolve();
  273. });
  274. }
  275. highlight(node1: Block, node2: Block) {
  276. if (!node1 || !node1.isValid) return;
  277. node1.getComponent(Sprite).color = new Color(0, 255, 0);
  278. node2.getComponent(Sprite).color = new Color(0, 255, 0);
  279. }
  280. private resetAllBlocksAnimation1() {
  281. let blockNode = this.blockLink.sleeBlock;
  282. if (!blockNode || !blockNode.isValid) return;
  283. const blockComp = blockNode.getComponent(Block);
  284. if (blockComp && blockComp.isValid) {
  285. blockComp.stateBlock = BlockState.Normal;
  286. this.blockLink.sleeBlock = null;
  287. blockComp.applyStateStyle();
  288. }
  289. }
  290. //关闭或打开按钮
  291. guanbibtn(tiaojian: boolean) {
  292. this.sx_btn.interactable = tiaojian;
  293. this.cz_btn.interactable = tiaojian;
  294. this.xc_btn.interactable = tiaojian;
  295. this.js_btn.interactable = tiaojian;
  296. }
  297. }