Hall.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { _decorator, Component, director, Node, profiler } from 'cc';
  2. import { ResUtil } from '../../core/util/ResUtil';
  3. import TableLoadUtil from '../../core/util/TableLoadUtil';
  4. import get_new_head_icon from '../../core/util_class/HeadIcon';
  5. import { TableUtil } from '../../module_extra/table_ts/TableUtil';
  6. import { ModuleDef } from '../../Scripts/ModuleDef';
  7. import { SceneDef } from '../../Scripts/SceneDef';
  8. import PlayerData from '../game/PlayerData';
  9. import { ch } from '../../ch/ch';
  10. import { gui } from '../../core/ui/ui';
  11. import { UI_Hall } from '../ui/UI_Hall/UI_Hall';
  12. const { ccclass, property } = _decorator;
  13. export enum GameState {
  14. gameing,
  15. wait,
  16. win,
  17. fail
  18. }
  19. export const life_countdown_Max = 1800;
  20. @ccclass('Hall')
  21. export class Hall extends Component {
  22. private static instance: Hall;
  23. public player: PlayerData;
  24. public head_icon = get_new_head_icon();
  25. sceneChanging: boolean = false;//场景切换
  26. bornRunning: boolean = false; // 是否在生成中
  27. public gameState: GameState = GameState.win;
  28. firstEnter: boolean = false;
  29. public static getInstance(): Hall {
  30. return Hall.instance;
  31. }
  32. protected onEnable(): void {
  33. // 计算离线时间,并更新倒计时
  34. this.startLifeTimer();
  35. }
  36. protected onLoad(): void {
  37. window.addEventListener('beforeunload', async () => {
  38. let res = Date.now();
  39. if (res) {
  40. const nowTimestamp = Math.floor(res / 1000); // 记录当前时间(秒)
  41. this.player.set_last_exit_time(nowTimestamp);
  42. if (this.gameState == GameState.fail || this.gameState == GameState.wait || this.gameState == GameState.gameing) {
  43. if (this.player.get_life() > 0) {
  44. this.player.set_life(this.player.get_life() - 1);
  45. }
  46. }
  47. }
  48. });
  49. window['wx'].onShow(async () => {
  50. let res = Date.now();
  51. if (res) {
  52. const nowTimestamp = Math.floor(res / 1000); // 记录当前时间(秒)
  53. this.player.set_last_exit_time(nowTimestamp);
  54. if (this.gameState == GameState.wait || this.gameState == GameState.gameing) {
  55. this.player.set_life(this.player.get_life() + 1);
  56. }
  57. }
  58. })
  59. window['wx'].onHide(async () => {
  60. let res = Date.now();
  61. if (res) {
  62. const nowTimestamp = Math.floor(res / 1000); // 记录当前时间(秒)
  63. this.player.set_last_exit_time(nowTimestamp);
  64. if (this.gameState == GameState.fail || this.gameState == GameState.wait || this.gameState == GameState.gameing) {
  65. if (this.player.get_life() > 0) {
  66. this.player.set_life(this.player.get_life() - 1);
  67. }
  68. }
  69. }
  70. })
  71. }
  72. async start() {
  73. if (!Hall.instance) {
  74. Hall.instance = this;
  75. }
  76. this.init();
  77. director.addPersistRootNode(this.node);
  78. // if(chsdk.checkFromSidebar())
  79. // {
  80. // Hall.getInstance().player.add_item(2,2);
  81. // }
  82. }
  83. async init() {
  84. await this.loadTable();
  85. this.player = PlayerData.getInstance(ch.sdk.get_gid(), ch.sdk.get_uid().toString());
  86. await this.player.init_user_info();
  87. await this.player.load();
  88. this.player.set_login_num(1);
  89. if (this.player.get_max_floor() == 0) {
  90. this.firstEnter = true;
  91. Hall.getInstance().player.set_life(5);
  92. ResUtil.loadScene(SceneDef.GAME, ModuleDef.GAME, true);
  93. } else {
  94. gui.show(UI_Hall);
  95. }
  96. await this.calculateOfflineTime();
  97. this.startLifeTimer();
  98. }
  99. private loadTable(): void {
  100. ch.log.log_start("加载配置初始化");
  101. TableLoadUtil.preloadAll(ModuleDef.EXTRA, "table_json", async () => {
  102. }, TableUtil.set);
  103. }
  104. private async calculateOfflineTime() {
  105. const lastExitTime = this.player.get_last_exit_time();
  106. console.log(`上次退出时间: ${new Date(lastExitTime * 1000)}`);
  107. if (!lastExitTime) return;
  108. const lastExitTimestamp = lastExitTime;
  109. let res = Date.now();
  110. if (res) {
  111. const nowTimestamp = Math.floor(res / 1000); // 以秒为单位
  112. console.log(`当前时间: ${new Date(nowTimestamp * 1000)}`);
  113. const elapsedTime = nowTimestamp - lastExitTimestamp;
  114. console.log(`离线时间: ${elapsedTime} 秒`);
  115. let countdown = this.player.get_life_countdown();
  116. this.player.set_life(this.player.get_life() + Math.floor(elapsedTime / life_countdown_Max));
  117. if (this.player.get_life() >= 10) {
  118. this.player.set_life_countdown(0);
  119. } else {
  120. this.player.set_life_countdown((countdown - elapsedTime) % life_countdown_Max);
  121. }
  122. }
  123. }
  124. startLifeTimer() {
  125. this.unschedule(this.LifeCountDownTimer);
  126. this.schedule(this.LifeCountDownTimer, 1.0);
  127. }
  128. LifeCountDownTimer() {
  129. if (!this.player) {
  130. console.warn("Player 未初始化,跳过倒计时");
  131. return;
  132. }
  133. console.log('计时器启动');
  134. if (this.player.get_life() < 5) {
  135. if (this.player.get_life_countdown() > 0) {
  136. console.log("正在执行倒计时:" + (this.player.get_life_countdown() - 1));
  137. this.player.set_life_countdown(this.player.get_life_countdown() - 1);
  138. if (director.getScene().name == "hall") {
  139. console.log("倒计时显示刷新:" + this.player.get_life_countdown());
  140. gui.get(UI_Hall)?.set_Life_CountDown(this.player.get_life_countdown(), true);
  141. }
  142. } else {
  143. this.player.set_life(this.player.get_life() + 1);
  144. gui.get(UI_Hall)?.set_Life(this.player.get_life());
  145. if (this.player.get_life() != 5) {
  146. this.player.set_life_countdown(life_countdown_Max);
  147. gui.get(UI_Hall)?.set_Life_CountDown(this.player.get_life_countdown(), true);
  148. }
  149. }
  150. } else {
  151. if (director.getScene().name == "hall") {
  152. gui.get(UI_Hall)?.set_Life_CountDown(this.player.get_life_countdown(), false);
  153. }
  154. }
  155. }
  156. }