GamePlay.ts 37 KB

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