Turnable.ts 6.4 KB

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