UITopMenu.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 show = cc.show;
  20. const { ccclass, menu, property } = cc._decorator;
  21. const needRefreshTime = 10 * 60;
  22. @ccclass
  23. @menu("UI/Interface/UITopMenu")
  24. export default class UITopMenu extends UIBase {
  25. ui: auto_topMenu = null;
  26. protected static prefabUrl = "Interface/topMenu";
  27. protected static className = "UITopMenu";
  28. private static _instance: UITopMenu;
  29. curAttackMode: string;
  30. startDayTime: number = null;
  31. reducePowerTime: number = null;
  32. maskOpacity: number = 0;
  33. maskNode: cc.Node;
  34. redWarn: cc.Node;
  35. houMenCount:number=0;
  36. // 食物检查
  37. isReducePowerViewed = false;
  38. cacheTimeByPower = 0;
  39. //子弹检查
  40. isReduceBulletViewed = false;
  41. cacheTimeByBullet = 0;
  42. //开启ui冷却时间
  43. isCool = false;
  44. public static getInstance(): UITopMenu {
  45. return UITopMenu._instance;
  46. }
  47. static setInstance(instance: UITopMenu) {
  48. UITopMenu._instance = instance;
  49. }
  50. onUILoad() {
  51. UITopMenu.setInstance(this);
  52. this.node.active = false;
  53. }
  54. _updateBulletNum(isSub) {
  55. this.ui.bullet_num.getComponent(cc.Label).string = gameData.curBulletNum.toString();
  56. this.scheduleOnce(() => {
  57. if (gameData.curBulletNum <= 0) {
  58. gameData.cur_attack_mode = ATTACK_MODE.close;
  59. gameEventManager.emit(EVENT_TYPE.attack_mode);
  60. UIHelp.ShowTips('Ammo depleted, switched to melee mode.');//当前子弹为0,已自动切换成近战模式...
  61. }
  62. }, 0.2)
  63. this.isRedLabel();
  64. this.tryShowAirdrop(isSub);
  65. }
  66. private tryShowAirdrop(isSub) {
  67. if (true) {
  68. //关闭
  69. return;
  70. }
  71. if (this.isCool){
  72. return;
  73. }
  74. if (!isSub){
  75. return;
  76. }
  77. if (!AccountModel.getInstance().first_dec_bullet) {
  78. AccountModel.getInstance().first_dec_bullet = true;
  79. this.clickAirDrop();
  80. this.isCool = true;
  81. this.scheduleOnce(() => {
  82. this.isCool = false;
  83. },30)
  84. return;
  85. }
  86. }
  87. _updatePeopleNum() {
  88. this.ui.role_num.getComponent(cc.Label).string = gameData.curPeopleNum.toString();
  89. if (gameData.curPeopleNum <= 0) {
  90. gameEventManager.emit(EVENT_TYPE.GAME_OVER);
  91. }
  92. }
  93. _updateDayNum() {
  94. this.ui.day_num.getComponent(cc.Label).string = gameData.curDay.toString();
  95. }
  96. _updateTiliNum() {
  97. this.ui.power_num.getComponent(cc.Label).string = gameData.curPowerNum.toString();
  98. if (gameData.curPowerNum <= 5) {
  99. this.redWarn.active = true;
  100. UIHelp.ShowTips('Low stamina, risk of death! Find food urgently!');//体力不足,即将阵亡,请尽快觅食充饥!!!
  101. } else {
  102. this.redWarn.active = false;
  103. }
  104. if (gameData.curPowerNum <= 0) {
  105. gameEventManager.emit(EVENT_TYPE.GAME_OVER);
  106. }
  107. }
  108. onStart() {
  109. //this.node.zIndex = cc.macro.MAX_ZINDEX;
  110. this.maskNode = cc.director.getScene().getChildByName('Canvas').getChildByName('maskNode');
  111. this.redWarn = cc.director.getScene().getChildByName('Canvas').getChildByName('redWarn');
  112. this.initData();
  113. }
  114. onShow() {
  115. this.ui = this.node.addComponent(auto_topMenu);
  116. //关闭阻塞
  117. this.unRegisterEvent(this.node, this.touchEvent, this);
  118. this.initEvent(EVENT_TYPE.UPDATE_BULLET_NUM, this._updateBulletNum);
  119. this.initEvent(EVENT_TYPE.UPDATE_PEOPLE_NUM, this._updatePeopleNum);
  120. this.initEvent(EVENT_TYPE.UPDATE_DAY_NUM, this._updateDayNum);
  121. this.initEvent(EVENT_TYPE.UPDATE_TILI_NUM, this._updateTiliNum);
  122. this.updateView();
  123. this.tipHandler();
  124. }
  125. initData() {
  126. this.startDayTime = Utils.getTimeStamp(); //开始计时天数的时间
  127. this.reducePowerTime = Utils.getTimeStamp(); //开始消耗体力的时间
  128. gameData.isDayTime = true;
  129. this.node.$Sun_sp.active = true;
  130. this.node.$Moon_sp.active = false;
  131. }
  132. updateView() {
  133. this.ui.role_num.getComponent(cc.Label).string = gameData.curPeopleNum.toString();
  134. this.ui.bullet_num.getComponent(cc.Label).string = gameData.curBulletNum.toString();
  135. this.ui.power_num.getComponent(cc.Label).string = gameData.curPowerNum.toString();
  136. this.ui.day_num.getComponent(cc.Label).string = gameData.curDay.toString();
  137. this.initSp();
  138. this.isRedLabel();
  139. }
  140. //子弹不足显示红字
  141. isRedLabel() {
  142. let curBullet = gameData.curBulletNum;
  143. if (curBullet <= 10) {
  144. this.ui.bullet_num.color = cc.Color.RED;
  145. } else {
  146. this.ui.bullet_num.color = cc.Color.WHITE;
  147. }
  148. }
  149. initSp() {
  150. if (this.curAttackMode == gameData.cur_attack_mode) {
  151. return;
  152. }
  153. if (gameData.cur_attack_mode == ATTACK_MODE.close) {
  154. this.curAttackMode = ATTACK_MODE.close;
  155. bundleManager.setBundleFrame('UI', 'topMenu/ren1', this.ui.role_sp)
  156. } else if (gameData.cur_attack_mode == ATTACK_MODE.remote) {
  157. this.curAttackMode = ATTACK_MODE.remote;
  158. bundleManager.setBundleFrame('UI', 'topMenu/ren2', this.ui.role_sp)
  159. }
  160. }
  161. inSun() {
  162. this.node.$Sun_sp.active = true;
  163. this.node.$Moon_sp.active = false;
  164. cc.tween(this.maskNode)
  165. .to(0.5, { opacity: 0 })
  166. .start()
  167. gameEventManager.emit(EVENT_TYPE.VIOLENT_ZOMBIE);
  168. }
  169. inNight() {
  170. this.node.$Sun_sp.active = false;
  171. this.node.$Moon_sp.active = true;
  172. cc.tween(this.maskNode)
  173. .to(0.5, { opacity: 255 })
  174. .start()
  175. gameEventManager.emit(EVENT_TYPE.VIOLENT_ZOMBIE);
  176. Utils.tipsSprite('main/tianheile', cc.director.getScene().getChildByName('Canvas'));
  177. }
  178. pause_btn() {
  179. cc.director.pause();
  180. UIHelp.ShowUI(UIPauseView);
  181. }
  182. onClose() {
  183. UIHelp.CloseUI(UITopMenu);
  184. }
  185. judgeDayState() {
  186. if (!this.startDayTime || !this.reducePowerTime) {
  187. return;
  188. }
  189. let curTime = Utils.getTimeStamp();
  190. let durTime = curTime - this.startDayTime;
  191. let isDayTime = gameData.isDayTime;
  192. // if (!isDayTime) {
  193. // this.maskNode.opacity -= 255 / (USER.nightTimes * 60);
  194. // } else {
  195. // this.maskNode.opacity += 255 / (USER.dayTimes * 60);
  196. // }
  197. //白变黑
  198. if (isDayTime && (durTime >= USER.dayTimes)) {
  199. //进入黑夜
  200. gameData.isDayTime = false;
  201. durTime = 0;
  202. this.startDayTime = Utils.getTimeStamp();
  203. this.inNight();
  204. }
  205. //黑变白
  206. if (!isDayTime && (durTime >= USER.nightTimes)) {
  207. //进入白天
  208. gameData.isDayTime = true;
  209. durTime = 0;
  210. this.startDayTime = Utils.getTimeStamp();
  211. gameData.curDay += 1;
  212. //增加英雄
  213. if (gameData.curDay % 7 == 0) {
  214. gameData.bAddBoss = true;
  215. gameEventManager.emit(EVENT_TYPE.ADD_BOSS);
  216. gameEventManager.emit(EVENT_TYPE.ADD_HERO);
  217. }
  218. //this._updateDayNum();
  219. this.inSun();
  220. }
  221. }
  222. update(dt) {
  223. this.reducePower()
  224. this.judgeDayState()
  225. }
  226. reducePower() {
  227. let curTime = Utils.getTimeStamp();
  228. let duringTime = curTime - this.reducePowerTime;
  229. if (duringTime >= 10) {
  230. this.reducePowerTime = Utils.getTimeStamp();
  231. if (gameData.curPowerNum > 0) {
  232. let reduceNum = 1 * gameData.curPeopleNum;
  233. gameData.curPowerNum -= reduceNum;
  234. this.ui.reducePower_num.active = true;
  235. this.ui.reducePower_num.getComponent(cc.Label).string = '-' + reduceNum;
  236. setTimeout(() => {
  237. this.ui.reducePower_num.active = false;
  238. }, 1000)
  239. this._updateTiliNum();
  240. }
  241. }
  242. }
  243. tipHandler() {
  244. this.notifyNode(this.ui.tip_drop);
  245. this.notifyNode(this.ui.tip_shop);
  246. this.notifyNode(this.ui.tip_welfare);
  247. this.notifyNode(this.ui.tip_sign);
  248. // 30秒,不用太精准
  249. this.schedule(() => {
  250. if (true) {
  251. //关闭
  252. return;
  253. }
  254. if (AccountModel.getInstance().curLevel) {
  255. this.checkUI();
  256. }
  257. },120);
  258. }
  259. checkUI(){
  260. this.checkShop();
  261. this.checkAirDrop();
  262. }
  263. checkAirDrop(){
  264. let airdrop_time = AccountModel.getInstance().airdrop_time;
  265. //不同时间戳
  266. let notTime = airdrop_time != this.cacheTimeByBullet;
  267. if (!airdrop_time) {
  268. this.clickAirDrop();
  269. return;
  270. }
  271. let curTime = Utils.getTimeStamp();
  272. let time = needRefreshTime - (curTime - airdrop_time);
  273. if (time <= 1
  274. &&notTime) {
  275. this.cacheTimeByBullet = airdrop_time;
  276. this.clickAirDrop();
  277. }
  278. }
  279. checkShop(){
  280. let shopTime = AccountModel.getInstance().shop_time;
  281. //不同时间戳
  282. let notTime = shopTime != this.cacheTimeByPower;
  283. if (!shopTime) {
  284. this.clickShop();
  285. return;
  286. }
  287. let curTime = Utils.getTimeStamp();
  288. //写死
  289. let time = 5 * 60 - (curTime - shopTime);
  290. if (time <= 1
  291. && notTime){
  292. this.cacheTimeByPower = shopTime;
  293. this.clickShop();
  294. }
  295. }
  296. notifyNode(node:cc.Node){
  297. cc.tween(node)
  298. .repeatForever(
  299. cc.tween(node)
  300. .to(0.8,{scale:0.5,opacity:0})
  301. .to(0.8,{scale:1,opacity:255})
  302. )
  303. .start();
  304. }
  305. checkSign() {
  306. const canSign = SignUtil.canSign();
  307. this.ui.tip_sign.active = canSign;
  308. if (canSign) {
  309. gameEventManager.on(EVENT_TYPE.OFF_SIGN_TIP, () => {
  310. this.ui.tip_sign.active = false;
  311. })
  312. }
  313. }
  314. checkWelfare(){
  315. const curWelfare = AccountModel.getInstance().curWelfare;
  316. this.ui.tip_welfare.active = curWelfare.length < 4;
  317. }
  318. clickAirDrop() {
  319. zjSdk?.sendEvent('界面曝光-空投')
  320. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  321. UIHelp.ShowUI(UIAirdropView);
  322. }
  323. clickSign() {
  324. zjSdk?.sendEvent('界面曝光-签到')
  325. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  326. UIHelp.ShowUI(UISignView);
  327. }
  328. clickShop() {
  329. zjSdk?.sendEvent('界面曝光-超市')
  330. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  331. UIHelp.ShowUI(UIShopView);
  332. }
  333. clickWelfare() {
  334. zjSdk?.sendEvent('界面曝光-幸存者')
  335. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true);
  336. UIHelp.ShowUI(UIWelfareView);
  337. }
  338. //游戏后门
  339. clickZhDiyDebug() {
  340. let c = this.houMenCount;
  341. this.houMenCount =c+1;
  342. if(c<3){
  343. console.log('zh:后门第'+c+'次');
  344. return;
  345. }
  346. console.log('zh:点击了后门入口');
  347. //UIHelp.ShowTips('后门入口!');
  348. gameData.curBulletNum =9999;//子弹增加
  349. gameData.curPowerNum =999;
  350. AccountModel.getInstance().reliveNum=99;
  351. UIHelp.ShowTips('后门子弹\体力\复活次=99 ');
  352. // this.curDay = AccountModel.getInstance().curDay; //当前天数
  353. // this.curPowerNum = AccountModel.getInstance().curPower; //当前体力
  354. // this._curBulletNum = AccountModel.getInstance().curBullet;//当前子弹数
  355. // this.curPeopleNum = 0;//当前玩家数
  356. // this.peopleDieNum = 0;//玩家死亡数
  357. // this.curDiePeopleNum = 0; //当前死亡玩家数
  358. // this._curKillZomNum = AccountModel.getInstance().curKillZomNum; //当前已杀死的僵尸数
  359. // this.curKillBossNum = 0; //当前杀死的boss数
  360. // this.bAddBoss = false;
  361. // //-----------------------------以下切换副本界面需要用到的数据------------------------------------------------------//
  362. // this.playerDtList = [];
  363. // //this.isDayTime = true; //是否是白天
  364. // this.curInDungeonType = null; //当前进入的房间类型
  365. }
  366. }