BuyLifeLayer.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { _decorator, Component, Label, Node } from 'cc';
  2. import platformSystem from '../platform/platformSystem';
  3. import BasePanel from '../core/component/BasePanel';
  4. import { audioMgr } from '../core/manager/AudioManager';
  5. import Data from '../core/manager/Data';
  6. import Utils from '../core/utils/Utils';
  7. import { GameConst } from '../core/common/GameConst';
  8. import i18n from '../core/i18n/runtime-scripts/LanguageData';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('BuyLifeLayer')
  11. export class BuyLifeLayer extends BasePanel {
  12. update() {
  13. this.SetText("lbl_life", Data.user.life + "")
  14. if (Data.user.life >= GameConst.MAX_LIFE) {
  15. this.SetText("lbl_life_time", i18n("main.已满"))
  16. }
  17. else {
  18. if (Data.user.life_cost_time == 0) {
  19. Data.user.life_cost_time = Date.now();
  20. }
  21. let nLeft = (Date.now() - Data.user.life_cost_time) / 1000;
  22. let addLife = Math.floor(nLeft / 60 / GameConst.MAX_LIFE_RECOVERY_TIME);
  23. if (addLife > 0)
  24. Data.user.life += addLife;
  25. nLeft -= addLife * GameConst.MAX_LIFE_RECOVERY_TIME * 60;
  26. this.SetText("lbl_life_time", Utils.getTimeStrByS(GameConst.MAX_LIFE_RECOVERY_TIME * 60 - nLeft))
  27. }
  28. }
  29. star() {
  30. platformSystem.platform.showInterstitialAd();
  31. }
  32. onBtnClicked(event: any, customEventData: any) {
  33. audioMgr.playOneShot(GameConst.audios.btnclick);
  34. var btnName = event.target.name;
  35. switch (btnName) {
  36. case "btn_close":
  37. this.close();
  38. break;
  39. case "btn_ad":
  40. platformSystem.platform.showRewardVideo((b) => {
  41. if (b) {
  42. Data.user.life += 1;
  43. Data.save();
  44. this.close();
  45. }
  46. })
  47. break;
  48. }
  49. }
  50. }