Reward.ts 9.2 KB

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