Reward.ts 8.3 KB

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