UIPassReward.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Widget, _decorator, Node, Label, ProgressBar } from 'cc';
  2. import { UIBase } from '../scriptBase/UIBase';
  3. import { GameMgr } from '../manager/GameMgr';
  4. import { CoinItem } from '../uiItem/CoinItem';
  5. import { CoinMgr } from '../manager/CoinMgr';
  6. import { StorageUtil } from '../util/StorageUtil';
  7. import { EventMgr } from '../manager/EventMgr';
  8. import { EventType } from '../enum/EventType';
  9. import { CfgPassReward } from '../config/cfg_passReward';
  10. import { MathUtil } from '../util/MathUtil';
  11. import { UI } from '../enum/UI';
  12. import { DtoReward } from '../dto/DtoReward';
  13. const { ccclass, property, requireComponent } = _decorator;
  14. @ccclass('UI/UIPassReward')
  15. @requireComponent(Widget)
  16. export class UIPassReward extends UIBase {
  17. private btnGet: Node = null
  18. private btnContinue: Node = null
  19. private coinItem: CoinItem = null
  20. private curPassLevel: number = 0
  21. private targetPassLevel: number = 0
  22. private passRewardLevel: number = 0
  23. protected onLoad(): void {
  24. this.btnGet = this.findNode('BtnGet')
  25. this.btnContinue = this.findNode('BtnContinue')
  26. this.coinItem = this.findComp('CoinItem', CoinItem)
  27. }
  28. public onOpen(data?: any): void {
  29. this.updateState()
  30. }
  31. public onClose(data?: any): void {
  32. }
  33. private updateState(): void {
  34. this.coinItem.init(CoinMgr.CurCoin)
  35. this.passRewardLevel = StorageUtil.getItem('passRewardLevel', 0)
  36. this.curPassLevel = GameMgr.CurPassedLevel
  37. this.targetPassLevel = Math.ceil(this.curPassLevel / 5) * 5
  38. if (this.passRewardLevel === this.targetPassLevel) this.targetPassLevel += 5
  39. const canGet: boolean = this.curPassLevel >= this.targetPassLevel
  40. this.btnGet.active = canGet
  41. this.btnContinue.active = !canGet
  42. }
  43. protected onBtnCloseClick(): void {
  44. this.close()
  45. }
  46. protected onBtnGetClick(): void {
  47. this.getReward()
  48. StorageUtil.setItem('passRewardLevel', this.targetPassLevel)
  49. this.updateState()
  50. EventMgr.emit(EventType.GetPassReward)
  51. }
  52. protected getReward(): void {
  53. const weights: number[] = []
  54. for (let i = 0; i < CfgPassReward.length; i++) {
  55. const passReward = CfgPassReward[i]
  56. weights.push(passReward.weight)
  57. }
  58. const idx: number = MathUtil.randWeight(weights)
  59. const passReward = CfgPassReward[idx]
  60. const reward: DtoReward = passReward
  61. this.open(UI.GetReward, [reward])
  62. }
  63. }