GamePlay.ts 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. // LogUtil.log("progressprogressprogressprogressprogressprogress", progress);
  248. }
  249. /**初始化道具 */
  250. initPropNum() {
  251. this.initHammerPropNum();
  252. this.initResetPropNum();
  253. this.initChanegPropNum();
  254. }
  255. /**初始化锤子道具数目 */
  256. initHammerPropNum() {
  257. if (PlayerConst.hanmmerPropNum > 0) {
  258. this.label_hammerPropNum.string = PlayerConst.hanmmerPropNum.toString();
  259. }
  260. else {
  261. this.label_hammerPropNum.string = "+";
  262. }
  263. }
  264. /**初始化重置道具数目 */
  265. initResetPropNum() {
  266. if (PlayerConst.resetPropNum) {
  267. this.label_resetPropNum.string = PlayerConst.resetPropNum.toString();
  268. }
  269. else {
  270. this.label_resetPropNum.string = "+";
  271. }
  272. }
  273. /**初始化替换道具数目 */
  274. initChanegPropNum() {
  275. if (PlayerConst.changePropNum) {
  276. this.label_changePropNum.string = PlayerConst.changePropNum.toString();
  277. }
  278. else {
  279. this.label_changePropNum.string = "+";
  280. }
  281. }
  282. // update (dt) {}
  283. /**初始化cellBg */
  284. initCellBg() {
  285. for (var i = 0; i < GameConst.col_num; i++) {
  286. for (var j = 0; j < GameConst.row_num; j++) {
  287. let prefab_cellBg = PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.CellBg);
  288. // LogUtil.log("prefab_cellBg", prefab_cellBg);
  289. let node_cellBg = cc.instantiate(prefab_cellBg);
  290. this.node_cellBg.addChild(node_cellBg);
  291. }
  292. }
  293. }
  294. /**初始化cell */
  295. public initCell() {
  296. this.addCellItemFuc = () => { this.addCellItem(); };
  297. this.schedule(this.addCellItemFuc, 0.05);
  298. }
  299. public addCellItemFuc: Function = null;
  300. public curXIndex: number = 0;
  301. public curYIndex: number = 7;
  302. /**添加CellItem */
  303. public addCellItem() {
  304. mk.console.log("addCellItem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  305. this.addColCellItem();
  306. this.curXIndex++;
  307. if (this.curXIndex >= GameConst.col_num) {
  308. this.unschedule(this.addCellItemFuc);
  309. }
  310. }
  311. /**添加纵列的cellItem */
  312. public addColCellItem() {
  313. this.cellItemDic[this.curXIndex] = [];
  314. for (var i = GameConst.row_num - 1; i >= 0; i--) {
  315. let prefab_cellItem = PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.CellItem);
  316. let node_prefab = cc.instantiate(prefab_cellItem);
  317. let cellItem = node_prefab.getComponent(CellItem);
  318. cellItem.init(this.curXIndex, i);
  319. this.cellItemDic[this.curXIndex][i] = cellItem;
  320. this.cellItemArr.push(cellItem);
  321. this.node_content.addChild(node_prefab);
  322. }
  323. }
  324. /**点击按钮 */
  325. onClickBtn(event: cc.Event.EventTouch) {
  326. this.intervalShowGuide();
  327. //是否已经通关
  328. if (this.ifPass) {
  329. EffectMgr.Inst.addTip("通关结算啦,请稍后操作哈")
  330. return;
  331. }
  332. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.buttonClick);
  333. mk.audio.playEffect("ef_button_click");
  334. switch (event.currentTarget) {
  335. case this.node_pauseBtn:
  336. this.onClickPauseBtn();
  337. break;
  338. case this.node_hammerBtn:
  339. this.onClickHammerBtn();
  340. break;
  341. case this.node_changeBtn:
  342. this.onClickChangeBtn();
  343. break;
  344. case this.node_resetBtn:
  345. this.onClickResetBtn();
  346. break;
  347. }
  348. }
  349. /**点击暂停按钮 */
  350. onClickPauseBtn() {
  351. GameMgr.Inst.sendEvent(UI_NAME.Game, "点击暂停按钮");
  352. mk.ui.openPanel("game/prefab/uiPanel/PauseUI")
  353. }
  354. /**点击重置按钮 */
  355. onClickResetBtn() {
  356. //this.intervalShowGuide();
  357. GameMgr.Inst.sendEvent(UI_NAME.Game, "点击重置道具按钮");
  358. //设置当前选择的道具按钮
  359. this.setCurSelectPropBtn(this.node_resetBtn);
  360. this.curPropType = PROPTYPE.Reset;
  361. console.log("PlayerConst.resetPropNum", PlayerConst.resetPropNum);
  362. if (PlayerConst.resetPropNum <= 0) {
  363. mk.ui.openPanel("game/prefab/uiPanel/GetPropUI");
  364. return;
  365. }
  366. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.refresh);
  367. mk.audio.playEffect("ef_refresh");
  368. let keys = Object.keys(this.cellItemDic);
  369. for (var i = 0; i < keys.length; i++) {
  370. let key = Number(keys[i]);
  371. let colCellItemArr = this.cellItemDic[key];
  372. for (var j = 0; j < colCellItemArr.length; j++) {
  373. let cellItem = colCellItemArr[j];
  374. if (cellItem) {
  375. cellItem.reset();
  376. }
  377. }
  378. }
  379. //暂时不扣
  380. DataMgr.Inst.updatePropNum(2, GamePlay.Inst.curPropType, -1);
  381. this.curPropType = PROPTYPE.Null;
  382. //this.recycleAllCellItem();
  383. }
  384. //点击锤子按钮
  385. onClickHammerBtn() {
  386. GameMgr.Inst.sendEvent(UI_NAME.Game, "点击消除锤道具按钮");
  387. //this.intervalShowGuide();
  388. //设置当前选择的道具按钮
  389. this.setCurSelectPropBtn(this.node_hammerBtn);
  390. this.curPropType = PROPTYPE.Hammer;
  391. console.log("PlayerConst.hanmmerPropNum", PlayerConst.hanmmerPropNum);
  392. if (PlayerConst.hanmmerPropNum <= 0) {
  393. mk.ui.openPanel("game/prefab/uiPanel/GetPropUI");
  394. return;
  395. }
  396. DataMgr.Inst.updatePropNum(2, GamePlay.Inst.curPropType, -1);
  397. }
  398. //点击更换按钮
  399. onClickChangeBtn() {
  400. GameMgr.Inst.sendEvent(UI_NAME.Game, "点击变化方块按钮");
  401. //this.intervalShowGuide();
  402. //设置当前选择的道具按钮
  403. this.setCurSelectPropBtn(this.node_changeBtn);
  404. console.log("PlayerConst.changePropNum", PlayerConst.changePropNum);
  405. this.curPropType = PROPTYPE.Change;
  406. if (PlayerConst.changePropNum <= 0) {
  407. mk.ui.openPanel("game/prefab/uiPanel/GetPropUI");
  408. return;
  409. }
  410. DataMgr.Inst.updatePropNum(2, GamePlay.Inst.curPropType, -1);
  411. }
  412. /**更改cellItem类型
  413. * @param cellItemType
  414. */
  415. changeCellItemType(cellItemType: number) {
  416. let pos_x = this.curSelectCellItem.node.x + GamePlay.Inst.node_content.x;
  417. let pos_y = this.curSelectCellItem.node.y + GamePlay.Inst.node_content.y;
  418. let node_change = PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.Change);
  419. node_change.setPosition(pos_x, pos_y);
  420. GamePlay.Inst.node_effectUI.addChild(node_change);
  421. this.curSelectCellItem.setType(cellItemType)
  422. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.change);
  423. mk.audio.playEffect("ef_change");
  424. this.curSelectCellItem = null;
  425. }
  426. /**设置当前 */
  427. setCurSelectPropBtn(propBtn: cc.Node) {
  428. if (this.curSelectPropBtn) {
  429. let node_ani = this.curSelectPropBtn.children[1];
  430. if (node_ani) {
  431. let ani = node_ani.getComponent(cc.Animation);
  432. if (ani) {
  433. ani.play("ani_normalBtn");
  434. }
  435. }
  436. this.setPropBtnNormalTip(this.curSelectPropBtn);
  437. }
  438. this.curSelectPropBtn = propBtn;
  439. }
  440. /**设置道具普通提示 */
  441. setPropBtnNormalTip(propBtn: cc.Node) {
  442. if (!propBtn) {
  443. return;
  444. }
  445. let node_tip = propBtn.children[2];
  446. if (node_tip) {
  447. let label = null;
  448. switch (propBtn) {
  449. case this.node_hammerBtn:
  450. label = this.label_hammerPropNum;
  451. break;
  452. case this.node_resetBtn:
  453. label = this.label_resetPropNum;
  454. break;
  455. case this.node_changeBtn:
  456. label = this.label_changePropNum;
  457. break;
  458. }
  459. let str = label.string;
  460. let num = Number(str);
  461. let label_tip = node_tip.getComponent(cc.Label);
  462. mk.console.log("propBtn.name", propBtn.name);
  463. mk.console.log("num", num);
  464. if (num && num >= 1) {
  465. label_tip.string = "";
  466. }
  467. else {
  468. label_tip.string = "点我领取";
  469. }
  470. }
  471. }
  472. /**设置道具使用提示 */
  473. setPropBtnSelectTip(propBtn: cc.Node) {
  474. if (!propBtn) {
  475. return;
  476. }
  477. let node_tip = propBtn.children[2];
  478. if (node_tip) {
  479. let label = node_tip.getComponent(cc.Label)
  480. label.string = "道具使用中";
  481. }
  482. }
  483. /**显示CellItemUI */
  484. showChangeCellItemUI() {
  485. let cruSelectCellItem = this.curSelectCellItem;
  486. GamePlay.Inst.node_changeCellItemUI.active = true;
  487. // let min_x = -this.node_content.width * 0.5 + this.node_changeCellItemUI.width * 0.5;
  488. // let max_x = this.node_content.width * 0.5 + this.node_changeCellItemUI.width * 0.5;
  489. GamePlay.Inst.node_changeCellItemUI.y = cruSelectCellItem.node.y + cruSelectCellItem.node.height * 0.5 + GamePlay.Inst.node_changeCellItemUI.height * 0.5 + 20;
  490. }
  491. //onClick
  492. onClickRedPacketIcon() {
  493. if (this.spr_progress.fillRange == 1) {
  494. EffectMgr.Inst.addTip("已达成目标啦,过关发放哦");
  495. }
  496. else {
  497. EffectMgr.Inst.addTip("达到目标分数,才能领取哦");
  498. }
  499. }
  500. //找到红包的那个水果
  501. /**
  502. * 获取红包cellItem
  503. */
  504. public getRedPacketCellItem() {
  505. let totalCellItemArr: CellItem[] = this.cellItemArr.concat();
  506. let largeCellItemVec: cc.Vec2[] = [];
  507. for (var i = 0; i < totalCellItemArr.length; i++) {
  508. let cellItem = totalCellItemArr[0];
  509. if (!!cellItem) {
  510. GameLogic.getCouldCleanVecList(cellItem.x, cellItem.y, cellItem.type, false);
  511. if (largeCellItemVec.length < this.couldCleanVecArr.length) {
  512. largeCellItemVec = this.couldCleanVecArr;
  513. }
  514. this.couldCleanVecArr.forEach(element => {
  515. let index = GameConst.col_num * element.x + element.y;
  516. totalCellItemArr[index] = null;
  517. });
  518. }
  519. }
  520. let random_index = mk.math.random(0, largeCellItemVec.length - 1);
  521. let vec = largeCellItemVec[random_index];
  522. let redPcaketCellItem = this.cellItemDic[vec.x][vec.y];
  523. }
  524. //自定义事件---------------------------------------------------------------------
  525. /**微信授权返回 */
  526. onWxAuthBack() {
  527. if (this.node.active) {
  528. this.restart(false);
  529. //
  530. let node_redPacketUI = mk.ui.getCurOnPanel("RedPacketUI")
  531. if (node_redPacketUI) {
  532. mk.ui.closePanel("RedPacketUI");
  533. }
  534. }
  535. this.initLevel();
  536. this.initTotalScore();
  537. this.initPropNum();
  538. mk.event.remove(EVENT_TYPE.BACK_WxAuth, this.onWxAuthBack, this);
  539. }
  540. //其他显示或逻辑项-------------------------------------------------------------------
  541. /**根据vecList清除cellItem */
  542. public cleanCellItemByVecList() {
  543. //LogUtil.log("[Game] Game.Inst.cleanCellItemVecArr", Game.Inst.cleanCellItemVecArr);
  544. //没有可清清除
  545. if (GamePlay.Inst.cleanedVecArr.length <= 0) {
  546. //FC:+ 是否可以点击
  547. this.ifCouldClick = true;
  548. EffectMgr.Inst.addTip("点击两个或以上消除哦");
  549. return;
  550. }
  551. //区分消除音效
  552. if (GamePlay.Inst.cleanedVecArr.length >= 4) {
  553. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.eliminate_bonus);
  554. mk.audio.playEffect("ef_eliminate_bonus");
  555. }
  556. else {
  557. //AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.eliminate);
  558. mk.audio.playEffect("ef_eliminate");
  559. }
  560. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.fly);
  561. mk.audio.playEffect("ef_fly");
  562. //同时消除
  563. for (var i = 0; i < GamePlay.Inst.cleanedVecArr.length; i++) {
  564. let vec = GamePlay.Inst.cleanedVecArr[i];
  565. //最后一个判定
  566. if (i == GamePlay.Inst.cleanedVecArr.length - 1) {
  567. this.checkBonusNum(GamePlay.Inst.cleanedVecArr.length);
  568. GamePlay.Inst.cellItemDic[vec.x][vec.y].recycle(false, true);
  569. }
  570. else {
  571. GamePlay.Inst.cellItemDic[vec.x][vec.y].recycle();
  572. }
  573. //回收 注释 改成如上
  574. //Game.Inst.cellItemDic[vec.x][vec.y].recycle();
  575. //Game.Inst.cellItemDic[vec.x][vec.y] = null;
  576. if (this.cleanXIndexArr.indexOf(vec.x) == -1) {
  577. this.cleanXIndexArr.push(vec.x);
  578. }
  579. }
  580. this.cleanXIndexArr.sort();
  581. //计算下全部移除的
  582. this.allCleanedXArr = GameLogic.getAllCleanXIndex(this.cleanXIndexArr);
  583. //FC:移除完,检测下落
  584. this.scheduleOnce(() => {
  585. //检测下落
  586. this.checkMoveDownCellItem();
  587. }, 0.2)
  588. }
  589. /**检测向下移动的cellItem */
  590. checkMoveDownCellItem() {
  591. mk.console.log("this.cleanXIndexArr", this.cleanXIndexArr);
  592. for (var i = 0; i < this.cleanXIndexArr.length; i++) {
  593. let xIndex = this.cleanXIndexArr[i];
  594. this.moveYCellItem(xIndex);
  595. }
  596. }
  597. /**移动
  598. * @param xIndex 移除的纵列
  599. * @param removedYIndex 移除的横向index
  600. */
  601. public moveYCellItem(xIndex: number) {
  602. // LogUtil.log("moveYCellItem x", xIndex, this.allCleanedXArr);
  603. /**移动的次序(第几个移动的用来控制延迟移动的时间) */
  604. let movedIndex: number = 0;
  605. //移动位置间隔(比如前面两个空位就移动两格)
  606. let intervalNum: number = 0;
  607. /**是否是全部清理的x */
  608. let ifAllCleanedX: boolean = this.allCleanedXArr.indexOf(xIndex) != -1;
  609. /**是否是最后清理的x(全部清理完才开始左移) */
  610. let ifLastCleanX: boolean = (xIndex == this.cleanXIndexArr[this.cleanXIndexArr.length - 1]);
  611. let cellItemDic = GamePlay.Inst.cellItemDic[xIndex];
  612. //计算y方向移动的最后一位(这样只计算一次不用循环)
  613. let lastMoveCellItemY: number = null;
  614. let leftLastCellItemY: number = null;
  615. if (ifLastCleanX) {
  616. let lastCellItemArrSort = this.cleanedVecArr.filter((vec) => vec.x == xIndex).sort();
  617. lastMoveCellItemY = lastCellItemArrSort[0].y;
  618. // LogUtil.log("lastCellItemY", xIndex, lastMoveCellItemY, lastCellItemArrSort);
  619. for (var i = 0; i < GameConst.row_num; i++) {
  620. if (cellItemDic) {
  621. let cellItem = cellItemDic[i];
  622. if (cellItem) {
  623. leftLastCellItemY = cellItem.y;
  624. // LogUtil.log("leftLastCellItemY", leftLastCellItemY);
  625. break;
  626. }
  627. }
  628. }
  629. // let leftCellItemArrSort = this.cl
  630. }
  631. //如果最后一行 并且 是被全部清除的x
  632. if (ifLastCleanX && ifAllCleanedX) {
  633. // LogUtil.log("【checkMoveLeftCellItem】检测消除 全部消除的一列就是消除xIndex中最后一列");
  634. this.checkMoveLeftCellItem();
  635. return;
  636. }
  637. //y方向从下至上循环排查
  638. for (var i = GameConst.row_num - 1; i >= 0; i--) {
  639. if (cellItemDic) {
  640. let cellItem = cellItemDic[i];
  641. //该xIndex纵列没有全部移除
  642. if (cellItem) {
  643. if (ifLastCleanX && i == leftLastCellItemY) {
  644. // LogUtil.log("【checkMoveLeftCellItem】检测消除 移除了最后一个");
  645. cellItem.moveDown(xIndex, i + intervalNum, movedIndex, true);
  646. }
  647. else {
  648. cellItem.moveDown(xIndex, i + intervalNum, movedIndex);
  649. }
  650. movedIndex++;
  651. }
  652. else {
  653. intervalNum += 1;
  654. }
  655. }
  656. else {
  657. continue;
  658. }
  659. }
  660. }
  661. /**检测向左移动的cellItem */
  662. checkMoveLeftCellItem() {
  663. //计算消除的纵列中,有没有被全部移除
  664. //let allCleanedXArr = GameUtil.getAllCleanXIndex(this.cleanXIndexArr);
  665. // LogUtil.log("[Game] checkMoveLeftCellItem allCleanXArr ---------------------", this.allCleanedXArr);
  666. //如果没有全部移除得就return
  667. if (this.allCleanedXArr.length <= 0) {
  668. //LogUtil.log("【checkMoveLeftCellItem】检测是否能消除 没有全部移除的");
  669. GamePlay.Inst.ifCouldClick = true;
  670. GamePlay.Inst.checkIfEliminate();
  671. return;
  672. }
  673. //剩余一列未移除的xIndex
  674. let leftXIndexArr: number[] = GameLogic.getLeftXIndexArr();
  675. let leftMaxXIndex: number = leftXIndexArr[leftXIndexArr.length - 1];
  676. if (leftXIndexArr.length <= 0) {
  677. //检测是否移除
  678. this.checkIfEliminate();
  679. }
  680. else {
  681. mk.console.log("leftXIndexArr", leftXIndexArr);
  682. let intervalNum: number = 0;
  683. for (var i = 0; i < GameConst.col_num; i++) {
  684. let cellItemArr = GamePlay.Inst.cellItemDic[i];
  685. if (this.allCleanedXArr.indexOf(i) == -1) {
  686. //剩余存在的cellItem
  687. let leftCellItemArr = cellItemArr.filter((cellItem) => cellItem != null);
  688. if (intervalNum > 0) {
  689. for (var j = 0; j < cellItemArr.length; j++) {
  690. let cellItem = cellItemArr[j];
  691. if (cellItem) {
  692. if (i == leftMaxXIndex && j == (leftCellItemArr[0].y)) {
  693. mk.console.log("i,j,intervalNum", i, j, intervalNum);
  694. cellItem.moveLeft(i - intervalNum, j, true);
  695. }
  696. else {
  697. cellItem.moveLeft(i - intervalNum, j);
  698. }
  699. }
  700. }
  701. }
  702. else {
  703. //对应 1 2 3 列 中 3全部消除
  704. if (i == leftMaxXIndex) {
  705. GamePlay.Inst.ifCouldClick = true;
  706. GamePlay.Inst.checkIfEliminate();
  707. }
  708. }
  709. }
  710. else {
  711. intervalNum += 1;
  712. }
  713. }
  714. }
  715. }
  716. public timeout_pass: number = 0;
  717. /**检测是否能移除 */
  718. checkIfEliminate() {
  719. mk.console.log("开始检测是否还能够消除!!!!!!!!!!!!!!!!!!!!!");
  720. //FC:测试替换
  721. GameLogic.getCouldCleanVec();
  722. if (this.couldCleanVecArr.length <= 0 && !this.ifPass) {
  723. this.ifPass = true;
  724. EffectMgr.Inst.addSpriteTip(TIP_SPRITEITEM_TYPE.NormalPass);
  725. let timeOut = setTimeout(() => {
  726. this.pass();
  727. clearTimeout(timeOut);
  728. }, 1500);
  729. }
  730. //FC:正式使用
  731. // let timeOut = setTimeout(() => {
  732. // GameUtil.getCouldCleanVec();
  733. // if (this.couldCleanVecArr.length <= 0 && !this.ifPass) {
  734. // this.pass();
  735. // }
  736. // clearTimeout(timeOut);
  737. // }, 1500);
  738. }
  739. getCouldCleanCellItem() {
  740. for (var i = 0; this.cellItemArr.length; i++) {
  741. let cellItem = this.cellItemArr[i];
  742. let aroundSameType = GameLogic.getAroundSameType(cellItem.x, cellItem.y, cellItem.type, false);
  743. if (aroundSameType.length > 0) {
  744. return;
  745. }
  746. }
  747. //this.nextLevel();
  748. this.pass();
  749. //this.recycleAllCellItem();
  750. }
  751. /**restart
  752. * @param 是否扣除体力
  753. */
  754. restart(ifMinusEnergy: boolean = false) {
  755. mk.console.log("restart!!!!!!!!!!!!!!!")
  756. this.refreshGame();
  757. }
  758. /**下一关 */
  759. nextLevel(ifMinusEnergy: boolean = false) {
  760. mk.console.log("nextLevel!!!!!!!!!!!!!!!")
  761. this.refreshGame();
  762. }
  763. /**刷新关卡 */
  764. refreshGame() {
  765. this.recycleAllCellItem();
  766. this.ifPass = false;
  767. this.ifGetPass = false;
  768. this.ifCouldClick = true;
  769. this.initLevel();
  770. this.initTotalScore();
  771. this.curProgressScore = 0;
  772. this.curGetScore = 0;
  773. this.finalGetScore = 0;
  774. this.initScore();
  775. }
  776. /**回收所有的 */
  777. recycleAllCellItem() {
  778. for (var i = 0; i < GameConst.col_num; i++) {
  779. for (var j = 0; j < GameConst.row_num; j++) {
  780. let cellItem = this.cellItemDic[i][j];
  781. if (cellItem) {
  782. // mk.console.log("回收把>>>>>>>>>>>>>>>>>>>>>>");
  783. cellItem.recycle(true, false, false);
  784. }
  785. }
  786. }
  787. // while (this.cellItemList.length > 0) {
  788. // this.cellItemList.splice(0, 1);
  789. // this.cellItemList[0].recycle();
  790. // }
  791. this.cellItemArr = [];
  792. this.cellItemDic = {};
  793. this.curXIndex = 0;
  794. this.curYIndex = GameConst.row_num - 1;
  795. this.initCell();
  796. }
  797. /**通关 */
  798. pass() {
  799. //这边会多次进入
  800. if (GamePlay.Inst.node.active == false) {
  801. return;
  802. }
  803. this.ifPass = true;
  804. GamePlay.Inst.ifGetPass = false;
  805. let passLevelNum = PlayerConst.levelNum + 1;
  806. mk.console.log("[Game]pass passLevelNum", passLevelNum);
  807. GamePlay.Inst.ifGetPass = true;
  808. PlayerConst.levelNum = passLevelNum;
  809. //游戏结算
  810. this.gameCount();
  811. }
  812. /**游戏结算 */
  813. gameCount() {
  814. let node_getPropUI = mk.ui.getCurOnPanel("GetPropUI");
  815. if (node_getPropUI) {
  816. mk.ui.closePanel("GetPropUI");
  817. }
  818. this.gameOver();
  819. }
  820. /**游戏结束 */
  821. gameOver() {
  822. mk.ui.openPanel("module/reward/rewardMission");
  823. }
  824. checkBonusNum(cleanCellItemNum: number) {
  825. if (cleanCellItemNum >= 4) {
  826. let node_bonusTip = PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.BonusTip);
  827. let bonusTip = node_bonusTip.getComponent(BonusTip);
  828. //LogUtil.log("bonusTip",bonusTip);
  829. if (cleanCellItemNum == 4) {
  830. bonusTip.init(4);
  831. mk.audio.playEffect("ef_bonus4");
  832. }
  833. else if (cleanCellItemNum == 5) {
  834. bonusTip.init(5);
  835. mk.audio.playEffect("ef_bonus5");
  836. }
  837. else if (cleanCellItemNum == 6) {
  838. bonusTip.init(6);
  839. mk.audio.playEffect("ef_bonus6");
  840. }
  841. else if (cleanCellItemNum == 7) {
  842. bonusTip.init(7);
  843. mk.audio.playEffect("ef_bonus7");
  844. }
  845. else if (cleanCellItemNum == 8) {
  846. bonusTip.init(8);
  847. mk.audio.playEffect("ef_bonus8");
  848. }
  849. else {
  850. bonusTip.init(8);
  851. mk.audio.playEffect("ef_bonus8");
  852. }
  853. this.node_effectUI.addChild(node_bonusTip);
  854. }
  855. }
  856. checkAddLevelRedPacket(eliminateNum: number): boolean {
  857. let chance = (eliminateNum - 3) * 0.2;
  858. let random = Math.random();
  859. if (random < chance) {
  860. return true;
  861. }
  862. else {
  863. return false;
  864. }
  865. }
  866. public interval_ShowGuide: number = null;
  867. //间隔显示
  868. intervalShowGuide() {
  869. this.cancelShowCouldCleanCellItem();
  870. this.interval_ShowGuide = setInterval(() => {
  871. this.showCouldCleanCellItem();
  872. }, 10000);
  873. }
  874. /**显示 */
  875. showCouldCleanCellItem() {
  876. if (mk.ui.getCurOnPanel("RedPacketUI") || mk.ui.getCurOnPanel("GameOverUI") || !this.couldCleanVecArr || !this.cellItemDic) {
  877. return;
  878. }
  879. if (this.couldCleanVecArr.length <= 0) {
  880. GameLogic.getCouldCleanVec();
  881. }
  882. if (this.couldCleanVecArr.length > 0) {
  883. for (var i = 0; i < this.couldCleanVecArr.length; i++) {
  884. let vec = this.couldCleanVecArr[i];
  885. if (this.cellItemDic[vec.x]) {
  886. let cellItem = this.cellItemDic[vec.x][vec.y];
  887. if (cellItem) {
  888. cellItem.shake();
  889. }
  890. }
  891. else {
  892. }
  893. }
  894. }
  895. }
  896. /**取消显示 */
  897. cancelShowCouldCleanCellItem() {
  898. if (this.interval_ShowGuide) {
  899. clearInterval(this.interval_ShowGuide);
  900. this.interval_ShowGuide = null;
  901. if (this.couldCleanVecArr.length > 0) {
  902. for (var i = 0; i < this.couldCleanVecArr.length; i++) {
  903. let vec = this.couldCleanVecArr[i];
  904. let cellItem = this.cellItemDic[vec.x][vec.y];
  905. if (cellItem) {
  906. cellItem.normal();
  907. }
  908. }
  909. }
  910. this.couldCleanVecArr = [];
  911. }
  912. }
  913. }