LuckBoxPannel.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import { utils } from "./Utils";
  2. import { BannerLocation, ViewLocation, YZ_Reward } from "./YZ_Constant";
  3. import AldUtils from "./AldUtils";
  4. import PlatUtils from "./PlatUtils";
  5. const { ccclass, property } = cc._decorator;
  6. /**
  7. * 五倍奖励宝箱
  8. */
  9. @ccclass
  10. export default class LuckBoxPanel extends cc.Component {
  11. @property(cc.Node)
  12. showVideoBtn: cc.Node = null
  13. @property(cc.Node)
  14. clickBtn: cc.Node = null
  15. @property(cc.Label)
  16. btnLabel: cc.Label = null
  17. @property(cc.Node)
  18. panel: cc.Node = null
  19. @property(cc.Node)
  20. videoIcon: cc.Node = null
  21. @property(cc.ProgressBar)
  22. progressBar: cc.ProgressBar = null;
  23. @property(cc.Node)
  24. boxNode: cc.Node = null;
  25. /**
  26. * 奖励回调
  27. */
  28. rewardCallFunc: Function = null;
  29. /**
  30. * 奖励值
  31. */
  32. rewardValue: number = 0;
  33. //幸运宝箱显示的广告类型
  34. _luck_box_ad_type: string = "1";
  35. //跳过按钮延迟显示时间
  36. _delay_show_btn_time: number = 0;
  37. //幸运宝箱开始回退的时间
  38. _luck_box_progressbar_back_time: number = 1000;
  39. /**
  40. * 展示广告的进度条百分比
  41. */
  42. _showAdProgress: number = 0.8;
  43. onLoad() {
  44. if (utils.otherConfig && utils.otherConfig.group) {
  45. this.node.group = utils.otherConfig.group;
  46. }
  47. this.rewardCallFunc = utils.rewardCallFunc;
  48. this.rewardValue = utils.rewardValue;
  49. this.panel.scale = 0;
  50. utils.SendEvent("幸运宝箱-显示成功!");
  51. this.clickBtn.on(cc.Node.EventType.TOUCH_CANCEL, this.clickBtnTouchCancel.bind(this));
  52. this.clickBtn.on(cc.Node.EventType.TOUCH_END, this.clickBtnTouchCancel.bind(this));
  53. this.progressBar.progress = 0;
  54. this.btnLabel.node.opacity = 0;
  55. this.btnLabel.node.active = false;
  56. utils.luckBoxShowCount++;
  57. let adTypes = utils.ServerConfig.luck_box_ad_type ? utils.ServerConfig.luck_box_ad_type.split(",") : this._luck_box_ad_type;
  58. let closeBtnShowDelays = utils.ServerConfig.luck_box_close_btn_show_delay ? utils.ServerConfig.luck_box_close_btn_show_delay.split(",") : this._delay_show_btn_time;
  59. let progressbarBackTimes = utils.ServerConfig.luck_box_progressbar_back_time ? utils.ServerConfig.luck_box_progressbar_back_time.split(",") : this._luck_box_progressbar_back_time;
  60. if (utils.luckBoxShowCount > adTypes.length - 1) {
  61. utils.luckBoxShowCount = 0;
  62. }
  63. utils.showLog(`幸运宝箱显示次数:${utils.luckBoxShowCount}`);
  64. this._luck_box_ad_type = adTypes[utils.luckBoxShowCount];
  65. this._delay_show_btn_time = closeBtnShowDelays[utils.luckBoxShowCount];
  66. this._luck_box_progressbar_back_time = progressbarBackTimes[utils.luckBoxShowCount];
  67. if (utils.ServerConfig.luck_box_show_ad_progress_percent) {
  68. let showAdProPercents = utils.ServerConfig.luck_box_show_ad_progress_percent.split(",");
  69. this._showAdProgress = showAdProPercents[utils.luckBoxShowCount];
  70. } else {
  71. switch (this._luck_box_ad_type) {
  72. case "1":
  73. case "5":
  74. this._showAdProgress = 0.45;
  75. break;
  76. case "2":
  77. this._showAdProgress = 0.3;
  78. break
  79. case "3":
  80. //插屏点击进度超过0.3就调用广告
  81. this._showAdProgress = 0.85;
  82. break
  83. case "4":
  84. this._showAdProgress = 0.85;
  85. break;
  86. default:
  87. this._showAdProgress = 0.45;
  88. break;
  89. }
  90. }
  91. utils.showLog(`幸运宝箱显示类型:${this._luck_box_ad_type} #showBtnTime=${this._delay_show_btn_time} #progressbarBackTime=${this._luck_box_progressbar_back_time}`);
  92. if (this._luck_box_ad_type != "4") {
  93. this.scheduleOnce(() => {
  94. if (this.btnLabel.node && cc.isValid(this.btnLabel.node)) {
  95. this.btnLabel.node.active = true;
  96. this.btnLabel.node.runAction(cc.fadeIn(0.3));
  97. }
  98. }, this._delay_show_btn_time);
  99. }
  100. if (utils.ServerConfig.luck_box_video_icon_is_show && utils.ServerConfig.luck_box_video_icon_is_show == "true") {
  101. this.videoIcon.active = true;
  102. } else {
  103. this.videoIcon.active = false;
  104. }
  105. if (this._luck_box_ad_type == "1") {
  106. utils.adManager.HideBanner(BannerLocation.Game);
  107. if (PlatUtils.IsIOS && PlatUtils.IsQQ) {
  108. this.scheduleOnce(() => {
  109. if (this.clickBtn && cc.isValid(this.clickBtn)) {
  110. this.clickBtn.setPosition(this.clickBtn.x, this.progressBar.node.position.y - 200);
  111. }
  112. }, 10)
  113. }
  114. } else {
  115. this.clickBtn.getComponent(cc.Widget).isAlignBottom = false;
  116. this.clickBtn.getComponent(cc.Widget).updateAlignment();
  117. this.clickBtn.setPosition(this.clickBtn.x, this.progressBar.node.position.y - 200);
  118. }
  119. // this.boxNode.runAction(cc.sequence(cc.scaleTo(1, 1.3).easing(cc.easeElasticIn(1.0)), cc.scaleTo(1, 1).easing(cc.easeElasticOut(3.0)), cc.delayTime(1)).repeatForever())
  120. }
  121. _openSlowDown: boolean = false;
  122. _timetaskId: number = 0;
  123. _cancelSlowDown: boolean = false;
  124. clickBtnTouchCancel() {
  125. clearTimeout(this._timetaskId);
  126. this._timetaskId = setTimeout(() => {
  127. this._openSlowDown = true;
  128. }, this._luck_box_progressbar_back_time * 1000);
  129. }
  130. _slowDownSpeed: number = 0.2;
  131. update(dt) {
  132. if (this.progressBar.progress > 0 && this.progressBar.progress < 1 && this._openSlowDown && !this._cancelSlowDown) {
  133. this.progressBar.progress -= dt * this._slowDownSpeed;
  134. if (this.progressBar.progress <= 0) {
  135. this.progressBar.progress = 0;
  136. }
  137. }
  138. }
  139. onEnable() {
  140. let ratio: number = 1;
  141. if (cc.winSize.height < cc.winSize.width) {
  142. // 横屏游戏
  143. ratio = cc.winSize.width / 1920 * 0.6;
  144. } else {
  145. ratio = cc.winSize.width / 1080;
  146. }
  147. this.panel.scale = ratio;
  148. }
  149. onDestroy() {
  150. utils.adManager.HideBanner(BannerLocation.Game)
  151. utils.hideRecommendGamesBanner();
  152. if (utils.rewardLuckBoxPanelCloseFunc) {
  153. utils.rewardLuckBoxPanelCloseFunc();
  154. utils.rewardBoxPanelCloseFunc = null;
  155. } else {
  156. utils.rewardCloseFunc && utils.rewardCloseFunc();
  157. utils.rewardCloseFunc = null;
  158. }
  159. }
  160. onClose() {
  161. this.panel.runAction(cc.sequence(cc.scaleTo(0.3, 0).easing(cc.easeBackIn()), cc.callFunc(() => {
  162. this.node.destroy();
  163. })));
  164. }
  165. _progresss: number = 0;
  166. _totalProgress: number = 5;
  167. _isShowAd: boolean = false;
  168. _videoIsPlay: boolean = false;
  169. /**
  170. * 狂点
  171. */
  172. onBtnClick() {
  173. this._openSlowDown = false;
  174. if (this.progressBar.progress < 1) {
  175. this.progressBar.progress += 0.10;
  176. if (this.progressBar.progress >= 1) {
  177. this.progressBar.progress = 1;
  178. this.openLuckBox();
  179. }
  180. }
  181. if (this.videoIcon.active && !this._videoIsPlay) {
  182. this._videoIsPlay = true;
  183. utils.adManager.ShowVideo((res, msg) => {
  184. if (PlatUtils.IsDouyin) {
  185. if (res) {
  186. utils.showMsg("获得奖励! +" + this.rewardValue);
  187. let result: YZ_Reward = new YZ_Reward();
  188. result.rewardValue = this.rewardValue;
  189. if (this.rewardCallFunc) {
  190. this.rewardCallFunc(result);
  191. }
  192. this.onClose();
  193. } else {
  194. this._videoIsPlay = false;
  195. utils.showMsg(msg ? msg : "视频加载失败3!");
  196. }
  197. } else {
  198. utils.showMsg("获得奖励! +" + this.rewardValue);
  199. let result: YZ_Reward = new YZ_Reward();
  200. result.rewardValue = this.rewardValue;
  201. if (this.rewardCallFunc) {
  202. this.rewardCallFunc(result);
  203. }
  204. this.onClose();
  205. }
  206. });
  207. }
  208. if (this.progressBar.progress >= this._showAdProgress && !this._isShowAd) {
  209. this._isShowAd = true;
  210. //1、banner(默认) 2、插屏 3、盒子广告 4、视频广告
  211. switch (this._luck_box_ad_type) {
  212. case "1":
  213. utils.showLog("服务器配置幸运宝箱展示-banner广告!");
  214. utils.adManager.ShowBanner(BannerLocation.Game);
  215. setTimeout(() => {
  216. if (this.clickBtn && cc.isValid(this.clickBtn)) {
  217. this.clickBtn.setPosition(this.clickBtn.x, this.progressBar.node.position.y - 200);
  218. }
  219. }, 900)
  220. break;
  221. case "2":
  222. utils.showLog("服务器配置幸运宝箱展示-插屏广告!");
  223. utils.adManager.ShowInterstitial();
  224. break;
  225. case "3":
  226. utils.showLog("服务器配置幸运宝箱展示-盒子广告!");
  227. utils.adManager.ShowAppBox();
  228. break
  229. case "4":
  230. utils.showLog("服务器配置幸运宝箱展示-视频广告!");
  231. // this.progressBar.progress = 1;
  232. this.videoIcon.active = true;
  233. // cc.find("Background/clickTxt", this.clickBtn).active = false;
  234. // cc.find("Background/btn_ok", this.clickBtn).active = true;
  235. this._cancelSlowDown = true;
  236. this.scheduleOnce(() => {
  237. if (this.btnLabel.node && cc.isValid(this.btnLabel.node)) {
  238. this.btnLabel.node.active = true;
  239. this.btnLabel.node.runAction(cc.fadeIn(0.3));
  240. }
  241. }, this._delay_show_btn_time);
  242. break;
  243. case "5":
  244. utils.showLog("服务器配置幸运宝箱展示-互推banner广告!");
  245. utils.showRecommendGamesBanner();
  246. break;
  247. default:
  248. utils.showLog("服务器配置幸运宝箱展示banner广告!");
  249. utils.adManager.ShowBanner(BannerLocation.Game);
  250. setTimeout(() => {
  251. if (this.clickBtn && cc.isValid(this.clickBtn)) {
  252. this.clickBtn.setPosition(this.clickBtn.x, this.progressBar.node.position.y - 200);
  253. }
  254. }, 500)
  255. break;
  256. }
  257. }
  258. }
  259. /**
  260. * 打开宝箱
  261. */
  262. openLuckBox() {
  263. if (utils.ServerConfig.luck_box_play_video == "true") {
  264. this.onPlayVideo();
  265. this.onClose();
  266. } else {
  267. utils.showMsg("获得奖励! +" + this.rewardValue);
  268. let result: YZ_Reward = new YZ_Reward();
  269. result.rewardValue = this.rewardValue;
  270. if (this.rewardCallFunc) {
  271. this.rewardCallFunc(result);
  272. }
  273. this.onClose();
  274. }
  275. }
  276. onHideBtn() {
  277. this.onClose();
  278. }
  279. onPlayVideo() {
  280. utils.adManager.ShowVideo((ret, msg) => {
  281. if (ret) {
  282. utils.showMsg("获得奖励! +" + this.rewardValue);
  283. this.rewardValue = this.rewardValue;
  284. AldUtils.SendEvent("幸运宝箱-获取奖励成功!");
  285. } else {
  286. utils.showMsg("获得奖励! +" + this.rewardValue);
  287. AldUtils.SendEvent("幸运宝箱-视频播放失败!");
  288. }
  289. let result: YZ_Reward = new YZ_Reward();
  290. result.rewardValue = this.rewardValue;
  291. if (this.rewardCallFunc) {
  292. this.rewardCallFunc(result);
  293. }
  294. this.onClose();
  295. })
  296. }
  297. }