Turnable.ts 4.8 KB

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