Turnable.ts 4.1 KB

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