GamePlay.ts 40 KB

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