| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- 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();
- }
- }
|