MouseWinView.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { MouseConst } from "./MouseConst";
  2. const { ccclass, property, menu } = cc._decorator;
  3. const delayFinishTime: number = 0.2;
  4. const startAudioTime = 17;
  5. const endAudioTime = 8;
  6. @ccclass
  7. @menu("Game/Mouse/MouseWinView")
  8. export default class MouseWinView extends cc.Component {
  9. @property(cc.Node)
  10. winNode: cc.Node = null;
  11. @property(sp.Skeleton)
  12. coinSke: sp.Skeleton = null;
  13. @property(sp.Skeleton)
  14. bigWinSke: sp.Skeleton = null;
  15. @property(cc.Sprite)
  16. winTitle: cc.Sprite;
  17. @property(cc.Label)
  18. winLabel: cc.Label = null;
  19. @property([cc.SpriteFrame])
  20. winTitleImgs: cc.SpriteFrame[] = [];
  21. @property({ type: cc.Node, tooltip: '立即完成滚动', displayName: '完成得分滚动' })
  22. finishNode: cc.Node = null;
  23. @property({ type: cc.Node, displayName: '关闭winView' })
  24. touchNode: cc.Node = null;
  25. private _callback: Function = null;
  26. private _winScore: number = 0;
  27. private _bigWinStartAudioId: number = 0;
  28. private _bigWinEndAudioId: number = 0;
  29. private _isBigWin: boolean = false;//是否大奖
  30. private _winType: number = -1;
  31. private bigWinMultiple: number[];
  32. start() {
  33. this.resetView();
  34. this.finishNode.setContentSize(cc.winSize);
  35. this.touchNode.setContentSize(cc.winSize); //这个在结算的下面
  36. }
  37. winFinish() {
  38. if (this._callback) {
  39. this._callback?.();
  40. this._callback = null;
  41. this.finishNode.active = false;
  42. this._bigWinStartAudioId && AudioPlayer.stopEffect(this._bigWinStartAudioId);
  43. if (this._isBigWin) {
  44. AudioPlayer.playEffect(MouseConst.Sound.bigWinEnd, false, 1, false, (audioID: number) => {
  45. this._bigWinEndAudioId = audioID;
  46. });
  47. this.scheduleOnce(() => {
  48. this.resetView();
  49. }, endAudioTime);
  50. }
  51. }
  52. }
  53. showWinView(betScore: number, winScore: number, callback: (multiple: number) => void) {
  54. this.bigWinMultiple = MouseConst.getBigWinMultiple(true);
  55. cc.warn("========== BigWinMultiple:" + this.bigWinMultiple);
  56. let multiple = winScore / betScore;
  57. this._isBigWin = multiple >= this.bigWinMultiple[0];
  58. // 小奖不显示
  59. if (!this._isBigWin) {
  60. return callback?.(multiple);
  61. }
  62. cc.warn("==================> showWinView, isBigWin:" + this._isBigWin);
  63. this._winScore = winScore;
  64. this._callback = callback;
  65. // let numScrollTime: number;
  66. if (multiple >= this.bigWinMultiple[2]) { // super
  67. // numScrollTime = MouseConst.BigWinScrollTime[2];
  68. this._winType = 2;
  69. } else if (multiple >= this.bigWinMultiple[1]) { // mega
  70. // numScrollTime = MouseConst.BigWinScrollTime[1];
  71. this._winType = 1;
  72. } else if (multiple >= this.bigWinMultiple[0]) { // big
  73. // numScrollTime = MouseConst.BigWinScrollTime[0];
  74. this._winType = 0;
  75. } else {
  76. // numScrollTime = multiple / 2;
  77. }
  78. // 大奖音效
  79. this._bigWinStartAudioId && AudioPlayer.stopEffect(this._bigWinStartAudioId);
  80. AudioPlayer.playEffect(MouseConst.Sound.bigWinStart, false, 1, false, (audioID: number) => {
  81. this._bigWinStartAudioId = audioID;
  82. });
  83. // 滚动时间
  84. // let audioTime = cc.audioEngine.getDuration(this._bigWinStartAudioId);
  85. let audioTime = startAudioTime;
  86. let musicTime = multiple / 90 * audioTime;
  87. let numScrollTime = musicTime > audioTime ? audioTime : musicTime;
  88. this.winNode.active = true;
  89. this.playBigWinAnim(0);
  90. this.scrollCoin(numScrollTime, betScore, winScore);
  91. this.initEventGray(true);
  92. this.initEventUnderButton(true);
  93. }
  94. scrollCoin(time: number, betScore: number, winScore: number) {
  95. let hasWinPlayed = [false, false]; // mega / super
  96. cc.tween({ a: 0 }).tag(101).to(time, { a: winScore }, { //数字滚动
  97. progress: (start: number, end: number, current: any, ratio: number) => {
  98. let now = (start + (end - start) * ratio);
  99. this.winLabel.string = MouseConst.toFixedString(now);
  100. if (this._isBigWin) {
  101. let nowMultiple = now / betScore;
  102. if (nowMultiple >= this.bigWinMultiple[1] && !hasWinPlayed[0]) {
  103. hasWinPlayed[0] = true;
  104. this.playBigWinAnim(1);
  105. } else if (nowMultiple >= this.bigWinMultiple[2] && !hasWinPlayed[1]) {
  106. hasWinPlayed[1] = true;
  107. this.playBigWinAnim(2);
  108. }
  109. }
  110. },
  111. }).delay(delayFinishTime).call(() => this.winFinish()).start();
  112. }
  113. /**
  114. * 播放大奖
  115. * 0: big, 1: mega, 2: super
  116. */
  117. playBigWinAnim(winType: number, manual?: boolean) {
  118. if (winType < 0)
  119. return;
  120. let anim1: string;
  121. let anim2: string;
  122. let titleImg: cc.SpriteFrame;
  123. switch (winType) {
  124. case 2: // super
  125. anim1 = "tansuper";
  126. anim2 = "xuanzhuansuper";
  127. titleImg = this.winTitleImgs[2];
  128. // super金币
  129. this.coinSke.node.active = true;
  130. this.coinSke.setAnimation(0, "jinbichuxian", false);
  131. this.coinSke.setCompleteListener(() => {
  132. this.coinSke.setCompleteListener(null);
  133. this.coinSke.setAnimation(0, "jinbixunhuan", true);
  134. });
  135. break;
  136. case 1: // mega
  137. anim1 = "tanmaga";
  138. anim2 = "xuanzhuanmaga";
  139. titleImg = this.winTitleImgs[1];
  140. break;
  141. default: // big
  142. anim1 = "tanbig";
  143. anim2 = "xuanzhuanbig";
  144. titleImg = this.winTitleImgs[0];
  145. break;
  146. }
  147. // 大奖动效
  148. this.bigWinSke.node.active = true;
  149. this.bigWinSke.setAnimation(0, anim1, false);
  150. this.bigWinSke.setCompleteListener(() => {
  151. this.bigWinSke.setCompleteListener(null);
  152. this.bigWinSke.setAnimation(0, anim2, true);
  153. });
  154. if (winType == 0) { // big
  155. this.winTitle.spriteFrame = titleImg;
  156. cc.tween(this.winTitle.node)
  157. .set({ scale: 0 })
  158. .to(0.3, { scale: 1.2 })
  159. .to(0.1, { scale: 1 })
  160. .start();
  161. } else {
  162. cc.tween(this.winTitle.node)
  163. .to(0.1, { scale: 0 })
  164. .call(() => {
  165. this.winTitle.spriteFrame = titleImg;
  166. })
  167. .to(0.2, { scale: 1.2 })
  168. .to(0.1, { scale: 1 })
  169. .start();
  170. }
  171. cc.tween(this.winLabel.node.parent)
  172. .set({ scale: 0.9 })
  173. .to(0.2, { scale: 1.05 })
  174. .to(0.1, { scale: 0.9 })
  175. .start();
  176. }
  177. //强行结算完毕
  178. immediatelyResult() {
  179. this.initEventGray(false);
  180. cc.Tween.stopAllByTag(101);
  181. cc.tween({}).tag(102).delay(delayFinishTime).call(() => {
  182. this.winFinish();
  183. }).start();
  184. this.winLabel.string = MouseConst.toFixedString(this._winScore);
  185. this.playBigWinAnim(this._winType, true);
  186. }
  187. //结束
  188. clickBigwinHide() {
  189. this.initEventUnderButton(false);
  190. this.resetView();
  191. }
  192. initEventUnderButton(value: boolean) {
  193. if (value) {
  194. this.touchNode.on(cc.Node.EventType.TOUCH_START, this.clickBigwinHide.bind(this), this);
  195. } else {
  196. this.touchNode.off(cc.Node.EventType.TOUCH_START);
  197. }
  198. this.touchNode.active = value;
  199. }
  200. initEventGray(value: boolean) {
  201. if (value) {
  202. this.finishNode.on(cc.Node.EventType.TOUCH_START, this.immediatelyResult.bind(this), this);
  203. } else {
  204. this.finishNode.off(cc.Node.EventType.TOUCH_START);
  205. }
  206. this.finishNode.active = value;
  207. }
  208. resetView() {
  209. cc.Tween.stopAllByTag(101);
  210. cc.Tween.stopAllByTag(102);
  211. this.unscheduleAllCallbacks();
  212. this._winScore = 0;
  213. this._winType = -1;
  214. this._callback = null;
  215. this._isBigWin = false;
  216. this._bigWinStartAudioId = 0;
  217. this._bigWinEndAudioId && AudioPlayer.stopEffect(this._bigWinEndAudioId);
  218. this._bigWinEndAudioId = 0;
  219. this.coinSke.node.active = false;
  220. this.winNode.active = false;
  221. this.finishNode.active = false;
  222. this.touchNode.active = false;
  223. }
  224. protected onDestroy(): void {
  225. cc.Tween.stopAllByTag(101);
  226. cc.Tween.stopAllByTag(102);
  227. }
  228. }