Przeglądaj źródła

体力恢复 + 网页离线时间戳记录

woso_javan 1 miesiąc temu
rodzic
commit
42fd22be4e

+ 2 - 0
assets/module_aliens/RoosterAliens.ts

@@ -10,6 +10,7 @@ import { AliensGlobalInstance } from './Script/AliensGlobalInstance';
 import { UI_Setting } from '../scripts/UIDef';
 import { tgxUIMgr } from '../core_tgx/tgx';
 import { EventDispatcher } from '../core_tgx/easy_ui_framework/EventDispatcher';
+import { UserManager } from './Script/Manager/UserMgr';
 const { ccclass, property } = _decorator;
 
 const duration = 0.3;
@@ -19,6 +20,7 @@ export class RoosterAliens extends Component {
     onLoad() {
         AliensAudioMgr.initilize();
         // AliensAudioMgr.play(AliensAudioMgr.getMusicIdName(2), 1.0);
+        UserManager.instance.initilizeModel();
         LevelManager.instance.initilizeModel();
         AliensGlobalInstance.instance.initUI(); //初始化u
         this.registerListener();

+ 7 - 7
assets/module_aliens/Script/GameUtil.ts

@@ -5,13 +5,13 @@ import { AliensGlobalInstance } from "./AliensGlobalInstance";
 export class GameUtil {
 
     /** 转换成hh:mm格式*/
-    // static formatToTimeString(totalSeconds: number): string {
-    //     const minutes = Math.floor(totalSeconds / 60);
-    //     const seconds = totalSeconds % 60;
-    //     const formattedMinutes = String(minutes).padStart(2, '0');
-    //     const formattedSeconds = String(seconds).padStart(2, '0');
-    //     return `${formattedMinutes}:${formattedSeconds}`;
-    // }
+    static formatToTimeString(totalSeconds: number): string {
+        const minutes = Math.floor(totalSeconds / 60);
+        const seconds = totalSeconds % 60;
+        const formattedMinutes = String(minutes).padStart(2, '0');
+        const formattedSeconds = String(seconds).padStart(2, '0');
+        return `${formattedMinutes}:${formattedSeconds}`;
+    }
 
     /** 重量单位转换*/
     static formatWeight(weight: number): string {

+ 81 - 0
assets/module_aliens/Script/HomeUI.ts

@@ -0,0 +1,81 @@
+import { _decorator, Component, Label, Node } from 'cc';
+import { UserManager } from './Manager/UserMgr';
+import { GameUtil } from './GameUtil';
+const { ccclass, property } = _decorator;
+
+@ccclass('HomeUI')
+export class HomeUI extends Component {
+
+    @property(Label)
+    public lbPower: Label = null; 
+
+    @property(Label)
+    public lbTimeCount: Label = null; // 倒计时组件
+
+    //体力恢复时间
+    private powerRecoverTime: number = 1;
+    //体力最大值
+    private powerMax: number = 1;
+    //当前体力
+    private powerCurrent: number = 0;
+    //剩余恢复时间
+    private remainingTime: number = 0;
+
+    start() {
+        const {powerRecoverTime,powerMax,powerCurrent} = UserManager.instance.userModel;
+        this.powerRecoverTime = powerRecoverTime;
+        this.powerMax = powerMax;
+        this.powerCurrent = powerCurrent;
+
+        const lastLeave = localStorage.getItem("lastLeaveTime");
+        if (lastLeave) {
+            const lastTime = parseInt(lastLeave);
+            const now = Date.now();
+            const offlineSeconds = Math.floor((now - lastTime) / 1000);
+            
+            // 计算可以恢复的体力数量
+            const recoverCount = Math.floor(offlineSeconds / this.powerRecoverTime);
+            this.powerCurrent = Math.min(this.powerCurrent + recoverCount, this.powerMax);
+            
+            // 计算剩余恢复时间
+            if (this.powerCurrent < this.powerMax) {
+                this.remainingTime = this.powerRecoverTime - (offlineSeconds % this.powerRecoverTime);
+            } else {
+                this.remainingTime = 0;
+            }
+            
+            console.log(`离线恢复: ${recoverCount}点体力, 当前体力: ${this.powerCurrent}/${this.powerMax}`);
+        } else {
+            this.remainingTime = this.powerRecoverTime;
+        }
+
+        // 保存当前时间为新的退出时间
+        localStorage.setItem("lastLeaveTime", Date.now().toString());
+        this.updatePowerUI();
+    }
+
+    update(deltaTime: number) {
+        if(!this.lbTimeCount || !this.lbPower) return;
+        if (this.powerCurrent >= this.powerMax) {
+            this.lbTimeCount.string = 'Max';
+            return;
+        }
+
+        this.remainingTime -= deltaTime;
+        if (this.remainingTime <= 0) {
+            this.powerCurrent = Math.min(this.powerCurrent + 1, this.powerMax);
+            this.remainingTime = this.powerRecoverTime;
+            this.updatePowerUI();
+        }
+
+        this.lbTimeCount.string = GameUtil.formatToTimeString(Math.ceil(this.remainingTime));
+    }
+
+    private updatePowerUI() {
+        if(!this.lbPower) return;
+        this.lbPower.string = this.powerCurrent.toString();
+        if (this.powerCurrent >= this.powerMax) {
+            this.lbTimeCount.string = 'Max';
+        }
+    }
+}

+ 9 - 0
assets/module_aliens/Script/HomeUI.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.23",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "b1833ebc-0ee2-4c4c-bba1-9d36051f0689",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 95 - 0
assets/module_aliens/Script/Manager/TimerMgr.ts

@@ -0,0 +1,95 @@
+import { assetManager, instantiate, Prefab, Node, UITransform, Vec3, Vec2, view, game, director, Scheduler, Label } from "cc";
+
+import { GameUtil } from "../GameUtil";
+import { AliensGlobalInstance } from "../AliensGlobalInstance";
+
+
+/** 时间管理器*/
+export class TimerMgr {
+    private static _instance: TimerMgr;
+    public static get Instance(): TimerMgr {
+        if (this._instance == null) {
+            this._instance = new TimerMgr();
+        }
+        return this._instance;
+    }
+
+    public static get inst(): TimerMgr {
+        return this.Instance;
+    }
+
+    public countDownTime: number = 1;
+    private timerId: number = 0;
+    private isPaused: boolean = false;  // 添加暂停标志
+
+    constructor() {
+    }
+
+    // 开始倒计时
+    public startCountdown(): void {
+        if (this.isPaused) {
+            this.resumeCountdown();
+            return;
+        }
+        this.upateLbTime();
+        this.timerId = setInterval(() => {
+            if (!this.isPaused) {
+                this.countDownTime--;
+                if (this.countDownTime <= 0) {
+                    this.stopCountdown();
+                }
+                this.upateLbTime();
+            }
+        }, 1000); // 每秒减少一次
+
+        Scheduler.enableForTarget(this);
+        director.getScheduler().schedule(this.update, this, 0);
+    }
+
+    private upateLbTime() {
+        const battleUI = AliensGlobalInstance.instance.battleUI;
+        const lbTime = battleUI.getChildByPath('Times/LbTime')!;
+        // lbTime.getComponent(Label).string = this.countDownTime.toString();
+        const format = GameUtil.formatToTimeString(this.countDownTime);
+        lbTime.getComponent(Label).string = format;
+    }
+
+    // 停止倒计时
+    private stopCountdown(): void {
+        if (this.timerId) {
+            clearInterval(this.timerId);
+            this.timerId = 0;
+        }
+    }
+
+    // 暂停倒计时
+    public pauseCountdown(): void {
+        this.isPaused = true;
+        director.getScheduler().pauseTarget(this);
+    }
+
+    // 恢复倒计时
+    public resumeCountdown(): void {
+        this.isPaused = false;
+        director.getScheduler().resumeTarget(this);
+    }
+
+    // 获取暂停状态
+    public isPausedState(): boolean {
+        return this.isPaused;
+    }
+
+    // update方法,每帧调用
+    public update(dt: number): void {
+
+    }
+
+    // 销毁时清理
+    public reset(): void {
+        this.stopCountdown();
+        this.isPaused = false;  // 重置暂停状态
+        Scheduler.enableForTarget(this);
+        director.getScheduler().unscheduleAllForTarget(this);
+        this.countDownTime = 10; //测试
+    }
+}

+ 9 - 0
assets/module_aliens/Script/Manager/TimerMgr.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.23",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "b8f71d11-eeb0-4ce1-95e1-84bb7e339357",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 19 - 0
assets/module_aliens/Script/Manager/UserMgr.ts

@@ -0,0 +1,19 @@
+import { Node, Prefab, _decorator, assetManager, find, instantiate, sys } from 'cc';
+import { UserModel } from '../Model/UserModel';
+const { ccclass, property } = _decorator;
+
+@ccclass('UserManager')
+export class UserManager {
+    private static _instance: UserManager | null = null;
+    public static get instance(): UserManager {
+        if (!this._instance) this._instance = new UserManager();
+        return this._instance;
+    }
+
+    public userModel: UserModel = null;
+
+    initilizeModel(): void {
+        this.userModel = new UserModel();
+        this.userModel.initialize();
+    }
+}

+ 9 - 0
assets/module_aliens/Script/Manager/UserMgr.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.23",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "5ea1a5c5-8b95-407d-b9c7-78bea548b626",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 20 - 0
assets/module_aliens/Script/Model/UserModel.ts

@@ -0,0 +1,20 @@
+export class UserModel {
+    money: number = 0;
+    nickName: string = 'player'; //昵称
+    //恢复体力时间 5s
+    powerRecoverTime: number = 1;
+    //自然恢复体力最大值
+    powerMax: number = 1;
+    //当前体力
+    powerCurrent: number = 0;
+
+    constructor() {
+    }
+
+    initialize() {
+       this.money = 0;
+       this.powerRecoverTime = 3000;
+       this.powerMax = 5;
+       this.powerCurrent = 1;
+    }
+}

+ 9 - 0
assets/module_aliens/Script/Model/UserModel.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.23",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "1aeabde9-e0d3-45ef-8a0a-092e687bbb87",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

Plik diff jest za duży
+ 164 - 144
assets/module_aliens/rooster_aliens.scene


+ 5 - 0
assets/start/Start.ts

@@ -57,6 +57,11 @@ export class Start extends Component {
         if (!GlobalConfig.isDebug) {
             AdvertMgr.instance.showInterstitial();
         }
+
+        window.addEventListener("beforeunload", () => {
+            const timestamp = Date.now(); // 获取当前时间戳(毫秒)
+            localStorage.setItem("lastLeaveTime", timestamp.toString());
+        });
     }
 
     start() {

+ 1 - 1
profiles/v2/packages/preview.json

@@ -5,7 +5,7 @@
   },
   "preview": {
     "current": {
-      "platform": "gameView"
+      "platform": "browser"
     }
   }
 }

+ 30 - 30
profiles/v2/packages/scene.json

@@ -5,7 +5,7 @@
     "__version__": "1.3.7"
   },
   "gizmos-infos": {
-    "is2D": false,
+    "is2D": true,
     "is3DIcon": false,
     "iconSize": 2,
     "gridVisible": true,
@@ -1214,15 +1214,15 @@
     },
     "19323c5d-5d36-438a-86ee-8288c690e5b0": {
       "position": {
-        "x": 31.570027850394066,
-        "y": 31.570027850394066,
-        "z": 31.570027850394062
+        "x": 59.36833333333331,
+        "y": 126.36499999999995,
+        "z": 5000
       },
       "rotation": {
-        "x": -0.27984814233312133,
-        "y": 0.3647051996310009,
-        "z": 0.11591689595929512,
-        "w": 0.8804762392171493
+        "x": 0,
+        "y": 0,
+        "z": 0,
+        "w": 1
       },
       "viewCenter": {
         "x": 0,
@@ -1230,24 +1230,24 @@
         "z": 0
       },
       "contentRect": {
-        "x": 0,
-        "y": 0,
-        "width": 360,
-        "height": 760
+        "x": 180,
+        "y": 380,
+        "width": 350,
+        "height": 738.8888888888889
       },
-      "scale": 1
+      "scale": 0.9722222222222222
     },
     "9e293cde-e27b-4902-808b-e884f3e9da32": {
       "position": {
-        "x": 19.176880099210653,
-        "y": 19.17688009921065,
-        "z": 19.176880099210646
+        "x": 180,
+        "y": 380.00000000000006,
+        "z": 5000
       },
       "rotation": {
-        "x": -0.13053661033751934,
-        "y": 0.1701186226765409,
-        "z": 0.0540700343880125,
-        "w": 0.97524165157232
+        "x": 0,
+        "y": 0,
+        "z": 0,
+        "w": 1
       },
       "viewCenter": {
         "x": 0,
@@ -1257,15 +1257,15 @@
       "contentRect": {
         "x": 0,
         "y": 0,
-        "width": 360,
-        "height": 760
+        "width": 350,
+        "height": 738.8888888888889
       },
-      "scale": 1
+      "scale": 0.9722222222222222
     },
     "be14c61f-22d8-4bb9-b444-ad9f29740469": {
       "position": {
-        "x": 681.2654783392229,
-        "y": 623.20703537804,
+        "x": 361,
+        "y": 398.5,
         "z": 5000
       },
       "rotation": {
@@ -1280,12 +1280,12 @@
         "z": 0
       },
       "contentRect": {
-        "x": 0,
-        "y": 0,
-        "width": 360,
-        "height": 760
+        "x": 183.54213483146066,
+        "y": 23.86672908863918,
+        "width": 712,
+        "height": 785.9612188365651
       },
-      "scale": 1
+      "scale": 0.9861495844875346
     }
   },
   "camera-uuids": [

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików