EliminateViewComp.ts 92 KB

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