Reward.ts 8.4 KB

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