Reward.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.Sprite })
  37. private node_coin: cc.Sprite = null;
  38. @property({ displayName: '奖励数量', type: cc.Label })
  39. private lbl_reward_value: cc.Label = null;
  40. // 底部提现相关
  41. @property({ displayName: 'spr提现进度', type: cc.Sprite })
  42. private spr_cash_out: cc.Sprite = null;
  43. @property({ displayName: 'lbl提现进度', type: cc.Label })
  44. private lbl_cash_out: cc.Label = null;
  45. @property({ displayName: '提现金额', type: cc.Label })
  46. private lbl_cash: cc.Label = null;
  47. /** 当前阶段 */
  48. private cur_stage: number = LidType.none;
  49. onLoad() {
  50. this.lbl_reward_value.string = '0';
  51. // gData.reward.data = [{ rewardType: 3, rewardNum: 100 }]// 测试数据
  52. }
  53. start() {
  54. /** 盖子 */
  55. this.initLid();
  56. // gData.gameData.gameData.redMoney = 1500;// 测试数据
  57. this.initCashOutStyle(gData.gameData.gameData.redMoney);
  58. }
  59. update(dt) {
  60. if (gData.reward.add_redbag_value) {
  61. this.initCashOutStyle(gData.gameData.gameData.redMoney + gData.reward.add_redbag_value);
  62. }
  63. if (gData.reward.adData) {
  64. // 展示奖励
  65. this.watchVideoCall();
  66. return;
  67. }
  68. }
  69. lateUpdate() {
  70. gData.reward.add_redbag_value = 0;
  71. gData.reward.adData = null;
  72. }
  73. /**
  74. * 初始化封面样式
  75. */
  76. private initLid() {
  77. if (this.lid_type === LidType.none) {// 无盖子样式
  78. this.node_luck.active = false;
  79. this.node_mission.active = false;
  80. this.btn_watch_video.node.active = false;
  81. this.watchVideoCall();
  82. } else if (this.lid_type === LidType.luck) {
  83. this.node_luck.active = true;
  84. this.node_mission.active = false;
  85. this.btn_watch_video.node.active = true;
  86. this.cur_stage = LidType.luck;
  87. mk.audio.playEffect("reward");
  88. } else if (this.lid_type === LidType.mission) {
  89. this.node_luck.active = false;
  90. this.node_mission.active = true;
  91. this.btn_watch_video.node.active = true;
  92. this.cur_stage = LidType.mission;
  93. mk.audio.playEffect("reward");
  94. }
  95. }
  96. /**
  97. * 底部提现相关样式
  98. */
  99. private initCashOutStyle(redMoney: number = 0) {
  100. const cash_bar = gData.redBagCash.cash_bar;
  101. let cash_data = gData.redBagCash.getItemDataByIndex(cash_bar);
  102. if (!cash_data) return;
  103. const newRedMoney = redMoney > cash_data.type_value ? cash_data.type_value : redMoney;
  104. this.lbl_cash.string = cash_data.money / 100 + '元';
  105. this.lbl_cash_out.string = newRedMoney + '/' + cash_data.type_value;
  106. const fillRange = newRedMoney / cash_data.type_value >= 1 ? 1 : newRedMoney / cash_data.type_value;
  107. // this.spr_cash_out.fillRange = fillRange;
  108. cc.tween(this.spr_cash_out).to(fillRange, { fillRange: fillRange }).start();
  109. }
  110. /**
  111. * 看广告
  112. */
  113. private clickWatchVideo() {
  114. cc.log('看广告请求');
  115. if (this.lid_type === LidType.mission) {
  116. // 关卡结算红包
  117. mk.ad.watchAd((success: boolean) => {
  118. mk.console.log("watchAD:" + success);
  119. if (success) {
  120. gData.adData.watchVideo(AdFun.settlement);
  121. }
  122. });
  123. } else {
  124. mk.ad.watchAd((success: boolean) => {
  125. mk.console.log("watchAD:" + success);
  126. if (success) {
  127. gData.adData.watchVideo(gData.reward.subType === 2 ? AdFun.checkpoint : AdFun.bubble);
  128. gData.reward.subType = null;
  129. }
  130. });
  131. }
  132. // gData.reward.adData = {}
  133. // gData.reward.adData.videoRedMoney = {}
  134. // gData.reward.adData.videoRedMoney.videoRewardList = [{ rewardType: 3, rewardNum: 1000 }, { rewardType: 1, rewardNum: 1000 }]// 测试数据
  135. }
  136. /**
  137. * 看广告回调:开奖
  138. */
  139. private async watchVideoCall() {
  140. if (gData.reward.adData) {
  141. // 气泡视频奖励 通关视频奖励
  142. // gData.reward.adData.videoRedMoney.videoRewardList = [{ rewardType: 3, rewardNum: 1000 }, { rewardType: 1, rewardNum: 1000 }]// 测试数据
  143. gData.reward.data = gData.reward.adData.videoRedMoney.videoRewardList;
  144. }
  145. if (!gData.reward.data || !gData.reward.data.length) return;
  146. mk.audio.playEffect("rewardOpen");
  147. this.cur_stage = LidType.none;
  148. // 数据排序:多条奖励,只展示红包,把红包放在首位
  149. const r_count = gData.reward.data.length;
  150. if (r_count >= 2) {
  151. for (let i = 0; i < r_count; i++) {
  152. if (gData.reward.data[i].rewardType === RewardType.redBag) {
  153. let new_data = gData.reward.data.splice(i, 1);
  154. gData.reward.data.unshift(new_data[0]);
  155. break;
  156. }
  157. }
  158. }
  159. // 图标展示
  160. if (gData.reward.data[0].rewardType === RewardType.redBag) {
  161. this.node_coin.spriteFrame = await mk.loader.load('game/texture/coin/' + RewardType.redBag, cc.SpriteFrame);
  162. // this.node_redBag.active = true;
  163. // this.node_pig.active = false;
  164. } else {
  165. this.node_coin.spriteFrame = await mk.loader.load('game/texture/coin/' + gData.reward.data[0].rewardType, cc.SpriteFrame);
  166. // this.node_redBag.active = false;
  167. // this.node_pig.active = true;
  168. }
  169. let rewardNum = gData.reward.data[0].rewardNum || 0;
  170. this.lbl_reward_value.string = `${rewardNum}`;
  171. // 动画
  172. this.anim_open_redbag.play();
  173. this.node_luck.active = false;
  174. this.node_mission.active = false;
  175. this.btn_watch_video.node.active = false;
  176. // 底部进度动画
  177. if (gData.reward.data[0].rewardType === RewardType.redBag) {
  178. gData.reward.add_redbag_value = gData.reward.data[0].rewardNum;
  179. }
  180. }
  181. /**
  182. * 点击获得奖励
  183. */
  184. private clickGetReward() {
  185. cc.log('获取奖励:', gData.reward.data);
  186. gData.reward.flyCoinAnim();
  187. }
  188. /**
  189. * 提现操作
  190. */
  191. private clickCashOut() {
  192. let path = 'module/redBagCash/redBagCash';
  193. mk.ui.openPanel(path);
  194. }
  195. /**
  196. * 下一关操作
  197. */
  198. public clickNextMission() {
  199. GamePlay.Inst.nextLevel();
  200. }
  201. /** 点击返回 */
  202. public clickBack() {
  203. GamePlay.Inst.restart(false);//退出不扣体力
  204. GamePlay.Inst.node.active = false;
  205. }
  206. onDestroy() {
  207. if (this.cur_stage === LidType.none) {
  208. mk.audio.playEffect("rewardClose");
  209. }
  210. }
  211. }