GamePlay.ts 36 KB

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