GameUI.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { _decorator, v3, Node, tween, Material, Sprite } from 'cc';
  2. import { Main } from './Main';
  3. import BasePanel from '../core/component/BasePanel';
  4. import { audioMgr } from '../core/manager/AudioManager';
  5. import Data from '../core/manager/Data';
  6. import WindowManager from '../core/manager/WindowManager';
  7. import MsgHints from '../core/utils/MsgHints';
  8. import Utils from '../core/utils/Utils';
  9. import { BuyItemLayer } from '../gameui/BuyItemLayer';
  10. import { GameConst, ITEM_FROZEN_LV, ITEM_HINT_LV, ITEM_TYPE } from '../core/common/GameConst';
  11. import { levelsData } from '../user/LevelsData';
  12. import i18n from '../core/i18n/runtime-scripts/LanguageData';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('GameUI')
  15. export class GameUI extends BasePanel {
  16. private hint_time = 0;
  17. public starNode: Node = null;
  18. @property({type: Material,tooltip: "灰色材质"})
  19. public garyMaterial: Material = null!;
  20. onBtnClicked(event: any, customEventData: any) {
  21. var btnName = event.target.name;
  22. switch (btnName) {
  23. case "btn_pause":
  24. audioMgr.playOneShot(GameConst.audios.btnclick);
  25. this.pasue = true;
  26. WindowManager.ins.open("PauseLayer")
  27. break;
  28. case "item_left":
  29. audioMgr.playOneShot(GameConst.audios.btnclick);
  30. if (Date.now() - this.hint_time < 2000) return;
  31. if (Data.user.lv < ITEM_HINT_LV) {
  32. MsgHints.show(i18n("main.关卡%{value}解锁", { value: ITEM_HINT_LV}));
  33. return;
  34. }
  35. if(Data.user.hint > 0) {
  36. this.dispatch(GameConst.USE_ITEM_HINT);
  37. this.hint_time = Date.now();
  38. }else{
  39. WindowManager.ins.open("BuyItemLayer").then((com: BuyItemLayer) => {
  40. com.setItemType(ITEM_TYPE.Hint);
  41. })
  42. }
  43. break;
  44. case "btn_sp_01"://移除
  45. audioMgr.playOneShot(GameConst.audios.btnclick);
  46. Main.I._GameNode.remove();
  47. break;
  48. case "btn_sp_02"://凑齐
  49. audioMgr.playOneShot(GameConst.audios.btnclick);
  50. Main.I._GameNode.prompt();
  51. break;
  52. case "btn_sp_03"://乱序
  53. audioMgr.playOneShot(GameConst.audios.btnclick);
  54. Main.I._GameNode.mess();
  55. break;
  56. case "item_right":
  57. if (this.freezetime) {
  58. MsgHints.show(i18n("main.冰冻中..."));
  59. return;
  60. }
  61. if (Data.user.lv < ITEM_FROZEN_LV) {
  62. MsgHints.show(i18n("main.关卡%{value}解锁", { value: ITEM_FROZEN_LV}));
  63. return;
  64. }
  65. if (Data.user.frozen > 0) {
  66. Data.user.frozen--;
  67. this.freezetime = true;
  68. setTimeout(() => {
  69. this.freezetime = false;
  70. }, 5000);
  71. audioMgr.playOneShot(GameConst.audios.freeze);
  72. }
  73. else {
  74. WindowManager.ins.open("BuyItemLayer").then((com: BuyItemLayer) => {
  75. com.setItemType(ITEM_TYPE.Frozen);
  76. })
  77. }
  78. break;
  79. }
  80. }
  81. start() {
  82. this.register(GameConst.USE_ITEM_TIME, this.doUseTime.bind(this))
  83. this.register("PAUSE_LAYER_CLOSE", () => {
  84. this.pasue = false;
  85. })
  86. this.starNode = this.GetGameObject("star-with-outline");
  87. }
  88. restart() {
  89. this.SetText("lbl_lv", i18n("main.LV:%{value}", { value: Data.user.lv}));
  90. let obj = levelsData.getCurLevelInfo();
  91. console.log("关卡数据:", obj);
  92. this.levelTime = obj.time;
  93. this.GetGameObject("match-timer-freeze-background").active = false;
  94. this.GetGameObject("match-extra-time-power-up-small").active = false;
  95. this.SetText("lbl_left_item_lv", i18n("main.关卡%{value}", { value: ITEM_HINT_LV}));
  96. this.SetText("lbl_right_item_lv", i18n("main.关卡%{value}", { value: ITEM_FROZEN_LV}));
  97. this.freezetime = false;
  98. this.pasue = false;
  99. this.passedTime = 0;
  100. }
  101. doUseTime() {
  102. Data.user.time--;
  103. let node = this.GetGameObject("match-extra-time-power-up-small");
  104. node.active = true;
  105. node.setPosition(v3(0, 0, 0));
  106. tween(node).to(0.5, { worldPosition: this.GetGameObject("lbl_time").worldPosition }).call(() => {
  107. this.levelTime += 60;
  108. node.active = false;
  109. }).start();
  110. }
  111. levelTime = 0;
  112. passedTime = 0;
  113. pasue: boolean = false;
  114. freezetime: boolean = false;
  115. public star = 0;
  116. update(dt) {
  117. if(WindowManager.ins.isShow("OutOfBoxLayer")||WindowManager.ins.isShow("DrawStarLayer")||WindowManager.ins.isShow("LevelClearLayer")||WindowManager.ins.isShow("FailLayer"))return
  118. this.GetGameObject("match-timer-freeze-background").active = this.freezetime;
  119. if (!this.pasue && !this.freezetime)
  120. this.passedTime += dt;
  121. this.SetText("lbl_star", this.star + "");
  122. let nLeft = this.levelTime - this.passedTime;
  123. if (nLeft > 0) {
  124. this.SetText("lbl_time", Utils.getTimeStrByS(nLeft));
  125. }else {//游戏结束
  126. WindowManager.ins.open("FailLayer")
  127. }
  128. if (Data.user.lv < ITEM_HINT_LV) {
  129. this.GetGameObject("item_left_Locked").active = true;
  130. this.GetGameObject("item_left_icon").active = false;
  131. this.GetGameObject("match-power-up-bg-001").getComponent(Sprite).customMaterial = this.garyMaterial;
  132. }else {
  133. this.GetGameObject("item_left_Locked").active = false;
  134. this.GetGameObject("item_left_icon").active = true;
  135. this.SetText("lbl_item_left_count", Data.user.hint == 0 ? "+" : Data.user.hint + "");
  136. this.GetGameObject("match-power-up-bg-001").getComponent(Sprite).customMaterial = null;
  137. }
  138. if (Data.user.lv < ITEM_FROZEN_LV) {
  139. this.GetGameObject("item_right_Locked").active = true;
  140. this.GetGameObject("item_right_icon").active = false;
  141. this.GetGameObject("match-power-up-bg-002").getComponent(Sprite).customMaterial = this.garyMaterial;
  142. }else {
  143. this.GetGameObject("item_right_Locked").active = false;
  144. this.GetGameObject("item_right_icon").active = true;
  145. this.SetText("lbl_item_rihgt_count", Data.user.frozen == 0 ? "+" : Data.user.frozen + "");
  146. this.GetGameObject("match-power-up-bg-002").getComponent(Sprite).customMaterial = null;
  147. }
  148. }
  149. }