UIGetReward.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { Widget, _decorator, Node, Sprite, SpriteFrame, Label, Tween, tween, v3 } from 'cc';
  2. import { DtoReward } from '../dto/DtoReward';
  3. import { ItemType } from '../enum/ItemType';
  4. import { CoinMgr } from '../manager/CoinMgr';
  5. import { ItemMgr } from '../manager/ItemMgr';
  6. import { ResMgr } from '../manager/ResMgr';
  7. import { InputBlock } from '../misc/InputBlock';
  8. import { UIBase } from '../scriptBase/UIBase';
  9. import { CoinItem } from '../uiItem/CoinItem';
  10. import { CfgItem } from '../config/CfgItem';
  11. const { ccclass, property, requireComponent } = _decorator;
  12. @ccclass('UI/UIGetReward')
  13. @requireComponent(Widget)
  14. export class UIGetReward extends UIBase {
  15. private icon: Sprite = null
  16. private lbCnt: Label = null
  17. private coinItem: CoinItem = null
  18. private rewardArr: DtoReward[] = []
  19. private tw: Tween<Node>
  20. private curReward: DtoReward = null
  21. public set CurReward(reward: DtoReward) {
  22. this.curReward = reward
  23. const { itemId, cnt } = reward
  24. const { bundle, icon, type } = CfgItem[itemId]
  25. this.lbCnt.string = `x${cnt}`
  26. const spf: SpriteFrame = ResMgr.getSpriteFrame(bundle, icon)
  27. this.icon.spriteFrame = spf
  28. if (this.tw) this.tw.stop()
  29. this.tw = tween(this.icon.node)
  30. this.tw.set({ scale: v3() })
  31. this.tw.to(0.5, { scale: v3(1, 1, 1) }, { easing: 'backOut' })
  32. this.tw.start()
  33. }
  34. protected onLoad(): void {
  35. this.icon = this.findComp('Icon', Sprite)
  36. this.lbCnt = this.findComp('LbCnt', Label)
  37. this.coinItem = this.findComp('CoinItem', CoinItem)
  38. }
  39. public onOpen(data?: any): void {
  40. this.coinItem.init(CoinMgr.CurCoin)
  41. this.rewardArr = data
  42. this.CurReward = this.rewardArr.shift()
  43. }
  44. public onClose(data?: any): void {
  45. }
  46. protected async onBtnTripleGetClick() {
  47. const len: number = this.rewardArr.length
  48. if (len <= 0) {
  49. await this.getItem(3)
  50. this.close()
  51. } else {
  52. await this.getItem(3)
  53. this.CurReward = this.rewardArr.shift()
  54. }
  55. }
  56. protected async onBtnNormalGetClick() {
  57. const len: number = this.rewardArr.length
  58. if (len <= 0) {
  59. await this.getItem()
  60. this.close()
  61. } else {
  62. await this.getItem()
  63. this.CurReward = this.rewardArr.shift()
  64. }
  65. }
  66. private getItem(time: number = 1) {
  67. return new Promise<void>((resolve, reject) => {
  68. const { itemId, cnt } = this.curReward
  69. const itemType: ItemType = ItemMgr.getType(itemId)
  70. this.lbCnt.string = `x${time * cnt}`
  71. ItemMgr.updateItem(itemId, time * cnt)
  72. switch (itemType) {
  73. case ItemType.Coin:
  74. this.coinItem.updateCoin(this.coinItem.Coin + time * cnt)
  75. InputBlock.setActive(2)
  76. this.scheduleOnce(() => {
  77. resolve()
  78. }, 2)
  79. break;
  80. case ItemType.Skill:
  81. resolve()
  82. break
  83. case ItemType.Collection:
  84. resolve()
  85. default:
  86. break;
  87. }
  88. })
  89. }
  90. }