Files
2025-09-25 11:20:03 +08:00

144 lines
4.1 KiB
TypeScript

import { _decorator, sys } from "cc";
import Utils from "../Common/Utils";
import { CommonConfig } from "../Config/CommonConfig";
import AudioManager from "./AudioManager";
import { logger } from "db://assets/Scripts/Main/Common/Logger";
const { ccclass } = _decorator;
@ccclass("PlayerDataManager")
export default class PlayerDataManager {
private static _I: PlayerDataManager = null;
public static get I(): PlayerDataManager {
if (!PlayerDataManager._I) {
PlayerDataManager._I = new PlayerDataManager();
PlayerDataManager._I.init();
}
return PlayerDataManager._I;
}
private isShakeOn: boolean = true; //震动
private isMusicOn: boolean = true; //音乐
private isSoundOn: boolean = true; //音效
private guideMainFinished: boolean = false; //主界面引导是否完成
private guideTalkFinished: boolean = false; //聊天界面引导是否完成
public get IsShakeOn(): boolean {
return this.isShakeOn;
}
public get IsMusicOn(): boolean {
return this.isMusicOn;
}
public get IsSoundOn(): boolean {
return this.isSoundOn;
}
public get GuideMainFinished(): boolean {
return this.guideMainFinished;
}
public get GuideTalkFinished(): boolean {
return this.guideTalkFinished;
}
private init() {
this.InitPlayerData();
}
public initEmpty() {}
private InitPlayerData() {
// this.isShakeOn = PlayerDataManager.getForKey(CommonConfig.StorageConfig.SETTING_SHAKEON, true)
this.isMusicOn = PlayerDataManager.getForKey(
CommonConfig.StorageConfig.SETTING_MUSIC,
true
);
this.isSoundOn = PlayerDataManager.getForKey(
CommonConfig.StorageConfig.SETTING_SOUND,
true
);
this.guideMainFinished = PlayerDataManager.getForKey(
CommonConfig.StorageConfig.GUIDE_MAIN,
false
);
this.guideTalkFinished = PlayerDataManager.getForKey(
CommonConfig.StorageConfig.GUIDE_TALK,
false
);
logger.log(
"InitPlayerData",
`music=${this.isMusicOn}, sound=${this.isSoundOn}, guideMain=${this.guideMainFinished}, guideTalk=${this.guideTalkFinished}`
);
}
public SetMusicOn(bo: boolean) {
if (this.isMusicOn == bo) return;
this.isMusicOn = bo;
PlayerDataManager.setForKey(CommonConfig.StorageConfig.SETTING_MUSIC, bo);
if (bo) {
AudioManager.I.ReplayMusic();
} else AudioManager.I.StopMusic();
}
public SetSoundOn(bo: boolean) {
if (this.isSoundOn == bo) return;
this.isSoundOn = bo;
PlayerDataManager.setForKey(CommonConfig.StorageConfig.SETTING_SOUND, bo);
}
/**主界面引导完成*/
public SetGuideMainFinish(bo: boolean) {
if (this.guideMainFinished == bo) return;
this.guideMainFinished = bo;
PlayerDataManager.setForKey(CommonConfig.StorageConfig.GUIDE_MAIN, bo);
}
/**聊天界面引导完成*/
public SetGuideTalkFinish(bo: boolean) {
if (this.guideTalkFinished == bo) return;
this.guideTalkFinished = bo;
PlayerDataManager.setForKey(CommonConfig.StorageConfig.GUIDE_TALK, bo);
}
/**
* 类型内部处理,外层部分类型 isBindingId //标识位数据需要绑定 playerid的时候,默认值为 true,若是传 false,则不进入绑定
* */
static setForKey(key: number | string, data) {
if (!key || void 0 === data) {
Utils.Log("key can`t nil");
return;
}
key = key + "";
sys.localStorage.setItem(key, JSON.stringify(data));
}
static getForKey(key: number | string, defaultVal): any {
if (!key) {
Utils.Log("key can`t nil");
return;
}
key = key + "";
let jsonObj;
try {
let str = sys.localStorage.getItem(key);
try {
jsonObj = JSON.parse(str);
} catch (error) {
jsonObj = str;
}
} catch (error) {
Utils.Log(`getForKey--->>>解析${key}的值的时候报错`);
}
if (jsonObj == null) {
jsonObj = defaultVal;
}
return jsonObj;
}
static removeForKey(key: number | string): void {
key = key + "";
sys.localStorage.setItem(key, "");
}
/**清档 */
static clearAll(): void {
sys.localStorage.clear();
}
}