Turnable.ts 5.9 KB

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