Reward.ts 12 KB

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