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