Reward.ts 11 KB

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