import { _decorator, sys } from 'cc'; import { IncrementData } from '../IncrementData'; import { Hall } from '../../hall/Hall'; import { data_type } from '../PlayerData'; export enum LvDir { none = 0, left = 1, right = 2, up = 3, down = 4, xCenter = 5, xOut = 6, yCenter = 7, yOut = 8, } export class LvData { private static _instance: LvData = null; private _life: IncrementData = new IncrementData(); // 生命值 private _is_win: boolean = false; // 连胜 private _dir: LvDir = LvDir.none; // 关卡状态 private static readonly STORAGE_KEY = 'game_data'; private _rwpf: number = 1//人物皮肤 private _fkpf: number = 1//方块皮肤 private _stpf: number = 1//石头皮肤 // 存储已解锁的皮肤ID数组 private _unlockedRoleSkins: number[] = [1]; // 默认解锁第一个角色皮肤 private _unlockedBlockSkins: number[] = [1]; // 默认解锁第一个方块皮肤 private _unlockedStoneSkins: number[] = [1]; // 默认解锁第一个石头皮肤 // 方块销毁统计 private _destroyedBlocks: number = 0; private _totalBlocks: number = 0; // 单例模式 public static get instance(): LvData { if (!this._instance) { this._instance = new LvData(); this._instance.load(); // 加载保存的数据 this._instance.updateDirByLevel(); // 初始化时设置关卡状态 } return this._instance; } get lifeContor() { return this._life } private constructor() { } // 获取生命值 public get life(): number { this._life.addLastTime(chsdk.date.now()) return this._life.value; } // 设置生命值 public set life(value: number) { this._life.value = value; this.save(); } // 获取连胜状态 public get is_win(): boolean { return this._is_win; } // 设置连胜状态 public set is_win(value: boolean) { this._is_win = value; this.save(); } // 获取关卡状态 public get dir(): LvDir { return this._dir; } // 设置关卡状态 public set dir(value: LvDir) { this._dir = value; this.save(); } //人物皮肤 public set rwpf(value: number) { this._rwpf = value; this.save(); } public get rwpf(): number { return this._rwpf; } //方块皮肤 public set fkpf(value: number) { this._fkpf = value; this.save(); } public get fkpf(): number { return this._fkpf; } //石头皮肤 public set stpf(value: number) { this._stpf = value; this.save(); } public get stpf(): number { return this._stpf; } // 检查角色皮肤是否已解锁 public isRoleSkinUnlocked(skinId: number): boolean { return this._unlockedRoleSkins.indexOf(skinId) !== -1; } // 解锁角色皮肤 public unlockRoleSkin(skinId: number): void { if (this._unlockedRoleSkins.indexOf(skinId) === -1) { // 检查是否未解锁 this._unlockedRoleSkins.push(skinId); this.save(); } } // 检查方块皮肤是否已解锁 public isBlockSkinUnlocked(skinId: number): boolean { return this._unlockedBlockSkins.indexOf(skinId) !== -1; } // 解锁方块皮肤 public unlockBlockSkin(skinId: number): void { if (this._unlockedBlockSkins.indexOf(skinId) === -1) { this._unlockedBlockSkins.push(skinId); this.save(); } } // 检查石头皮肤是否已解锁 public isStoneSkinUnlocked(skinId: number): boolean { return this._unlockedStoneSkins.indexOf(skinId) !== -1; } // 解锁石头皮肤 public unlockStoneSkin(skinId: number): void { if (this._unlockedStoneSkins.indexOf(skinId) === -1) { this._unlockedStoneSkins.push(skinId); this.save(); } } // 初始化时设置总方块数 public initBlockData(total: number) { this._totalBlocks = total; this._destroyedBlocks = 0; this.save(); } // 增加销毁数量 public addDestroyedBlocks(count: number) { this._destroyedBlocks += count; this._destroyedBlocks = Math.min(this._destroyedBlocks, this._totalBlocks); this.save(); } // 获取进度 public get blockProgress(): number { return this._totalBlocks > 0 ? this._destroyedBlocks / this._totalBlocks : 0; } // 根据关卡自动设置对应的LvDir状态 private updateDirByLevel() { // 计算有效关卡(10关后循环3-10) const lv = Hall.getInstance().player.data.get(data_type.max_floor) const cycleBase = lv > 10 ? ((lv - 3) % 8) + 3 // 3-10的8个关卡循环 : lv; switch (cycleBase) { // 改用计算后的有效关卡 case 1: case 2: this._dir = LvDir.none; break; case 3: this._dir = LvDir.left; break; case 4: this._dir = LvDir.up; break; case 5: this._dir = LvDir.right; break; case 6: this._dir = LvDir.down; break; case 7: this._dir = LvDir.xOut; break; case 8: this._dir = LvDir.yOut; break; case 9: this._dir = LvDir.xCenter; break; case 10: this._dir = LvDir.yCenter; break; } } // 从本地存储加载数据 private load() { const savedData = sys.localStorage.getItem(LvData.STORAGE_KEY); this._life.init(5 *60 * 1000,5,5); debugger if (savedData) { try { const data = JSON.parse(savedData); // this._lv = data.lv || 1; this._life.unserialize(data.life) ; this._is_win = data.is_win || false; this._dir = data.dir !== undefined ? data.dir : LvDir.none; this._rwpf = data.rwpf || 1;//人物皮肤 this._fkpf = data.fkpf || 1;//方块皮肤 this._stpf = data.stpf || 1;//石头皮肤 // 加载已解锁的皮肤 this._unlockedRoleSkins = data.unlockedRoleSkins || [1]; this._unlockedBlockSkins = data.unlockedBlockSkins || [1]; this._unlockedStoneSkins = data.unlockedStoneSkins || [1]; } catch (error) { console.error('Failed to load saved data:', error); } }else{ this._life.unserialize(null) ; } } // 保存数据到本地存储 private save() { const data = { // lv: this._lv, life: this._life.serialize(), is_win: this._is_win, dir: this._dir, rwpf: this._rwpf,//人物皮肤 fkpf: this._fkpf,//方块皮肤 stpf: this._stpf,//石头皮肤 // 保存已解锁的皮肤 unlockedRoleSkins: this._unlockedRoleSkins, unlockedBlockSkins: this._unlockedBlockSkins, unlockedStoneSkins: this._unlockedStoneSkins }; sys.localStorage.setItem(LvData.STORAGE_KEY, JSON.stringify(data)); } // 重置游戏数据 public reset() { // this._lv = 1; // this._life = 5; this._is_win = false; this._dir = LvDir.none; this._rwpf = 1;//人物皮肤 this._fkpf = 1;//方块皮肤 this._stpf = 1;//石头皮肤 // 重置已解锁的皮肤 this._unlockedRoleSkins = [1]; this._unlockedBlockSkins = [1]; this._unlockedStoneSkins = [1]; this.save(); } }