Reward.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 (this.lid_type === LidType.mission) {
  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. mk.ad.watchAd((success: boolean) => {
  121. mk.console.log("watchAD:" + success);
  122. if (success) {
  123. gData.adData.watchVideo(gData.reward.subType === 2 ? AdFun.checkpoint : AdFun.bubble);
  124. gData.reward.subType = null;
  125. }
  126. });
  127. }
  128. }
  129. /**
  130. * 看广告回调:开奖
  131. */
  132. private async watchVideoCall() {
  133. if (gData.reward.adData) {
  134. // 气泡视频奖励 通关视频奖励
  135. gData.reward.data = gData.reward.adData.videoRedMoney.videoRewardList;
  136. }
  137. if (!gData.reward.data || !gData.reward.data.length) return;
  138. mk.audio.playEffect("rewardOpen");
  139. this.cur_stage = LidType.none;
  140. // 数据排序:多条奖励,只展示红包,把红包放在首位
  141. const r_count = gData.reward.data.length;
  142. if (r_count >= 2) {
  143. for (let i = 0; i < r_count; i++) {
  144. if (gData.reward.data[i].rewardType === RewardType.redBag) {
  145. let new_data = gData.reward.data.splice(i, 1);
  146. gData.reward.data.unshift(new_data[0]);
  147. break;
  148. }
  149. }
  150. }
  151. // 图标展示
  152. if (gData.reward.data[0].rewardType === RewardType.redBag) {
  153. this.node_coin.spriteFrame = await mk.loader.load('game/texture/coin/' + RewardType.redBag, cc.SpriteFrame);
  154. // this.node_redBag.active = true;
  155. // this.node_pig.active = false;
  156. } else {
  157. this.node_coin.spriteFrame = await mk.loader.load('game/texture/coin/' + gData.reward.data[0].rewardType, cc.SpriteFrame);
  158. // this.node_redBag.active = false;
  159. // this.node_pig.active = true;
  160. }
  161. let rewardNum = gData.reward.data[0].rewardNum || 0;
  162. this.lbl_reward_value.string = `${rewardNum}`;
  163. // 动画
  164. this.anim_open_redbag.play();
  165. this.node_luck.active = false;
  166. this.node_mission.active = false;
  167. this.btn_watch_video.node.active = false;
  168. }
  169. /**
  170. * 点击获得奖励
  171. */
  172. private clickGetReward() {
  173. cc.log('获取奖励:', gData.reward.data);
  174. gData.reward.addReward();
  175. this.flyCoinAnim();
  176. gData.reward.data = [];
  177. }
  178. /**
  179. * 提现操作
  180. */
  181. private clickCashOut() {
  182. let path = 'module/redBagCash/redBagCash';
  183. mk.ui.openPanel(path);
  184. }
  185. /**
  186. * 飞coin动画
  187. */
  188. private flyCoinAnim() {
  189. if (!gData.reward.data || !gData.reward.data.length) return;
  190. let target_node = gData.gameData.gameStyle.icon_hb;
  191. if (gData.reward.data[0].rewardType == RewardType.redBag) {
  192. target_node = gData.gameData.gameStyle.icon_hb;
  193. } else if (gData.reward.data[0].rewardType == RewardType.rmb) {
  194. // target_node = gData.gameData.gameStyle.icon_zb;
  195. } else if (gData.reward.data[0].rewardType == RewardType.pigRmb) {
  196. target_node = gData.gameData.gameStyle.icon_zb;
  197. }
  198. const end_world_pos = target_node.parent.convertToWorldSpaceAR(target_node.getPosition());
  199. const end_node_pos = gData.gameData.gameStyle.node.parent.convertToNodeSpaceAR(end_world_pos);
  200. let start_node_pos = new cc.Vec2(0, -300);
  201. mk.fly.PlayCoinAnim(gData.reward.data[0].rewardType, 10, start_node_pos, end_node_pos)
  202. }
  203. /**
  204. * 下一关操作
  205. */
  206. public clickNextMission() {
  207. GamePlay.Inst.nextLevel();
  208. }
  209. /** 点击返回 */
  210. public clickBack() {
  211. GamePlay.Inst.restart(false);//退出不扣体力
  212. GamePlay.Inst.node.active = false;
  213. }
  214. onDestroy() {
  215. if (this.cur_stage === LidType.none) {
  216. mk.audio.playEffect("rewardClose");
  217. }
  218. }
  219. }