Reward.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { AdFun } from "../../data/AdData";
  2. import { RewardType } from "../../data/GameData";
  3. const { ccclass, property } = cc._decorator;
  4. /**
  5. * 盖子类型
  6. */
  7. enum LidType {
  8. /** 无盖子 */
  9. none = 0,
  10. /** 幸运红包盖子 */
  11. luck = 1,
  12. /** 通关红包盖子 */
  13. mission = 2
  14. }
  15. /**
  16. * 获取奖励界面-红包
  17. * @author 薛鸿潇
  18. */
  19. @ccclass
  20. export default class Reward extends cc.Component {
  21. @property({ displayName: '红包类型', type: cc.Enum(LidType), tooltip: 'none无类型,luck幸运红包,mission通关红包' })
  22. private lid_type: LidType = LidType.none;
  23. @property({ displayName: '幸运红包', type: cc.Node })
  24. private node_luck: cc.Node = null;
  25. @property({ displayName: '通关红包', type: cc.Node })
  26. private node_mission: cc.Node = null;
  27. @property({ displayName: '开红包动画', type: cc.Animation })
  28. private anim_open_redbag: cc.Animation = null;
  29. @property({ displayName: '按钮看视频', type: cc.Button })
  30. private btn_watch_video: cc.Button = null;
  31. @property({ displayName: '红包图标', type: cc.Node })
  32. private node_redBag: cc.Node = null;
  33. @property({ displayName: '金猪图标', type: cc.Node })
  34. private node_pig: cc.Node = null;
  35. @property({ displayName: '奖励数量', type: cc.Label })
  36. private lbl_reward_value: cc.Label = null;
  37. // 底部提现相关
  38. @property({ displayName: 'spr提现进度', type: cc.Sprite })
  39. private spr_cash_out: cc.Sprite = null;
  40. @property({ displayName: 'lbl提现进度', type: cc.Label })
  41. private lbl_cash_out: cc.Label = null;
  42. @property({ displayName: '提现金额', type: cc.Label })
  43. private lbl_cash: cc.Label = null;
  44. /** 当前阶段 */
  45. private cur_stage: number = LidType.none;
  46. onLoad() {
  47. this.lbl_reward_value.string = '0';
  48. // gData.reward.data = [{ rewardType: 3, rewardNum: 100 }]// 测试数据
  49. }
  50. start() {
  51. /** 盖子 */
  52. this.initLid();
  53. }
  54. update(dt) {
  55. if (gData.reward.init_cash_style) {
  56. this.initCashOutStyle();
  57. return;
  58. }
  59. if (gData.reward.adData) {
  60. // 展示奖励
  61. this.watchVideoCall();
  62. return;
  63. }
  64. }
  65. lateUpdate() {
  66. gData.reward.init_cash_style = null;
  67. gData.reward.adData = null;
  68. }
  69. /**
  70. * 初始化封面样式
  71. */
  72. private initLid() {
  73. if (this.lid_type === LidType.none) {// 无盖子样式
  74. this.node_luck.active = false;
  75. this.node_mission.active = false;
  76. this.btn_watch_video.node.active = false;
  77. this.watchVideoCall();
  78. } else if (this.lid_type === LidType.luck) {
  79. this.node_luck.active = true;
  80. this.node_mission.active = false;
  81. this.btn_watch_video.node.active = true;
  82. this.cur_stage = LidType.luck;
  83. mk.audio.playEffect("reward");
  84. } else if (this.lid_type === LidType.mission) {
  85. this.node_luck.active = false;
  86. this.node_mission.active = true;
  87. this.btn_watch_video.node.active = true;
  88. this.cur_stage = LidType.mission;
  89. mk.audio.playEffect("reward");
  90. }
  91. }
  92. /**
  93. * 底部提现相关样式
  94. */
  95. private initCashOutStyle() {
  96. const cash_bar = gData.redBagCash.cash_bar;
  97. let cash_data = gData.redBagCash.getItemDataByIndex(cash_bar);
  98. if (!cash_data) return;
  99. this.lbl_cash.string = cash_data.money + '元';
  100. this.lbl_cash_out.string = gData.gameData.gameData.redMoney + '/' + cash_data.type_value;
  101. this.spr_cash_out.fillRange = gData.gameData.gameData.redMoney / cash_data.type_value;
  102. }
  103. /**
  104. * 看广告
  105. */
  106. private clickWatchVideo() {
  107. cc.log('看广告请求');
  108. if (gData.reward.data[0] && gData.reward.data[0].rewardType === 1) {
  109. gData.adData.watchVideo(AdFun.settlement)// 关卡结算视频
  110. } else {
  111. gData.adData.watchVideo(AdFun.bubble);// 气泡视频
  112. }
  113. // 测试数据
  114. gData.reward.adData = gData.reward.data;
  115. }
  116. /**
  117. * 看广告回调:开奖
  118. */
  119. private watchVideoCall() {
  120. if (!gData.reward.data || !gData.reward.data.length) return;
  121. mk.audio.playEffect("rewardOpen");
  122. this.cur_stage = LidType.none;
  123. // 数据排序:多条奖励,只展示红包,把红包放在首位
  124. const r_count = gData.reward.data.length;
  125. if (r_count >= 2) {
  126. for (let i = 0; i < r_count; i++) {
  127. if (gData.reward.data[i].rewardType === 1) {
  128. let new_data = gData.reward.data.splice(i, 1);
  129. gData.reward.data.unshift(new_data);
  130. break;
  131. }
  132. }
  133. }
  134. // 图标展示
  135. if (gData.reward.data[0].rewardType === RewardType.redBag) {
  136. this.node_redBag.active = true;
  137. this.node_pig.active = false;
  138. } else {
  139. this.node_redBag.active = false;
  140. this.node_pig.active = true;
  141. }
  142. let rewardNum = gData.reward.data[0].rewardNum || 0;
  143. this.lbl_reward_value.string = `${rewardNum}`;
  144. // 动画
  145. this.anim_open_redbag.play();
  146. this.node_luck.active = false;
  147. this.node_mission.active = false;
  148. this.btn_watch_video.node.active = false;
  149. }
  150. /**
  151. * 点击获得奖励
  152. */
  153. private clickGetReward() {
  154. cc.log('获取奖励:', gData.reward.data);
  155. gData.reward.addReward();
  156. this.flyCoinAnim();
  157. gData.reward.data = [];
  158. }
  159. /**
  160. * 提现操作
  161. */
  162. private clickCashOut() {
  163. let path = 'module/redBagCash/redBagCash';
  164. mk.ui.openPanel(path);
  165. }
  166. /**
  167. * 飞coin动画
  168. */
  169. private flyCoinAnim() {
  170. if (!gData.reward.data || !gData.reward.data.length) return;
  171. let target_node = gData.gameData.gameStyle.icon_hb;
  172. if (gData.reward.data[0].rewardType == RewardType.redBag) {
  173. target_node = gData.gameData.gameStyle.icon_hb;
  174. } else if (gData.reward.data[0].rewardType == RewardType.rmb) {
  175. // target_node = gData.gameData.gameStyle.icon_zb;
  176. } else if (gData.reward.data[0].rewardType == RewardType.pigRmb) {
  177. target_node = gData.gameData.gameStyle.icon_zb;
  178. }
  179. const end_world_pos = target_node.parent.convertToWorldSpaceAR(target_node.getPosition());
  180. const end_node_pos = gData.gameData.gameStyle.node.parent.convertToNodeSpaceAR(end_world_pos);
  181. let start_node_pos = new cc.Vec2(0, -300);
  182. mk.fly.PlayCoinAnim(gData.reward.data[0].rewardType, 10, start_node_pos, end_node_pos)
  183. }
  184. /**
  185. * 下一关操作
  186. */
  187. private clickNextMission() {
  188. }
  189. onDestroy() {
  190. if (this.cur_stage === LidType.none) {
  191. mk.audio.playEffect("rewardClose");
  192. }
  193. }
  194. }