BuyLifeLayer.ts 1.9 KB

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