UI_Win.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { _decorator, Component, Label, Node, Sprite, v3, SpriteFrame } from 'cc';
  2. import { UI_Dialog } from '../UI_Dialog';
  3. import { aa, UIController, UIElement } from 'db://assets/scripts/aa';
  4. import { rootMgr } from '../../../scene/RootMgr';
  5. import { rotate_anim_ext, rotate_anim_stop } from '../../../util/Customize_Ani';
  6. import { RewardData, RewardItem } from '../../../data/RewardData';
  7. import { RewardDataManager } from '../../../data/RewardDataManager';
  8. import { Layout_Win } from 'db://assets/dialog/Win/Layout_Win';
  9. import { ItemKey, ItemService } from '../../../data/Item/ItemService';
  10. import { Notify } from '../../../scene/GameConfing';
  11. import { BgmName } from 'db://assets/scripts/Audio/BgmName';
  12. import { audioManager } from 'db://assets/scripts/Audio/AudioManager';
  13. const { ccclass, property } = _decorator;
  14. // 记录指针为 0 的位置
  15. const round = {
  16. start: -276,
  17. s1: -176 + 276,
  18. s2: -43 + 276,
  19. s3: 31 + 276,
  20. s4: 163 + 276,
  21. s5: 276 + 276,
  22. }
  23. const roundLen = [
  24. { value: 2, len: 0, num: 90 },
  25. { value: 3, len: round.s1, num: 135 },
  26. { value: 5, len: round.s2, num: 225 },
  27. { value: 3, len: round.s3, num: 135 },
  28. { value: 2, len: round.s4, num: 90 }]
  29. @UIController({ bundleName: 'dialog', path: 'Win/win' })
  30. export class UI_Win extends UI_Dialog {
  31. @UIElement(Node)
  32. contineBtn: Node
  33. @UIElement(Node)
  34. shareBtn: Node
  35. @UIElement(Node)
  36. useCoinBtn: Node
  37. @UIElement(Node)
  38. needle: Node//指针
  39. @UIElement(Node)
  40. light: Node
  41. @UIElement(Sprite)
  42. cocoTea: Sprite
  43. @UIElement(Label)
  44. cionLabel: Label
  45. @UIElement(Label)
  46. winLabel: Label
  47. @UIElement(Sprite)
  48. bottel: Sprite
  49. // 奖励图片数组
  50. protected onCreated(): void {
  51. super.onCreated()
  52. audioManager.playOneShot(BgmName.win)
  53. this.contineBtn.on(Node.EventType.TOUCH_END, this.playContine, this)
  54. this.shareBtn.on(Node.EventType.TOUCH_END, this.onShareBtn, this)
  55. this.useCoinBtn.on(Node.EventType.TOUCH_END, this.onUseCoinBtnClick, this)
  56. this.bottel.node.active = false
  57. this.lightAnim()
  58. // 显示当前关卡的奖励
  59. this.showStageReward();
  60. }
  61. private showStageReward() {
  62. const rewards = RewardDataManager.instance.getAllRewards();
  63. // 找出第一个未解锁的奖励(按数组顺序)
  64. let reward: RewardItem | null = null;
  65. for (let i = 0; i < rewards.length; i++) {
  66. if (!rewards[i].unlocked) {
  67. reward = rewards[i];
  68. break;
  69. }
  70. }
  71. if (!reward) {
  72. this.cocoTea.node.active = false;
  73. this.winLabel.string = "恭喜通过本关"
  74. this.bottel.node.active=true
  75. return;
  76. } // 所有奖励都已解锁
  77. // 解锁奖励
  78. RewardDataManager.instance.unlockReward(reward.id);
  79. RewardDataManager.instance.saveRewards();
  80. // 显示奖励图片
  81. if (this.cocoTea) {
  82. const layoutWin = this.node.getComponent(Layout_Win);
  83. this.cocoTea.node.active = true;
  84. this.cocoTea.spriteFrame = layoutWin.rewardSprites[reward.id];
  85. this.winLabel.string = "恭喜获得" + reward.name;
  86. }
  87. rootMgr.game.evt.emit('rewardUnlocked');
  88. }
  89. // 通知收藏面板更新
  90. notifyCollectPanel(reward: RewardItem) {
  91. // 发送事件通知收藏面板更新
  92. rootMgr.game.evt.emit('rewardUnlocked');
  93. }
  94. len: number = 0
  95. speed: number = 500 * 1.5
  96. stop = false
  97. protected onUpdate(dt: number): void {
  98. if (this.stop) return
  99. // 指针移动逻辑
  100. let lenAll = round.s5
  101. this.len += this.speed * dt
  102. this.len = this.len % (2 * (lenAll))
  103. let endx = this.len > lenAll ? round.start + 2 * lenAll - this.len : round.start + this.len
  104. this.needle.position = v3(endx, this.needle.position.y, 0)
  105. // 更新金币显示
  106. this.updateCoinLabel();
  107. }
  108. private updateCoinLabel() {
  109. if (!this.cionLabel) return;
  110. // 计算当前指针位置对应的奖励值
  111. const currentReward = this.getCurrentRewardValue();
  112. // 更新金币
  113. this.cionLabel.string = currentReward.toString();
  114. }
  115. // 获取当前指针位置的奖励值
  116. private getCurrentRewardValue(): number {
  117. const pointerX = this.needle.position.x;
  118. let rewardValue = 0; // 默认值
  119. // 查找指针所在的奖励区间
  120. for (let i = roundLen.length - 1; i >= 0; i--) {
  121. const section = roundLen[i];
  122. const sectionStart = round.start + section.len;
  123. if (pointerX >= sectionStart) {
  124. rewardValue = section.num;
  125. break;
  126. }
  127. }
  128. return rewardValue;
  129. }
  130. playContine() {
  131. // this.stop = true
  132. // for (let i = roundLen.length - 1; i >= 0; i--) {
  133. // let r = roundLen[i]
  134. // if (this.needle.position.x >= r.len + round.start) {
  135. // // debugger
  136. // // alert(r.value)
  137. // break
  138. // }
  139. // }
  140. const currentReward = 45
  141. rootMgr.dataControl.getCompent(ItemService).getRewarded(ItemKey.coin, currentReward);
  142. rootMgr.game.evt.emit('save');//保存
  143. rootMgr.game.evt.emit('toNextStage')
  144. this.close()
  145. }
  146. onUseCoinBtnClick() {
  147. // 获取当前指针位置对应的奖励值
  148. const currentReward = this.getCurrentRewardValue();
  149. // 观看广告
  150. const adSuccess = aa.sdk.playRewardAd('金币翻倍奖励');
  151. if (adSuccess) {
  152. // 广告观看成功奖励金币
  153. rootMgr.dataControl.getCompent(ItemService).getRewarded(ItemKey.coin, currentReward);
  154. rootMgr.game.evt.emit('save');//保存
  155. Notify("金币获取成功")
  156. console.log(`恭喜获得${currentReward}金币`);
  157. }
  158. rootMgr.game.evt.emit('toNextStage')//下一关
  159. this.close()
  160. }
  161. onShareBtn() {
  162. aa.sdk.shareAppMessageAsync();
  163. console.log("分享");
  164. }
  165. lightAnim() {
  166. rotate_anim_ext(this.light, 'loop', {
  167. duration: 3, // 每圈旋转时间(秒)
  168. angle: 360, // 每圈旋转角度(360度)
  169. });
  170. }
  171. onClose() {
  172. rotate_anim_stop(this.light); // 停止旋转动画
  173. super.onClose();
  174. }
  175. }