RedPacketUI.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import DataMgr from "../mgr/DataMgr";
  2. import PlayerConst, { DATA_STORAGE_KEY } from "../data/PlayerConst";
  3. import HttpMgr from "../mgr/HttpMgr";
  4. import EffectMgr from "../mgr/EffectMgr";
  5. import GameMgr, { UI_NAME } from "../mgr/GameMgr";
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class RedPacketUI extends cc.Component {
  9. @property(cc.Node)
  10. node_rectBg: cc.Node = null;
  11. @property(cc.Node)
  12. node_unopen: cc.Node = null;
  13. @property(cc.Node)
  14. node_top: cc.Node = null;
  15. @property(cc.Node)
  16. node_bottom: cc.Node = null;
  17. @property(cc.Label)
  18. label_redPacketName_open: cc.Label = null;
  19. @property(cc.Node)
  20. node_openBtn: cc.Node = null;
  21. @property(cc.Animation)
  22. ani_openBtn: cc.Animation = null;
  23. @property(cc.Node)
  24. node_closeBtn: cc.Node = null;
  25. @property(cc.Node)
  26. node_open: cc.Node = null;
  27. @property(cc.Node)
  28. node_cashInfo: cc.Node = null;
  29. @property(cc.Label)
  30. label_redPacketName_unopen: cc.Label = null;
  31. @property(cc.Label)
  32. label_redPacketTip: cc.Label = null;
  33. @property(cc.Label)
  34. label_cashNum: cc.Label = null;
  35. @property(cc.Node)
  36. node_cashOutBtn: cc.Node = null;
  37. @property(cc.Node)
  38. node_getCashBtn: cc.Node = null;
  39. @property(cc.Node)
  40. node_doubleCashBtn: cc.Node = null;
  41. public top_oirgin_y: number;
  42. public top_scale_size: number;
  43. public bottom_origin_y: number;
  44. public bottom_scale_size: number;
  45. public cashNum: number = 0.1;
  46. public getCallBacK: Function = null;
  47. // LIFE-CYCLE CALLBACKS:
  48. onLoad() {
  49. this.top_oirgin_y = this.node_top.y;
  50. this.bottom_origin_y = this.node_bottom.y;
  51. this.top_scale_size = (cc.winSize.width + 20) / this.node_top.width;
  52. this.bottom_scale_size = cc.winSize.width / this.node_bottom.width;
  53. }
  54. start() {
  55. this.node_rectBg.setContentSize(cc.winSize.width, cc.winSize.height);
  56. this.initView();
  57. this.initEvent();
  58. }
  59. // update (dt) {}
  60. onEnable() {
  61. GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "进入过关红包界面");
  62. //播放红包来了音效
  63. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.redPacket_come);
  64. mk.audio.playEffect("ef_redPacket_come");
  65. mk.console.log("设置红包来了1!!!!!!!!!!!!!!!!!!!!!");
  66. this.node_closeBtn.active = false;
  67. let timeout = setTimeout(() => {
  68. this.node_closeBtn.active = true;
  69. clearTimeout(timeout);
  70. },800);
  71. }
  72. onDisable() {
  73. this.initView();
  74. }
  75. /**初始化
  76. * @param 红包名字
  77. * @param 红包现金数目
  78. * @param 回调
  79. * @param 提示
  80. */
  81. init(readPacketName: string, cashNum: number, getCallBack: Function = null, tip: string = null) {
  82. this.getCallBacK = getCallBack;
  83. this.cashNum = cashNum;
  84. this.label_redPacketName_open.string = readPacketName;
  85. this.label_redPacketName_unopen.string = readPacketName;
  86. this.label_cashNum.string = cashNum.toString();
  87. if (tip) {
  88. this.label_redPacketTip.string = tip.toString();
  89. this.label_redPacketTip.node.active = true;
  90. }
  91. else {
  92. this.label_redPacketTip.node.active = false;
  93. }
  94. }
  95. /**初始化视图 */
  96. initView() {
  97. this.node_open.active = false;
  98. this.node_unopen.active = true;
  99. this.node_top.y = this.top_oirgin_y;
  100. this.node_top.scaleX = this.node_top.scaleY = 1;
  101. this.node_bottom.y = this.bottom_origin_y;
  102. this.node_bottom.scaleX = this.node_bottom.scaleX = 1;
  103. this.node_openBtn.active = true;
  104. this.node_closeBtn.active = false;
  105. this.node_rectBg.opacity = 50;
  106. }
  107. /**初始化事件 */
  108. initEvent() {
  109. this.node_closeBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
  110. this.node_openBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
  111. this.node_getCashBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
  112. this.node_doubleCashBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
  113. this.node_cashOutBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
  114. }
  115. onClick(event: cc.Event.EventTouch) {
  116. switch (event.currentTarget) {
  117. case this.node_closeBtn:
  118. this.onClickClose();
  119. break;
  120. case this.node_openBtn:
  121. this.onClickOpenBtn();
  122. break;
  123. case this.node_getCashBtn:
  124. this.onClickGetCash();
  125. break;
  126. case this.node_doubleCashBtn:
  127. this.onClickDoubelCash();
  128. break;
  129. case this.node_cashOutBtn:
  130. this.onClickCashOutBtn();
  131. break;
  132. }
  133. }
  134. /**点击关闭 */
  135. onClickClose() {
  136. GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "点击关闭按钮");
  137. // this.node.active = false;
  138. mk.ui.closePanel("RedPacketUI");
  139. if (this.getCallBacK) {
  140. this.getCallBacK();
  141. }
  142. }
  143. /**点击领取双倍 */
  144. onClickOpenBtn() {
  145. GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "点击打开过关红包按钮");
  146. // AdMgr.Inst.watchAd(VIDEOTYPE.OpenRedPacket, (ifsuccess) => { this, this.watchCallBack(ifsuccess) });
  147. }
  148. /**观看回调 */
  149. watchCallBack(ifSuccess: boolean = false) {
  150. if (ifSuccess) {
  151. mk.console.log("观看成功")
  152. this.watchSuccess();
  153. }
  154. else {
  155. mk.console.log("观看失败")
  156. this.watchFailed();
  157. }
  158. }
  159. /**观看成功 */
  160. watchSuccess() {
  161. GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "视频打开过关红包成功");
  162. this.onClickOpen();
  163. }
  164. /**观看失败 */
  165. watchFailed() {
  166. GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "视频打开过关红包失败");
  167. // EffectMgr.Inst.addTip("观看视频失败,请稍后再试");
  168. }
  169. /**点击打开 */
  170. onClickOpen() {
  171. this.ani_openBtn.play("ani_openRedPacket");
  172. this.scheduleOnce(() => {
  173. this.ani_openBtn.play();
  174. this.node_openBtn.active = false;
  175. this.node_closeBtn.active = false;
  176. // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.redPacket_open);
  177. mk.audio.playEffect("ef_redPacket_open");
  178. cc.tween(this.node_rectBg).to(0.2, { opacity: 255 }).call(() => {
  179. this.node_open.active = true;
  180. this.node_cashInfo.scaleX = this.node_cashInfo.scaleY = 1.1;
  181. this.node_getCashBtn.active = false;
  182. let timeOut = setTimeout(() => {
  183. clearTimeout(timeOut);
  184. this.node_getCashBtn.active = true;
  185. }, 1500)
  186. }).start();
  187. let top_end_height = this.node_top.height * this.top_scale_size;
  188. let top_end_y = cc.winSize.height * 0.5 + top_end_height * 0.5 - 150;
  189. cc.tween(this.node_top).to(0.3, { scaleX: this.top_scale_size, scaleY: this.top_scale_size, y: top_end_y }).call(() => {
  190. cc.tween(this.node_cashInfo).to(0.2, { scaleX: 1, scaleY: 1 }).call(() => { }).start();
  191. }).start();
  192. let bottom_end_height = this.node_top.height * this.bottom_scale_size;
  193. let bottom_end_y = -(cc.winSize.height * 0.5 + bottom_end_height * 0.5);
  194. cc.tween(this.node_bottom).to(0.3, { scaleX: this.bottom_scale_size, scaleY: this.bottom_scale_size, y: bottom_end_y }).call(() => {
  195. }).start();
  196. }, 0.9)
  197. }
  198. onClickCashOutBtn() {
  199. GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "去提现");
  200. mk.ui.openPanel("CashOutUI");
  201. // Main.Inst.node_game.active = true;
  202. }
  203. /**点击领取 */
  204. onClickGetCash() {
  205. GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "点击普通领取");
  206. this.getCash(1);
  207. }
  208. /**点击领取双倍 */
  209. onClickDoubelCash() {
  210. GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "点击双倍领取");
  211. // AdMgr.Inst.watchAd(VIDEOTYPE.PassRedPacket, (ifsuccess) => { this, this.watchDoubleCallBack(ifsuccess) });
  212. }
  213. /**观看回调 */
  214. watchDoubleCallBack(ifSuccess: boolean = false) {
  215. if (ifSuccess) {
  216. mk.console.log("观看成功")
  217. this.watchDoubleSuccess();
  218. }
  219. else {
  220. mk.console.log("观看失败")
  221. this.watchDoubleFailed();
  222. }
  223. }
  224. /**观看成功 */
  225. watchDoubleSuccess() {
  226. GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "双倍领取过关红包成功");
  227. this.getCash(2);
  228. }
  229. /**观看失败 */
  230. watchDoubleFailed() {
  231. GameMgr.Inst.sendEvent(UI_NAME.RedPacketUI, "双倍领取过关红包失败");
  232. // EffectMgr.Inst.addTip("观看视频失败,请稍后再试");
  233. }
  234. /**是否获取红包 */
  235. getCash(multiple: number = 1) {
  236. // this.node.active = false;
  237. mk.ui.closePanel("RedPacketUI");
  238. PlayerConst.todayPassRedPacketNum++;
  239. DataMgr.Inst.updateTodayPassRedPacketNum(PlayerConst.todayPassRedPacketNum);
  240. DataMgr.Inst.updateMoneyNum(this.cashNum * multiple);
  241. let get_type = multiple === 1 ? 0 : 1;
  242. HttpMgr.Inst.passRedPacket(get_type, PlayerConst.levelNum, this.cashNum).then((data) => {
  243. mk.console.log("passRedPacket data", data);
  244. EffectMgr.Inst.addTip("观看视频成功,成功领取双倍红包");
  245. DataMgr.Inst.updateMoneyNum(this.cashNum);
  246. }).catch((err) => {
  247. });
  248. if (this.getCallBacK) {
  249. this.getCallBacK();
  250. }
  251. }
  252. }
  253. export enum REDPACKETTYPE {
  254. /**新手 */
  255. NewPlayer = 0,
  256. /**过关红包 */
  257. Pass = 1,
  258. /** */
  259. }