GamePlay.ts 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. import CellItem from "./view/uiItem/CellItem";
  2. import { PROPTYPE, CHECKDIR, SURPRISETASKTYPE } from "./data/Enum";
  3. import PoolMgr, { NODEPOOLPREFABTYPE } from "./mgr/PoolMgr";
  4. import GameConst from "./data/GameConst";
  5. import GameLogic from "./util/GameLogic";
  6. import ArtNum from "./util/common/ArtNum";
  7. import BonusTip from "./view/effect/BonusTip";
  8. import PlayerConst from "./data/PlayerConst";
  9. import DataMgr from "./mgr/DataMgr";
  10. import RedPacketUI from "./view/RedPacketUI";
  11. import LevelRedPacketItem from "./view/uiItem/LevelRedPacketItem";
  12. import EffectMgr, { TIP_SPRITEITEM_TYPE } from "./mgr/EffectMgr";
  13. import GameMgr, { UI_NAME } from "./mgr/GameMgr";
  14. import { AUDIO_TYPE, EVENT_TYPE } from "../game/data/GameData";
  15. const { ccclass, property } = cc._decorator;
  16. @ccclass
  17. export default class GamePlay extends cc.Component {
  18. /**单例模式 */
  19. public static Inst: GamePlay = null;
  20. /**背景 */
  21. @property(cc.Node)
  22. node_bg: cc.Node = null;
  23. /**标题 */
  24. @property(cc.Node)
  25. node_top: cc.Node = null;
  26. @property(cc.Label)
  27. label_level: cc.Label = null;
  28. @property(cc.Node)
  29. node_redPacketIcon: cc.Node = null;
  30. /**内容 */
  31. @property(cc.Node)
  32. node_content: cc.Node = null;
  33. /**cell背景块 */
  34. @property(cc.Node)
  35. node_cellBg: cc.Node = null;
  36. /**暂停按钮 */
  37. @property(cc.Node)
  38. node_pauseBtn: cc.Node = null;
  39. /**进度条UI */
  40. @property(cc.Node)
  41. node_progressUI: cc.Node = null;
  42. @property(cc.Sprite)
  43. spr_progress: cc.Sprite = null;
  44. /**重置按钮 */
  45. @property(cc.Node)
  46. node_resetBtn: cc.Node = null;
  47. @property(cc.Label)
  48. label_resetPropNum: cc.Label = null;
  49. /**锤子按钮 */
  50. @property(cc.Node)
  51. node_hammerBtn: cc.Node = null;
  52. @property(cc.Label)
  53. label_hammerPropNum: cc.Label = null;
  54. /**变色按钮 */
  55. @property(cc.Node)
  56. node_changeBtn: cc.Node = null;
  57. @property(cc.Label)
  58. label_changePropNum: cc.Label = null;
  59. /**更改CellItemUI */
  60. @property(cc.Node)
  61. node_changeCellItemUI: cc.Node = null;
  62. /**特效UI */
  63. @property(cc.Node)
  64. node_effectUI: cc.Node = null;
  65. /**关卡红包UI */
  66. @property(cc.Node)
  67. node_levelRedPacketUI: cc.Node = null;
  68. /**点击检测遮罩 */
  69. @property(cc.Node)
  70. node_touchListenMask: cc.Node = null;
  71. /**当前进度得分 */
  72. public curProgressScore: number = 0;
  73. /**当前得分 */
  74. public curGetScore: number = 0;
  75. /**目标分数 */
  76. public targetScore: number = 100000;
  77. /**最终得分 */
  78. public finalGetScore: number = 0;
  79. /**增加得分速度 */
  80. public addScoreSpeed: number = 4;
  81. // /**道具类型 */
  82. // public curPropType: PROPTYPE = PROPTYPE.Null;
  83. /**消消的方块数组
  84. * @param:key index_x
  85. * @param: value 整个纵列数组
  86. */
  87. public cellItemDic: { [key: number]: CellItem[] } = {};
  88. public cellItemArr: CellItem[] = [];
  89. /**清理CellItem的Vec数组 */
  90. public cleanedVecArr: cc.Vec2[] = [];
  91. /**能够清理的vecArr组 */
  92. public couldCleanVecArr: cc.Vec2[] = [];
  93. /**清理纵列的x */
  94. public cleanXIndexArr: number[] = [];
  95. /**所有的清理 */
  96. public allCleanedXArr: number[] = [];
  97. /**剩余未移除的xIndex */
  98. public leftXIndexArr: number[] = [];
  99. /**当前选择的cellItem */
  100. public curSelectCellItem: CellItem = null;
  101. /**当前点击得cellItem */
  102. public curClickCellItem: CellItem = null;
  103. /**当前选择的道具按钮 */
  104. public curSelectPropBtn: cc.Node = null;
  105. /**是否可以点击 */
  106. public ifCouldClick: boolean = true;
  107. /**是否已经通关 */
  108. public ifPass: boolean = false;
  109. /**是否获取后台回调 */
  110. public ifGetPass: boolean = false;
  111. /**当前的道具类型 */
  112. private _curPropType: PROPTYPE;
  113. public get curPropType(): PROPTYPE {
  114. return this._curPropType;
  115. }
  116. public set curPropType(v: PROPTYPE) {
  117. //LogUtil.log("this.curSelectPropBtn", this.curSelectPropBtn);
  118. let ani = null;
  119. if (this.curSelectPropBtn) {
  120. mk.console.log("this.curSelectPropBtn", this.curSelectPropBtn);
  121. ani = this.curSelectPropBtn.children[1].getComponent(cc.Animation);
  122. }
  123. if (v == PROPTYPE.Null) {
  124. if (ani) {
  125. ani.play("ani_normalBtn");
  126. }
  127. this.setPropBtnNormalTip(this.curSelectPropBtn)
  128. }
  129. else {
  130. if (ani) {
  131. ani.play("ani_selectBtn");
  132. }
  133. this.setPropBtnSelectTip(this.curSelectPropBtn)
  134. }
  135. this._curPropType = v;
  136. }
  137. // LIFE-CYCLE CALLBACKS:
  138. onLoad() {
  139. GamePlay.Inst = this;
  140. }
  141. public start() {
  142. PoolMgr.Inst.initPoolPrefab();
  143. GameConst.loadConfig().then(() => {
  144. this.adapt();
  145. // AudioMgr.Inst.playMusic(AUDIO_CLIP_NAME.bg_game);
  146. mk.audio.playMusic("music_gameBg");
  147. this.initView();
  148. this.initEvent();
  149. this.intervalShowGuide();
  150. }).catch();
  151. }
  152. onEnable() {
  153. mk.console.log("PROPTYPE[PROPTYPE.Change]", PROPTYPE.Change);
  154. GameMgr.Inst.sendEvent(UI_NAME.Game, "进入游戏界面");
  155. // this.initLevel();
  156. }
  157. onDisable() {
  158. //退出游戏归置下道具状态
  159. this.curPropType = PROPTYPE.Null;
  160. if (this.curSelectCellItem) {
  161. this.curSelectCellItem.normal();
  162. }
  163. if (this.node_changeCellItemUI.active = true) {
  164. this.node_changeCellItemUI.active = false;
  165. }
  166. //this.clearSurpriseTask();
  167. }
  168. adapt() {
  169. mk.console.log("进行适配!!!!!!!!!!!!!!!!");
  170. //背景适配
  171. this.node_bg.setContentSize(new cc.Size(cc.winSize.width, cc.winSize.height));
  172. this.node_top.y = (cc.winSize.height - this.node_top.height) * 0.5 + 5;
  173. }
  174. update(dt) {
  175. if (this.curProgressScore >= this.curGetScore) {
  176. if (this.curProgressScore == 0) {
  177. this.spr_progress.fillRange = 0;
  178. }
  179. return;
  180. }
  181. else {
  182. if (this.curProgressScore >= this.targetScore) {
  183. this.curProgressScore = this.curGetScore;
  184. this.initScore();
  185. }
  186. else {
  187. let nextProgressScore: number = this.curProgressScore + this.addScoreSpeed;
  188. if (this.curProgressScore < GamePlay.Inst.targetScore && nextProgressScore >= GamePlay.Inst.targetScore) {
  189. let node_getTargetScoreTip = PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.GetTargetScoreTip);
  190. GamePlay.Inst.node_effectUI.addChild(node_getTargetScoreTip);
  191. }
  192. this.curProgressScore = nextProgressScore;
  193. if (this.curProgressScore >= this.curGetScore) {
  194. this.curProgressScore = this.curGetScore;
  195. }
  196. this.initScore();
  197. }
  198. }
  199. }
  200. /**初始化视图 */
  201. initView() {
  202. this.initLevel();
  203. this.initTotalScore();
  204. this.initScore();
  205. this.initProgress();
  206. this.initHammerPropNum();
  207. this.setPropBtnNormalTip(this.node_hammerBtn);
  208. this.initResetPropNum();
  209. this.setPropBtnNormalTip(this.node_resetBtn);
  210. this.initChanegPropNum();
  211. this.setPropBtnNormalTip(this.node_changeBtn);
  212. this.initCellBg();
  213. this.initCell();
  214. }
  215. /**初始化事件 */
  216. initEvent() {
  217. this.node_pauseBtn.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
  218. this.node_resetBtn.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
  219. this.node_hammerBtn.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
  220. this.node_changeBtn.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
  221. this.node_redPacketIcon.on(cc.Node.EventType.TOUCH_END, this.onClickRedPacketIcon, this);
  222. if (!GameConst.isAuth) {
  223. mk.event.register(EVENT_TYPE.BACK_WxAuth, this.onWxAuthBack, this);
  224. }
  225. }
  226. /**初始化关卡数目 */
  227. initLevel() {
  228. mk.console.log("[Game] PlayerConst.levelNum", PlayerConst.levelNum);
  229. this.label_level.string = `第${PlayerConst.levelNum + 1}关`
  230. }
  231. /**初始化总分数 */
  232. initTotalScore() {
  233. //加入一些随机值,而不是都能获得过关红包
  234. this.targetScore = 450 + PlayerConst.levelNum * (5 + Math.floor(Math.random() * 15));
  235. // //测试切换
  236. // this.targetScore = 5000;
  237. this.addScoreSpeed = Math.floor(this.targetScore / 400) * 2 + 4;
  238. mk.console.log("this.addScoreSpeed", this.addScoreSpeed);
  239. }
  240. /**初始化得分 */
  241. initScore() {
  242. this.initProgress();
  243. }
  244. /**初始化进度 */
  245. initProgress() {
  246. let progress = this.curProgressScore / this.targetScore;
  247. progress = progress > 1 ? 1 : progress;
  248. this.spr_progress.fillRange = progress;
  249. // LogUtil.log("progressprogressprogressprogressprogressprogress", progress);
  250. }
  251. /**初始化道具 */
  252. initPropNum() {
  253. this.initHammerPropNum();
  254. this.initResetPropNum();
  255. this.initChanegPropNum();
  256. }
  257. /**初始化锤子道具数目 */
  258. initHammerPropNum() {
  259. if (PlayerConst.hanmmerPropNum > 0) {
  260. this.label_hammerPropNum.string = PlayerConst.hanmmerPropNum.toString();
  261. }
  262. else {
  263. this.label_hammerPropNum.string = "+";
  264. }
  265. }
  266. /**初始化重置道具数目 */
  267. initResetPropNum() {
  268. if (PlayerConst.resetPropNum) {
  269. this.label_resetPropNum.string = PlayerConst.resetPropNum.toString();
  270. }
  271. else {
  272. this.label_resetPropNum.string = "+";
  273. }
  274. }
  275. /**初始化替换道具数目 */
  276. initChanegPropNum() {
  277. if (PlayerConst.changePropNum) {
  278. this.label_changePropNum.string = PlayerConst.changePropNum.toString();
  279. }
  280. else {
  281. this.label_changePropNum.string = "+";
  282. }
  283. }
  284. // update (dt) {}
  285. /**初始化cellBg */
  286. initCellBg() {
  287. for (var i = 0; i < GameConst.col_num; i++) {
  288. for (var j = 0; j < GameConst.row_num; j++) {
  289. let prefab_cellBg = PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.CellBg);
  290. // LogUtil.log("prefab_cellBg", prefab_cellBg);
  291. let node_cellBg = cc.instantiate(prefab_cellBg);
  292. this.node_cellBg.addChild(node_cellBg);
  293. }
  294. }
  295. }
  296. /**初始化cell */
  297. public initCell() {
  298. this.addCellItemFuc = () => { this.addCellItem(); };
  299. this.schedule(this.addCellItemFuc, 0.05);
  300. }
  301. public addCellItemFuc: Function = null;
  302. public curXIndex: number = 0;
  303. public curYIndex: number = 7;
  304. /**添加CellItem */
  305. public addCellItem() {
  306. mk.console.log("addCellItem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  307. this.addColCellItem();
  308. this.curXIndex++;
  309. if (this.curXIndex >= GameConst.col_num) {
  310. this.unschedule(this.addCellItemFuc);
  311. }
  312. }
  313. /**添加纵列的cellItem */
  314. public addColCellItem() {
  315. this.cellItemDic[this.curXIndex] = [];
  316. for (var i = GameConst.row_num - 1; i >= 0; i--) {
  317. let prefab_cellItem = PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.CellItem);
  318. let node_prefab = cc.instantiate(prefab_cellItem);
  319. let cellItem = node_prefab.getComponent(CellItem);
  320. cellItem.init(this.curXIndex, i);
  321. this.cellItemDic[this.curXIndex][i] = cellItem;
  322. this.cellItemArr.push(cellItem);
  323. this.node_content.addChild(node_prefab);
  324. }
  325. }
  326. /**点击按钮 */
  327. onClickBtn(event: cc.Event.EventTouch) {
  328. this.intervalShowGuide();
  329. //是否已经通关
  330. if (this.ifPass) {
  331. EffectMgr.Inst.addTip("通关结算啦,请稍后操作哈")
  332. return;
  333. }
  334. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.buttonClick);
  335. mk.audio.playEffect("ef_button_click");
  336. switch (event.currentTarget) {
  337. case this.node_pauseBtn:
  338. this.onClickPauseBtn();
  339. break;
  340. case this.node_hammerBtn:
  341. this.onClickHammerBtn();
  342. break;
  343. case this.node_changeBtn:
  344. this.onClickChangeBtn();
  345. break;
  346. case this.node_resetBtn:
  347. this.onClickResetBtn();
  348. break;
  349. }
  350. }
  351. /**点击暂停按钮 */
  352. onClickPauseBtn() {
  353. GameMgr.Inst.sendEvent(UI_NAME.Game, "点击暂停按钮");
  354. mk.ui.openPanel("game/prefab/uiPanel/PauseUI")
  355. }
  356. /**点击重置按钮 */
  357. onClickResetBtn() {
  358. //this.intervalShowGuide();
  359. GameMgr.Inst.sendEvent(UI_NAME.Game, "点击重置道具按钮");
  360. //设置当前选择的道具按钮
  361. this.setCurSelectPropBtn(this.node_resetBtn);
  362. this.curPropType = PROPTYPE.Reset;
  363. console.log("PlayerConst.resetPropNum", PlayerConst.resetPropNum);
  364. if (PlayerConst.resetPropNum <= 0) {
  365. mk.ui.openPanel("game/prefab/uiPanel/GetPropUI");
  366. return;
  367. }
  368. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.refresh);
  369. mk.audio.playEffect("ef_refresh");
  370. let keys = Object.keys(this.cellItemDic);
  371. for (var i = 0; i < keys.length; i++) {
  372. let key = Number(keys[i]);
  373. let colCellItemArr = this.cellItemDic[key];
  374. for (var j = 0; j < colCellItemArr.length; j++) {
  375. let cellItem = colCellItemArr[j];
  376. if (cellItem) {
  377. cellItem.reset();
  378. }
  379. }
  380. }
  381. //暂时不扣
  382. DataMgr.Inst.updatePropNum(2, GamePlay.Inst.curPropType, -1);
  383. this.curPropType = PROPTYPE.Null;
  384. //this.recycleAllCellItem();
  385. }
  386. //点击锤子按钮
  387. onClickHammerBtn() {
  388. GameMgr.Inst.sendEvent(UI_NAME.Game, "点击消除锤道具按钮");
  389. //this.intervalShowGuide();
  390. //设置当前选择的道具按钮
  391. this.setCurSelectPropBtn(this.node_hammerBtn);
  392. this.curPropType = PROPTYPE.Hammer;
  393. console.log("PlayerConst.hanmmerPropNum", PlayerConst.hanmmerPropNum);
  394. if (PlayerConst.hanmmerPropNum <= 0) {
  395. mk.ui.openPanel("game/prefab/uiPanel/GetPropUI");
  396. return;
  397. }
  398. DataMgr.Inst.updatePropNum(2, GamePlay.Inst.curPropType, -1);
  399. }
  400. //点击更换按钮
  401. onClickChangeBtn() {
  402. GameMgr.Inst.sendEvent(UI_NAME.Game, "点击变化方块按钮");
  403. //this.intervalShowGuide();
  404. //设置当前选择的道具按钮
  405. this.setCurSelectPropBtn(this.node_changeBtn);
  406. console.log("PlayerConst.changePropNum", PlayerConst.changePropNum);
  407. this.curPropType = PROPTYPE.Change;
  408. if (PlayerConst.changePropNum <= 0) {
  409. mk.ui.openPanel("game/prefab/uiPanel/GetPropUI");
  410. return;
  411. }
  412. DataMgr.Inst.updatePropNum(2, GamePlay.Inst.curPropType, -1);
  413. }
  414. /**更改cellItem类型
  415. * @param cellItemType
  416. */
  417. changeCellItemType(cellItemType: number) {
  418. let pos_x = this.curSelectCellItem.node.x + GamePlay.Inst.node_content.x;
  419. let pos_y = this.curSelectCellItem.node.y + GamePlay.Inst.node_content.y;
  420. let node_change = PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.Change);
  421. node_change.setPosition(pos_x, pos_y);
  422. GamePlay.Inst.node_effectUI.addChild(node_change);
  423. this.curSelectCellItem.setType(cellItemType)
  424. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.change);
  425. mk.audio.playEffect("ef_change");
  426. this.curSelectCellItem = null;
  427. }
  428. /**设置当前 */
  429. setCurSelectPropBtn(propBtn: cc.Node) {
  430. if (this.curSelectPropBtn) {
  431. let node_ani = this.curSelectPropBtn.children[1];
  432. if (node_ani) {
  433. let ani = node_ani.getComponent(cc.Animation);
  434. if (ani) {
  435. ani.play("ani_normalBtn");
  436. }
  437. }
  438. this.setPropBtnNormalTip(this.curSelectPropBtn);
  439. }
  440. this.curSelectPropBtn = propBtn;
  441. }
  442. /**设置道具普通提示 */
  443. setPropBtnNormalTip(propBtn: cc.Node) {
  444. if (!propBtn) {
  445. return;
  446. }
  447. let node_tip = propBtn.children[2];
  448. if (node_tip) {
  449. let label = null;
  450. switch (propBtn) {
  451. case this.node_hammerBtn:
  452. label = this.label_hammerPropNum;
  453. break;
  454. case this.node_resetBtn:
  455. label = this.label_resetPropNum;
  456. break;
  457. case this.node_changeBtn:
  458. label = this.label_changePropNum;
  459. break;
  460. }
  461. let str = label.string;
  462. let num = Number(str);
  463. let label_tip = node_tip.getComponent(cc.Label);
  464. mk.console.log("propBtn.name", propBtn.name);
  465. mk.console.log("num", num);
  466. if (num && num >= 1) {
  467. label_tip.string = "";
  468. }
  469. else {
  470. label_tip.string = "点我领取";
  471. }
  472. }
  473. }
  474. /**设置道具使用提示 */
  475. setPropBtnSelectTip(propBtn: cc.Node) {
  476. if (!propBtn) {
  477. return;
  478. }
  479. let node_tip = propBtn.children[2];
  480. if (node_tip) {
  481. let label = node_tip.getComponent(cc.Label)
  482. label.string = "道具使用中";
  483. }
  484. }
  485. /**显示CellItemUI */
  486. showChangeCellItemUI() {
  487. let cruSelectCellItem = this.curSelectCellItem;
  488. GamePlay.Inst.node_changeCellItemUI.active = true;
  489. // let min_x = -this.node_content.width * 0.5 + this.node_changeCellItemUI.width * 0.5;
  490. // let max_x = this.node_content.width * 0.5 + this.node_changeCellItemUI.width * 0.5;
  491. GamePlay.Inst.node_changeCellItemUI.y = cruSelectCellItem.node.y + cruSelectCellItem.node.height * 0.5 + GamePlay.Inst.node_changeCellItemUI.height * 0.5 + 20;
  492. }
  493. //onClick
  494. onClickRedPacketIcon() {
  495. if (this.bar_progress.progress == 1) {
  496. EffectMgr.Inst.addTip("已达成目标啦,过关发放哦");
  497. }
  498. else {
  499. EffectMgr.Inst.addTip("达到目标分数,才能领取哦");
  500. }
  501. }
  502. //自定义事件---------------------------------------------------------------------
  503. /**微信授权返回 */
  504. onWxAuthBack() {
  505. if (this.node.active) {
  506. this.restart(false);
  507. //
  508. let node_redPacketUI = mk.ui.getCurOnPanel("RedPacketUI")
  509. if (node_redPacketUI) {
  510. mk.ui.closePanel("RedPacketUI");
  511. }
  512. }
  513. this.initLevel();
  514. this.initTotalScore();
  515. this.initPropNum();
  516. mk.event.remove(EVENT_TYPE.BACK_WxAuth, this.onWxAuthBack, this);
  517. }
  518. //其他显示或逻辑项-------------------------------------------------------------------
  519. /**根据vecList清除cellItem */
  520. public cleanCellItemByVecList() {
  521. //LogUtil.log("[Game] Game.Inst.cleanCellItemVecArr", Game.Inst.cleanCellItemVecArr);
  522. //没有可清清除
  523. if (GamePlay.Inst.cleanedVecArr.length <= 0) {
  524. //FC:+ 是否可以点击
  525. this.ifCouldClick = true;
  526. EffectMgr.Inst.addTip("点击两个或以上消除哦");
  527. return;
  528. }
  529. //区分消除音效
  530. if (GamePlay.Inst.cleanedVecArr.length >= 4) {
  531. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.eliminate_bonus);
  532. mk.audio.playEffect("ef_eliminate_bonus");
  533. }
  534. else {
  535. //AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.eliminate);
  536. mk.audio.playEffect("ef_eliminate");
  537. }
  538. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.fly);
  539. mk.audio.playEffect("ef_fly");
  540. //同时消除
  541. for (var i = 0; i < GamePlay.Inst.cleanedVecArr.length; i++) {
  542. let vec = GamePlay.Inst.cleanedVecArr[i];
  543. //最后一个判定
  544. if (i == GamePlay.Inst.cleanedVecArr.length - 1) {
  545. this.checkBonusNum(GamePlay.Inst.cleanedVecArr.length);
  546. GamePlay.Inst.cellItemDic[vec.x][vec.y].recycle(false, true);
  547. }
  548. else {
  549. GamePlay.Inst.cellItemDic[vec.x][vec.y].recycle();
  550. }
  551. //回收 注释 改成如上
  552. //Game.Inst.cellItemDic[vec.x][vec.y].recycle();
  553. //Game.Inst.cellItemDic[vec.x][vec.y] = null;
  554. if (this.cleanXIndexArr.indexOf(vec.x) == -1) {
  555. this.cleanXIndexArr.push(vec.x);
  556. }
  557. }
  558. this.cleanXIndexArr.sort();
  559. //计算下全部移除的
  560. this.allCleanedXArr = GameLogic.getAllCleanXIndex(this.cleanXIndexArr);
  561. //FC:移除完,检测下落
  562. this.scheduleOnce(() => {
  563. //检测下落
  564. this.checkMoveDownCellItem();
  565. }, 0.2)
  566. }
  567. /**检测向下移动的cellItem */
  568. checkMoveDownCellItem() {
  569. mk.console.log("this.cleanXIndexArr", this.cleanXIndexArr);
  570. for (var i = 0; i < this.cleanXIndexArr.length; i++) {
  571. let xIndex = this.cleanXIndexArr[i];
  572. this.moveYCellItem(xIndex);
  573. }
  574. }
  575. /**移动
  576. * @param xIndex 移除的纵列
  577. * @param removedYIndex 移除的横向index
  578. */
  579. public moveYCellItem(xIndex: number) {
  580. // LogUtil.log("moveYCellItem x", xIndex, this.allCleanedXArr);
  581. /**移动的次序(第几个移动的用来控制延迟移动的时间) */
  582. let movedIndex: number = 0;
  583. //移动位置间隔(比如前面两个空位就移动两格)
  584. let intervalNum: number = 0;
  585. /**是否是全部清理的x */
  586. let ifAllCleanedX: boolean = this.allCleanedXArr.indexOf(xIndex) != -1;
  587. /**是否是最后清理的x(全部清理完才开始左移) */
  588. let ifLastCleanX: boolean = (xIndex == this.cleanXIndexArr[this.cleanXIndexArr.length - 1]);
  589. let cellItemDic = GamePlay.Inst.cellItemDic[xIndex];
  590. //计算y方向移动的最后一位(这样只计算一次不用循环)
  591. let lastMoveCellItemY: number = null;
  592. let leftLastCellItemY: number = null;
  593. if (ifLastCleanX) {
  594. let lastCellItemArrSort = this.cleanedVecArr.filter((vec) => vec.x == xIndex).sort();
  595. lastMoveCellItemY = lastCellItemArrSort[0].y;
  596. // LogUtil.log("lastCellItemY", xIndex, lastMoveCellItemY, lastCellItemArrSort);
  597. for (var i = 0; i < GameConst.row_num; i++) {
  598. if (cellItemDic) {
  599. let cellItem = cellItemDic[i];
  600. if (cellItem) {
  601. leftLastCellItemY = cellItem.y;
  602. // LogUtil.log("leftLastCellItemY", leftLastCellItemY);
  603. break;
  604. }
  605. }
  606. }
  607. // let leftCellItemArrSort = this.cl
  608. }
  609. //如果最后一行 并且 是被全部清除的x
  610. if (ifLastCleanX && ifAllCleanedX) {
  611. // LogUtil.log("【checkMoveLeftCellItem】检测消除 全部消除的一列就是消除xIndex中最后一列");
  612. this.checkMoveLeftCellItem();
  613. return;
  614. }
  615. //y方向从下至上循环排查
  616. for (var i = GameConst.row_num - 1; i >= 0; i--) {
  617. if (cellItemDic) {
  618. let cellItem = cellItemDic[i];
  619. //该xIndex纵列没有全部移除
  620. if (cellItem) {
  621. if (ifLastCleanX && i == leftLastCellItemY) {
  622. // LogUtil.log("【checkMoveLeftCellItem】检测消除 移除了最后一个");
  623. cellItem.moveDown(xIndex, i + intervalNum, movedIndex, true);
  624. }
  625. else {
  626. cellItem.moveDown(xIndex, i + intervalNum, movedIndex);
  627. }
  628. movedIndex++;
  629. }
  630. else {
  631. intervalNum += 1;
  632. }
  633. }
  634. else {
  635. continue;
  636. }
  637. }
  638. }
  639. /**检测向左移动的cellItem */
  640. checkMoveLeftCellItem() {
  641. //计算消除的纵列中,有没有被全部移除
  642. //let allCleanedXArr = GameUtil.getAllCleanXIndex(this.cleanXIndexArr);
  643. // LogUtil.log("[Game] checkMoveLeftCellItem allCleanXArr ---------------------", this.allCleanedXArr);
  644. //如果没有全部移除得就return
  645. if (this.allCleanedXArr.length <= 0) {
  646. //LogUtil.log("【checkMoveLeftCellItem】检测是否能消除 没有全部移除的");
  647. GamePlay.Inst.ifCouldClick = true;
  648. GamePlay.Inst.checkIfEliminate();
  649. return;
  650. }
  651. //剩余一列未移除的xIndex
  652. let leftXIndexArr: number[] = GameLogic.getLeftXIndexArr();
  653. let leftMaxXIndex: number = leftXIndexArr[leftXIndexArr.length - 1];
  654. if (leftXIndexArr.length <= 0) {
  655. //检测是否移除
  656. this.checkIfEliminate();
  657. }
  658. else {
  659. mk.console.log("leftXIndexArr", leftXIndexArr);
  660. let intervalNum: number = 0;
  661. for (var i = 0; i < GameConst.col_num; i++) {
  662. let cellItemArr = GamePlay.Inst.cellItemDic[i];
  663. if (this.allCleanedXArr.indexOf(i) == -1) {
  664. //剩余存在的cellItem
  665. let leftCellItemArr = cellItemArr.filter((cellItem) => cellItem != null);
  666. if (intervalNum > 0) {
  667. for (var j = 0; j < cellItemArr.length; j++) {
  668. let cellItem = cellItemArr[j];
  669. if (cellItem) {
  670. if (i == leftMaxXIndex && j == (leftCellItemArr[0].y)) {
  671. mk.console.log("i,j,intervalNum", i, j, intervalNum);
  672. cellItem.moveLeft(i - intervalNum, j, true);
  673. }
  674. else {
  675. cellItem.moveLeft(i - intervalNum, j);
  676. }
  677. }
  678. }
  679. }
  680. else {
  681. //对应 1 2 3 列 中 3全部消除
  682. if (i == leftMaxXIndex) {
  683. GamePlay.Inst.ifCouldClick = true;
  684. GamePlay.Inst.checkIfEliminate();
  685. }
  686. }
  687. }
  688. else {
  689. intervalNum += 1;
  690. }
  691. }
  692. }
  693. }
  694. public timeout_pass: number = 0;
  695. /**检测是否能移除 */
  696. checkIfEliminate() {
  697. mk.console.log("开始检测是否还能够消除!!!!!!!!!!!!!!!!!!!!!");
  698. //FC:测试替换
  699. GameLogic.getCouldCleanVec();
  700. if (this.couldCleanVecArr.length <= 0 && !this.ifPass) {
  701. this.ifPass = true;
  702. EffectMgr.Inst.addSpriteTip(TIP_SPRITEITEM_TYPE.NormalPass);
  703. let timeOut = setTimeout(() => {
  704. this.pass();
  705. clearTimeout(timeOut);
  706. }, 1500);
  707. }
  708. //FC:正式使用
  709. // let timeOut = setTimeout(() => {
  710. // GameUtil.getCouldCleanVec();
  711. // if (this.couldCleanVecArr.length <= 0 && !this.ifPass) {
  712. // this.pass();
  713. // }
  714. // clearTimeout(timeOut);
  715. // }, 1500);
  716. }
  717. getCouldCleanCellItem() {
  718. for (var i = 0; this.cellItemArr.length; i++) {
  719. let cellItem = this.cellItemArr[i];
  720. let aroundSameType = GameLogic.getAroundSameType(cellItem.x, cellItem.y, cellItem.type, false);
  721. if (aroundSameType.length > 0) {
  722. return;
  723. }
  724. }
  725. //this.nextLevel();
  726. this.pass();
  727. //this.recycleAllCellItem();
  728. }
  729. /**restart
  730. * @param 是否扣除体力
  731. */
  732. restart(ifMinusEnergy: boolean = false) {
  733. mk.console.log("restart!!!!!!!!!!!!!!!")
  734. this.refreshGame();
  735. }
  736. /**下一关 */
  737. nextLevel(ifMinusEnergy: boolean = false) {
  738. mk.console.log("nextLevel!!!!!!!!!!!!!!!")
  739. this.refreshGame();
  740. }
  741. /**刷新关卡 */
  742. refreshGame() {
  743. this.recycleAllRedPacket();
  744. this.recycleAllCellItem();
  745. this.ifPass = false;
  746. this.ifGetPass = false;
  747. this.ifCouldClick = true;
  748. this.initLevel();
  749. this.initTotalScore();
  750. this.curProgressScore = 0;
  751. this.curGetScore = 0;
  752. this.finalGetScore = 0;
  753. this.initScore();
  754. }
  755. /**回收红包 */
  756. recycleAllRedPacket() {
  757. while (this.node_levelRedPacketUI.childrenCount > 0) {
  758. let node_levelRedPacket = this.node_levelRedPacketUI.children[0];
  759. node_levelRedPacket.getComponent(LevelRedPacketItem).recycle();
  760. }
  761. }
  762. /**回收所有的 */
  763. recycleAllCellItem() {
  764. for (var i = 0; i < GameConst.col_num; i++) {
  765. for (var j = 0; j < GameConst.row_num; j++) {
  766. let cellItem = this.cellItemDic[i][j];
  767. if (cellItem) {
  768. mk.console.log("回收把>>>>>>>>>>>>>>>>>>>>>>");
  769. cellItem.recycle(true, false, false);
  770. }
  771. }
  772. }
  773. // while (this.cellItemList.length > 0) {
  774. // this.cellItemList.splice(0, 1);
  775. // this.cellItemList[0].recycle();
  776. // }
  777. this.cellItemArr = [];
  778. this.cellItemDic = {};
  779. this.curXIndex = 0;
  780. this.curYIndex = GameConst.row_num - 1;
  781. this.initCell();
  782. }
  783. /**通关 */
  784. pass() {
  785. //这边会多次进入
  786. if (GamePlay.Inst.node.active == false) {
  787. return;
  788. }
  789. this.ifPass = true;
  790. GamePlay.Inst.ifGetPass = false;
  791. let passLevelNum = PlayerConst.levelNum + 1;
  792. mk.console.log("[Game]pass passLevelNum", passLevelNum);
  793. GamePlay.Inst.ifGetPass = true;
  794. PlayerConst.levelNum = passLevelNum;
  795. //游戏结算
  796. this.gameCount();
  797. // LogUtil.log("SurpriseTaskUI.ifAllCompleted", SurpriseTaskUI.ifAllCompleted);
  798. // LogUtil.log("score", score);
  799. // //发送过关消息
  800. // HttpMgr.Inst.pass(passLevelNum, score).then((data) => {
  801. // LogUtil.log("pass data", data);
  802. // Game.Inst.ifGetPass = true;
  803. // PlayerConst.levelNum = passLevelNum;
  804. // //视频计数
  805. // DataMgr.Inst.updatePassNum(++PlayerConst.passNum);
  806. // LogUtil.log("PlayerConst.passNum", PlayerConst.passNum);
  807. // if (score && !data) {
  808. // DataMgr.Inst.updateShopScoreNum(score);
  809. // }
  810. // else {
  811. // //如果提示 已达单关积分获取次数!
  812. // if (data) {
  813. // SurpriseTaskUI.ifGetScore = true;
  814. // SurpriseTaskUI.getSurpriseTaskScore = 0;
  815. // }
  816. // }
  817. // //游戏结算
  818. // this.gameCount();
  819. // }).catch((err) => {
  820. // LogUtil.log("pass err", err);
  821. // Game.Inst.ifGetPass = true;
  822. // if (err) {
  823. // let errCode = err.errcode;
  824. // switch (errCode) {
  825. // case 500:
  826. // GameMgr.Inst.sendEvent("Error", `服务器发生500错误`);
  827. // break;
  828. // case PASS_ERR_CODE.PASSNO_INCORRECT:
  829. // case PASS_ERR_CODE.PASSNO_NULL:
  830. // GameMgr.Inst.sendEvent("PassError", `过关异常,第${PlayerConst.levelNum}关`);
  831. // DataMgr.Inst.updatePassNum(++PlayerConst.passNum);
  832. // PlayerConst.levelNum = passLevelNum;
  833. // break;
  834. // case PASS_ERR_CODE.SCORE_OVER_TIMES:
  835. // //视频计数
  836. // DataMgr.Inst.updatePassNum(++PlayerConst.passNum);
  837. // PlayerConst.levelNum = passLevelNum;
  838. // break;
  839. // case PASS_ERR_CODE.SCORE_PARAM_ILLEGAL:
  840. // GameMgr.Inst.sendEvent("PassError", `获取积分异常(积分传参非法)`);
  841. // EffectMgr.Inst.addTip("您本次积分异常,请不要作弊哦");
  842. // DataMgr.Inst.updatePassNum(++PlayerConst.passNum);
  843. // PlayerConst.levelNum = passLevelNum;
  844. // break;
  845. // case PASS_ERR_CODE.SCORE_REACH_LIMIT:
  846. // GameMgr.Inst.sendEvent("PassError", `已达今日积分上限`);
  847. // EffectMgr.Inst.addTip("您今天积分已达上限啦,明天再来吧");
  848. // DataMgr.Inst.updatePassNum(++PlayerConst.passNum);
  849. // PlayerConst.levelNum = passLevelNum;
  850. // break;
  851. // }
  852. // }
  853. // //游戏结算
  854. // this.gameCount();
  855. // });
  856. }
  857. /**游戏结算 */
  858. gameCount() {
  859. //如果随机红包激活了,那么
  860. let node_levelRedPacketUI = mk.ui.getCurOnPanel("LevelRedPacketUI");
  861. if (node_levelRedPacketUI) {
  862. mk.ui.closePanel("LevelRedPacketUI");
  863. }
  864. let node_getPropUI = mk.ui.getCurOnPanel("GetPropUI");
  865. if (node_getPropUI) {
  866. mk.ui.closePanel("GetPropUI");
  867. }
  868. //前2关 取消红包显示 放在这边主要是 能够继续玩下去
  869. if (this.finalGetScore >= this.targetScore && PlayerConst.levelNum > 2) {
  870. // Main.Inst.node_gameOverUI.active = true;
  871. let num = PlayerConst.todayTargetRedPacketNum - PlayerConst.todayPassRedPacketNum;
  872. mk.console.log("num", num);
  873. let tip = "";
  874. if (num > 0) {
  875. tip = `再领取${num}个过关红包完成今日打卡`;
  876. }
  877. else {
  878. tip = `今日已经打卡成功,快去提现吧`;
  879. }
  880. mk.ui.openPanel("RedPacketUI").then((node) => {
  881. let redPacketUI = node.getComponent(RedPacketUI);
  882. redPacketUI.init("过关红包", 0.2, () => { this.gameOver() }, tip)
  883. })
  884. }
  885. else {
  886. this.gameOver();
  887. }
  888. //测试切换
  889. // this.gameOver();
  890. }
  891. /**游戏结束 */
  892. gameOver() {
  893. mk.ui.openPanel("GameOverUI");
  894. }
  895. checkBonusNum(cleanCellItemNum: number) {
  896. if (cleanCellItemNum >= 4) {
  897. let node_bonusTip = PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.BonusTip);
  898. let bonusTip = node_bonusTip.getComponent(BonusTip);
  899. //LogUtil.log("bonusTip",bonusTip);
  900. if (cleanCellItemNum == 4) {
  901. bonusTip.init(4);
  902. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.bonus4);
  903. mk.audio.playEffect("ef_bonus4");
  904. }
  905. else if (cleanCellItemNum == 5) {
  906. bonusTip.init(5);
  907. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.bonus5);
  908. mk.audio.playEffect("ef_bonus5");
  909. }
  910. else if (cleanCellItemNum == 6) {
  911. bonusTip.init(6);
  912. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.bonus6);
  913. mk.audio.playEffect("ef_bonus6");
  914. }
  915. else if (cleanCellItemNum == 7) {
  916. bonusTip.init(7);
  917. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.bonus7);
  918. mk.audio.playEffect("ef_bonus7");
  919. }
  920. else if (cleanCellItemNum == 8) {
  921. bonusTip.init(8);
  922. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.bonus8);
  923. mk.audio.playEffect("ef_bonus8");
  924. }
  925. else {
  926. bonusTip.init(8);
  927. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.bonus8);
  928. mk.audio.playEffect("ef_bonus8");
  929. }
  930. this.node_effectUI.addChild(node_bonusTip);
  931. }
  932. }
  933. checkAddLevelRedPacket(eliminateNum: number): boolean {
  934. let chance = (eliminateNum - 3) * 0.2;
  935. let random = Math.random();
  936. if (random < chance) {
  937. return true;
  938. }
  939. else {
  940. return false;
  941. }
  942. }
  943. public interval_ShowGuide: number = null;
  944. //间隔显示
  945. intervalShowGuide() {
  946. this.cancelShowCouldCleanCellItem();
  947. this.interval_ShowGuide = setInterval(() => {
  948. this.showCouldCleanCellItem();
  949. }, 10000);
  950. }
  951. /**显示 */
  952. showCouldCleanCellItem() {
  953. if (mk.ui.getCurOnPanel("RedPacketUI") || mk.ui.getCurOnPanel("GameOverUI") || !this.couldCleanVecArr || !this.cellItemDic) {
  954. return;
  955. }
  956. if (this.couldCleanVecArr.length <= 0) {
  957. GameLogic.getCouldCleanVec();
  958. }
  959. if (this.couldCleanVecArr.length > 0) {
  960. for (var i = 0; i < this.couldCleanVecArr.length; i++) {
  961. let vec = this.couldCleanVecArr[i];
  962. if (this.cellItemDic[vec.x]) {
  963. let cellItem = this.cellItemDic[vec.x][vec.y];
  964. if (cellItem) {
  965. cellItem.shake();
  966. }
  967. }
  968. else {
  969. }
  970. }
  971. }
  972. }
  973. /**取消显示 */
  974. cancelShowCouldCleanCellItem() {
  975. if (this.interval_ShowGuide) {
  976. clearInterval(this.interval_ShowGuide);
  977. this.interval_ShowGuide = null;
  978. if (this.couldCleanVecArr.length > 0) {
  979. for (var i = 0; i < this.couldCleanVecArr.length; i++) {
  980. let vec = this.couldCleanVecArr[i];
  981. let cellItem = this.cellItemDic[vec.x][vec.y];
  982. if (cellItem) {
  983. cellItem.normal();
  984. }
  985. }
  986. }
  987. this.couldCleanVecArr = [];
  988. }
  989. }
  990. }