EliminateViewComp.ts 93 KB

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