DrawAPrize.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { _decorator, Component, Node, find, tween, v3, Button, Sprite, Animation, Event, EventHandler, Label, Vec3 } from 'cc';
  2. import { Http } from '../core/net/Http';
  3. import { Sound } from '../core/sound/Sound';
  4. import { OpenWindow } from '../core/ui/window/OpenWindow';
  5. import { WindowManager } from '../core/ui/window/WindowManager';
  6. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  7. import { MultipleType } from '../Data/ADData';
  8. import { ConfigData } from '../Data/ConfigData';
  9. import { g } from '../Data/g';
  10. import { LoginWaitWin } from '../Windows/LoginWaitWin';
  11. const { ccclass, property } = _decorator;
  12. @ccclass('DrawAPrize')
  13. export class DrawAPrize extends Component {
  14. @property({ tooltip: "开奖选中动画图片", type: Node }) public prize: Node;
  15. @property({ tooltip: "开奖边框等效", type: Node }) public prizeBg: Node;
  16. @property({ tooltip: "http服务", type: Http }) public http: Http;
  17. @property({ tooltip: "领完神石回调", type: EventHandler }) public getDiamondBack: EventHandler;
  18. @property({ tooltip: "领取完奖品回调", type: EventHandler }) public getPrizeBack: EventHandler;
  19. @property({ tooltip: "转盘窗口", type: Node }) public turntableWin: Node;
  20. @property({ tooltip: "申请抽奖成功回调", type: EventHandler }) public drawBack: EventHandler;
  21. @property({ tooltip: "今日剩余次数文本", type: Node }) public drawTimes: Node;
  22. @property({ tooltip: "关闭按钮", type: Node }) public closeBtn: Node;
  23. @property({ tooltip: "规则按钮", type: Node }) public ruleBtn: Node;
  24. @property({ tooltip: "转盘抽奖音效控制器", type: Sound }) public turntableSound: Sound;
  25. @property({ tooltip: "抽奖按键", type: Node }) public drawaBtn: Node;
  26. private button: Button;
  27. private tempAnimation: Animation;
  28. public ligthAnimation: Animation;
  29. start() {
  30. g.gameData.isPlayVideo = false;
  31. g.gameData.playAddMoneySound = false;
  32. this.button = this.drawaBtn.getComponent(Button);
  33. this.tempAnimation = this.prize.getComponent(Animation);
  34. this.ligthAnimation = this.prizeBg.getComponent(Animation);
  35. this.drawTimes.getComponent(Label).string = "今日剩余次数:" + g.gameData.drawTimes + "次 ";
  36. if (g.gameData.drawTimes <= 0) {
  37. this.button.interactable = false;//禁用鼠标事件
  38. this.drawaBtn.getComponent(Sprite).grayscale = true;//按键置灰
  39. }
  40. }
  41. public isSend = false;
  42. //请求服务器抽奖
  43. public async drawPrize() {
  44. this.initUIButton(true);
  45. if (g.gameData.drawTimes <= 0) {
  46. WindowManager.showTips("视频次数已经用完");
  47. return;
  48. }
  49. if (g.gameData.isPlayVideo) {
  50. return;
  51. }
  52. g.gameData.isPlayVideo = true;
  53. //观看视频抽奖,视频开始观看
  54. let data = await this.http.send("/api/ad/WatchVideoAD");
  55. switch (data.code) {
  56. case 0:
  57. //播放视频
  58. let adData = await g.showRewardVideo();
  59. if (adData) {
  60. this.videoBack();
  61. } else {
  62. //视频观看失败
  63. WindowManager.showTips("视频观看时间不足");
  64. }
  65. break;
  66. case 108:
  67. WindowManager.showTips("今日视频次数已用完,请明日再继续");
  68. g.gameData.isPlayVideo = false;
  69. break;
  70. default:
  71. // WindowManager.showTips(g.CodeMsg[data.code]);
  72. WindowManager.showTips("视频准备中,请稍后再试");
  73. g.gameData.isPlayVideo = false;
  74. break;
  75. }
  76. this.initUIButton(false);
  77. }
  78. //视频结束回调
  79. public async videoBack() {
  80. this.initUIButton(true);
  81. //#region 打开加载等待界面
  82. let _window = await WindowManager.open("Prefabs/Windows/加载等待窗口", WindowOpenMode.NotCloseAndCover);
  83. //#endregion
  84. let data = await this.http.send("/api/wealth/AddDiamondForVideo", { multipleType: MultipleType.Turntable, adData: g.adData });
  85. _window.getComponent(LoginWaitWin).close();//关闭加载等待界面
  86. if (data.code == 0) {
  87. if (data.data.addDiamond > 0) {
  88. this.turntableWin.active = false;
  89. this.getComponent(OpenWindow).prefabPath = "Prefabs/Windows/神石领取界面";
  90. let _window = this.getComponent(OpenWindow);
  91. _window.open([data.data.addDiamond, -1, null, this.getDiamondBack]);
  92. } else {
  93. this.getDiamondBack.emit([]);
  94. }
  95. } else {
  96. this.initUIButton(false);
  97. WindowManager.showTips(g.CodeMsg[data.code]);
  98. }
  99. g.gameData.isPlayVideo = false;
  100. }
  101. //领取神石后回调
  102. public getDiamondBackFun() {
  103. this.turntableWin.active = true;
  104. this.playEffect();
  105. }
  106. public prizeData: any;//获奖ID
  107. //请求抽奖
  108. public async playEffect() {
  109. //#region 打开加载等待界面
  110. let _window = await WindowManager.open("Prefabs/Windows/加载等待窗口", WindowOpenMode.NotCloseAndCover);
  111. //#endregion
  112. let data = await this.http.send("/api/turntalbe/Draw");
  113. _window.getComponent(LoginWaitWin).close();//关闭加载等待界面
  114. if (data.code == 0) {//播放抽奖动画
  115. // this.turntableSound.play();//音效
  116. this.prizeData = data.data;
  117. // this.prizeData.id = 6;//测试
  118. this.drawBack.emit([data.data.id]);
  119. this.tempAnimation.getState('openPrize').speed = 0.3;
  120. this.ligthAnimation.play('openPrizeBoxLight');
  121. this.tempAnimation.play('openPrize');
  122. } else {
  123. this.initUIButton(false);
  124. WindowManager.showTips(data.code);
  125. }
  126. }
  127. /**
  128. * 开奖动画结束回调
  129. */
  130. public animationStop() {
  131. // this.turntableSound.stop();//停止音效
  132. let data = ConfigData.configMap.get("turntable").prizeList;
  133. for (let i = 0; i < data.length; i++) {
  134. if (data[i].id == this.prizeData.id) {
  135. switch (data[i].id) {
  136. case 1:
  137. this.openGoldWin();
  138. break;
  139. case 2:
  140. this.openGoldWin();
  141. break;
  142. case 3:
  143. this.openGoldWin();
  144. break;
  145. case 4:
  146. this.openGoldWin();
  147. break;
  148. case 5:
  149. this.openGoldWin();
  150. break;
  151. case 6:
  152. this.openTenfoldWin();
  153. break;
  154. case 7:
  155. this.openDiamondWin();
  156. break;
  157. case 8:
  158. this.openDiamondWin();
  159. break;
  160. }
  161. break;
  162. }
  163. }
  164. this.ligthAnimation.stop();
  165. g.gameData.drawTimes--;//更新轮盘剩余次数
  166. g.gameData.drawTimes = g.gameData.drawTimes <= 0 ? 0 : g.gameData.drawTimes;
  167. this.drawTimes.getComponent(Label).string = "今日剩余次数:" + g.gameData.drawTimes + "次 ";//更新轮盘剩余次数
  168. this.turntableWin.active = false;
  169. if (g.gameData.drawTimes > 0) {
  170. this.drawaBtn.getComponent(Sprite).grayscale = false;
  171. this.button.interactable = true;
  172. }
  173. this.closeBtn.active = true;
  174. this.ruleBtn.active = true;
  175. this.prize.setPosition(new Vec3(-180, 180));
  176. }
  177. //打开奖品金币领取界面
  178. public openGoldWin() {
  179. let _openWindow = this.getComponent(OpenWindow);
  180. _openWindow.prefabPath = "Prefabs/Windows/金币领取窗口";
  181. _openWindow.open([this.prizeData.preze, 3, this.getPrizeBack]);
  182. }
  183. public getPrizeBackFun() {
  184. this.turntableWin.active = true;
  185. }
  186. //打开奖品神石领取界面
  187. public openDiamondWin() {
  188. let _openWindow = this.getComponent(OpenWindow);
  189. _openWindow.prefabPath = "Prefabs/Windows/神石领取界面";
  190. _openWindow.open([this.prizeData.preze, -1, null, this.getPrizeBack]);
  191. }
  192. //打开下次奖励十倍领取界面
  193. public openTenfoldWin() {
  194. let _openWindow = this.getComponent(OpenWindow);
  195. _openWindow.prefabPath = "Prefabs/Windows/翻倍奖励窗口";
  196. _openWindow.open([this.prizeData.preze, this.getPrizeBack]);
  197. }
  198. /**
  199. * 重置按钮状态
  200. * @param isDraw 是否在抽奖
  201. */
  202. public initUIButton(isDraw: boolean) {
  203. this.drawaBtn.getComponent(Sprite).grayscale = isDraw;//按键置灰
  204. this.button.interactable = !isDraw;
  205. this.closeBtn.active = !isDraw;
  206. this.ruleBtn.active = !isDraw;
  207. }
  208. onDestroy() {
  209. g.gameData.playAddMoneySound = false;
  210. g.gameData.isPlayVideo = false;
  211. }
  212. }