Reward.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. }
  49. start() {
  50. /** 盖子 */
  51. this.initLid();
  52. }
  53. update(dt) {
  54. if (gData.reward.init_cash_style) {
  55. gData.reward.init_cash_style = null;
  56. this.initCashOutStyle();
  57. return;
  58. }
  59. if (gData.reward.adData) {
  60. // 展示奖励
  61. this.watchVideoCall();
  62. gData.reward.adData = null;
  63. return;
  64. }
  65. }
  66. /**
  67. * 初始化封面样式
  68. */
  69. private initLid() {
  70. if (this.lid_type === LidType.none) {// 无盖子样式
  71. this.node_luck.active = false;
  72. this.node_mission.active = false;
  73. this.btn_watch_video.node.active = false;
  74. this.watchVideoCall();
  75. } else if (this.lid_type === LidType.luck) {
  76. this.node_luck.active = true;
  77. this.node_mission.active = false;
  78. this.btn_watch_video.node.active = true;
  79. this.cur_stage = LidType.luck;
  80. mk.audio.playEffect("reward");
  81. } else if (this.lid_type === LidType.mission) {
  82. this.node_luck.active = false;
  83. this.node_mission.active = true;
  84. this.btn_watch_video.node.active = true;
  85. this.cur_stage = LidType.mission;
  86. mk.audio.playEffect("reward");
  87. }
  88. }
  89. /**
  90. * 底部提现相关样式
  91. */
  92. private initCashOutStyle() {
  93. const cash_bar = gData.redBagCash.cash_bar;
  94. let cash_data = gData.redBagCash.getItemDataByIndex(cash_bar);
  95. if (!cash_data) return;
  96. this.lbl_cash.string = cash_data.money + '元';
  97. this.lbl_cash_out.string = gData.gameData.gameData.redMoney + '/' + cash_data.type_value;
  98. this.spr_cash_out.fillRange = gData.gameData.gameData.redMoney / cash_data.type_value;
  99. }
  100. /**
  101. * 看广告
  102. */
  103. private clickWatchVideo() {
  104. cc.log('看广告请求');
  105. if (gData.reward.data[0] && gData.reward.data[0].rewardType === 1) {
  106. gData.adData.watchVideo(AdFun.settlement)// 关卡结算视频
  107. } else {
  108. gData.adData.watchVideo(AdFun.bubble);// 气泡视频
  109. }
  110. // 测试数据
  111. gData.reward.adData = gData.reward.data;
  112. this.flyCoinAnim();
  113. }
  114. /**
  115. * 看广告回调:开奖
  116. */
  117. private watchVideoCall() {
  118. if (!gData.reward.data || !gData.reward.data.length) return;
  119. mk.audio.playEffect("rewardOpen");
  120. this.cur_stage = LidType.none;
  121. // 数据排序:多条奖励,只展示红包,把红包放在首位
  122. const r_count = gData.reward.data.length;
  123. if (r_count >= 2) {
  124. for (let i = 0; i < r_count; i++) {
  125. if (gData.reward.data[i].rewardType === 1) {
  126. let new_data = gData.reward.data.splice(i, 1);
  127. gData.reward.data.unshift(new_data);
  128. break;
  129. }
  130. }
  131. }
  132. // 图标展示
  133. if (gData.reward.data[0].rewardType === RewardType.redBag) {
  134. this.node_redBag.active = true;
  135. this.node_pig.active = false;
  136. } else {
  137. this.node_redBag.active = false;
  138. this.node_pig.active = true;
  139. }
  140. let rewardNum = gData.reward.data[0].rewardNum || 0;
  141. this.lbl_reward_value.string = `${rewardNum}`;
  142. // 动画
  143. this.anim_open_redbag.play();
  144. this.node_luck.active = false;
  145. this.node_mission.active = false;
  146. this.btn_watch_video.node.active = false;
  147. }
  148. /**
  149. * 获得奖励
  150. */
  151. private clickGetReward() {
  152. cc.log('获取奖励:', gData.reward.data);
  153. this.flyCoinAnim();
  154. gData.reward.data = [];
  155. }
  156. /**
  157. * 提现操作
  158. */
  159. private clickCashOut() {
  160. let path = 'module/redBagCash/redBagCash';
  161. mk.ui.openPanel(path);
  162. }
  163. /**
  164. * 飞coin动画
  165. */
  166. private flyCoinAnim() {
  167. gData.reward.data = [{ rewardType: 3 }]// 测试
  168. let target_node = gData.gameData.gameStyle.icon_hb;
  169. if (gData.reward.data[0].rewardType == RewardType.redBag) {
  170. target_node = gData.gameData.gameStyle.icon_hb;
  171. } else if (gData.reward.data[0].rewardType == RewardType.rmb) {
  172. // target_node = gData.gameData.gameStyle.icon_zb;
  173. } else if (gData.reward.data[0].rewardType == RewardType.pigRmb) {
  174. target_node = gData.gameData.gameStyle.icon_zb;
  175. }
  176. const end_world_pos = target_node.parent.convertToWorldSpaceAR(target_node.getPosition());
  177. const end_node_pos = gData.gameData.gameStyle.node.parent.convertToNodeSpaceAR(end_world_pos);
  178. let start_node_pos = new cc.Vec2(0, -300);
  179. mk.fly.PlayCoinAnim(gData.reward.data[0].rewardType, 10, start_node_pos, end_node_pos)
  180. }
  181. /**
  182. * 下一关操作
  183. */
  184. private clickNextMission() {
  185. }
  186. onDestroy() {
  187. if (this.cur_stage === LidType.none) {
  188. mk.audio.playEffect("rewardClose");
  189. }
  190. }
  191. }