Turnable.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. this.baozhaAni.node.active = false;
  36. this.initData();
  37. }
  38. /**
  39. * 初始化奖励和抽奖次数
  40. */
  41. private initData() {
  42. this.rewardWeights = [0, 0, 0];
  43. if (gData.turnable.config) {
  44. for (let i = 0; i < gData.turnable.config.length; i++) {
  45. let sc = this.node_items[i].getComponent(TurnableItem);
  46. let type = parseInt(gData.turnable.config[i].type);
  47. this.rewardWeights[type - 1]++;
  48. sc.initData(type);
  49. }
  50. }
  51. gData.turnable.requestData();
  52. this.updateLeftTimes();
  53. }
  54. /** 点击抽奖 */
  55. clickDraw() {
  56. if (gData.turnable.leftTimes > 0) {
  57. mk.ad.videoAdType = VideoAdType.Turntable;
  58. mk.ad.watchAd((success: boolean) => {
  59. mk.console.log("watchAD:" + success);
  60. if (success) {
  61. gData.adData.watchVideo(AdFun.turntable);
  62. }
  63. });
  64. }
  65. }
  66. private async play(type) {
  67. this.audioId = await mk.audio.playEffect("turnableplay", true);
  68. this.btn_close.getComponent(BtnClosePanel).block_click = true;
  69. this.perAdd = 10;
  70. this.node_turn.angle += 360 * 5;
  71. let index = 0;
  72. let i = 0;
  73. while (i < this.rewardWeights.length - 1) {
  74. if (i < type - 1) {
  75. index += this.rewardWeights[i];
  76. i++;
  77. }
  78. else {
  79. index += Math.floor(Math.random() * this.rewardWeights[i]);
  80. break;
  81. }
  82. }
  83. this.endAngle = index * -60;
  84. this.slowAngle = this.endAngle + this.slowDis;
  85. this.perMinus = this.perAdd / (this.slowDis / (this.perAdd * 0.5));
  86. this.startDraw = true;
  87. this.starAni.resetSystem()
  88. this.starAni.node.active = true
  89. // this.node_turn.angle = 1800;
  90. // let tar = index * 60;
  91. // cc.tween(this.node_turn)
  92. // .to(3, { angle: tar })
  93. // .call(this.playOver, this).start();
  94. }
  95. private async playOver() {
  96. mk.audio.stopEffect(this.audioId);
  97. this.btn_close.getComponent(BtnClosePanel).block_click = false;
  98. mk.audio.playEffect("turnplateDrawEnd");
  99. this.updateLeftTimes();
  100. await mk.time.WaitForSeconds(0.7);
  101. mk.ui.openPanel("module/reward/reward");
  102. }
  103. updateLeftTimes() {
  104. this.lbl_left.string = `剩余:<color=#FFED99>${gData.turnable.leftTimes}</color>次`;
  105. }
  106. update() {
  107. if (gData.turnable.requestFinish) {
  108. this.updateLeftTimes();
  109. gData.turnable.requestFinish = false;
  110. }
  111. if (gData.turnable.adData) {
  112. if (gData.turnable.adData && gData.turnable.adData.videoRedMoney) {
  113. let type = gData.turnable.adData.videoRedMoney.redMoneyType;
  114. this.play(type);
  115. gData.reward.data = gData.turnable.adData.videoRedMoney.videoRewardList;
  116. }
  117. gData.turnable.adData = null;
  118. }
  119. if (this.startDraw) {
  120. this.node_turn.angle -= this.perAdd;
  121. //开始减速
  122. if (this.node_turn.angle <= this.slowAngle) {
  123. if (this.perAdd <= this.perMinus * 4) {
  124. this.perAdd -= this.perMinus * 0.01;
  125. }
  126. else {
  127. this.perAdd -= this.perMinus;
  128. }
  129. }
  130. if (this.node_turn.angle <= this.endAngle) {
  131. this.startDraw = false;
  132. this.starAni.node.active = false
  133. this.baozhaAni.node.active = true
  134. this.baozhaAni.setAnimation(0, 'animation', false)
  135. this.playOver();
  136. }
  137. }
  138. }
  139. onDestroy() {
  140. }
  141. onClickClose() {
  142. mk.ad.checkShowInterByChance();
  143. }
  144. }