Reward.ts 7.3 KB

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