GamePlay.ts 39 KB

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