GamePlay.ts 41 KB

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