EliminateViewComp.ts 93 KB

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