init
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
import { _decorator, sys } from "cc";
|
||||
import Utils from "../Common/Utils";
|
||||
import { CommonConfig } from "../Config/CommonConfig";
|
||||
import AudioManager from "./AudioManager";
|
||||
|
||||
const { ccclass, property } = _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 lastPassLevel: number = 0; //普通关卡
|
||||
private lastPassLevelOfDailyChallenge: number = 0; //每日挑战关卡
|
||||
private myScore: number = 0; //玩家积分
|
||||
private skinId: number = 0; //当前使用的皮肤id
|
||||
private myCityId: number = -1; //所在省
|
||||
private isShakeOn: boolean = true; //震动
|
||||
private isMusicOn: boolean = true; //音乐
|
||||
private isSoundOn: boolean = true; //音效
|
||||
|
||||
|
||||
public get IsShakeOn() : boolean {return this.isShakeOn;}
|
||||
public get IsMusicOn() : boolean {return this.isMusicOn;}
|
||||
public get IsSoundOn() : boolean {return this.isSoundOn;}
|
||||
|
||||
|
||||
private init(){
|
||||
this.InitPlayerData();
|
||||
}
|
||||
public initEmpty(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
private InitPlayerData() {
|
||||
this.lastPassLevel = PlayerDataManager.getForKey(CommonConfig.StorageConfig.GAME_PASSLV, 0)
|
||||
this.lastPassLevelOfDailyChallenge = PlayerDataManager.getForKey(CommonConfig.StorageConfig.DAILYCHALLENGE_PASSLV, 0)
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 类型内部处理,外层部分类型 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 removeForKey(key: number | string): void {
|
||||
key = key + "";
|
||||
sys.localStorage.setItem(key, "");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user