EliminateViewComp.ts 92 KB

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