UI_NeedLife.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { _decorator, Node } from 'cc';
  2. import { aa, UIController, UIElement } from 'db://assets/scripts/aa';
  3. import { UI_Dialog } from '../UI_Dialog';
  4. import { rootMgr } from '../../../scene/RootMgr';
  5. import { Player } from '../../../data/player/Player';
  6. import { Notify } from '../../../scene/GameConfing';
  7. import { ItemKey, ItemService } from '../../../data/Item/ItemService';
  8. const { ccclass, property } = _decorator;
  9. @UIController({ bundleName: 'dialog', path: 'lifeOver/lifeOver' })
  10. export class UI_NeedLife extends UI_Dialog {
  11. @UIElement(Node)
  12. adBtn: Node
  13. @UIElement(Node)
  14. useCoinBtn: Node
  15. start(): void {
  16. this.adBtn.on(Node.EventType.TOUCH_END, this.useAd, this)
  17. this.useCoinBtn.on(Node.EventType.TOUCH_END, this.useCoin, this)
  18. }
  19. async useAd() {
  20. // debugger
  21. let player = rootMgr.dataControl.getCompent(Player)
  22. if (player.getLifeControl().isMax()) {
  23. Notify("无需领取")
  24. return
  25. }
  26. let boolean = await aa.sdk.playRewardAd('体力耗尽')
  27. if (boolean) {
  28. // 体力回满
  29. let player = rootMgr.dataControl.getCompent(Player)
  30. player.life = player.getLifeControl().max
  31. Notify("领取成功")
  32. this.onClose()
  33. }
  34. }
  35. useCoin() {
  36. let player = rootMgr.dataControl.getCompent(Player)
  37. if (player.getLifeControl().isMax()) {
  38. Notify("无需领取")
  39. return
  40. }
  41. let item = rootMgr.dataControl.getCompent(ItemService)
  42. let bool = item.consume(ItemKey.coin, 900)
  43. if (bool) {
  44. player.life = player.getLifeControl().max
  45. Notify("领取成功")
  46. this.onClose()
  47. } else {
  48. Notify("金币不足")
  49. }
  50. }
  51. }