| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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);
- }
- }
- }
- }
|