GamePlay.ts 40 KB

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