LevelRedPacketUI.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import PlayerConst from "../data/PlayerConst";
  8. import DataMgr from "../mgr/DataMgr";
  9. import EffectMgr from "../mgr/EffectMgr";
  10. import GameMgr, { UI_NAME } from "../mgr/GameMgr";
  11. import HttpMgr from "../mgr/HttpMgr";
  12. import Util from "../util/Util";
  13. const { ccclass, property } = cc._decorator;
  14. @ccclass
  15. export default class LevelRedPacketUI extends cc.Component {
  16. @property(cc.Node)
  17. node_rectBg: cc.Node = null;
  18. @property(cc.Label)
  19. label_redPacketCashNum: cc.Label = null;
  20. @property(cc.Node)
  21. node_unit: cc.Node = null;
  22. @property(cc.Label)
  23. label_playerCashNum: cc.Label = null;
  24. @property(cc.Node)
  25. node_info: cc.Node = null;
  26. @property(cc.Node)
  27. node_doubleGetBtn: cc.Node = null;
  28. @property(cc.Node)
  29. node_getBtn: cc.Node = null;
  30. public cashNum: number = 0;
  31. public eliminateNum: number = 4;
  32. // LIFE-CYCLE CALLBACKS:
  33. // onLoad () {}
  34. start() {
  35. this.adapt();
  36. this.initEvent();
  37. }
  38. // update (dt) {}
  39. onEnable() {
  40. GameMgr.Inst.sendEvent(UI_NAME.LevelRedPacketUI, "进入关卡红包界面");
  41. // //显示广告
  42. // AdMgr.Inst.showNativeAd(4, true);
  43. mk.audio.playEffect("ef_redPacket_open");
  44. }
  45. onDisable() {
  46. // //
  47. // AdMgr.Inst.destroyNativeAd();
  48. }
  49. //适配
  50. adapt() {
  51. this.node_rectBg.setContentSize(cc.winSize.width, cc.winSize.height);
  52. }
  53. /**初始化
  54. * @param eliminateNum 消除数目 根据消除数目来
  55. */
  56. init(eliminateNum: number) {
  57. mk.console.log("eliminateNum", eliminateNum);
  58. this.eliminateNum = eliminateNum;
  59. this.initPlayerCashNum();
  60. this.initRedPacketCashNum();
  61. this.showInfo();
  62. }
  63. /**初始化事件 */
  64. initEvent() {
  65. this.node_doubleGetBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
  66. this.node_getBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
  67. }
  68. /**显示 */
  69. showInfo() {
  70. this.node_info.y = -180;
  71. cc.tween(this.node_info).to(0.5, { y: 15 }).start();
  72. }
  73. /**初始化当前玩家获取比 */
  74. initPlayerCashNum() {
  75. // let num = Math.round((PlayerConst.cashNum * 100)) / 100;
  76. let num = Util.numberFixed(PlayerConst.cashNum, 2);
  77. this.label_playerCashNum.string = `当前余额:${num} 元`;
  78. }
  79. /**初始化关卡随机红包 */
  80. initRedPacketCashNum() {
  81. if (this.eliminateNum < 3) {
  82. this.eliminateNum = 3;
  83. }
  84. let randomCashNum = (Math.floor(Math.random() * (this.eliminateNum - 3)) + 1) * 0.01
  85. this.cashNum = Util.numberFixed(randomCashNum, 2);
  86. this.label_redPacketCashNum.string = this.cashNum.toString();
  87. }
  88. /**点击 */
  89. onClick(event: cc.Event.EventTouch) {
  90. switch (event.currentTarget) {
  91. case this.node_doubleGetBtn:
  92. this.onClickDoubleGetBtn();
  93. break;
  94. case this.node_getBtn:
  95. this.onClickGetBtn();
  96. break;
  97. }
  98. }
  99. /**点击获取 */
  100. onClickGetBtn() {
  101. GameMgr.Inst.sendEvent(UI_NAME.LevelRedPacketUI, `点击普通领取${this.cashNum}元`);
  102. this.getCash(1);
  103. }
  104. /**点击双倍领取 */
  105. onClickDoubleGetBtn() {
  106. GameMgr.Inst.sendEvent(UI_NAME.LevelRedPacketUI, `点击双倍领取${this.cashNum}元`);
  107. // AdMgr.Inst.watchAd(VIDEOTYPE.LevelRedPacket, (ifsuccess) => { this, this.watchCallBack(ifsuccess) });
  108. }
  109. /**观看回调 */
  110. watchCallBack(ifSuccess: boolean = false) {
  111. if (ifSuccess) {
  112. mk.console.log("观看成功")
  113. this.watchSuccess();
  114. }
  115. else {
  116. mk.console.log("观看失败")
  117. this.watchFailed();
  118. }
  119. }
  120. /**观看成功 */
  121. watchSuccess() {
  122. GameMgr.Inst.sendEvent(UI_NAME.LevelRedPacketUI, `视频双倍领取${this.cashNum}元成功`);
  123. EffectMgr.Inst.addTip("观看视频成功,成功双倍领取");
  124. this.getCash(2);
  125. }
  126. /**观看失败 */
  127. watchFailed() {
  128. GameMgr.Inst.sendEvent(UI_NAME.LevelRedPacketUI, `视频双倍领取${this.cashNum}元失败`);
  129. // EffectMgr.Inst.addTip("观看视频失败,请稍后再试");
  130. }
  131. /**
  132. * 获取体力
  133. * @param multiple 倍数 默认是1
  134. */
  135. getCash(multiple: number = 1) {
  136. mk.console.log("multiple", multiple);
  137. DataMgr.Inst.updateMoneyNum(this.cashNum * multiple)
  138. let get_type = multiple === 1 ? 0 : 1;
  139. HttpMgr.Inst.randomRedPacket(get_type, PlayerConst.levelNum, this.cashNum).then((data) => {
  140. EffectMgr.Inst.addTip("观看视频成功,成功领取双倍红包");
  141. mk.console.log("randomRedPacket data", data);
  142. }).catch((err) => {
  143. });
  144. mk.ui.closePanel("LevelRedPacketUI");
  145. }
  146. }