import { _decorator, Component, director, Node, profiler } from 'cc'; import { ResUtil } from '../../core/util/ResUtil'; import TableLoadUtil from '../../core/util/TableLoadUtil'; import get_new_head_icon from '../../core/util_class/HeadIcon'; import { TableUtil } from '../../module_extra/table_ts/TableUtil'; import { ModuleDef } from '../../Scripts/ModuleDef'; import { SceneDef } from '../../Scripts/SceneDef'; import PlayerData from '../game/PlayerData'; import { ch } from '../../ch/ch'; import { gui } from '../../core/ui/ui'; import { UI_Hall } from '../ui/UI_Hall/UI_Hall'; const { ccclass, property } = _decorator; export enum GameState { gameing, wait, win, fail } export const life_countdown_Max = 1800; @ccclass('Hall') export class Hall extends Component { private static instance: Hall; public player: PlayerData; public head_icon = get_new_head_icon(); sceneChanging: boolean = false;//场景切换 bornRunning: boolean = false; // 是否在生成中 public gameState: GameState = GameState.win; firstEnter: boolean = false; public static getInstance(): Hall { return Hall.instance; } protected onEnable(): void { // 计算离线时间,并更新倒计时 this.startLifeTimer(); } protected onLoad(): void { window.addEventListener('beforeunload', async () => { let res = Date.now(); if (res) { const nowTimestamp = Math.floor(res / 1000); // 记录当前时间(秒) this.player.set_last_exit_time(nowTimestamp); if (this.gameState == GameState.fail || this.gameState == GameState.wait || this.gameState == GameState.gameing) { if (this.player.get_life() > 0) { this.player.set_life(this.player.get_life() - 1); } } } }); window['wx'].onShow(async () => { let res = Date.now(); if (res) { const nowTimestamp = Math.floor(res / 1000); // 记录当前时间(秒) this.player.set_last_exit_time(nowTimestamp); if (this.gameState == GameState.wait || this.gameState == GameState.gameing) { this.player.set_life(this.player.get_life() + 1); } } }) window['wx'].onHide(async () => { let res = Date.now(); if (res) { const nowTimestamp = Math.floor(res / 1000); // 记录当前时间(秒) this.player.set_last_exit_time(nowTimestamp); if (this.gameState == GameState.fail || this.gameState == GameState.wait || this.gameState == GameState.gameing) { if (this.player.get_life() > 0) { this.player.set_life(this.player.get_life() - 1); } } } }) } async start() { if (!Hall.instance) { Hall.instance = this; } this.init(); director.addPersistRootNode(this.node); // if(chsdk.checkFromSidebar()) // { // Hall.getInstance().player.add_item(2,2); // } } async init() { await this.loadTable(); this.player = PlayerData.getInstance(ch.sdk.get_gid(), ch.sdk.get_uid().toString()); await this.player.init_user_info(); await this.player.load(); this.player.set_login_num(1); if (this.player.get_max_floor() == 0) { this.firstEnter = true; Hall.getInstance().player.set_life(5); ResUtil.loadScene(SceneDef.GAME, ModuleDef.GAME, true); } else { gui.show(UI_Hall); } await this.calculateOfflineTime(); this.startLifeTimer(); } private loadTable(): void { ch.log.log_start("加载配置初始化"); TableLoadUtil.preloadAll(ModuleDef.EXTRA, "table_json", async () => { }, TableUtil.set); } private async calculateOfflineTime() { const lastExitTime = this.player.get_last_exit_time(); console.log(`上次退出时间: ${new Date(lastExitTime * 1000)}`); if (!lastExitTime) return; const lastExitTimestamp = lastExitTime; let res = Date.now(); if (res) { const nowTimestamp = Math.floor(res / 1000); // 以秒为单位 console.log(`当前时间: ${new Date(nowTimestamp * 1000)}`); const elapsedTime = nowTimestamp - lastExitTimestamp; console.log(`离线时间: ${elapsedTime} 秒`); let countdown = this.player.get_life_countdown(); this.player.set_life(this.player.get_life() + Math.floor(elapsedTime / life_countdown_Max)); if (this.player.get_life() >= 10) { this.player.set_life_countdown(0); } else { this.player.set_life_countdown((countdown - elapsedTime) % life_countdown_Max); } } } startLifeTimer() { this.unschedule(this.LifeCountDownTimer); this.schedule(this.LifeCountDownTimer, 1.0); } LifeCountDownTimer() { if (!this.player) { console.warn("Player 未初始化,跳过倒计时"); return; } console.log('计时器启动'); if (this.player.get_life() < 5) { if (this.player.get_life_countdown() > 0) { console.log("正在执行倒计时:" + (this.player.get_life_countdown() - 1)); this.player.set_life_countdown(this.player.get_life_countdown() - 1); if (director.getScene().name == "hall") { console.log("倒计时显示刷新:" + this.player.get_life_countdown()); gui.get(UI_Hall)?.set_Life_CountDown(this.player.get_life_countdown(), true); } } else { this.player.set_life(this.player.get_life() + 1); gui.get(UI_Hall)?.set_Life(this.player.get_life()); if (this.player.get_life() != 5) { this.player.set_life_countdown(life_countdown_Max); gui.get(UI_Hall)?.set_Life_CountDown(this.player.get_life_countdown(), true); } } } else { if (director.getScene().name == "hall") { gui.get(UI_Hall)?.set_Life_CountDown(this.player.get_life_countdown(), false); } } } }