Reward.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import GamePlay from "../../../before/GamePlay";
  2. import { AdFun } from "../../data/AdData";
  3. import { GameProp, 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. @property({ displayName: '关闭按钮', type: cc.Node })
  43. private btn_close: cc.Node = null;
  44. // 底部提现相关
  45. @property({ displayName: 'spr提现进度', type: cc.Sprite })
  46. private spr_cash_out: cc.Sprite = null;
  47. @property({ displayName: 'lbl提现进度', type: cc.Label })
  48. private lbl_cash_out: cc.Label = null;
  49. @property({ displayName: '提现金额', type: cc.Label })
  50. private lbl_cash: cc.Label = null;
  51. @property({ displayName: '提现按钮', type: cc.Animation })
  52. private node_cash: cc.Animation = null;
  53. /** 当前阶段 */
  54. private cur_stage: number = LidType.none;
  55. onLoad() {
  56. this.hb_star.active = false;
  57. this.lbl_reward_value.string = '0';
  58. // gData.reward.data = [{ rewardType: 3, rewardNum: 100 }]// 测试数据
  59. }
  60. start() {
  61. mk.ad.showBanner();
  62. mk.ad.checkShowInterByChance();
  63. /** 盖子 */
  64. this.initLid();
  65. // gData.gameData.gameData.redMoney = 1500;// 测试数据
  66. this.initCashOutStyle(gData.gameData.gameData.redMoney);
  67. }
  68. update(dt) {
  69. if (gData.reward.add_redbag_value) {
  70. this.initCashOutStyle(gData.gameData.gameData.redMoney + gData.reward.add_redbag_value);
  71. }
  72. if (gData.reward.adData) {
  73. // 展示奖励
  74. this.watchVideoCall();
  75. return;
  76. }
  77. }
  78. lateUpdate() {
  79. gData.reward.add_redbag_value = 0;
  80. gData.reward.adData = null;
  81. }
  82. /**
  83. * 初始化封面样式
  84. */
  85. private initLid() {
  86. if (this.lid_type === LidType.none) {// 无盖子样式
  87. this.node_luck.active = false;
  88. this.node_mission.active = false;
  89. this.btn_watch_video.node.active = false;
  90. this.watchVideoCall();
  91. } else if (this.lid_type === LidType.luck) {
  92. this.node_luck.active = true;
  93. this.node_mission.active = false;
  94. this.btn_watch_video.node.active = true;
  95. this.cur_stage = LidType.luck;
  96. mk.audio.playEffect("reward");
  97. } else if (this.lid_type === LidType.mission) {
  98. this.node_luck.active = false;
  99. this.node_mission.active = true;
  100. this.btn_watch_video.node.active = true;
  101. this.cur_stage = LidType.mission;
  102. mk.audio.playEffect("reward");
  103. }
  104. }
  105. /**
  106. * 底部提现相关样式
  107. */
  108. private initCashOutStyle(redMoney: number = 0) {
  109. const cash_bar = gData.redBagCash.cash_bar;
  110. let cash_data = gData.redBagCash.getItemDataByIndex(cash_bar);
  111. if (!cash_data) return;
  112. const newRedMoney = redMoney > cash_data.type_value ? cash_data.type_value : redMoney;
  113. this.lbl_cash.string = cash_data.money / 100 + '元';
  114. this.lbl_cash_out.string = newRedMoney + '/' + cash_data.type_value;
  115. const fillRange = newRedMoney / cash_data.type_value >= 1 ? 1 : newRedMoney / cash_data.type_value;
  116. // this.spr_cash_out.fillRange = fillRange;
  117. cc.tween(this.spr_cash_out).to(fillRange, { fillRange: fillRange }).start();
  118. if (redMoney >= cash_data.type_value) {
  119. this.node_cash.play();
  120. } else {
  121. this.node_cash.stop();
  122. }
  123. }
  124. /**
  125. * 看广告
  126. */
  127. private async clickWatchVideo() {
  128. cc.log('看广告请求');
  129. if (gData.gameData.gameData.isFirstRedMoney) {
  130. gData.gameData.gameData.isFirstRedMoney = 0;
  131. // 用户第一次看此视频时,可不需要看直接领取奖励
  132. let response = await mk.http.sendData('getFreeRedMoney', {});
  133. if (response.errcode != 0) {
  134. mk.tip.pop(response.errmsg);
  135. return;
  136. }
  137. gData.reward.adData = {}
  138. gData.reward.adData.videoRedMoney = {}
  139. gData.reward.adData.videoRedMoney.videoRewardList = [{ rewardType: RewardType.redBag, rewardNum: response.data.freeRedMoney }]
  140. return;
  141. }
  142. if (this.lid_type === LidType.mission) {
  143. // 关卡结算红包
  144. mk.ad.watchAd((success: boolean) => {
  145. mk.console.log("watchAD:" + success);
  146. if (success) {
  147. gData.adData.watchVideo(AdFun.settlement);
  148. this.clickNextMission();
  149. }
  150. });
  151. } else {
  152. mk.ad.watchAd((success: boolean) => {
  153. mk.console.log("watchAD:" + success);
  154. if (success) {
  155. gData.adData.watchVideo(gData.reward.subType === 2 ? AdFun.checkpoint : AdFun.bubble);
  156. //FC:不需要置空
  157. // gData.reward.subType = null;
  158. }
  159. });
  160. }
  161. // gData.reward.adData = {}
  162. // gData.reward.adData.videoRedMoney = {}
  163. // gData.reward.adData.videoRedMoney.videoRewardList = [{ rewardType: 3, rewardNum: 1000 }, { rewardType: 1, rewardNum: 1000 }]// 测试数据
  164. }
  165. /**
  166. * 看广告回调:开奖
  167. */
  168. private async watchVideoCall() {
  169. if (gData.reward.adData) {
  170. // 气泡视频奖励 通关视频奖励
  171. // gData.reward.adData.videoRedMoney.videoRewardList = [{ rewardType: 3, rewardNum: 1000 }, { rewardType: 1, rewardNum: 1000 }]// 测试数据
  172. gData.reward.data = gData.reward.adData.videoRedMoney.videoRewardList;
  173. }
  174. if (!gData.reward.data || !gData.reward.data.length) return;
  175. this.btn_close && (this.btn_close.active = false);
  176. mk.audio.playEffect("rewardOpen");
  177. this.hb_star.active = true;
  178. this.hb_star.getComponent(sp.Skeleton).animation = 'animation';
  179. this.hb_star.getComponent(sp.Skeleton).loop = false;
  180. this.cur_stage = LidType.none;
  181. // 数据排序:多条奖励,只展示红包,把红包放在首位
  182. const r_count = gData.reward.data.length;
  183. if (r_count >= 2) {
  184. for (let i = 0; i < r_count; i++) {
  185. if (gData.reward.data[i].rewardType === RewardType.redBag) {
  186. let new_data = gData.reward.data.splice(i, 1);
  187. gData.reward.data.unshift(new_data[0]);
  188. break;
  189. }
  190. }
  191. }
  192. // 图标展示
  193. if (gData.reward.data[0].rewardType === RewardType.redBag) {
  194. this.node_coin.spriteFrame = await mk.loader.load('game/texture/coin/' + RewardType.redBag, cc.SpriteFrame);
  195. // this.node_redBag.active = true;
  196. // this.node_pig.active = false;
  197. } else {
  198. this.node_coin.spriteFrame = await mk.loader.load('game/texture/coin/' + gData.reward.data[0].rewardType, cc.SpriteFrame);
  199. // this.node_redBag.active = false;
  200. // this.node_pig.active = true;
  201. }
  202. let rewardNum: number = gData.reward.data[0].rewardNum || 0;
  203. if (gData.reward.data[0].rewardType === RewardType.pigRmb) {
  204. this.lbl_reward_value.string = (rewardNum / 100).toFixed(2) + '元';
  205. } else {
  206. this.lbl_reward_value.string = `${rewardNum}`;
  207. }
  208. // 动画
  209. this.anim_open_redbag.play();
  210. this.node_luck.active = false;
  211. this.node_mission.active = false;
  212. this.btn_watch_video.node.active = false;
  213. // 底部进度动画
  214. if (gData.reward.data[0].rewardType === RewardType.redBag) {
  215. gData.reward.add_redbag_value = gData.reward.data[0].rewardNum;
  216. }
  217. }
  218. /**
  219. * 点击获得奖励
  220. */
  221. private clickGetReward() {
  222. cc.log('获取奖励:', gData.reward.data);
  223. gData.reward.flyCoinAnim();
  224. this.onClickClose();
  225. if(gData.gameData.getProp(GameProp.videoTimes) == 0){
  226. mk.data.sendXYEvent("guide_receive","新手引导领取2500红包币");
  227. }
  228. }
  229. /**
  230. * 提现操作
  231. */
  232. private clickCashOut() {
  233. gData.reward.addReward();
  234. mk.ui.closePanel(this.node.name);
  235. let path = 'module/redBagCash/redBagCash';
  236. mk.ui.openPanel(path);
  237. }
  238. /**
  239. * 下一关操作
  240. */
  241. public clickNextMission() {
  242. GamePlay.Inst.nextLevel();
  243. }
  244. /** 点击返回 */
  245. public clickBack() {
  246. GamePlay.Inst.restart();//退出不扣体力
  247. GamePlay.Inst.node.active = false;
  248. }
  249. onDestroy() {
  250. if (this.cur_stage === LidType.none) {
  251. mk.audio.playEffect("rewardClose");
  252. }
  253. }
  254. onClickClose() {
  255. mk.ad.destoryBanner();
  256. //如果是关卡消除红包 关闭之后则清除
  257. if (gData.reward.subType == 2) {
  258. console.log("清除积分啦啦啦啦啦阿拉拉拉啦--------------------------------", GamePlay.Inst.ifPass)
  259. gData.gameData.setProp(GameProp.curTotalScore, 0);
  260. GamePlay.Inst.curProgressScore = 0;
  261. GamePlay.Inst.curGetScore = 0;
  262. GamePlay.Inst.finalGetScore = 0;
  263. GamePlay.Inst.initScore();
  264. }
  265. if (GamePlay.Inst) {
  266. if (GamePlay.Inst.ifPass) {
  267. GamePlay.Inst.gameCount();
  268. }
  269. else {
  270. mk.ad.checkShowInterByChance();
  271. }
  272. }
  273. else {
  274. mk.ad.checkShowInterByChance();
  275. }
  276. }
  277. }