UITopMenu.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. import auto_topMenu from "../../../ui/uidata/Interface/auto_topMenu";
  2. import UIBase from "../../../framework/ui/UIBase";
  3. import UIHelp from "../../../framework/ui/UIHelp";
  4. import AccountModel from "../../../data/Account/AccountModel";
  5. import { ATTACK_MODE, EVENT_TYPE, JOY_STATE } from "../../../gameLogic/utrl/gameEnum";
  6. import gameEventManager from "../../../gameLogic/utrl/gameEventManager";
  7. import UIPauseView from "./UIPauseView";
  8. import gameData from "../../../gameLogic/utrl/gameData";
  9. import levelManager from "../../../manager/levelManager";
  10. import bundleManager from "../../../manager/bundleManager";
  11. import Utils from "../../../framework/utils/utils";
  12. import EventManager from "../../../framework/event/EventManager";
  13. import { USER } from "../../../constant/constant-user";
  14. import UIWelfareView from "./UIWelfareView";
  15. import UIAirdropView from "./UIAirdropView";
  16. import UISignView from "./UISignView";
  17. import UIShopView from "./UIShopView";
  18. import SignUtil from "../../../framework/utils/signUtil";
  19. import AESUtil from "../../../AESUtil"
  20. import ATSDK from "../../../AnyThinkAds/ATJSSDK";
  21. import ATRewardedVideoSDK from "../../../AnyThinkAds/ATRewardedVideoJSSDK";
  22. import ATInterstitialSDK from "../../../AnyThinkAds/ATInterstitialJSSDK";
  23. import ATBannerJSSDK from "../../../AnyThinkAds/ATBannerJSSDK";
  24. import { AdType } from '../../../ATAndroidJS2';
  25. import AAJS2 from "../../../ATAndroidJS2";
  26. import GlobalManager from '../../../GlobalManager';
  27. import show = cc.show;
  28. const { ccclass, menu, property } = cc._decorator;
  29. const needRefreshTime = 10 * 60;
  30. @ccclass
  31. @menu("UI/Interface/UITopMenu")
  32. export default class UITopMenu extends UIBase {
  33. ui: auto_topMenu = null;
  34. protected static prefabUrl = "Interface/topMenu";
  35. protected static className = "UITopMenu";
  36. private static _instance: UITopMenu;
  37. curAttackMode: string;
  38. startDayTime: number = null;
  39. reducePowerTime: number = null;
  40. maskOpacity: number = 0;
  41. maskNode: cc.Node;
  42. redWarn: cc.Node;
  43. houMenCount:number=0;
  44. // 食物检查
  45. isReducePowerViewed = false;
  46. cacheTimeByPower = 0;
  47. //子弹检查
  48. isReduceBulletViewed = false;
  49. cacheTimeByBullet = 0;
  50. //开启ui冷却时间
  51. isCool = false;
  52. public static getInstance(): UITopMenu {
  53. return UITopMenu._instance;
  54. }
  55. static setInstance(instance: UITopMenu) {
  56. UITopMenu._instance = instance;
  57. }
  58. onUILoad() {
  59. UITopMenu.setInstance(this);
  60. this.node.active = false;
  61. //this.initAdInster();
  62. this.initAdForPageInter();
  63. }
  64. //add判断插屏 开屏等,需要多次准备
  65. initAdForPageInter() {
  66. if (cc.sys.os === cc.sys.OS_ANDROID) {
  67. console.log('zh:initAdForPageInter sml .....');
  68. let deviceId = AAJS2.getDeviceUserId();
  69. var setting = {};
  70. setting[ATRewardedVideoSDK.userIdKey] = deviceId;
  71. //add判断插屏 开屏等
  72. let pid_inter = AAJS2.getPlacementId2(AdType.Inter);
  73. console.log("zh: pid_inter sml checkstatus:", ATInterstitialSDK.checkAdStatus(pid_inter));
  74. ATInterstitialSDK.loadInterstitial(pid_inter, setting);
  75. }
  76. }
  77. /**
  78. * 显示插屏广告
  79. * @param adMarkForClose 插屏广告关闭后调用那个方法的具体指示标识
  80. * @param callback 插屏广告关闭(加载失败\非ANDROID平台)的回调
  81. */
  82. private showInterAd(adMarkForClose: string, callback: () => void) {
  83. if (cc.sys.os === cc.sys.OS_ANDROID) {
  84. cc.sys.localStorage.setItem("adMarkForClose", adMarkForClose);//关闭后,调用哪个方法的标识
  85. //add判断插屏 开屏等
  86. let pid_inter = AAJS2.getPlacementId2(AdType.Inter);
  87. let boo = ATInterstitialSDK.hasAdReady(pid_inter);
  88. if (boo) {
  89. console.log('zh:initAdInster AD OK');
  90. ATInterstitialSDK.showAd(pid_inter);
  91. //因为跨脚本,所以使用全局注册函数调用,后来发现切换APP,导致ad close事件失效
  92. callback();
  93. } else {
  94. console.log('zh:initAdInster AD 没有准备好');
  95. GlobalManager.getInstance().callMethod('initAdForPageInter');
  96. callback();
  97. }
  98. } else {
  99. callback();
  100. }
  101. }
  102. _updateBulletNum(isSub) {
  103. this.ui.bullet_num.getComponent(cc.Label).string = gameData.curBulletNum.toString();
  104. this.scheduleOnce(() => {
  105. if (gameData.curBulletNum <= 0) {
  106. gameData.cur_attack_mode = ATTACK_MODE.close;
  107. gameEventManager.emit(EVENT_TYPE.attack_mode);
  108. UIHelp.ShowTips('Ammo depleted, switched to melee mode.');//当前子弹为0,已自动切换成近战模式...
  109. }
  110. }, 0.2)
  111. this.isRedLabel();
  112. this.tryShowAirdrop(isSub);
  113. }
  114. private tryShowAirdrop(isSub) {
  115. if (true) {
  116. //关闭
  117. return;
  118. }
  119. if (this.isCool){
  120. return;
  121. }
  122. if (!isSub){
  123. return;
  124. }
  125. if (!AccountModel.getInstance().first_dec_bullet) {
  126. AccountModel.getInstance().first_dec_bullet = true;
  127. this.clickAirDrop();
  128. this.isCool = true;
  129. this.scheduleOnce(() => {
  130. this.isCool = false;
  131. },30)
  132. return;
  133. }
  134. }
  135. _updatePeopleNum() {
  136. this.ui.role_num.getComponent(cc.Label).string = gameData.curPeopleNum.toString();
  137. if (gameData.curPeopleNum <= 0) {
  138. gameEventManager.emit(EVENT_TYPE.GAME_OVER);
  139. }
  140. }
  141. _updateDayNum() {
  142. this.ui.day_num.getComponent(cc.Label).string = gameData.curDay.toString();
  143. }
  144. _updateTiliNum() {
  145. this.ui.power_num.getComponent(cc.Label).string = gameData.curPowerNum.toString();
  146. if (gameData.curPowerNum <= 5) {
  147. this.redWarn.active = true;
  148. UIHelp.ShowTips('Low stamina, risk of death! Find food urgently!');//体力不足,即将阵亡,请尽快觅食充饥!!!
  149. } else {
  150. this.redWarn.active = false;
  151. }
  152. if (gameData.curPowerNum <= 0) {
  153. gameEventManager.emit(EVENT_TYPE.GAME_OVER);
  154. }
  155. }
  156. onStart() {
  157. //this.node.zIndex = cc.macro.MAX_ZINDEX;
  158. this.maskNode = cc.director.getScene().getChildByName('Canvas').getChildByName('maskNode');
  159. this.redWarn = cc.director.getScene().getChildByName('Canvas').getChildByName('redWarn');
  160. this.initData();
  161. }
  162. onShow() {
  163. this.ui = this.node.addComponent(auto_topMenu);
  164. //关闭阻塞
  165. this.unRegisterEvent(this.node, this.touchEvent, this);
  166. this.initEvent(EVENT_TYPE.UPDATE_BULLET_NUM, this._updateBulletNum);
  167. this.initEvent(EVENT_TYPE.UPDATE_PEOPLE_NUM, this._updatePeopleNum);
  168. this.initEvent(EVENT_TYPE.UPDATE_DAY_NUM, this._updateDayNum);
  169. this.initEvent(EVENT_TYPE.UPDATE_TILI_NUM, this._updateTiliNum);
  170. this.updateView();
  171. this.tipHandler();
  172. }
  173. initData() {
  174. this.startDayTime = Utils.getTimeStamp(); //开始计时天数的时间
  175. this.reducePowerTime = Utils.getTimeStamp(); //开始消耗体力的时间
  176. gameData.isDayTime = true;
  177. this.node.$Sun_sp.active = true;
  178. this.node.$Moon_sp.active = false;
  179. }
  180. updateView() {
  181. this.ui.role_num.getComponent(cc.Label).string = gameData.curPeopleNum.toString();
  182. this.ui.bullet_num.getComponent(cc.Label).string = gameData.curBulletNum.toString();
  183. this.ui.power_num.getComponent(cc.Label).string = gameData.curPowerNum.toString();
  184. this.ui.day_num.getComponent(cc.Label).string = gameData.curDay.toString();
  185. this.initSp();
  186. this.isRedLabel();
  187. }
  188. //子弹不足显示红字
  189. isRedLabel() {
  190. let curBullet = gameData.curBulletNum;
  191. if (curBullet <= 10) {
  192. this.ui.bullet_num.color = cc.Color.RED;
  193. } else {
  194. this.ui.bullet_num.color = cc.Color.WHITE;
  195. }
  196. }
  197. initSp() {
  198. if (this.curAttackMode == gameData.cur_attack_mode) {
  199. return;
  200. }
  201. if (gameData.cur_attack_mode == ATTACK_MODE.close) {
  202. this.curAttackMode = ATTACK_MODE.close;
  203. bundleManager.setBundleFrame('UI', 'topMenu/ren1', this.ui.role_sp)
  204. } else if (gameData.cur_attack_mode == ATTACK_MODE.remote) {
  205. this.curAttackMode = ATTACK_MODE.remote;
  206. bundleManager.setBundleFrame('UI', 'topMenu/ren2', this.ui.role_sp)
  207. }
  208. }
  209. inSun() {
  210. this.node.$Sun_sp.active = true;
  211. this.node.$Moon_sp.active = false;
  212. cc.tween(this.maskNode)
  213. .to(0.5, { opacity: 0 })
  214. .start()
  215. gameEventManager.emit(EVENT_TYPE.VIOLENT_ZOMBIE);
  216. }
  217. inNight() {
  218. this.node.$Sun_sp.active = false;
  219. this.node.$Moon_sp.active = true;
  220. cc.tween(this.maskNode)
  221. .to(0.5, { opacity: 255 })
  222. .start()
  223. gameEventManager.emit(EVENT_TYPE.VIOLENT_ZOMBIE);
  224. Utils.tipsSprite('main/tianheile', cc.director.getScene().getChildByName('Canvas'));
  225. }
  226. pause_btn() {
  227. cc.director.pause();
  228. UIHelp.ShowUI(UIPauseView);
  229. }
  230. onClose() {
  231. UIHelp.CloseUI(UITopMenu);
  232. }
  233. judgeDayState() {
  234. if (!this.startDayTime || !this.reducePowerTime) {
  235. return;
  236. }
  237. let curTime = Utils.getTimeStamp();
  238. let durTime = curTime - this.startDayTime;
  239. let isDayTime = gameData.isDayTime;
  240. // if (!isDayTime) {
  241. // this.maskNode.opacity -= 255 / (USER.nightTimes * 60);
  242. // } else {
  243. // this.maskNode.opacity += 255 / (USER.dayTimes * 60);
  244. // }
  245. //白变黑
  246. if (isDayTime && (durTime >= USER.dayTimes)) {
  247. //进入黑夜
  248. gameData.isDayTime = false;
  249. durTime = 0;
  250. this.startDayTime = Utils.getTimeStamp();
  251. this.inNight();
  252. }
  253. //黑变白
  254. if (!isDayTime && (durTime >= USER.nightTimes)) {
  255. //进入白天
  256. gameData.isDayTime = true;
  257. durTime = 0;
  258. this.startDayTime = Utils.getTimeStamp();
  259. gameData.curDay += 1;
  260. //增加英雄
  261. if (gameData.curDay % 7 == 0) {
  262. gameData.bAddBoss = true;
  263. gameEventManager.emit(EVENT_TYPE.ADD_BOSS);
  264. gameEventManager.emit(EVENT_TYPE.ADD_HERO);
  265. }
  266. //this._updateDayNum();
  267. this.inSun();
  268. }
  269. }
  270. update(dt) {
  271. this.reducePower()
  272. this.judgeDayState()
  273. }
  274. reducePower() {
  275. let curTime = Utils.getTimeStamp();
  276. let duringTime = curTime - this.reducePowerTime;
  277. if (duringTime >= 10) {
  278. this.reducePowerTime = Utils.getTimeStamp();
  279. if (gameData.curPowerNum > 0) {
  280. let reduceNum = 1 * gameData.curPeopleNum;
  281. gameData.curPowerNum -= reduceNum;
  282. this.ui.reducePower_num.active = true;
  283. this.ui.reducePower_num.getComponent(cc.Label).string = '-' + reduceNum;
  284. setTimeout(() => {
  285. this.ui.reducePower_num.active = false;
  286. }, 1000)
  287. this._updateTiliNum();
  288. }
  289. }
  290. }
  291. tipHandler() {
  292. this.notifyNode(this.ui.tip_drop);
  293. this.notifyNode(this.ui.tip_shop);
  294. this.notifyNode(this.ui.tip_welfare);
  295. this.notifyNode(this.ui.tip_sign);
  296. // 30秒,不用太精准
  297. this.schedule(() => {
  298. if (true) {
  299. //关闭
  300. return;
  301. }
  302. if (AccountModel.getInstance().curLevel) {
  303. this.checkUI();
  304. }
  305. },120);
  306. }
  307. checkUI(){
  308. this.checkShop();
  309. this.checkAirDrop();
  310. }
  311. checkAirDrop(){
  312. let airdrop_time = AccountModel.getInstance().airdrop_time;
  313. //不同时间戳
  314. let notTime = airdrop_time != this.cacheTimeByBullet;
  315. if (!airdrop_time) {
  316. this.clickAirDrop();
  317. return;
  318. }
  319. let curTime = Utils.getTimeStamp();
  320. let time = needRefreshTime - (curTime - airdrop_time);
  321. if (time <= 1
  322. &&notTime) {
  323. this.cacheTimeByBullet = airdrop_time;
  324. this.clickAirDrop();
  325. }
  326. }
  327. checkShop(){
  328. let shopTime = AccountModel.getInstance().shop_time;
  329. //不同时间戳
  330. let notTime = shopTime != this.cacheTimeByPower;
  331. if (!shopTime) {
  332. this.clickShop();
  333. return;
  334. }
  335. let curTime = Utils.getTimeStamp();
  336. //写死
  337. let time = 5 * 60 - (curTime - shopTime);
  338. if (time <= 1
  339. && notTime){
  340. this.cacheTimeByPower = shopTime;
  341. this.clickShop();
  342. }
  343. }
  344. notifyNode(node:cc.Node){
  345. cc.tween(node)
  346. .repeatForever(
  347. cc.tween(node)
  348. .to(0.8,{scale:0.5,opacity:0})
  349. .to(0.8,{scale:1,opacity:255})
  350. )
  351. .start();
  352. }
  353. checkSign() {
  354. const canSign = SignUtil.canSign();
  355. this.ui.tip_sign.active = canSign;
  356. if (canSign) {
  357. gameEventManager.on(EVENT_TYPE.OFF_SIGN_TIP, () => {
  358. this.ui.tip_sign.active = false;
  359. })
  360. }
  361. }
  362. checkWelfare(){
  363. const curWelfare = AccountModel.getInstance().curWelfare;
  364. this.ui.tip_welfare.active = curWelfare.length < 4;
  365. }
  366. clickAirDrop() {
  367. zjSdk?.sendEvent('界面曝光-空投')
  368. // gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  369. // UIHelp.ShowUI(UIAirdropView);
  370. this.showInterAd("callBackForClickDrop", () => this.callBackForClickDrop());
  371. }
  372. callBackForClickDrop(){
  373. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  374. UIHelp.ShowUI(UIAirdropView);
  375. }
  376. clickSign() {
  377. zjSdk?.sendEvent('界面曝光-签到')
  378. // gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  379. // UIHelp.ShowUI(UISignView);
  380. this.showInterAd("callBackForClickSign", () => this.callBackForClickSign());
  381. }
  382. callBackForClickSign(){
  383. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  384. UIHelp.ShowUI(UISignView);
  385. }
  386. clickShop() {
  387. zjSdk?.sendEvent('界面曝光-超市')
  388. // gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  389. // UIHelp.ShowUI(UIShopView);
  390. //虽然不是最佳,但也无妨
  391. GlobalManager.getInstance().registerMethod('callBackForClickShop', this.callBackForClickShop.bind(this));
  392. this.showInterAd("callBackForClickShop", () => this.callBackForClickShop());
  393. }
  394. callBackForClickShop() {
  395. zjSdk?.sendEvent('zh:回调callBackForClickShop被触发')
  396. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  397. UIHelp.ShowUI(UIShopView);
  398. }
  399. clickWelfare() {
  400. zjSdk?.sendEvent('界面曝光-幸存者')
  401. // gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  402. // UIHelp.ShowUI(UIWelfareView);
  403. this.showInterAd("callBackForClickWelfare", () => this.callBackForClickWelfare());
  404. }
  405. callBackForClickWelfare(){
  406. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  407. UIHelp.ShowUI(UIWelfareView);
  408. }
  409. //游戏后门
  410. clickZhDiyDebug() {
  411. let c = this.houMenCount;
  412. this.houMenCount =c+1;
  413. if(c<3){
  414. console.log('zh:后门第'+c+'次');
  415. return;
  416. }
  417. console.log('zh:点击了后门入口');
  418. //UIHelp.ShowTips('后门入口!');
  419. gameData.curBulletNum =9999;//子弹增加
  420. gameData.curPowerNum =999;
  421. AccountModel.getInstance().reliveNum=99;
  422. UIHelp.ShowTips('后门子弹\体力\复活次=99 ');
  423. // this.curDay = AccountModel.getInstance().curDay; //当前天数
  424. // this.curPowerNum = AccountModel.getInstance().curPower; //当前体力
  425. // this._curBulletNum = AccountModel.getInstance().curBullet;//当前子弹数
  426. // this.curPeopleNum = 0;//当前玩家数
  427. // this.peopleDieNum = 0;//玩家死亡数
  428. // this.curDiePeopleNum = 0; //当前死亡玩家数
  429. // this._curKillZomNum = AccountModel.getInstance().curKillZomNum; //当前已杀死的僵尸数
  430. // this.curKillBossNum = 0; //当前杀死的boss数
  431. // this.bAddBoss = false;
  432. // //-----------------------------以下切换副本界面需要用到的数据------------------------------------------------------//
  433. // this.playerDtList = [];
  434. // //this.isDayTime = true; //是否是白天
  435. // this.curInDungeonType = null; //当前进入的房间类型
  436. }
  437. }