platformManager.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import { game, native, sys } from "cc";
  2. import { GlobalData } from "../GlobalData";
  3. import { EDITOR, PREVIEW } from "cc/env";
  4. import { CarColorsGlobalInstance } from "../../CarColorsGame/Scrips/CarColorsGlobalInstance";
  5. export class PlatformManager {
  6. private level: number = 1
  7. private isVibrate: boolean = true
  8. public callBack: Function = null
  9. public fcallBack: Function = null
  10. public static adsVideoId: string = ""
  11. public static adsInsId: string = ""
  12. public static adsBannerId: string = ""
  13. private static adsVideoKey: string = "default"
  14. private static adsBannerKey: string = "default"
  15. private static _instance: PlatformManager;
  16. public static interstitialAd: any = null
  17. public static rewardedVideoAd : any = null
  18. public static bannerAd : any = null
  19. // private static bannerAd : ;
  20. public static get instance () {
  21. if (this._instance) {
  22. return this._instance;
  23. }
  24. this._instance = new PlatformManager();
  25. // 安卓端
  26. if(sys.platform === "ANDROID"){
  27. native.bridge.onNative = (arg0:string, arg1?: string | null):void=>{
  28. // 激励相关回调
  29. if(arg0 == 'video_ad_close'){
  30. game.resume()
  31. this._instance.callBack && this._instance.callBack();
  32. }else if (arg0 == 'video_ad_fail'){
  33. game.resume()
  34. this._instance.fcallBack && this._instance.fcallBack()
  35. }else if (arg0 == 'video_ad_show_fail'){
  36. game.resume()
  37. this._instance.fcallBack && this._instance.fcallBack()
  38. }else if (arg0 == 'video_ad_show'){
  39. }
  40. // 插屏相关回调
  41. else if(arg0 == 'ins_ad_close'){
  42. game.resume()
  43. this._instance.callBack && this._instance.callBack();
  44. }else if (arg0 == 'ins_ad_fail'){
  45. game.resume()
  46. this._instance.fcallBack && this._instance.fcallBack()
  47. }else if (arg0 == 'ins_ad_show_fail'){
  48. game.resume()
  49. this._instance.fcallBack && this._instance.fcallBack()
  50. }else if (arg0 == 'ins_ad_show'){
  51. }
  52. // 开屏相关回调
  53. else if (arg0 == 'open_ad_show'){
  54. }else if (arg0 == 'open_ad_show_fail'){
  55. }
  56. // banner相关回调
  57. else if (arg0 == 'banner_ad_show'){
  58. }else if (arg0 == 'banner_ad_show_fail'){
  59. }
  60. return;
  61. };
  62. }
  63. // 微信小游戏
  64. else if (sys.platform === sys.Platform.WECHAT_GAME && this.adsVideoId !== ""){
  65. this.interstitialAd = wx.createInterstitialAd({ adUnitId: this.adsInsId })
  66. this.rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: this.adsVideoId })
  67. this.rewardedVideoAd.onClose((res)=>{
  68. if (res && res.isEnded || res === undefined) {
  69. // 正常播放结束,可以下发游戏奖励
  70. game.resume()
  71. this._instance.callBack && this._instance.callBack();
  72. }
  73. else {
  74. game.resume()
  75. // 播放中途退出,不下发游戏奖励
  76. this._instance.fcallBack && this._instance.fcallBack()
  77. }
  78. })
  79. }
  80. // 字节小游戏
  81. else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME && this.adsVideoId !== ""){
  82. this.interstitialAd = tt.createInterstitialAd({ adUnitId: this.adsInsId })
  83. this.rewardedVideoAd = tt.createRewardedVideoAd({ adUnitId: this.adsVideoId })
  84. this.rewardedVideoAd.onClose((res)=>{
  85. if (res && res.isEnded || res === undefined) {
  86. // 正常播放结束,可以下发游戏奖励
  87. game.resume()
  88. this._instance.callBack && this._instance.callBack();
  89. }
  90. else {
  91. // 播放中途退出,不下发游戏奖励
  92. game.resume()
  93. this._instance.fcallBack && this._instance.fcallBack()
  94. }
  95. })
  96. }
  97. return this._instance;
  98. }
  99. getVibrate(){
  100. this.isVibrate = GlobalData.instance.getConfigData("vibrate")
  101. if (this.isVibrate === null){
  102. this.isVibrate = true
  103. }
  104. return this.isVibrate
  105. }
  106. setVibrate(isVibrate) {
  107. this.isVibrate = isVibrate
  108. GlobalData.instance.setConfigData("vibrate", this.isVibrate)
  109. }
  110. startVibrate(time: number = 100) {
  111. if (!this.isVibrate) return
  112. // 安卓端
  113. if(sys.isNative){
  114. // @ts-ignore
  115. jsb.Device.vibrate(time / 1000)
  116. return;
  117. };
  118. // 小游戏
  119. if (sys.platform === sys.Platform.WECHAT_GAME) {
  120. let type = "medium"
  121. if (time < 100){
  122. type = "light"
  123. }
  124. wx.vibrateShort({type})
  125. return
  126. }
  127. // 字节
  128. if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME){
  129. tt.vibrateShort()
  130. }
  131. }
  132. public GAInit() {
  133. // if(sys.platform !== "ANDROID") return
  134. // let build = "android0.7"
  135. // let game = "d4cbf6a01f5d61a1acddbba7da124f40"
  136. // let secret = "f77e3a63de19c11462eeb8e562a19877a3ce4659";
  137. // (window as any).gameanalytics.GameAnalytics.setEnabledInfoLog(true);
  138. // (window as any).gameanalytics.GameAnalytics.setEnabledVerboseLog(true);
  139. // (window as any).gameanalytics.GameAnalytics.configureBuild(build);
  140. // // (window as any).gameanalytics.GameAnalytics.configureAvailableResourceCurrencies(["toola", "toolb","toolc", "toold","toole", "toolf", "toolg"]);
  141. // // (window as any).gameanalytics.GameAnalytics.configureAvailableResourceItemTypes(["toola", "toolb","toolc", "toold","toole", "toolf", "toolg"]);
  142. // (window as any).gameanalytics.GameAnalytics.initialize(game, secret);
  143. }
  144. // 关卡开始时上报
  145. public setLevel(level:number = 1) {
  146. this.level = level;
  147. }
  148. // 关卡开始时上报
  149. public GaLevelStart(level:number = 1) {
  150. this.level = level;
  151. // if(sys.platform !== "ANDROID") return
  152. // (window as any).gameanalytics.GameAnalytics.addProgressionEvent(
  153. // (window as any).gameanalytics.EGAProgressionStatus.Start,
  154. // `level_${level}`,
  155. // );
  156. }
  157. // 关卡失败时上报
  158. public GaLevelFail() {
  159. // if(sys.platform !== "ANDROID") return
  160. // (window as any).gameanalytics.GameAnalytics.addProgressionEvent(
  161. // (window as any).gameanalytics.EGAProgressionStatus.Fail,
  162. // `level_${this.level}`,
  163. // );
  164. // if (this.level < 4){
  165. // native.bridge.sendToNative('Level_fail', `level${this.level}`);
  166. // }
  167. }
  168. // 关卡通过时上报
  169. public GaLevelComplete() {
  170. // if(sys.platform !== "ANDROID") return
  171. // (window as any).gameanalytics.GameAnalytics.addProgressionEvent(
  172. // (window as any).gameanalytics.EGAProgressionStatus.Complete,
  173. // `level_${this.level}`,
  174. // );
  175. // if (this.level < 4){
  176. // native.bridge.sendToNative('Level_pass', `level${this.level}`);
  177. // }
  178. }
  179. public showUpgradeTips(json: string){
  180. // 安卓端
  181. if(sys.platform === "ANDROID"){
  182. native.bridge.sendToNative('showUpgradeTips', json);
  183. return;
  184. };
  185. }
  186. public showOpenAd(callback?: Function){
  187. console.log("showOpenAd")
  188. // 安卓端
  189. if(sys.platform === "ANDROID"){
  190. native.bridge.sendToNative('showOpenAd', "defaultAdUrl");
  191. if(callback) callback()
  192. return;
  193. };
  194. }
  195. public showBanner(callback?: Function){
  196. // console.log("showBanner")
  197. // 安卓端
  198. if(sys.platform === "ANDROID"){
  199. native.bridge.sendToNative('showBannerAd', "defaultAdUrl");
  200. // native.reflection.callStaticMethod("com/cocos/game/AdManage", "showBannerAd", "()V");
  201. if(callback) callback()
  202. return;
  203. };
  204. callback && callback()
  205. return
  206. // 微信小游戏
  207. if (sys.platform === sys.Platform.WECHAT_GAME && PlatformManager.adsBannerId !== ""){
  208. let { screenWidth, screenHeight } = wx.getSystemInfoSync()
  209. callback && callback()
  210. if (PlatformManager.bannerAd !== null){
  211. PlatformManager.bannerAd.destroy()
  212. }
  213. PlatformManager.bannerAd = wx.createBannerAd({
  214. adUnitId: PlatformManager.adsBannerId,
  215. style: {
  216. left: 0,
  217. top: screenHeight - 90,
  218. width: screenWidth
  219. },
  220. adIntervals: 30,
  221. })
  222. PlatformManager.bannerAd.onLoad(()=>{
  223. PlatformManager.bannerAd.show()
  224. })
  225. return
  226. }
  227. // 字节小游戏
  228. if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME && PlatformManager.adsBannerId !== ""){
  229. let { screenWidth, screenHeight } = tt.getSystemInfoSync()
  230. callback && callback()
  231. if (PlatformManager.bannerAd !== null){
  232. PlatformManager.bannerAd.destroy()
  233. }
  234. PlatformManager.bannerAd = tt.createBannerAd({
  235. adUnitId: PlatformManager.adsBannerId,
  236. style: {
  237. left: 0,
  238. top: screenHeight - 90,
  239. width: screenWidth
  240. },
  241. adIntervals: 30,
  242. })
  243. PlatformManager.bannerAd.onLoad(()=>{
  244. PlatformManager.bannerAd.show()
  245. })
  246. return
  247. }
  248. }
  249. public hideBanner(callback?: Function){
  250. // console.log("hideBanner")
  251. // 安卓端
  252. if(sys.platform === "ANDROID"){
  253. native.bridge.sendToNative('hideBannerAd', "defaultAdUrl");
  254. // native.reflection.callStaticMethod("com/cocos/game/AdManage", "hideBannerAd", "()V");
  255. if(callback) callback()
  256. return;
  257. };
  258. callback && callback()
  259. return
  260. // 微信小游戏
  261. if (sys.platform === sys.Platform.WECHAT_GAME && PlatformManager.adsBannerId !== ""){
  262. callback && callback()
  263. if (PlatformManager.bannerAd !== null){
  264. PlatformManager.bannerAd.hide()
  265. PlatformManager.bannerAd.destroy()
  266. PlatformManager.bannerAd = null
  267. }
  268. return
  269. }
  270. // 字节小游戏
  271. if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME && PlatformManager.adsBannerId !== ""){
  272. callback && callback()
  273. if (PlatformManager.bannerAd !== null){
  274. PlatformManager.bannerAd.hide()
  275. PlatformManager.bannerAd.destroy()
  276. PlatformManager.bannerAd = null
  277. }
  278. return
  279. }
  280. }
  281. public showInsAd(callback?: Function){
  282. if (this.level < 4) return
  283. // 安卓端
  284. if(sys.platform === "ANDROID"){
  285. game.pause()
  286. this.callBack = callback
  287. native.bridge.sendToNative('showInterstitialAd', "defaultAdUrl");
  288. return;
  289. };
  290. // 微信小游戏
  291. if (sys.platform === sys.Platform.WECHAT_GAME && PlatformManager.interstitialAd !== null){
  292. callback && callback()
  293. PlatformManager.interstitialAd.show()
  294. return
  295. }
  296. // 字节小游戏
  297. if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME && PlatformManager.interstitialAd !== null){
  298. callback && callback()
  299. PlatformManager.interstitialAd.show()
  300. return
  301. }
  302. callback && callback()
  303. }
  304. showVideoAd(callback?: Function,fcallback?: Function, videoKey:string = "default") {
  305. console.log("showVideoAd")
  306. callback&&callback()
  307. return
  308. PlatformManager.adsVideoKey = videoKey
  309. // 安卓端
  310. if(sys.platform === "ANDROID"){
  311. game.pause()
  312. this.callBack = callback
  313. this.fcallBack = fcallback
  314. native.bridge.sendToNative('showRewardVideoAd', "defaultAdUrl");
  315. return;
  316. };
  317. // 微信小游戏
  318. if (sys.platform === sys.Platform.WECHAT_GAME && PlatformManager.rewardedVideoAd !== null){
  319. game.pause()
  320. // console.log("调用广告")
  321. this.callBack = callback
  322. this.fcallBack = fcallback
  323. PlatformManager.rewardedVideoAd.show()
  324. .catch(err => {
  325. fcallback && fcallback()
  326. game.resume()
  327. PlatformManager.rewardedVideoAd.load()
  328. })
  329. return
  330. }
  331. // 字节小游戏
  332. if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME && PlatformManager.rewardedVideoAd !== null){
  333. this.callBack = callback
  334. this.fcallBack = fcallback
  335. game.pause()
  336. PlatformManager.rewardedVideoAd.show()
  337. .catch(err => {
  338. fcallback && fcallback()
  339. game.resume()
  340. PlatformManager.rewardedVideoAd.load()
  341. })
  342. return
  343. }
  344. // 测试环境
  345. callback&&callback()
  346. }
  347. shareGame(callback?: Function,fcallback?: Function){
  348. console.log("shareGame")
  349. // 安卓端
  350. if(sys.platform === "ANDROID"){
  351. // fcallback && fcallback()
  352. // game.pause()
  353. callback&&callback()
  354. CarColorsGlobalInstance.instance.toastSysterm.showToast("分享暂不可用,已免费解锁")
  355. return;
  356. };
  357. // 微信小游戏
  358. if (sys.platform === sys.Platform.WECHAT_GAME){
  359. const nowTime = new Date().getTime()
  360. wx.shareAppMessage()
  361. CarColorsGlobalInstance.instance.bundleSysterm.scheduleOnce(()=>{
  362. if (new Date().getTime() - nowTime < 3000){
  363. fcallback && fcallback()
  364. }else {
  365. // console.log("分享成功")
  366. callback&&callback()
  367. }
  368. }, 1)
  369. return
  370. }
  371. // 字节小游戏
  372. if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME){
  373. tt.shareAppMessage({
  374. success:()=>{
  375. callback&&callback()
  376. },
  377. fail: ()=>{
  378. fcallback && fcallback()
  379. }
  380. })
  381. return
  382. }
  383. callback&&callback()
  384. }
  385. }