import { VideoAdType } from "../../game/data/GameData"; import { IADSDKMgr } from "../sdk/ad/IADSDKMgr"; import JsbSystem from "./JsbSystem"; /** * 广告管理类脚本 * @description 显示/播放广告 * @author 冯聪 */ export default class AdSystem { /** 构造函数 * @param adSdk 传入当前使用广告sdk */ constructor(adSdk: IADSDKMgr) { this.adSdk = adSdk; // this.init(); } /** 广告sdk */ private adSdk: IADSDKMgr = null; /** 是否显示广告 */ public ifShowAd: boolean = true; /** 视频广告类型 */ public videoAdType: VideoAdType = VideoAdType.Null; /** 视频广告回调 */ public videoAdcallBack: Function = null; /** 初始化广告 */ public init() { //判定下平台 if (cc.sys.os == cc.sys.OS_ANDROID) { this.ifShowAd = true; } else { this.ifShowAd = false; } //初始化Topon广告SDK this.adSdk.initSDK(); } /** * 观看广告 * @param callBack 广告相关的回调 */ public watchAd(callBack: Function) { if (this.isAdCooling()) { return; } this.videoAdcallBack = callBack; if (!this.ifShowAd) { this.wacthAdSuccess() } else { if (cc.sys.platform != cc.sys.WECHAT_GAME) { if (cc.sys.os == cc.sys.OS_ANDROID) { // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop); this.adSdk.showVideo(); } else if (cc.sys.os == cc.sys.OS_IOS) { jsb.reflection.callStaticMethod("AppController", "showVideo", ""); } } else { // WxMgr.getInstance().showRewardAd(pop) } } } /** 观看广告失败 */ public watchAdFail() { mk.console.log("=== watchAdFail"); this.videoAdcallBack(false); } /** 观看广告取消 */ public watchAdCancel() { mk.console.log("=== watchAdCancel"); this.videoAdcallBack(false); } /** * 观看广告成功 * @param placementId 广告位id */ public wacthAdSuccess(placementId = '') { mk.console.log("=== wacthAdSuccess"); //广告过后,渲染会有问题,统一下一帧再处理逻辑 mk.ui.scheduleOnce(() => { if (!gData.adData.checkAdMax()) { this.videoAdcallBack(true); } this.initLastTimeStamp(); }, 0) } /** 显示原生广告 */ public showNative(type = 4) { if (!this.ifShowAd) { return; } if (cc.sys.platform != cc.sys.WECHAT_GAME) { if (cc.sys.os == cc.sys.OS_ANDROID) { // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop); this.adSdk.showNative(type); } else if (cc.sys.os == cc.sys.OS_IOS) { } } else { // WxMgr.getInstance().showRewardAd(pop) } } /** 销毁原生广告 */ public destroyNativeAd() { if (!this.ifShowAd) { return; } if (cc.sys.platform != cc.sys.WECHAT_GAME) { if (cc.sys.os == cc.sys.OS_ANDROID) { // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop); this.adSdk.destroyNative(); } else if (cc.sys.os == cc.sys.OS_IOS) { } } else { // WxMgr.getInstance().showRewardAd(pop) } } /** 上次播放视频时间戳,单位:毫秒 */ private last_time_stamp: number = 0; private initLastTimeStamp() { let new_date = new Date(); this.last_time_stamp = new_date.getTime(); } /** * 视频是否冷却 * @returns 视频冷却中 */ private isAdCooling(): boolean { let new_date = new Date(); let new_time_stamp = new_date.getTime(); let gap_time_stamp = new_time_stamp - this.last_time_stamp; if (gap_time_stamp <= 5000) { const time = 6 - Math.ceil(gap_time_stamp / 1000); mk.tip.pop('当前看视频太频繁了,请' + time + '秒后再试'); return true; } return false; } public showBanner() { if (!this.ifShowAd) { return; } // console.log("===[AdSystem gameData.adShowConfig", gData.gameData.adShowConfig.is_show_banner); // if (gData.gameData.adShowConfig && gData.gameData.adShowConfig.is_show_banner == 1) { this.adSdk.showBanner(); //} } public destoryBanner() { if (!this.ifShowAd) { return; } this.adSdk.destoryBanner(); } /** * showInterAd * @param type 0 插屏 1 全屏 */ public showInterAd(type: number) { if (!this.ifShowAd) { return; } this.adSdk.showInter(type); } /** * 根据几率检测弹出插屏 */ public checkShowInterByChance() { if (!this.ifShowAd) { return; } if (!gData.gameData.adShowConfig) { return; } let chance = gData.gameData.configs.ServerConfig.interOpenChance; if (!chance) { chance = 0.4; } if (Math.random() <= chance) { this.showInterAd(0); } else { gData.moreGame.popMoreGamePopNode(); } } }