Turnable.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import BtnClosePanel from "../../component/BtnClosePanel";
  2. import { AdFun } from "../../data/AdData";
  3. import { VideoAdType } from "../../data/GameData";
  4. import TurnableItem from "./TurnableItem";
  5. const { ccclass, property } = cc._decorator;
  6. /**
  7. * 转盘
  8. * @author 邹勇 kaka
  9. */
  10. @ccclass
  11. export default class Turnable extends cc.Component {
  12. @property(cc.Node)
  13. node_turn: cc.Node = null;
  14. @property(cc.Node)
  15. node_items: cc.Node[] = [];
  16. @property(cc.Node)
  17. btn_draw: cc.Node = null;
  18. @property(cc.Sprite)
  19. btn_close: cc.Sprite = null;
  20. @property(cc.RichText)
  21. lbl_left: cc.RichText = null;
  22. @property(cc.ParticleSystem)
  23. starAni: cc.ParticleSystem = null;
  24. @property(sp.Skeleton)
  25. baozhaAni: sp.Skeleton = null;
  26. private audioId: number;
  27. private endAngle = 0
  28. private startDraw = false
  29. private slowDis = 720
  30. private slowAngle = 0
  31. private perAdd = 10
  32. private perMinus = 0
  33. private rewardWeights: number[];
  34. onLoad() {
  35. mk.ad.bannerOperOnlyFullScreen(0);
  36. this.baozhaAni.node.active = false;
  37. this.initData();
  38. }
  39. /**
  40. * 初始化奖励和抽奖次数
  41. */
  42. private initData() {
  43. this.rewardWeights = [0, 0, 0];
  44. if (gData.turnable.config) {
  45. for (let i = 0; i < gData.turnable.config.length; i++) {
  46. let sc = this.node_items[i].getComponent(TurnableItem);
  47. let type = parseInt(gData.turnable.config[i].type);
  48. this.rewardWeights[type - 1]++;
  49. sc.initData(type);
  50. }
  51. }
  52. gData.turnable.requestData();
  53. this.updateLeftTimes();
  54. }
  55. /** 点击抽奖 */
  56. clickDraw() {
  57. if (gData.turnable.leftTimes > 0) {
  58. mk.ad.videoAdType = VideoAdType.Turntable;
  59. mk.ad.watchAd((success: boolean,request_id: string) => {
  60. mk.console.log("watchAD:" + success);
  61. if (success) {
  62. gData.adData.watchVideo(AdFun.turntable,request_id);
  63. }
  64. });
  65. }
  66. }
  67. private async play(type) {
  68. this.audioId = await mk.audio.playEffect("turnableplay", true);
  69. this.btn_close.getComponent(BtnClosePanel).block_click = true;
  70. this.perAdd = 10;
  71. this.node_turn.angle += 360 * 5;
  72. let index = 0;
  73. let i = 0;
  74. while (i < this.rewardWeights.length - 1) {
  75. if (i < type - 1) {
  76. index += this.rewardWeights[i];
  77. i++;
  78. }
  79. else {
  80. index += Math.floor(Math.random() * this.rewardWeights[i]);
  81. break;
  82. }
  83. }
  84. this.endAngle = index * -60;
  85. this.slowAngle = this.endAngle + this.slowDis;
  86. this.perMinus = this.perAdd / (this.slowDis / (this.perAdd * 0.5));
  87. this.startDraw = true;
  88. this.starAni.resetSystem()
  89. this.starAni.node.active = true
  90. // this.node_turn.angle = 1800;
  91. // let tar = index * 60;
  92. // cc.tween(this.node_turn)
  93. // .to(3, { angle: tar })
  94. // .call(this.playOver, this).start();
  95. }
  96. private async playOver() {
  97. mk.audio.stopEffect(this.audioId);
  98. this.btn_close.getComponent(BtnClosePanel).block_click = false;
  99. mk.audio.playEffect("turnplateDrawEnd");
  100. this.updateLeftTimes();
  101. await mk.time.WaitForSeconds(0.7);
  102. mk.ui.openPanel("module/reward/reward");
  103. }
  104. updateLeftTimes() {
  105. this.lbl_left.string = `剩余:<color=#FFED99>${gData.turnable.leftTimes}</color>次`;
  106. }
  107. update() {
  108. if (gData.turnable.requestFinish) {
  109. this.updateLeftTimes();
  110. gData.turnable.requestFinish = false;
  111. }
  112. if (gData.turnable.adData) {
  113. if (gData.turnable.adData && gData.turnable.adData.videoRedMoney) {
  114. let type = gData.turnable.adData.videoRedMoney.redMoneyType;
  115. this.play(type);
  116. gData.reward.data = gData.turnable.adData.videoRedMoney.videoRewardList;
  117. }
  118. gData.turnable.adData = null;
  119. }
  120. if (this.startDraw) {
  121. this.node_turn.angle -= this.perAdd;
  122. //开始减速
  123. if (this.node_turn.angle <= this.slowAngle) {
  124. if (this.perAdd <= this.perMinus * 4) {
  125. this.perAdd -= this.perMinus * 0.01;
  126. }
  127. else {
  128. this.perAdd -= this.perMinus;
  129. }
  130. }
  131. if (this.node_turn.angle <= this.endAngle) {
  132. this.startDraw = false;
  133. this.starAni.node.active = false
  134. this.baozhaAni.node.active = true
  135. this.baozhaAni.setAnimation(0, 'animation', false)
  136. this.playOver();
  137. }
  138. }
  139. }
  140. onDestroy() {
  141. }
  142. onClickClose() {
  143. mk.ad.bannerOperOnlyFullScreen(1);
  144. mk.ad.checkShowInterByChance();
  145. }
  146. }