EliminateViewComp.ts 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-03-20 15:01:09
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-04-30 15:42:36
  6. * @Description: 消除游戏主场景
  7. */
  8. import { _decorator, Button, Color, EventTouch, instantiate, JsonAsset, Animation, v3, Label, Node, Prefab, randomRangeInt, Sprite, tween, UITransform, Vec2, Vec3, Widget } from "cc";
  9. import { oops } from "db://oops-framework/core/Oops";
  10. import { DeviceUtil } from "db://oops-framework/core/utils/DeviceUtil";
  11. import { ecs } from "db://oops-framework/libs/ecs/ECS";
  12. import { LabelChange } from "db://oops-framework/libs/gui/label/LabelChange";
  13. import { CCComp } from "db://oops-framework/module/common/CCComp";
  14. import { GameEvent } from "../common/config/GameEvent";
  15. import { UIID } from "../common/config/GameUIConfig";
  16. import { ServerHandler } from "../common/manager/ServerHandler";
  17. import { smc } from "../common/SingletonModuleComp";
  18. import { Format } from "../utils/Format";
  19. import { DCHandler } from "../common/manager/DCHandler";
  20. import { Tween } from "cc";
  21. import { AD_TYPE } from "../common/config/GameDefine";
  22. import { ADHandler } from "../common/manager/ADHandler";
  23. import { director } from "cc";
  24. import { Game } from "cc";
  25. const { ccclass, property } = _decorator;
  26. // 游戏状态枚举
  27. enum GameState {
  28. READY, // 准备中
  29. PLAYING, // 游戏中
  30. PAUSED, // 暂停 --进其他界面,广告暂停
  31. GAME_OVER, // 游戏结束
  32. GAME_PASS
  33. }
  34. // 格子状态
  35. enum CellState {
  36. EMPTY, // 空格子
  37. FILLED, // 有方块
  38. HIGHLIGHTED // 高亮(可放置)
  39. }
  40. //网格数据接口
  41. interface GridData {
  42. name: string, // 名称
  43. status: CellState, // 状态
  44. gridNode: Node | null // 网格节点
  45. row: number, // 行
  46. col: number, // 列
  47. type: number, // 类型--gridColorKey
  48. }
  49. interface Grids {
  50. row: number,
  51. column: number,
  52. }
  53. interface EditingData {
  54. brickData: BrickData | null,
  55. gridList: GridData[],
  56. }
  57. //底部方块数据定义
  58. interface BrickData {
  59. index: number,
  60. brickKey: string | null,
  61. rotateFlag: boolean,
  62. gridConfig: Grids[],
  63. deg: number,
  64. brickNode: Node | null,
  65. brickInitPos: Vec3, // 方块初始位置
  66. type: number, // 方块类型--gridColorKey
  67. rotateNode: Node | null,
  68. }
  69. interface GridConfigData {
  70. row: number,
  71. column: number,
  72. }
  73. /** 视图层对象 */
  74. @ccclass('EliminateViewComp')
  75. @ecs.register('EliminateView', false)
  76. export class EliminateViewComp extends CCComp {
  77. @property({ type: LabelChange, displayName: "自动提现金额" })
  78. private lab_wxCoin: LabelChange = null!;
  79. @property({ type: LabelChange, displayName: "额外奖励" })
  80. private lab_hbCoin: LabelChange = null!;
  81. @property({ type: Node, displayName: "tween微信钱Node" })
  82. private tweenWechatNode: Node = null!;
  83. @property({ type: Node, displayName: "tween红包Node" })
  84. private tweenRedNode: Node = null!;
  85. @property({ type: Prefab, displayName: "金币预制体" })
  86. private coinPrefab: Prefab = null!
  87. @property({ type: Prefab, displayName: "红包预制体" })
  88. private redPacketPrefab: Prefab = null!;
  89. @property({ type: Label, displayName: "本局分数" })
  90. private lab_score: Label = null!;
  91. @property({ type: Label, displayName: "目标分数" })
  92. private lab_taget: Label = null!;
  93. @property({ type: Prefab, displayName: "item预制体列表" })
  94. private itemPrefabs: Prefab[] = [];
  95. @property({ type: Node, displayName: "网格Node" })
  96. private gridNode: Node = null!;
  97. @property({ type: Node, displayName: "移动Node" })
  98. private moveNode: Node = null!;
  99. @property({ type: Node, displayName: "旋转Node" })
  100. private rotateNode: Node = null!;
  101. @property({ type: Node, displayName: "方块Node" })
  102. private brickNode: Node = null!;
  103. @property({ type: Prefab, displayName: "旋转预制体" })
  104. private rotatePrefab: Node = null!;
  105. @property({ type: Label, displayName: "累计消除次数" })
  106. private lab_total: Label = null!;
  107. @property({ type: Label, displayName: "每次放置添加的分数" })
  108. private lab_addScore: Label = null!;
  109. @property({ type: Node, displayName: "自动按钮节点" })
  110. private autoBtnNode: Node = null!;
  111. //二倍速按钮
  112. @property({ type: Button, displayName: "二倍速按钮" })
  113. private doubleSpeedBtn: Button = null!;
  114. @property({ type: Label, displayName: "第几块金砖" })
  115. private lab_goldNum: Label = null!;
  116. @property({ type: Label, displayName: "二倍速时间" })
  117. private lab_doubleTime: Label = null!;
  118. @property({ type: Node, displayName: "引导层" })
  119. private guideNode: Node = null!;
  120. @property({ type: Node, displayName: "引导item1" })
  121. private guideItem1: Node = null!;
  122. @property({ type: Node, displayName: "引导item2" })
  123. private guideItem2: Node = null!;
  124. @property({ type: Node, displayName: "引导item3" })
  125. private guideItem3: Node = null!;
  126. //引导手指
  127. @property({ type: Node, displayName: "引导手指" })
  128. private guideFinger: Node = null!;
  129. @property({ type: Node, displayName: "ComboNode" })
  130. private comboNode: Node = null!;
  131. @property({ type: Node, displayName: "自动放置Node拦截事件" })
  132. private autoNode: Node = null!;
  133. //游戏配置行列
  134. private rows: number = 8; // 行数
  135. private cols: number = 8; // 列数
  136. private itemSize: number = 76.25; // 格子大小
  137. private brickNum: number = 3; // 砖块数量
  138. private yOffset = 100;
  139. private aniBrickRotate = 0
  140. private operateFlag: boolean = false //是否可以操作
  141. private touchStartPos = new Vec2()
  142. private gameState: GameState = GameState.READY;
  143. private score: number = 0; //本局分数
  144. private money: number = 0; //左边金钱
  145. private cash: number = 0; //右边红包钱数
  146. private targetScore: number = 0; //目标分数
  147. private isDoubleSpeed: boolean = false; //是否开启二倍速
  148. private doubleNum: number = 1.5; //除2就是开启了2倍速,除1就是关闭了2倍速
  149. private callback: Function | null = null; //回调函数
  150. //tween时间控制变量
  151. private autoMoveTime: number = 0.4; //自动移动时间
  152. //再次自动放置间隔时间
  153. private autoPlaceInterval: number = 0.4;
  154. //没使用颜色
  155. notUseColor = new Color(255, 255, 255, 255)
  156. //可用的颜色
  157. usableColor = new Color(0, 255, 0, 100)
  158. //不可用的颜色
  159. unavailableColor = new Color(255, 0, 0, 100)
  160. //旋转容错
  161. rotateFaultTolerant = 50;
  162. //游戏暂停状态
  163. private gamePause: boolean = false;
  164. isAutoMode: boolean = false;
  165. autoModeInterval: number = 1 // 自动模式的间隔时间(秒)
  166. autoModeTimer: number = 0 // 自动模式计时器
  167. adShowingFlag: boolean = false; // 广告展示标记
  168. //网格列表管理列表
  169. gridList: GridData[][] = [];
  170. //砖块列表
  171. bricksList: BrickData[] = [];
  172. //网格颜色列表
  173. gridColorList: GridData[] = [];
  174. brickConfig: { bricks?: any } = {} //方块配置
  175. editingData: EditingData = {
  176. brickData: null,
  177. gridList: [],
  178. }
  179. //消除区间数组,到时候去这两个中间值,然后消除次数到了就展示翻倍广告
  180. private eliminateInterval: number[] = [];
  181. // 添加新的属性来跟踪是否需要重置消除计数
  182. private shouldResetEliminateCount: boolean = true;
  183. private autoState: boolean = false; //自动状态
  184. private eliminateBaseScore: number = 10; //每行得多少分
  185. private extraGridScore: number = 1; //每个格子占用几分
  186. private placementBaseScore: number = 1; //每个格子占用几分
  187. private currentCombo: number = 0; // 当前连击次数 //这里要记录连击次数就是每次移动能消除就增加,如果不能就归零
  188. //消除总数
  189. private eliminateTotal: number = 0;
  190. //2倍速时间
  191. private doubleSpeedTime: number = 0;
  192. //新手引导
  193. private isGuideMode = false;
  194. private guideStep = 0;
  195. //自动间隔时间
  196. private autoInterval: number = 1.5;
  197. //福利弹窗
  198. private popupType: string = "";
  199. //新人引导提示语
  200. private guideTips: string[] = [
  201. "拖动方块,填满整行可以进行消除",
  202. "当行与列被砖块同时填满,砖块会被消除",
  203. "点击方块可以旋转90°,不限旋转次数哦",
  204. "放置&消除方块得分达成目标获得金砖"
  205. ];
  206. private randomList: number[] = [];
  207. private autoFunction: Function | null = null;
  208. /** 视图层逻辑代码分离演示 */
  209. async start() {
  210. // const entity = this.ent as ecs.Entity; // ecs.Entity 可转为当前模块的具体实体对象
  211. this.setButton();
  212. this.initButtonState(false);
  213. await this.loadConfig();
  214. this.initData();
  215. this.setData();
  216. this.addEventList();
  217. if (this.guideStep === 0 && smc.account.AccountModel.curLevel == 1) {
  218. this.startGuideMode()
  219. } else {
  220. this.guideNode.active = false;
  221. this.initGrid();
  222. }
  223. this.updateWelfarePoint();
  224. DCHandler.inst.reportData(3000005);
  225. oops.audio.playMusicLoop("common/audios/bgm");
  226. DCHandler.inst.reportData(3000504, smc.account.AccountModel.curLevel);
  227. }
  228. addEventList() {
  229. oops.message.on(GameEvent.RestartGame, this.restartGame, this);
  230. oops.message.on(GameEvent.DoubleSpeedOpenSuccess, this.doubleSpeedOpenSuccess, this);
  231. oops.message.on(GameEvent.openView, this.openView, this);
  232. oops.message.on(GameEvent.showCoinAnimation, this.showCoinAnimation, this);
  233. oops.message.on(GameEvent.updateHbAndWxCoin, this.updateCoin, this);
  234. oops.message.on(GameEvent.StartAutoGame, this.startAutoGame, this);
  235. oops.message.on(GameEvent.updateGameState, this.updateGameState, this);
  236. oops.message.on(GameEvent.updateGameScore, this.updateGameScore, this);
  237. oops.message.on(GameEvent.pauseGame, this.pauseGame, this);
  238. oops.message.on(GameEvent.resumeGame, this.resumeGame, this);
  239. }
  240. updateGameState(event: string, args: string) {
  241. switch (args) {
  242. case "paused":
  243. this.gameState = GameState.PAUSED;
  244. break;
  245. case "playing":
  246. if (this.gameState == GameState.PAUSED || this.autoState && this.gameState == GameState.GAME_OVER || this.autoState && this.gameState == GameState.GAME_PASS) {
  247. this.gameState = GameState.PLAYING;
  248. }
  249. break;
  250. }
  251. }
  252. pauseGame() {
  253. this.gamePause = true;
  254. director.pause();
  255. }
  256. resumeGame() {
  257. this.gamePause = false;
  258. director.resume();
  259. }
  260. //初始化网格
  261. private initGrid() {
  262. // 清理现有网格
  263. this.clearExistingGrids();
  264. // 生成网格矩阵
  265. this.createGridMatrix();
  266. }
  267. //初始化按钮状态
  268. private initButtonState(state: boolean) {
  269. //自动按钮默认关闭
  270. if (this.autoBtnNode) {
  271. //关闭
  272. const on = this.autoBtnNode.getChildByName("on");
  273. on ? on.active = state : null;
  274. const off = this.autoBtnNode.getChildByName("off");
  275. off ? off.active = !state : null;
  276. }
  277. //第一关隐藏二倍速和自动放置按钮
  278. const curLevel = smc.account.AccountModel.curLevel;
  279. this.autoBtnNode.active = curLevel > 1;
  280. this.doubleSpeedBtn.node.active = curLevel > 1;
  281. }
  282. //初始化数据
  283. private initData() {
  284. // this.gameMode = GameMode.MANUAL;
  285. this.score = 0;
  286. this.targetScore = 0;
  287. this.money = 0;
  288. this.cash = 0;
  289. this.operateFlag = true
  290. this.shouldResetEliminateCount = true;
  291. this.currentCombo = 0;
  292. this.autoNode.active = false;
  293. smc.game.GameModel.smallAdCount = 0;
  294. smc.game.GameModel.allAdCount = 0;
  295. smc.game.GameModel.directAdCount = 0;
  296. smc.game.GameModel.doubleSpeedAdCount = 0;
  297. }
  298. //设置数据
  299. setData() {
  300. if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
  301. if (smc.game.GameModel.curLevelConfig.eliminateScope) {
  302. this.eliminateInterval = smc.game.GameModel.curLevelConfig.eliminateScope || [];
  303. }
  304. }
  305. this.gameState = GameState.PLAYING;
  306. this.score = smc.game.GameModel.curScore || 0;
  307. this.money = smc.account.AccountModel.wxCoin || 0;
  308. this.cash = smc.account.AccountModel.hbCoin || 0;
  309. this.targetScore = smc.game.GameModel.targetScore || 0;
  310. this.popupType = smc.game.GameModel.popupType;
  311. this.lab_score.string = this.score.toString();
  312. this.lab_wxCoin.string = Format.formatWxCoin(this.money);
  313. this.lab_hbCoin.string = Format.formatRedPacketCoin(this.cash);
  314. this.lab_taget.string = this.targetScore.toString();
  315. //金砖数量要加1
  316. const num = smc.account.AccountModel.goldCoin
  317. this.lab_goldNum.string = `${num + 1}`;
  318. this.randomList = this.getRandomUniqueInts();
  319. const curLevel = smc.account.AccountModel.curLevel;
  320. DCHandler.inst.reportData(3000500, curLevel);
  321. }
  322. updateCoin() {
  323. this.money = smc.account.AccountModel.wxCoin;
  324. this.cash = smc.account.AccountModel.hbCoin;
  325. this.lab_wxCoin.string = Format.formatWxCoin(this.money);
  326. this.lab_hbCoin.string = Format.formatRedPacketCoin(this.cash);
  327. }
  328. //开始自动游戏
  329. startAutoGame() {
  330. if (!this.autoState) {
  331. this.autoGame();
  332. }
  333. }
  334. openView(event: string, args: string) {
  335. oops.log.logView(args, "<<<<<<<打开的弹窗类型");
  336. switch (args) {
  337. case "openRedBagView":
  338. oops.gui.open(UIID.RedPacketWithdraw);
  339. break;
  340. case "openPassView":
  341. oops.gui.open(UIID.GamePass);
  342. break;
  343. case "openDoubleSurprise":
  344. oops.gui.open(UIID.DoubleRewards);
  345. break;
  346. case "openRebateView":
  347. oops.gui.open(UIID.CashRebate);
  348. break;
  349. case "openCashWithdrawalView":
  350. oops.gui.open(UIID.WithSussce); //提现成功
  351. break;
  352. case "openWechatWithdrawalView":
  353. oops.gui.open(UIID.WechatWithdraw); //微信提现页
  354. break;
  355. case "openDoubleSpeedView":
  356. oops.gui.open(UIID.DoubleSpeed);
  357. break;
  358. case "openRecordView":
  359. oops.gui.open(UIID.WithdrawRecord);
  360. break;
  361. }
  362. }
  363. startGuideMode() {
  364. this.isGuideMode = true
  365. this.guideStep = 1
  366. this.guideNode.active = true;
  367. for (let rowIndex = 0; rowIndex < this.rows; rowIndex++) {
  368. const currentRow: GridData[] = [];
  369. this.gridList.push(currentRow);
  370. for (let columnIndex = 0; columnIndex < this.cols; columnIndex++) {
  371. const gridData = this.createGridData(rowIndex, columnIndex);
  372. currentRow.push(gridData);
  373. this.createGridNode(gridData);
  374. }
  375. }
  376. // 初始化格子状态
  377. for (let rowIndex = 0; rowIndex < this.rows; rowIndex++) {
  378. for (let columnIndex = 0; columnIndex < this.cols; columnIndex++) {
  379. this.gridList[rowIndex][columnIndex].status = CellState.EMPTY
  380. this.generateGrid(this.gridList[rowIndex][columnIndex])
  381. }
  382. }
  383. // 清除旋转数据
  384. if (this.rotateNode) {
  385. this.rotateNode.destroyAllChildren();
  386. }
  387. this.touchStartPos.set(Vec2.ZERO)
  388. this.setupGuideStep(this.guideStep)
  389. }
  390. setupGuideStep(step: number) {
  391. this.gameState = GameState.PLAYING;
  392. this.operateFlag = true
  393. this.bricksList.length = 0
  394. this.brickNode.destroyAllChildren();
  395. this.clearAllGuideGrids()
  396. this.guideStep = step
  397. const tips_node = this.guideNode.getChildByPath("tips/lab_tips");
  398. if (tips_node) {
  399. const tips = tips_node.getComponent(Label);
  400. if (tips) {
  401. tips.string = this.guideTips[step - 1];
  402. }
  403. }
  404. //tween移动
  405. const guideTipsNode = this.guideNode.getChildByPath("tips");
  406. if (guideTipsNode) {
  407. const guideTipsAni = guideTipsNode.getComponent(Animation);
  408. if (guideTipsAni) {
  409. guideTipsAni.play();
  410. }
  411. }
  412. if (step === 1) {
  413. this.guideStep1()
  414. }
  415. else if (step === 2) {
  416. this.guideStep2()
  417. }
  418. else if (step === 3) {
  419. this.guideStep3()
  420. }
  421. else if (step === 4) {
  422. this.guideStep4()
  423. }
  424. }
  425. clearAllGuideGrids() {
  426. for (let row = 0; row < this.rows; row++) {
  427. for (let col = 0; col < this.cols; col++) {
  428. const grid = this.gridList[row][col]
  429. grid.status = 0;
  430. grid.type = 0;
  431. this.generateGrid(grid)
  432. }
  433. }
  434. }
  435. // 引导砖块保持底部中间生成
  436. createGuideBrick(gridConfig: GridConfigData[], brickKey: string, colorIndex: number, index: number, rotateFlag = false) {
  437. const brickData = {
  438. index,
  439. brickKey: brickKey,
  440. rotateFlag,
  441. gridConfig,
  442. deg: 0,
  443. brickNode: new Node(),
  444. gridColorKey: "colorKey",
  445. brickInitPos: new Vec3(),
  446. type: colorIndex,
  447. rotateNode: new Node(),
  448. }
  449. const node = this.generateBrick(brickKey, colorIndex);
  450. this.brickNode.addChild(node);
  451. const transform = this.brickNode.getComponent(UITransform);
  452. if (transform) {
  453. const midX = transform.width / 2;
  454. node.setPosition(0, 0);
  455. brickData.brickNode = node
  456. brickData.brickNode.scale_x = 0.8;
  457. brickData.brickNode.scale_y = 0.8;
  458. brickData.brickInitPos = node.getWorldPosition()
  459. this.bricksList.push(brickData)
  460. this.brickAddEvent(brickData)
  461. }
  462. if (brickData.rotateFlag) {
  463. brickData.rotateNode = instantiate(this.rotatePrefab)
  464. this.rotateNode.addChild(brickData.rotateNode);
  465. //先隐藏
  466. brickData.rotateNode.active = false;
  467. brickData.rotateNode.setWorldPosition(this.brickNode.getWorldPosition())
  468. }
  469. }
  470. //引导步骤1
  471. private guideStep1() {
  472. // this.guideFinger.setPosition(v3(55, -461));
  473. const emptyIndex = Math.floor(this.cols / 2)
  474. for (let c = 0; c < this.cols; c++) {
  475. if (c !== emptyIndex) {
  476. const g = this.gridList[4][c]
  477. g.status = 1
  478. g.type = 1;
  479. this.generateGrid(g)
  480. }
  481. }
  482. this.createGuideBrick([
  483. { row: 0, column: 0 },
  484. ], "Brick1", 1, 1)
  485. //手指显示,方块显示
  486. this.guideFinger.active = true;
  487. this.guideItem1.active = true;
  488. //拿出this.guideFinger.x y
  489. //展示tween动画
  490. tween(this.guideFinger)
  491. .delay(0.2)
  492. .by(1, { position: v3(45, 500, 0) })
  493. .delay(0.2)
  494. .to(0.01, { position: v3(60, -450, 0) }) //原点
  495. .union()
  496. .repeatForever()
  497. .start()
  498. //方块
  499. tween(this.guideItem1)
  500. .delay(0.2)
  501. .by(1, { position: v3(37, 515, 0) })
  502. .delay(0.2)
  503. .to(0.01, { position: v3(0, -410, 0) }) //原点
  504. .union()
  505. .repeatForever()
  506. .start()
  507. }
  508. //引导步骤2
  509. private guideStep2() {
  510. for (let r = 0; r < this.rows; r++) {
  511. if (r !== 4) {
  512. const g = this.gridList[r][4]
  513. g.status = 1
  514. g.type = 1;
  515. this.generateGrid(g)
  516. }
  517. }
  518. this.createGuideBrick([{ row: 0, column: 0 }], "Brick1", 1, 1)
  519. }
  520. //引导步骤3
  521. private guideStep3() {
  522. // 填满前两列,除去 [2][0] 这个位置,制造一个L型缺口
  523. for (let r = 0; r < this.rows; r++) {
  524. for (let c = 3; c < 5; c++) {
  525. if (r === 3 && c === 3) continue
  526. if (r === 4 && c === 3) continue
  527. if (r === 5 && c === 3) continue
  528. if (r === 3 && c === 4) continue
  529. if (r === 4 && c === 4) continue
  530. if (r === 5 && c === 4) continue
  531. const g = this.gridList[r][c]
  532. g.status = 1
  533. g.type = 1;
  534. this.generateGrid(g)
  535. }
  536. }
  537. // 创建一个L型方块,引导玩家旋转后放置
  538. this.createGuideBrick([
  539. { row: 0, column: 0 },
  540. { row: 0, column: 1 },
  541. { row: 0, column: 2 },
  542. { row: 1, column: 0 },
  543. { row: 1, column: 1 },
  544. { row: 1, column: 2 }
  545. ], "BrickII", 1, 1, true) //这要双II
  546. //引导三
  547. this.guideItem1.active = false;
  548. this.guideItem2.active = true;
  549. //停止之前的tween
  550. this.guideFinger.setPosition(v3(55, -461));
  551. Tween.stopAllByTarget(this.guideFinger);
  552. Tween.stopAllByTarget(this.guideItem1);
  553. tween(this.guideFinger)
  554. .delay(0.2)
  555. .by(1, { position: v3(0, 500, 0) })
  556. .delay(0.2)
  557. .to(0.01, { position: v3(55, -450, 0) }) //原点
  558. .union()
  559. .repeatForever()
  560. .start()
  561. //方块
  562. tween(this.guideItem2)
  563. .delay(0.2)
  564. .by(1, { position: v3(0, 515, 0) })
  565. .delay(0.2)
  566. .to(0.01, { position: v3(0, -410, 0) }) //原点
  567. .union()
  568. .repeatForever()
  569. .start()
  570. }
  571. //引导步骤4
  572. private guideStep4() {
  573. const centerRow = 3;
  574. const centerCol = 3;
  575. // 遍历整个网格
  576. for (let r = 0; r < this.rows; r++) {
  577. for (let c = 0; c < this.cols; c++) {
  578. const inCenter =
  579. r >= centerRow && r <= centerRow + 1 &&
  580. c >= centerCol && c <= centerCol + 1;
  581. const isCross =
  582. r === centerRow || r === centerRow + 1 || // 中间两行
  583. c === centerCol || c === centerCol + 1; // 中间两列
  584. if (isCross && !inCenter) {
  585. const g = this.gridList[r][c];
  586. g.status = 1;
  587. g.type = 1;
  588. this.generateGrid(g);
  589. }
  590. }
  591. }
  592. this.createGuideBrick([
  593. { row: 0, column: 0 },
  594. { row: 0, column: 1 },
  595. { row: 1, column: 0 },
  596. { row: 1, column: 1 }
  597. ], "BrickO", 1, 1)
  598. this.guideItem2.active = false;
  599. Tween.stopAllByTarget(this.guideFinger);
  600. Tween.stopAllByTarget(this.guideItem2);
  601. this.guideFinger.setPosition(v3(55, -461));
  602. this.guideItem3.active = true;
  603. tween(this.guideFinger)
  604. .delay(0.2)
  605. .by(1, { position: v3(0, 440, 0) })
  606. .delay(0.2)
  607. .to(0.01, { position: v3(55, -450, 0) }) //原点
  608. .union()
  609. .repeatForever()
  610. .start()
  611. //方块
  612. tween(this.guideItem3)
  613. .delay(0.2)
  614. .by(1, { position: v3(0, 440, 0) })
  615. .delay(0.2)
  616. .to(0.01, { position: v3(0, -410, 0) }) //原点
  617. .union()
  618. .repeatForever()
  619. .start()
  620. }
  621. //显示金币动画 --是否只计算红包
  622. showCoinAnimation(event: string, args: string) {
  623. //计算中间的坐标就好
  624. const lastPos = this.moveNode.getWorldPosition();
  625. const score = this.score;
  626. if (smc.game.GameModel.changeHbCoin > 0) {
  627. this.createCoinFlyAnimation(
  628. this.redPacketPrefab,
  629. lastPos,
  630. this.lab_hbCoin.node.getWorldPosition(),
  631. 2,
  632. () => {
  633. // 显示红包分数增加动画
  634. this.showRedPacketScoreAnimation();
  635. }
  636. );
  637. }
  638. if (smc.game.GameModel.changeWxCoin > 0) {
  639. // 添加回调函数,在金币动画完成后显示微信分数增加
  640. this.createCoinFlyAnimation(
  641. this.coinPrefab,
  642. lastPos,
  643. this.lab_wxCoin.node.getWorldPosition(),
  644. 2,
  645. () => {
  646. // 显示微信分数增加动画
  647. this.showWechatScoreAnimation();
  648. }
  649. );
  650. }
  651. }
  652. //初始化网格
  653. private async loadConfig() {
  654. let json_name: string = "gui/eliminate/config/GridConfig";
  655. return new Promise<void>((resolve, reject) => {
  656. oops.res.load(json_name, JsonAsset, (err: Error | null, res: any) => {
  657. if (res) {
  658. this.brickConfig = res.json;
  659. resolve();
  660. } else {
  661. console.log("JSON数据加载失,请检查文件");
  662. reject(err);
  663. }
  664. });
  665. });
  666. }
  667. /**
  668. * @description: 清理现有的网格数据
  669. * @return {*}
  670. */
  671. private clearExistingGrids(): void {
  672. this.gridList = [];
  673. if (this.gridNode) {
  674. this.gridNode.destroyAllChildren();
  675. }
  676. }
  677. /**
  678. * @description: 设置网格容器大小
  679. * @return {*}
  680. */
  681. private createGridMatrix(): void {
  682. for (let rowIndex = 0; rowIndex < this.rows; rowIndex++) {
  683. const currentRow: GridData[] = [];
  684. this.gridList.push(currentRow);
  685. for (let columnIndex = 0; columnIndex < this.cols; columnIndex++) {
  686. const gridData = this.createGridData(rowIndex, columnIndex);
  687. currentRow.push(gridData);
  688. this.createGridNode(gridData);
  689. }
  690. }
  691. // 初始化格子状态
  692. for (let rowIndex = 0; rowIndex < this.rows; rowIndex++) {
  693. for (let columnIndex = 0; columnIndex < this.cols; columnIndex++) {
  694. this.gridList[rowIndex][columnIndex].status = CellState.EMPTY
  695. this.generateGrid(this.gridList[rowIndex][columnIndex])
  696. }
  697. }
  698. // 初始化方块
  699. if (this.brickNode) {
  700. this.brickNode.destroyAllChildren();
  701. }
  702. this.bricksList.length = 0
  703. for (let i = 1; i <= this.brickNum; i++) {
  704. this.addBrick(i)
  705. }
  706. // 清除旋转数据
  707. if (this.rotateNode) {
  708. this.rotateNode.destroyAllChildren();
  709. }
  710. this.touchStartPos.set(Vec2.ZERO)
  711. }
  712. /**
  713. * @description: 创建格子数据
  714. * @param {number} row
  715. * @param {number} column
  716. * @return {*}
  717. */
  718. private createGridData(row: number, column: number): GridData {
  719. return {
  720. name: `Grid-${row}-${column}`,
  721. status: CellState.EMPTY,
  722. gridNode: null,
  723. row: row,
  724. col: column,
  725. type: 0
  726. };
  727. }
  728. /**
  729. * @description: 创建网格Node
  730. * @param {GridData} gridData
  731. * @return {*}
  732. */
  733. private createGridNode(gridData: GridData): void {
  734. const gridNode = new Node(gridData.name);
  735. if (this.gridNode) {
  736. this.gridNode.addChild(gridNode);
  737. }
  738. gridData.gridNode = gridNode;
  739. // 设置网格大小
  740. gridNode.addComponent(UITransform).setContentSize(this.itemSize, this.itemSize);
  741. }
  742. /**
  743. * 生成或更新网格
  744. * @param gridData 网格数据
  745. */
  746. private generateGrid(gridData: GridData) {
  747. if (!gridData || !gridData.gridNode) {
  748. console.warn('无效的网格数据');
  749. return;
  750. }
  751. // 清理现有子节点
  752. this.clearGridChildren(gridData.gridNode);
  753. // 获取对应的预制体
  754. const prefab = this.getGridPrefab(gridData);
  755. if (!prefab) {
  756. console.warn('无法获取网格预制体');
  757. return;
  758. }
  759. // 创建并配置新节点
  760. const node = this.createNewGridNode(prefab, gridData);
  761. // 设置节点属性
  762. this.setupGridNode(node, gridData);
  763. }
  764. /**
  765. * 获取对应状态的预制体
  766. */
  767. private getGridPrefab(gridData: GridData): Prefab {
  768. if (gridData.status === CellState.EMPTY) {
  769. if (this.itemPrefabs[0]) {
  770. return this.itemPrefabs[0];
  771. } else {
  772. throw new Error('Grid prefab is not loaded');
  773. }
  774. }
  775. if (gridData.status === CellState.FILLED && gridData.type) {
  776. const type = gridData.type;
  777. return this.itemPrefabs[type];
  778. }
  779. throw new Error('Invalid grid status or missing gridColorKey');
  780. }
  781. /**
  782. * 创建网格节点
  783. */
  784. private createNewGridNode(prefab: Prefab, gridData: GridData): Node {
  785. const node = instantiate(prefab);
  786. if (gridData && gridData.gridNode) {
  787. gridData.gridNode.addChild(node);
  788. return node;
  789. }
  790. return node
  791. }
  792. /**
  793. * 设置网格节点的属性
  794. */
  795. private setupGridNode(node: Node, gridData: GridData): void {
  796. // 设置未使用状态的颜色
  797. if (gridData.status === CellState.EMPTY) {
  798. const sprite = node.getComponent(Sprite);
  799. if (sprite) {
  800. sprite.color = this.notUseColor;
  801. }
  802. }
  803. // 设置节点大小
  804. const transform = node.getComponent(UITransform);
  805. if (transform) {
  806. transform.setContentSize(
  807. this.itemSize,
  808. this.itemSize
  809. );
  810. }
  811. // 设置位置
  812. node.setPosition(Vec3.ZERO);
  813. }
  814. addBrick(index: number) {
  815. if (this.isGuideMode) {
  816. return;
  817. }
  818. const brickKey = Object.keys(this.brickConfig['bricks'])[randomRangeInt(0, Object.keys(this.brickConfig['bricks']).length)];
  819. const brickConfig = this.brickConfig['bricks'][brickKey];
  820. const num = Math.floor(Math.random() * this.randomList.length);
  821. const randomIndex = this.randomList[num];
  822. // const randomIndex = this.randomList[0];
  823. // 生成方块
  824. const brickData: BrickData = {
  825. index,
  826. brickKey: brickKey,
  827. rotateFlag: brickConfig['rotateFlag'],
  828. gridConfig: brickConfig['gridConfig'],
  829. deg: 0,
  830. brickNode: null,
  831. brickInitPos: new Vec3(),
  832. type: randomIndex,
  833. rotateNode: null,
  834. }
  835. this.bricksList.push(brickData)
  836. // 生成方块
  837. const brickNode = this.generateBrick(brickKey, randomIndex);
  838. if (this.brickNode) {
  839. this.brickNode.addChild(brickNode)
  840. }
  841. brickData.brickNode = brickNode;
  842. let offset = 220
  843. if (this.brickNum % 2 === 1) {
  844. const middleNum = Math.floor(this.brickNum / 2) + 1
  845. if (index < middleNum) {
  846. offset = - offset
  847. }
  848. else if (index === middleNum) {
  849. offset = 0
  850. }
  851. }
  852. if (brickData && brickData.brickNode) {
  853. brickData.brickNode.setPosition(offset, 0)
  854. brickData.brickNode.scale_x = 0.6;
  855. brickData.brickNode.scale_y = 0.6;
  856. brickData.brickInitPos = brickData.brickNode.getWorldPosition()
  857. }
  858. if (brickData.rotateFlag) {
  859. brickData.rotateNode = instantiate(this.rotatePrefab)
  860. this.rotateNode.addChild(brickData.rotateNode);
  861. //先隐藏
  862. brickData.rotateNode.active = false;
  863. brickData.rotateNode.setWorldPosition(brickNode.getWorldPosition())
  864. }
  865. this.brickAddEvent(brickData);
  866. }
  867. // 每个item生成独立的方块节点
  868. generateBrick(brickKey: string, randomIndex: number) {
  869. const brickConfig = this.brickConfig['bricks'][brickKey]
  870. let rowMin = 0
  871. let rowMax = 0
  872. let columnMin = 0
  873. let columnMax = 0
  874. brickConfig['gridConfig'].forEach((gridConfigData: GridConfigData) => {
  875. if (gridConfigData.row < rowMin) {
  876. rowMin = gridConfigData.row
  877. }
  878. else if (gridConfigData.row > rowMax) {
  879. rowMax = gridConfigData.row
  880. }
  881. if (gridConfigData.column < columnMin) {
  882. columnMin = gridConfigData.column
  883. }
  884. else if (gridConfigData.column > columnMax) {
  885. columnMax = gridConfigData.column
  886. }
  887. })
  888. const rowNum = (rowMax - rowMin + 1)
  889. const columnNum = (columnMax - columnMin + 1)
  890. // 生成独立的方块节点
  891. const brickNode = new Node()
  892. brickNode.name = brickKey
  893. // 设置方块大小
  894. const transformCom: UITransform = brickNode.addComponent(UITransform)
  895. transformCom.setContentSize(
  896. this.itemSize * columnNum,
  897. this.itemSize * rowNum
  898. )
  899. transformCom.setAnchorPoint(0.5, 0.5)
  900. // 设置方块位置
  901. const gridPrefab = this.itemPrefabs[randomIndex]
  902. //生成对应的配置方块设置地址
  903. brickConfig['gridConfig'].forEach((gridConfigData: GridConfigData) => {
  904. const gridNode = new Node()
  905. gridNode.name = 'grid'
  906. brickNode.addChild(gridNode)
  907. gridNode.addComponent(UITransform).setContentSize(this.itemSize, this.itemSize)
  908. const gridWidget: Widget = gridNode.addComponent(Widget)
  909. gridWidget.isAlignLeft = true
  910. gridWidget.left = this.itemSize * gridConfigData.column - this.itemSize * columnMin
  911. gridWidget.isAlignBottom = true
  912. gridWidget.bottom = this.itemSize * gridConfigData.row - this.itemSize * rowMin
  913. const node = instantiate(gridPrefab)
  914. gridNode.addChild(node)
  915. const uiTransform = node.getComponent(UITransform);
  916. if (uiTransform) {
  917. uiTransform.setContentSize(
  918. this.itemSize,
  919. this.itemSize,
  920. )
  921. }
  922. node.setPosition(Vec3.ZERO)
  923. node.on(Node.EventType.TOUCH_START, (event: EventTouch) => {
  924. event.preventSwallow = true;
  925. }, this);
  926. node.on(Node.EventType.TOUCH_MOVE, (event: EventTouch) => {
  927. event.preventSwallow = true;
  928. }, this);
  929. node.on(Node.EventType.TOUCH_END, (event: EventTouch) => {
  930. event.preventSwallow = true;
  931. }, this);
  932. })
  933. return brickNode
  934. }
  935. brickAddEvent(brickData: BrickData) {
  936. const brickNode = brickData.brickNode
  937. if (!brickNode) {
  938. console.error("brickNode为空,无法添加事件");
  939. return
  940. }
  941. // 记录初始位置和状态
  942. let startPos = new Vec3();
  943. let originalParent: Node | null = null;
  944. // 触摸开始事件
  945. brickNode.on(Node.EventType.TOUCH_START, (event: EventTouch) => {
  946. // 如果正在编辑其他方块,则忽略
  947. if (!this.operateFlag) return;
  948. // 清空编辑中的数据
  949. this.editingData.brickData = null;
  950. this.editingData.gridList.length = 0;
  951. // 记录触摸开始位置和方块原始信息
  952. this.touchStartPos.set(event.getUILocation());
  953. originalParent = brickNode.parent;
  954. startPos = brickNode.getWorldPosition().clone();
  955. // 将方块移到移动层并放大
  956. brickNode.setParent(this.moveNode);
  957. brickNode.setWorldPosition(startPos);
  958. // 从方块列表中移除该方块
  959. const index = this.bricksList.findIndex(item => item === brickData);
  960. if (index > -1) {
  961. this.editingData.brickData = this.bricksList.splice(index, 1)[0];
  962. } else {
  963. //返回原位置
  964. console.error(">>this.bricksList>>>>>>", JSON.stringify(this.bricksList));
  965. console.error("未找到方块数据:", JSON.stringify(brickData));
  966. this.bricksList.push(brickData)
  967. if (brickData.brickNode) {
  968. tween(brickData.brickNode)
  969. .to(0.15, {
  970. worldPosition: brickData.brickInitPos,
  971. scale: new Vec3(0.8, 0.8, 0.8)
  972. })
  973. .call(() => {
  974. if (brickData.brickNode) {
  975. this.operateFlag = true
  976. this.brickNode.addChild(brickData.brickNode)
  977. brickData.brickNode.setWorldPosition(brickData.brickInitPos)
  978. }
  979. })
  980. .start()
  981. }
  982. }
  983. }, this);
  984. // 触摸移动事件
  985. brickNode.on(Node.EventType.TOUCH_MOVE, (event: EventTouch) => {
  986. if (!this.operateFlag) return;
  987. const movePos: Vec2 = event.getUILocation().subtract(this.touchStartPos)
  988. // 挪动很小时,不移动方块组合
  989. if (movePos.length() <= this.rotateFaultTolerant) {
  990. return
  991. }
  992. // 隐藏旋转节点
  993. if (this.editingData.brickData && this.editingData.brickData.rotateFlag) {
  994. if (this.editingData.brickData.rotateNode) {
  995. this.editingData.brickData.rotateNode.active = false
  996. }
  997. }
  998. // 恢复所有网格颜色
  999. this.gridColorRecovery();
  1000. // 移动方块
  1001. brickNode.setWorldPosition(event.getUILocation().toVec3().add3f(0, this.yOffset, 0))
  1002. brickNode.scale_x = 1;
  1003. brickNode.scale_y = 1;
  1004. // 重置编辑中的网格数据
  1005. this.editingData.gridList.length = 0;
  1006. // 检查方块每个子网格是否与游戏网格重叠
  1007. const tempGridList: GridData[] = [];
  1008. let allEmptyGrids: boolean = true; // 标记是否所有网格都为空
  1009. brickNode.children.forEach((childNode) => {
  1010. const childWorldPos = childNode.getWorldPosition();
  1011. // 查找与子网格重叠的游戏网格
  1012. let matchedGrid: GridData | null = null;
  1013. for (let row = 0; row < this.rows && !matchedGrid; row++) {
  1014. for (let col = 0; col < this.cols && !matchedGrid; col++) {
  1015. const grid = this.gridList[row][col];
  1016. if (!grid || !grid.gridNode) continue;
  1017. const gridPos = grid.gridNode.getWorldPosition();
  1018. if (Vec3.distance(gridPos, childWorldPos) <= (this.itemSize / 2)) {
  1019. matchedGrid = grid;
  1020. }
  1021. }
  1022. }
  1023. if (matchedGrid) {
  1024. tempGridList.push(matchedGrid);
  1025. // 检查是否有非空网格
  1026. if (matchedGrid.status !== CellState.EMPTY) {
  1027. allEmptyGrids = false;
  1028. }
  1029. }
  1030. });
  1031. // 检查是否所有子网格都有对应的游戏网格,且都是空的
  1032. let canPlace = tempGridList.length === brickData.gridConfig.length && allEmptyGrids;
  1033. // 更新编辑中的网格列表
  1034. if (canPlace) {
  1035. this.editingData.gridList = [...tempGridList];
  1036. }
  1037. // 更新网格颜色提示 - 只改变空网格的颜色
  1038. tempGridList.forEach(grid => {
  1039. // 只处理空网格
  1040. if (grid.status === CellState.EMPTY) {
  1041. if (grid.gridNode && grid.gridNode.children[0]) {
  1042. const sprite = grid.gridNode.children[0].getComponent(Sprite);
  1043. if (sprite) {
  1044. sprite.color = canPlace ? this.usableColor : this.unavailableColor;
  1045. }
  1046. }
  1047. // 用于后续恢复颜色
  1048. this.gridColorList.push(grid);
  1049. }
  1050. });
  1051. }, this);
  1052. // 触摸结束或取消事件
  1053. const touchEndHandler = (event: EventTouch) => {
  1054. if (!this.operateFlag) return
  1055. this.operateFlag = false
  1056. // 单击旋转
  1057. if (!this.editingData.brickData) {
  1058. console.log("没有数据")
  1059. return
  1060. }
  1061. if (
  1062. this.editingData.brickData.rotateFlag &&
  1063. event.getUILocation().subtract(this.touchStartPos).length() <= this.rotateFaultTolerant
  1064. ) {
  1065. //旋转
  1066. const brickData = this.editingData.brickData
  1067. if (brickData && brickData.brickNode) {
  1068. this.bricksList.push(brickData)
  1069. this.brickNode.addChild(brickData.brickNode)
  1070. //显示旋转节点
  1071. if (brickData.rotateNode) {
  1072. brickData.rotateNode.active = true;
  1073. }
  1074. brickData.brickNode.setWorldPosition(brickData.brickInitPos)
  1075. // 旋转
  1076. this.brickGridRotate(brickData).then(() => {
  1077. this.operateFlag = true
  1078. })
  1079. // 隐藏旋转节点
  1080. this.scheduleOnce(() => {
  1081. if (brickData.rotateNode) {
  1082. brickData.rotateNode.active = false;
  1083. }
  1084. }, 0.4);
  1085. //打点
  1086. DCHandler.inst.reportData(3000600);
  1087. }
  1088. } else if (this.editingData.brickData && this.editingData.gridList.length > 0) {
  1089. if (this.isGuideMode && this.guideStep > 0) {
  1090. const brickData = this.editingData.brickData
  1091. const targetGrids = this.editingData.gridList
  1092. // 如果格子数量不足,直接视为无效放置
  1093. if (!brickData || !targetGrids || targetGrids.length !== brickData.gridConfig.length) {
  1094. this.bricksList.push(brickData)
  1095. if (brickData.brickNode) {
  1096. tween(brickData.brickNode)
  1097. .to(0.15, {
  1098. worldPosition: brickData.brickInitPos,
  1099. scale: new Vec3(0.8, 0.8, 0.8)
  1100. })
  1101. .call(() => {
  1102. if (brickData.brickNode) {
  1103. this.operateFlag = true
  1104. this.brickNode.addChild(brickData.brickNode)
  1105. brickData.brickNode.setWorldPosition(brickData.brickInitPos)
  1106. }
  1107. })
  1108. .start()
  1109. }
  1110. return
  1111. }
  1112. // 模拟砖块落下后的状态
  1113. const tempGridList = this.copyGridList()
  1114. for (let i = 0; i < targetGrids.length; i++) {
  1115. const g = targetGrids[i]
  1116. tempGridList[g.row][g.col].status = CellState.FILLED
  1117. }
  1118. const simulateResult = this.gridEliminateCheck(tempGridList)
  1119. const canEliminate = simulateResult.gridEliminateList.length > 0
  1120. //如果不能消除,则将方块返回原位置
  1121. if (!canEliminate) {
  1122. this.bricksList.push(brickData);
  1123. if (brickData.brickNode) {
  1124. tween(brickData.brickNode)
  1125. .to(0.15, {
  1126. worldPosition: brickData.brickInitPos,
  1127. scale: new Vec3(0.8, 0.8, 0.8)
  1128. })
  1129. .call(() => {
  1130. if (brickData.brickNode) {
  1131. this.operateFlag = true
  1132. this.brickNode.addChild(brickData.brickNode)
  1133. brickData.brickNode.setWorldPosition(brickData.brickInitPos)
  1134. }
  1135. })
  1136. .start()
  1137. }
  1138. return
  1139. }
  1140. }
  1141. // 显示放置得分动画(显示一次,包含总格子数)
  1142. this.showScoreAnimation();
  1143. // 放置方块到网格
  1144. this.editingData.gridList.forEach(grid => {
  1145. grid.status = CellState.FILLED;
  1146. grid.type = this.editingData.brickData!.type;
  1147. this.generateGrid(grid);
  1148. });
  1149. // 标记需要重置消除计数器
  1150. this.shouldResetEliminateCount = true;
  1151. brickNode.destroy();
  1152. this.addBrick(this.editingData.brickData.index);
  1153. // 检查消除
  1154. this.scheduleOnce(() => {
  1155. this.gridEliminate().then((hasElimination) => {
  1156. // 如果没有消除,确保下次消除会重置计数
  1157. if (!hasElimination) {
  1158. this.shouldResetEliminateCount = true;
  1159. }
  1160. this.operateFlag = true;
  1161. //新手引导
  1162. if (this.isGuideMode) {
  1163. this.scheduleOnce(() => {
  1164. if (this.guideStep < 4) {
  1165. this.setupGuideStep(this.guideStep + 1)
  1166. } else {
  1167. this.isGuideMode = false;
  1168. this.guideNode.active = false;
  1169. Tween.stopAllByTarget(this.guideFinger);
  1170. Tween.stopAllByTarget(this.guideItem3);
  1171. this.initGrid();
  1172. }
  1173. }, 0.3)
  1174. } else {
  1175. // 检查游戏是否结束
  1176. this.prompt(false).then(canContinue => {
  1177. if (!canContinue) {
  1178. this.gameOver();
  1179. }
  1180. });
  1181. }
  1182. });
  1183. }, 0.1);
  1184. } else {
  1185. // 无法放置,将方块返回原位置
  1186. const brickData = this.editingData.brickData;
  1187. if (brickData) {
  1188. this.bricksList.push(brickData);
  1189. this.operateFlag = false
  1190. // 添加回弹动画
  1191. tween(brickNode)
  1192. .to(0.2, {
  1193. worldPosition: brickData.brickInitPos,
  1194. scale: new Vec3(0.6, 0.6, 0.6)
  1195. })
  1196. .call(() => {
  1197. if (originalParent) {
  1198. this.operateFlag = true
  1199. brickNode.setParent(originalParent);
  1200. brickNode.setWorldPosition(brickData.brickInitPos);
  1201. }
  1202. })
  1203. .start();
  1204. }
  1205. }
  1206. // 格子颜色恢复
  1207. this.gridColorRecovery()
  1208. };
  1209. brickNode.on(Node.EventType.TOUCH_END, touchEndHandler, this);
  1210. brickNode.on(Node.EventType.TOUCH_CANCEL, touchEndHandler, this);
  1211. }
  1212. //格子颜色恢复
  1213. gridColorRecovery() {
  1214. while (this.gridColorList.length > 0) {
  1215. const gridData = this.gridColorList.pop();
  1216. if (gridData) {
  1217. if (gridData.status === CellState.EMPTY) {
  1218. if (gridData.gridNode) {
  1219. gridData.gridNode.children[0].getComponent(Sprite)!.color = this.notUseColor;
  1220. }
  1221. }
  1222. }
  1223. }
  1224. }
  1225. brickGridRotate(brickData: BrickData) {
  1226. return new Promise((resolve, reject) => {
  1227. const next = this.nextGridRotate(brickData.gridConfig, brickData.deg)
  1228. brickData.deg = next.deg
  1229. brickData.gridConfig = next.gridConfig
  1230. if (brickData.brickNode) {
  1231. tween(brickData.brickNode).to(this.aniBrickRotate, { angle: next.deg }).call(() => {
  1232. resolve(true)
  1233. }).start()
  1234. }
  1235. })
  1236. }
  1237. //下一个旋转
  1238. nextGridRotate(gridConfig: GridConfigData[], deg: number) {
  1239. const newGridConfig: GridConfigData[] = []
  1240. // 顺时针旋转
  1241. let newDeg = deg - 90
  1242. gridConfig.forEach((gridConfigData) => {
  1243. // 例如(1,2) => (-2,1),可以画图分析
  1244. newGridConfig.push({
  1245. row: -gridConfigData.column,
  1246. column: gridConfigData.row
  1247. })
  1248. })
  1249. return { gridConfig: newGridConfig, deg: newDeg }
  1250. }
  1251. // 显示分数增加动画
  1252. showScoreAnimation() {
  1253. if (!this.lab_addScore) return;
  1254. // 计算放置的格子数量
  1255. const placedGridCount = this.editingData.gridList.length;
  1256. // 获取中心位置用于显示分数
  1257. let centerPos = new Vec3(0, 0, 0);
  1258. if (this.editingData.gridList.length > 0 && this.editingData.gridList[0].gridNode) {
  1259. centerPos = this.editingData.gridList[0].gridNode.getWorldPosition().clone();
  1260. if (this.editingData.gridList.length > 1) {
  1261. // 计算所有格子的平均位置作为中心点
  1262. for (let i = 1; i < this.editingData.gridList.length; i++) {
  1263. const gridNode = this.editingData.gridList[i].gridNode;
  1264. if (gridNode) {
  1265. centerPos.add(gridNode.getWorldPosition());
  1266. }
  1267. }
  1268. const validGridCount = this.editingData.gridList.filter(grid => grid.gridNode).length;
  1269. centerPos.x /= validGridCount;
  1270. centerPos.y /= validGridCount;
  1271. }
  1272. }
  1273. let score = placedGridCount;
  1274. let position = centerPos
  1275. // 计算得分(未消除时的得分)
  1276. const calculatedScore = score * this.placementBaseScore;
  1277. // 复制得分Label
  1278. const scoreLabel = instantiate(this.lab_addScore.node);
  1279. scoreLabel.active = true;
  1280. // 设置文本和位置
  1281. const label = scoreLabel.getComponent(Label);
  1282. if (label) {
  1283. label.string = `+${calculatedScore}`;
  1284. }
  1285. // 添加到场景中
  1286. this.node.addChild(scoreLabel);
  1287. scoreLabel.setWorldPosition(position);
  1288. const num = this.isDoubleSpeed ? this.doubleNum : 1;
  1289. // 创建动画效果
  1290. tween(scoreLabel)
  1291. .to(0.5 / num, {
  1292. position: new Vec3(scoreLabel.position.x, scoreLabel.position.y + 50, 0),
  1293. scale: new Vec3(1.2, 1.2, 1.2)
  1294. })
  1295. .to(0.3 / num, { opacity: 0 })
  1296. .call(() => {
  1297. scoreLabel.destroy();
  1298. })
  1299. .start();
  1300. // 更新总分
  1301. this.score += calculatedScore;
  1302. }
  1303. // 显示消除次数动画
  1304. showEliminateCountAnimation(yPosition: number, eliminationCount: number) {
  1305. if (!this.comboNode) return;
  1306. // 如果需要重置计数器,先将计数归零
  1307. if (this.shouldResetEliminateCount) {
  1308. this.currentCombo = 0;
  1309. this.shouldResetEliminateCount = false;
  1310. }
  1311. // 增加累计消除次数
  1312. this.currentCombo += eliminationCount;
  1313. // 复制总次数Label
  1314. const comboNode = instantiate(this.comboNode);
  1315. comboNode.active = true;
  1316. // 设置文本
  1317. const label = comboNode.getChildByName("lab_combo");
  1318. if (label) {
  1319. const labelComponent = label.getComponent(Label);
  1320. if (labelComponent) {
  1321. labelComponent.string = `${this.currentCombo}`;
  1322. }
  1323. }
  1324. // 添加到场景中
  1325. this.node.addChild(comboNode);
  1326. // 计算网格区域的X轴中心位置,Y轴使用传入的消除行位置
  1327. const centerPos = new Vec3();
  1328. if (this.gridNode) {
  1329. // 获取网格区域的X轴中心
  1330. const worldPos = this.gridNode.getWorldPosition();
  1331. centerPos.set(worldPos.x, yPosition, worldPos.z);
  1332. }
  1333. comboNode.setWorldPosition(centerPos);
  1334. // 创建动画效果
  1335. const num = this.isDoubleSpeed ? this.doubleNum : 1;
  1336. tween(comboNode)
  1337. .to(0.3 / num, {
  1338. scale: new Vec3(1.5, 1.5, 1.5),
  1339. opacity: 255
  1340. })
  1341. .delay(0.5 / num) // 停留更长时间
  1342. .to(0.4 / num, {
  1343. scale: new Vec3(1.2, 1.2, 1.2),
  1344. position: new Vec3(comboNode.position.x, comboNode.position.y + 50, 0),
  1345. opacity: 0
  1346. })
  1347. .call(() => {
  1348. comboNode.destroy();
  1349. })
  1350. .start();
  1351. }
  1352. //
  1353. gridEliminate() {
  1354. return new Promise<boolean>((resolve, reject) => {
  1355. const d = this.gridEliminateCheck(this.gridList)
  1356. const gridEliminateList = d.gridEliminateList
  1357. const eliminateRowNum = d.eliminateRowNum
  1358. const eliminateColumnNum = d.eliminateColumnNum
  1359. const totalEliminationsInThisRound = eliminateRowNum + eliminateColumnNum
  1360. this.eliminateTotal += totalEliminationsInThisRound;
  1361. // console.log("消除总数", this.eliminateTotal)
  1362. if (gridEliminateList.length < 1) {
  1363. // 没有发生消除
  1364. this.updateGameScore();
  1365. this.shouldResetEliminateCount = true; // 下一次消除需要重置计数
  1366. resolve(false);
  1367. return;
  1368. }
  1369. // 如果有消除,计算消除行的平均Y轴位置
  1370. if (gridEliminateList.length > 0) {
  1371. // 计算所有被消除格子的平均Y轴位置
  1372. let totalY = 0;
  1373. let validGrids = 0;
  1374. for (const grid of gridEliminateList) {
  1375. if (grid.gridNode) {
  1376. totalY += grid.gridNode.getWorldPosition().y;
  1377. validGrids++;
  1378. }
  1379. }
  1380. // 计算平均Y位置
  1381. const avgY = validGrids > 0 ? totalY / validGrids : 0;
  1382. // 显示消除次数动画,传入Y轴位置和本次消除的行列总数
  1383. this.showEliminateCountAnimation(avgY, totalEliminationsInThisRound);
  1384. }
  1385. const num = this.isDoubleSpeed ? this.doubleNum : 1;
  1386. gridEliminateList.forEach((gridData) => {
  1387. if (gridData.gridNode?.children[0]) {
  1388. tween(gridData.gridNode.children[0])
  1389. .to(0.2 / num, { scale: new Vec3(0.5, 0.5) })
  1390. .call(() => {
  1391. gridData.status = CellState.EMPTY
  1392. this.generateGrid(gridData)
  1393. })
  1394. .start()
  1395. }
  1396. })
  1397. oops.audio.playEffect("common/audios/eliminate")
  1398. this.scheduleOnce(() => {
  1399. // 计算分数:每行/列的基础分 + 每个额外格子的分数
  1400. let score = 0;
  1401. // 行消除基础分
  1402. for (let i = 1; i <= eliminateRowNum; i++) {
  1403. score += this.eliminateBaseScore; // 每行基础分
  1404. score += (this.cols - 1) * this.extraGridScore; // 额外格子分
  1405. }
  1406. // 列消除基础分
  1407. for (let i = 1; i <= eliminateColumnNum; i++) {
  1408. score += this.eliminateBaseScore; // 每列基础分
  1409. score += (this.rows - 1) * this.extraGridScore; // 额外格子分
  1410. }
  1411. this.score += score;
  1412. if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
  1413. ServerHandler.inst.updateEliminationReward({
  1414. count: totalEliminationsInThisRound,
  1415. score: this.score,
  1416. level: smc.account.AccountModel.curLevel,
  1417. })
  1418. }
  1419. if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
  1420. //需要欧知道是否通关了,当前通关了就不要弹了
  1421. if (this.score < this.targetScore) {
  1422. if (this.eliminateInterval.length > 0) {
  1423. const minNum = this.eliminateInterval[0]
  1424. const maxNum = this.eliminateInterval[1]
  1425. if (minNum && maxNum) {
  1426. const randomNum = randomRangeInt(minNum, maxNum + 1);
  1427. //这个数可以被总消除次数整除那就可以弹广告
  1428. //console.log("最小随机数>>>>>>>>>", minNum);
  1429. //console.log("最大随机数>>>>>>>>>", maxNum);
  1430. //console.log("消除总数>>>>>>>>>>>>>", this.eliminateTotal);
  1431. //console.log("randomNum随机数>>>>>>>>>>", randomNum);
  1432. ////打印少量领取总数
  1433. //console.log("少量领取总数>>>>>>>>>>>>>>>", smc.game.GameModel.smallAdCount);
  1434. //console.log("skipAdCount>>>>>>>>>>>>>>", smc.game.GameModel.skipAdCount)
  1435. //console.log("skipAdConfig>>>>>>>>>>>>>>", smc.game.GameModel.skipAdConfig);
  1436. ////打印全都要总数
  1437. //console.log("全都要总数>>>>>>>>>>>>>>>", smc.game.GameModel.allAdCount);
  1438. ////打印直接弹出总数
  1439. //console.log("直接弹出总数>>>>>>>>>>>>>>>", smc.game.GameModel.directAdCount);
  1440. if (this.eliminateTotal % randomNum === 0) {
  1441. //如果有自动就暂停自动
  1442. if (smc.game.GameModel.skipAdConfig != -1) {
  1443. //大于就直接看视频
  1444. if (smc.game.GameModel.skipAdCount > smc.game.GameModel.skipAdConfig) {
  1445. smc.game.GameModel.viewType = "double_reward";
  1446. smc.game.GameModel.directAdCount++; //测试看
  1447. ADHandler.inst.showAd(AD_TYPE.Double_Receive);
  1448. } else {
  1449. if (smc.game.GameModel.skipAdCount == 0 || smc.game.GameModel.skipAdCount % smc.game.GameModel.skipAdConfig != 0) {
  1450. ServerHandler.inst.getDoubleSurprise();
  1451. } else {
  1452. smc.game.GameModel.viewType = "double_reward";
  1453. smc.game.GameModel.directAdCount++; //测试看
  1454. ADHandler.inst.showAd(AD_TYPE.Double_Receive);
  1455. }
  1456. }
  1457. } else {
  1458. ServerHandler.inst.getDoubleSurprise();
  1459. }
  1460. }
  1461. }
  1462. }
  1463. }
  1464. }
  1465. resolve(true)
  1466. }, 0.1)
  1467. })
  1468. }
  1469. //消除检查
  1470. gridEliminateCheck(gridList: GridData[][]) {
  1471. const gridEliminateList: GridData[] = []
  1472. let eliminateRowNum = 0
  1473. let eliminateColumnNum = 0
  1474. // 行检查
  1475. for (let rowIndex = 0; rowIndex < this.rows; rowIndex++) {
  1476. const rowData = gridList[rowIndex]
  1477. if (rowData.every(gridData => gridData.status === CellState.FILLED)) {
  1478. rowData.forEach(gridData => {
  1479. if (gridEliminateList.findIndex(data => data === gridData) < 0) {
  1480. gridEliminateList.push(gridData)
  1481. }
  1482. })
  1483. eliminateRowNum += 1
  1484. }
  1485. }
  1486. // 列检查
  1487. for (let columnIndex = 0; columnIndex < this.cols; columnIndex++) {
  1488. if (gridList.every(rowData => rowData[columnIndex].status === CellState.FILLED)) {
  1489. gridList.forEach(rowData => {
  1490. const gridData = rowData[columnIndex]
  1491. if (gridEliminateList.findIndex(data => data === gridData) < 0) {
  1492. gridEliminateList.push(gridData)
  1493. }
  1494. })
  1495. eliminateColumnNum += 1
  1496. }
  1497. }
  1498. return {
  1499. gridEliminateList,
  1500. eliminateRowNum,
  1501. eliminateColumnNum,
  1502. }
  1503. }
  1504. private createCoinFlyAnimation(prefab: Prefab, startPos: Vec3, endPos: Vec3, count: number, callback?: Function) {
  1505. if (!prefab) return;
  1506. let completedCount = 0;
  1507. const totalCoins = Math.min(count, 5); // 限制最大数量
  1508. const delayBetweenCoins = 0.1; // 每个金币之间的延迟时间
  1509. for (let i = 0; i < totalCoins; i++) {
  1510. const coin = instantiate(prefab);
  1511. this.node.addChild(coin);
  1512. //设置位置,取舞台中间位置世界坐标
  1513. coin.setWorldPosition(startPos);
  1514. // 创建曲线动画
  1515. const num = this.isDoubleSpeed ? this.doubleNum : 1;
  1516. // 添加延迟,使金币一个接一个飞出
  1517. tween(coin)
  1518. .delay(i * delayBetweenCoins / num) // 每个金币有不同的延迟
  1519. .to(0.2 / num, {
  1520. position: new Vec3(
  1521. coin.position.x,
  1522. coin.position.y,
  1523. 0
  1524. )
  1525. })
  1526. .to(0.5 / num, { worldPosition: endPos })
  1527. .call(() => {
  1528. coin.destroy();
  1529. completedCount++;
  1530. // 所有金币动画完成后执行回调
  1531. if (completedCount === totalCoins && callback) {
  1532. callback();
  1533. }
  1534. })
  1535. .start();
  1536. }
  1537. }
  1538. // 显示微信分数增加动画
  1539. private showWechatScoreAnimation() {
  1540. if (!this.tweenWechatNode) return;
  1541. // 生成随机小数(小于1,保留2位小数)
  1542. const changeNum = smc.game.GameModel.changeWxCoin;
  1543. this.money = smc.account.AccountModel.wxCoin;
  1544. const formattedValue = changeNum;
  1545. //console.log("微信币增加数值>>>>>>>>>", changeNum);
  1546. // 获取并设置分数标签
  1547. const scoreLabel = this.tweenWechatNode.getChildByName("lab_num")?.getComponent(Label);
  1548. if (scoreLabel) {
  1549. scoreLabel.string = "+" + Format.formatWxCoinModify(formattedValue);
  1550. }
  1551. // 保存原始位置
  1552. const originalPosition = this.lab_wxCoin.node.worldPosition;
  1553. //设置原始位置
  1554. this.tweenWechatNode.setWorldPosition(originalPosition);
  1555. // 显示节点
  1556. this.tweenWechatNode.active = true;
  1557. // 创建向上移动的动画
  1558. tween(this.tweenWechatNode)
  1559. .to(0.8, {
  1560. worldPosition: new Vec3(
  1561. originalPosition.x,
  1562. originalPosition.y + 50,
  1563. originalPosition.z
  1564. ),
  1565. opacity: 255
  1566. })
  1567. .to(0.2, { opacity: 0 })
  1568. .call(() => {
  1569. //设置位置y-100
  1570. this.tweenWechatNode.setWorldPosition(new Vec3(
  1571. originalPosition.x,
  1572. originalPosition.y,
  1573. originalPosition.z
  1574. ))
  1575. this.tweenWechatNode.active = false;
  1576. // 更新总金额
  1577. if (this.lab_wxCoin) {
  1578. this.lab_wxCoin.string = Format.formatWxCoin(this.money);
  1579. }
  1580. })
  1581. .start();
  1582. }
  1583. // 显示红包分数增加动画
  1584. private showRedPacketScoreAnimation() {
  1585. if (!this.tweenRedNode) return;
  1586. //如果有值就是要那个,没有就是取随机
  1587. const changeNum = smc.game.GameModel.changeHbCoin;
  1588. // console.log("红包分数增加动画", changeNum)
  1589. this.cash = smc.account.AccountModel.hbCoin;
  1590. if (!changeNum) return;
  1591. const formattedValue = changeNum;
  1592. // 获取并设置分数标签
  1593. const scoreLabel = this.tweenRedNode.getChildByName("lab_num")?.getComponent(Label);
  1594. if (scoreLabel) {
  1595. scoreLabel.string = "+" + Format.formatRedPacketCoin(formattedValue);
  1596. }
  1597. // 保存原始位置
  1598. const originalPosition = this.lab_hbCoin.node.worldPosition;
  1599. //设置原始位置
  1600. this.tweenRedNode.setWorldPosition(originalPosition);
  1601. // 显示节点
  1602. this.tweenRedNode.active = true;
  1603. // 创建向上移动的动画
  1604. const num = this.isDoubleSpeed ? this.doubleNum : 1;
  1605. tween(this.tweenRedNode)
  1606. .to(0.8 / num, {
  1607. worldPosition: new Vec3(
  1608. originalPosition.x,
  1609. originalPosition.y + 50,
  1610. originalPosition.z
  1611. ),
  1612. opacity: 255
  1613. })
  1614. .to(0.2 / num, { opacity: 0 })
  1615. .call(() => {
  1616. this.tweenRedNode.setWorldPosition(new Vec3(
  1617. originalPosition.x,
  1618. originalPosition.y,
  1619. originalPosition.z
  1620. ))
  1621. this.tweenRedNode.active = false;
  1622. // 更新总红包金额
  1623. if (this.lab_hbCoin) {
  1624. // this.lab_hbCoin.changeTo(0.5, newAmount, () => {
  1625. // })
  1626. this.lab_hbCoin.string = Format.formatRedPacketCoin(this.cash);
  1627. }
  1628. })
  1629. .start();
  1630. }
  1631. // 提示
  1632. prompt(tipFlag = true) {
  1633. return new Promise((resolve, reject) => {
  1634. const gridPromptList: GridData[] = []
  1635. let moveFlag = false
  1636. // 找方块可消除位置
  1637. for (let rowIndex = 0; rowIndex < this.rows && !moveFlag; rowIndex++) {
  1638. for (let columnIndex = 0; columnIndex < this.cols && !moveFlag; columnIndex++) {
  1639. const gridData = this.gridList[rowIndex][columnIndex]
  1640. if (gridData.status !== CellState.EMPTY) continue
  1641. // 方块不旋转检测是否能放
  1642. for (let brickI = 0; brickI < this.bricksList.length && !moveFlag; brickI++) {
  1643. const brickData = this.bricksList[brickI]
  1644. if (this.moveIf(rowIndex, columnIndex, brickData.gridConfig)) {
  1645. // 复制整体网格,以方块设置网格状态
  1646. const gridList = this.copyGridList()
  1647. brickData.gridConfig.forEach((gridConfigData) => {
  1648. gridList[gridConfigData.row + rowIndex][gridConfigData.column + columnIndex].status = CellState.FILLED
  1649. })
  1650. // 检查复制的整体网格是否有可消除
  1651. if (this.gridEliminateCheck(gridList).gridEliminateList.length > 0) {
  1652. moveFlag = true
  1653. brickData.gridConfig.forEach((gridConfigData) => {
  1654. gridPromptList.push(this.gridList[gridConfigData.row + rowIndex][gridConfigData.column + columnIndex])
  1655. })
  1656. }
  1657. }
  1658. }
  1659. // 方块旋转检测是否能放
  1660. for (let brickI = 0; brickI < this.bricksList.length && !moveFlag; brickI++) {
  1661. const brickData = this.bricksList[brickI]
  1662. if (!brickData.rotateFlag) continue
  1663. let gridConfig = brickData.gridConfig
  1664. let deg = brickData.deg
  1665. // 获得旋转的方块网格配置
  1666. for (let count = 1; count <= 3 && !moveFlag; count++) {
  1667. const next = this.nextGridRotate(gridConfig, deg)
  1668. if (this.moveIf(rowIndex, columnIndex, next.gridConfig)) {
  1669. // 复制整体网格,以方块设置网格状态
  1670. const gridList = this.copyGridList()
  1671. next.gridConfig.forEach((gridConfigData) => {
  1672. gridList[gridConfigData.row + rowIndex][gridConfigData.column + columnIndex].status = CellState.FILLED
  1673. })
  1674. // 检查复制的整体网格是否有可消除
  1675. if (this.gridEliminateCheck(gridList).gridEliminateList.length > 0) {
  1676. moveFlag = true
  1677. next.gridConfig.forEach((gridConfigData) => {
  1678. gridPromptList.push(this.gridList[gridConfigData.row + rowIndex][gridConfigData.column + columnIndex])
  1679. })
  1680. }
  1681. }
  1682. gridConfig = next.gridConfig
  1683. deg = next.deg
  1684. }
  1685. }
  1686. }
  1687. }
  1688. // 找方块可放置位置
  1689. for (let rowIndex = 0; rowIndex < this.rows && !moveFlag; rowIndex++) {
  1690. for (let columnIndex = 0; columnIndex < this.cols && !moveFlag; columnIndex++) {
  1691. const gridData = this.gridList[rowIndex][columnIndex]
  1692. if (gridData.status !== CellState.EMPTY) continue
  1693. // 方块不旋转检测是否能放
  1694. for (let brickI = 0; brickI < this.bricksList.length && !moveFlag; brickI++) {
  1695. const brickData = this.bricksList[brickI]
  1696. if (this.moveIf(rowIndex, columnIndex, brickData.gridConfig)) {
  1697. moveFlag = true
  1698. brickData.gridConfig.forEach((gridConfigData) => {
  1699. gridPromptList.push(this.gridList[gridConfigData.row + rowIndex][gridConfigData.column + columnIndex])
  1700. })
  1701. }
  1702. }
  1703. // 方块旋转检测是否能放
  1704. for (let brickI = 0; brickI < this.bricksList.length && !moveFlag; brickI++) {
  1705. const brickData = this.bricksList[brickI]
  1706. if (!brickData.rotateFlag) continue
  1707. let gridConfig = brickData.gridConfig
  1708. let deg = brickData.deg
  1709. for (let count = 1; count <= 3 && !moveFlag; count++) {
  1710. const next = this.nextGridRotate(gridConfig, deg)
  1711. if (this.moveIf(rowIndex, columnIndex, next.gridConfig)) {
  1712. moveFlag = true
  1713. next.gridConfig.forEach((gridConfigData) => {
  1714. gridPromptList.push(this.gridList[gridConfigData.row + rowIndex][gridConfigData.column + columnIndex])
  1715. })
  1716. }
  1717. gridConfig = next.gridConfig
  1718. deg = next.deg
  1719. }
  1720. }
  1721. }
  1722. }
  1723. if (gridPromptList.length < 1) {
  1724. resolve(false)
  1725. return
  1726. }
  1727. if (!tipFlag) {
  1728. resolve(true)
  1729. return
  1730. }
  1731. // 提示用户(网格变绿)
  1732. gridPromptList.forEach((gridData) => {
  1733. if (gridData.gridNode && gridData.gridNode.children[0]) {
  1734. const sprite = gridData.gridNode.children[0].getComponent(Sprite);
  1735. if (sprite) {
  1736. sprite.color = this.usableColor;
  1737. }
  1738. }
  1739. // 用于恢复格子
  1740. this.gridColorList.push(gridData)
  1741. })
  1742. resolve(true)
  1743. })
  1744. }
  1745. moveIf(row: number, column: number, gridConfig: GridConfigData[]) {
  1746. let moveFlag = true
  1747. for (let i = 0; i < gridConfig.length; i++) {
  1748. const gridConfigData = gridConfig[i]
  1749. const gridI = row + gridConfigData.row
  1750. const gridJ = column + gridConfigData.column
  1751. // 边界判断
  1752. if (
  1753. gridI < 0 ||
  1754. gridI > this.rows - 1 ||
  1755. gridJ < 0 ||
  1756. gridJ > this.cols - 1
  1757. ) {
  1758. moveFlag = false
  1759. break
  1760. }
  1761. // 已用
  1762. else if (this.gridList[gridI][gridJ].status === CellState.FILLED) {
  1763. moveFlag = false
  1764. break
  1765. }
  1766. }
  1767. return moveFlag
  1768. }
  1769. // 复制整体网格
  1770. copyGridList() {
  1771. const gridList: GridData[][] = []
  1772. for (let rowIndex = 0; rowIndex < this.rows; rowIndex++) {
  1773. gridList.push([])
  1774. for (let columnIndex = 0; columnIndex < this.cols; columnIndex++) {
  1775. const gridData = this.gridList[rowIndex][columnIndex]
  1776. gridList[rowIndex].push({
  1777. name: gridData.name,
  1778. status: gridData.status,
  1779. gridNode: null,
  1780. row: gridData.row,
  1781. col: gridData.col,
  1782. type: gridData.type,
  1783. })
  1784. }
  1785. }
  1786. return gridList
  1787. }
  1788. gameOver() {
  1789. this.gameState = GameState.GAME_OVER
  1790. smc.game.GameModel.curScore = this.score;
  1791. oops.gui.open(UIID.GameOver);
  1792. const curLevel = smc.account.AccountModel.curLevel;
  1793. DCHandler.inst.reportData(3000501, curLevel);
  1794. }
  1795. //更新游戏分数
  1796. updateGameScore() {
  1797. this.lab_score.string = this.score.toString();
  1798. smc.game.GameModel.curScore = this.score;
  1799. if (this.score >= this.targetScore) {
  1800. this.gameState = GameState.GAME_PASS;
  1801. //防止第一关的时候出问题
  1802. if (this.guideNode.active) {
  1803. this.guideNode.active = false;
  1804. }
  1805. //弹出通关奖励界面
  1806. DCHandler.inst.reportData(3000505, smc.account.AccountModel.curLevel);
  1807. if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
  1808. ServerHandler.inst.getGameAwardInfo();
  1809. //如果
  1810. } else {
  1811. oops.gui.open(UIID.GamePass);
  1812. }
  1813. }
  1814. }
  1815. /**
  1816. * 清理网格的子节点
  1817. */
  1818. private clearGridChildren(gridNode: Node): void {
  1819. const children = gridNode.children.slice(); // 创建副本避免遍历时修改问题
  1820. children.forEach(node => node.destroy());
  1821. }
  1822. //========================打开其他界面和按钮逻辑=======================
  1823. /** 视图对象通过 ecs.Entity.remove(eliminateViewComp) 删除组件是触发组件处理自定义释放逻辑 */
  1824. reset() {
  1825. this.node.destroy();
  1826. }
  1827. //设置按钮
  1828. private btn_setting() {
  1829. oops.gui.open(UIID.Setting);
  1830. this.gameState = GameState.PAUSED;
  1831. DCHandler.inst.reportData(3000011);
  1832. }
  1833. //左边微信按钮
  1834. private btn_withdraw() {
  1835. if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
  1836. this.gameState = GameState.PAUSED;
  1837. ServerHandler.inst.getWechatTxInfo();
  1838. DCHandler.inst.reportData(3000006);
  1839. } else {
  1840. oops.gui.open(UIID.WechatWithdraw);
  1841. }
  1842. }
  1843. //顶部红包按钮
  1844. private btn_award() {
  1845. if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
  1846. this.gameState = GameState.PAUSED;
  1847. ServerHandler.inst.getHbTxInfo();
  1848. DCHandler.inst.reportData(3000007);
  1849. } else {
  1850. oops.gui.open(UIID.RedPacketWithdraw);
  1851. }
  1852. }
  1853. //二倍速按钮继续
  1854. private btn_double() {
  1855. // oops.gui.open(UIID.DoubleSpeed);
  1856. if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
  1857. this.gameState = GameState.PAUSED;
  1858. ServerHandler.inst.getDoubleSpeedTime();
  1859. DCHandler.inst.reportData(3000010);
  1860. } else {
  1861. oops.gui.open(UIID.DoubleSpeed);
  1862. }
  1863. // console.log("二倍速看视频统计>>>>>>>>>>>>>>>", smc.game.GameModel.doubleSpeedAdCount);
  1864. }
  1865. //自动放置,不让点击方块
  1866. private btn_auto(event: EventTouch) {
  1867. this.autoGame();
  1868. }
  1869. private autoGame() {
  1870. this.autoState = !this.autoState;
  1871. this.initButtonState(this.autoState);
  1872. if (this.autoState) {
  1873. DCHandler.inst.reportData(3000008);
  1874. this.executeAutoPlace();
  1875. this.autoFunction = this.executeAutoPlace;
  1876. this.schedule(this.autoFunction, this.autoInterval);
  1877. this.autoNode.active = true;
  1878. } else {
  1879. //关闭
  1880. if (this.autoFunction) {
  1881. DCHandler.inst.reportData(3000009);
  1882. this.operateFlag = true;
  1883. this.unschedule(this.autoFunction);
  1884. this.autoFunction = null;
  1885. this.initButtonState(false);
  1886. this.autoState = false;
  1887. this.autoNode.active = false;
  1888. }
  1889. }
  1890. }
  1891. // 执行自动放置
  1892. executeAutoPlace() {
  1893. //console.log("自动放置状态", this.autoState);
  1894. // console.log("当前游戏状态", this.gameState);
  1895. if (!this.autoState || this.gameState !== GameState.PLAYING) {
  1896. return
  1897. }
  1898. const bestPlacement = this.findBestPlacement()
  1899. if (!bestPlacement) {
  1900. // 所有方块都无法放置,游戏结束
  1901. this.gameState = GameState.GAME_OVER;
  1902. this.gameOver()
  1903. return
  1904. }
  1905. // 执行放置
  1906. this.operateFlag = false;
  1907. this.placeBrickAtPosition(bestPlacement)
  1908. oops.log.logView
  1909. }
  1910. //寻找最佳位置
  1911. findBestPlacement() {
  1912. const placements = []
  1913. // 对每个方块计算所有可能的放置位置和分数
  1914. for (let brickIndex = 0; brickIndex < this.bricksList.length; brickIndex++) {
  1915. const brickData = this.bricksList[brickIndex]
  1916. // 检查不同旋转状态
  1917. let gridConfigs = [brickData.gridConfig]
  1918. let degrees = [brickData.deg]
  1919. // 如果可旋转,计算所有旋转状态
  1920. if (brickData.rotateFlag) {
  1921. for (let i = 1; i <= 3; i++) {
  1922. const next = this.nextGridRotate(
  1923. i === 1 ? brickData.gridConfig : gridConfigs[i - 1],
  1924. i === 1 ? brickData.deg : degrees[i - 1]
  1925. )
  1926. gridConfigs.push(next.gridConfig)
  1927. degrees.push(next.deg)
  1928. }
  1929. }
  1930. // 遍历所有网格位置
  1931. for (let rowIndex = 0; rowIndex < this.rows; rowIndex++) {
  1932. for (let columnIndex = 0; columnIndex < this.cols; columnIndex++) {
  1933. // 对每个旋转状态检查
  1934. for (let rotateIndex = 0; rotateIndex < gridConfigs.length; rotateIndex++) {
  1935. const currentGridConfig = gridConfigs[rotateIndex]
  1936. const currentDeg = degrees[rotateIndex]
  1937. // 检查是否可以放置
  1938. if (this.moveIf(rowIndex, columnIndex, currentGridConfig)) {
  1939. // 复制网格并模拟放置
  1940. const gridList = this.copyGridList()
  1941. currentGridConfig.forEach((gridConfigData) => {
  1942. const r = gridConfigData.row + rowIndex
  1943. const c = gridConfigData.column + columnIndex
  1944. gridList[r][c].status = CellState.FILLED
  1945. gridList[r][c].type = brickData.type;
  1946. })
  1947. // 检查是否可以消除,计算分数
  1948. const elimination = this.gridEliminateCheck(gridList)
  1949. let score = 0
  1950. if (elimination.gridEliminateList.length > 0) {
  1951. // 计算消除得分
  1952. for (let i = 1; i <= elimination.eliminateRowNum; i++) {
  1953. score += this.cols * i
  1954. }
  1955. for (let i = 1; i <= elimination.eliminateColumnNum; i++) {
  1956. score += this.rows * i
  1957. }
  1958. }
  1959. // 记录此放置选项
  1960. placements.push({
  1961. brickIndex,
  1962. brickData,
  1963. rowIndex,
  1964. columnIndex,
  1965. gridConfig: currentGridConfig, //这里有问题,拿到的和显示的不对
  1966. deg: currentDeg,
  1967. score,
  1968. canEliminate: elimination.gridEliminateList.length > 0
  1969. })
  1970. }
  1971. }
  1972. }
  1973. }
  1974. }
  1975. // 按优先级排序:最高分 > 有分数 > 没有分数但可放置
  1976. placements.sort((a, b) => {
  1977. // 首先按分数排序
  1978. if (a.score !== b.score) {
  1979. return b.score - a.score
  1980. }
  1981. // 其次按是否可消除排序
  1982. if (a.canEliminate !== b.canEliminate) {
  1983. return a.canEliminate ? -1 : 1
  1984. }
  1985. // 最后按照方块优先级排序(底部的方块优先)
  1986. return a.brickIndex - b.brickIndex
  1987. })
  1988. return placements.length > 0 ? placements[0] : null
  1989. }
  1990. // ... existing code ...
  1991. placeBrickAtPosition(placement: any) {
  1992. const brickData = placement.brickData
  1993. const index = this.bricksList.findIndex(data => data === brickData)
  1994. if (index === -1) {
  1995. console.error("无法找到要放置的方块:", brickData)
  1996. return
  1997. }
  1998. this.editingData.brickData = this.bricksList.splice(index, 1)[0];
  1999. // 应用旋转
  2000. if (brickData.deg !== placement.deg) {
  2001. brickData.gridConfig = placement.gridConfig
  2002. brickData.deg = placement.deg
  2003. tween(brickData.brickNode).to(0.1, { angle: placement.deg }).start()
  2004. }
  2005. // 构建对应网格列表--这里就有问题了,这算位置,有问题
  2006. this.editingData.gridList = [];
  2007. placement.gridConfig.forEach((gridConfigData: GridConfigData) => {
  2008. const r = gridConfigData.row + placement.rowIndex
  2009. const c = gridConfigData.column + placement.columnIndex
  2010. this.editingData.gridList.push(this.gridList[r][c])
  2011. })
  2012. // 计算移动位置(中点)
  2013. let centerPos = new Vec3(0, 0, 0)
  2014. let count = 0
  2015. for (const grid of this.editingData.gridList) {
  2016. if (grid.gridNode) {
  2017. const pos = grid.gridNode.getWorldPosition()
  2018. centerPos.add(pos)
  2019. count++
  2020. }
  2021. }
  2022. if (count > 0) {
  2023. centerPos.x /= count
  2024. centerPos.y /= count
  2025. centerPos.z /= count
  2026. }
  2027. const originPos = brickData.brickNode.getWorldPosition()
  2028. brickData.brickNode.setParent(this.moveNode)
  2029. brickData.brickNode.setWorldPosition(originPos);
  2030. //显示增加分数
  2031. // 动画放置方块
  2032. tween(brickData.brickNode)
  2033. .to(this.autoMoveTime, { worldPosition: centerPos })
  2034. .call(() => {
  2035. // ✅ 更新格子状态(像手动放置那样)
  2036. this.showScoreAnimation();
  2037. this.editingData.gridList.forEach(gridData => {
  2038. gridData.status = CellState.FILLED
  2039. gridData.type = brickData.type;
  2040. this.generateGrid(gridData)
  2041. })
  2042. // ✅ 销毁方块节点
  2043. brickData.brickNode.destroy()
  2044. // ✅ 补充新的砖块
  2045. this.addBrick(brickData.index)
  2046. // ✅ 消除检查逻辑保持和手动一致
  2047. this.scheduleOnce(() => {
  2048. this.gridEliminate().then(() => {
  2049. this.prompt(false).then((promptFlag) => {
  2050. if (!promptFlag) {
  2051. this.gameOver()
  2052. }
  2053. })
  2054. })
  2055. })
  2056. })
  2057. .start()
  2058. }
  2059. doubleSpeedOpenSuccess() {
  2060. //2倍速广告展示,页面展示,都会暂停游戏,
  2061. if (this.gameState !== GameState.PLAYING) {
  2062. return
  2063. }
  2064. if (this.callback) {
  2065. this.unschedule(this.callback);
  2066. }
  2067. oops.gui.toast("二倍速时长已增加3分钟~")
  2068. this.isDoubleSpeed = true;
  2069. this.doubleNum = 1.5;
  2070. //改变按钮时间
  2071. // let time = smc.game.GameModel.doubleSpeedTime;
  2072. this.doubleSpeedTime = this.doubleSpeedTime + smc.game.GameModel.doubleSpeedTime;
  2073. // this.doubleSpeedTime = 60;
  2074. if (this.doubleSpeedTime <= 0) {
  2075. return
  2076. }
  2077. if (!this.autoState) {
  2078. this.autoGame();
  2079. //改按钮状态
  2080. this.autoState = true;
  2081. this.initButtonState(this.autoState);
  2082. }
  2083. this.callback = function () {
  2084. if (this.gameState === GameState.PLAYING && this.autoState) {
  2085. this.doubleSpeedTime--
  2086. this.autoMoveTime = 0.25;
  2087. this.autoInterval = 1.1;
  2088. //修改剩余时间,秒格式化成时分10:59
  2089. const minutes = Math.floor(this.doubleSpeedTime / 60);
  2090. const seconds = this.doubleSpeedTime % 60;
  2091. const formattedTime = `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
  2092. this.lab_doubleTime.string = formattedTime;
  2093. //时间到了
  2094. if (this.doubleSpeedTime <= 0) {
  2095. this.autoMoveTime = 0.4;
  2096. this.autoInterval = 1.5;
  2097. this.lab_doubleTime.string = "二倍速";
  2098. this.isDoubleSpeed = false;
  2099. this.doubleNum = 1;
  2100. //再打开继续二倍速页面
  2101. //自动按钮关闭
  2102. this.autoState = false;
  2103. this.initButtonState(this.autoState);
  2104. oops.gui.open(UIID.KeepSpeed);
  2105. this.unschedule(this.callback);
  2106. }
  2107. }
  2108. if (!this.autoState && this.doubleSpeedTime) {
  2109. this.lab_doubleTime.string = "二倍速";
  2110. }
  2111. }
  2112. this.schedule(this.callback, 1);
  2113. }
  2114. //重新开始
  2115. private restartGame() {
  2116. if (this.gameState === GameState.READY) return;
  2117. this.initData();
  2118. this.setData();
  2119. this.reopenGrid();
  2120. this.initButtonState(this.autoState);
  2121. this.updateWelfarePoint();
  2122. }
  2123. //重新清除网格
  2124. private reopenGrid() {
  2125. for (let rowIndex = 0; rowIndex < this.rows; rowIndex++) {
  2126. for (let columnIndex = 0; columnIndex < this.cols; columnIndex++) {
  2127. this.gridList[rowIndex][columnIndex].status = CellState.EMPTY
  2128. this.generateGrid(this.gridList[rowIndex][columnIndex])
  2129. }
  2130. }
  2131. // 初始化方块
  2132. if (this.brickNode) {
  2133. if (this.brickNode) {
  2134. this.brickNode.destroyAllChildren();
  2135. }
  2136. }
  2137. this.bricksList.length = 0
  2138. for (let i = 1; i <= this.brickNum; i++) {
  2139. this.addBrick(i)
  2140. }
  2141. // 清除旋转数据
  2142. if (this.rotateNode) {
  2143. this.rotateNode.destroyAllChildren();
  2144. }
  2145. }
  2146. //===============初始化检查是否要打开福利界面====
  2147. private updateWelfarePoint() {
  2148. const showState = smc.game.GameModel.popupShow
  2149. if (!showState) {
  2150. //展示这些的时候,游戏要暂停
  2151. // if (this.popupType != "") {
  2152. // this.gameState = GameState.PAUSED;
  2153. // }
  2154. switch (this.popupType) {
  2155. case "weal_1":
  2156. oops.gui.open(UIID.WarmReminder);
  2157. ServerHandler.inst.getGuideInfo();
  2158. break;
  2159. case "weal_2":
  2160. //打开福利二
  2161. oops.gui.open(UIID.WelfareTwo);
  2162. ServerHandler.inst.getGuideInfo();
  2163. break;
  2164. case "weal_3":
  2165. //打开福利三
  2166. oops.gui.open(UIID.WelfareThree);
  2167. break;
  2168. case "sign":
  2169. //打开福利三
  2170. oops.gui.open(UIID.ReservePopup);
  2171. break;
  2172. case "handlingCharge":
  2173. oops.gui.open(UIID.WechatTransfer);
  2174. break;
  2175. }
  2176. smc.game.GameModel.popupShow = true;
  2177. }
  2178. }
  2179. protected onDestroy(): void {
  2180. oops.message.off(GameEvent.RestartGame, this.restartGame, this);
  2181. oops.message.off(GameEvent.DoubleSpeedOpenSuccess, this.doubleSpeedOpenSuccess, this);
  2182. oops.message.off(GameEvent.openView, this.openView, this);
  2183. oops.message.off(GameEvent.showCoinAnimation, this.showCoinAnimation, this);
  2184. oops.message.off(GameEvent.updateHbAndWxCoin, this.updateCoin, this);
  2185. oops.message.off(GameEvent.StartAutoGame, this.startAutoGame, this);
  2186. oops.message.off(GameEvent.updateGameState, this.updateGameState, this);
  2187. oops.message.off(GameEvent.updateGameScore, this.updateGameScore, this);
  2188. oops.message.off(GameEvent.pauseGame, this.pauseGame, this);
  2189. oops.message.off(GameEvent.resumeGame, this.resumeGame, this);
  2190. }
  2191. btn_text() {
  2192. // const param = {
  2193. // "code": 0,
  2194. // "data": {
  2195. // "ssid": "9201d75d019f46f98b8663adfd515a58",
  2196. // "props": {
  2197. // "1004": 7077279,
  2198. // "1005": 2143634,
  2199. // "1006": 12,
  2200. // "1007": 186,
  2201. // "1008": 22,
  2202. // "1009": 299980,
  2203. // "2001": 2,
  2204. // "8001": 530,
  2205. // "9001": 2
  2206. // },
  2207. // "changes": {
  2208. // "1004": 1,
  2209. // "1007": 1
  2210. // },
  2211. // "tipThreshold": true
  2212. // }
  2213. // }
  2214. // let str = JSON.stringify(param);
  2215. // ServerHandler.inst.onGetVideorReward(str);
  2216. // ServerHandler.inst.GetGuideInfo();
  2217. oops.gui.open(UIID.WechatTransfer);
  2218. }
  2219. getRandomUniqueInts(count: number = 3): number[] {
  2220. let min: number = 0;
  2221. let max: number = 0;
  2222. const curLevel = smc.account.AccountModel.curLevel || 1;
  2223. switch (curLevel) {
  2224. case 1:
  2225. return [1]
  2226. break;
  2227. case 2:
  2228. case 3:
  2229. min = 1;
  2230. max = 3;
  2231. break;
  2232. case 4:
  2233. case 5:
  2234. case 6:
  2235. min = 4;
  2236. max = 9;
  2237. break;
  2238. case 7:
  2239. case 8:
  2240. min = 10;
  2241. max = 18;
  2242. break;
  2243. case 9:
  2244. case 10:
  2245. case 11:
  2246. case 12:
  2247. min = 19;
  2248. max = 27;
  2249. break;
  2250. default:
  2251. min = 28;
  2252. max = 42;
  2253. break;
  2254. }
  2255. if (max - min + 1 < count) {
  2256. throw new Error(`区间 [${min}, ${max}] 无法提供 ${count} 个不重复整数`);
  2257. }
  2258. const pool: number[] = [];
  2259. for (let i = min; i <= max; i++) {
  2260. pool.push(i);
  2261. }
  2262. this.shuffle(pool);
  2263. return pool.slice(0, count);
  2264. }
  2265. // 洗牌算法(Fisher-Yates)
  2266. shuffle(arr: number[]) {
  2267. for (let i = arr.length - 1; i > 0; i--) {
  2268. const j = Math.floor(Math.random() * (i + 1));
  2269. [arr[i], arr[j]] = [arr[j], arr[i]];
  2270. }
  2271. }
  2272. }