UITopMenu.ts 10.0 KB

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