GamePlay.ts 39 KB

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