//本地数据管理 import EventName from "./EventName/EventName"; import { Global } from "./Global"; export default class LocalData { private static _instance: LocalData = null; public static getInstance() { if (!this._instance) { this._instance = new LocalData(); this._instance.init(); } return this._instance; } private readonly $localkey: string = "柑橘味"; //玩家金币 private $goldnum: number = Global.defaultGold; //玩家最高纪录 private $MaxRecord: number = Global.defaultMaxRecord; //拥有的抛物线皮肤 private $HasParabolaSkin: number[] = Global.defaultParabolaSkin; ///拥有的人物皮肤 private $HasCharacterSkin: number[] = Global.defaultCharacterSkin; ///当前使用的人物皮肤 private $useParabolaSkin: number = Global.useParabolaSkin; ///当前使用的人物皮肤 private $useCharacterSkin: number = Global.useCharacterSkin; ///当前使用的人物皮肤 public $Guide: string[] = [] private $lastLoginTime: number = 0; //设置引导 public setGuide(Action: string) { if (this.$Guide.includes(Action)) { return } this.$Guide.push(Action) this.save(); } public getGuide(Action: string) { if (this.$Guide.includes(Action)) { return true } return false } public init() { this.load(); let now = Date.now(); this.$lastLoginTime = now; this.save(); this.setCurrentParabolaSkin(1001) } public setGold(num: number, Action: string) { this.$goldnum = this.Action(this.$goldnum, num, Action) this.save(); cc.systemEvent.emit(EventName.changeGold); } public getGold() { return this.$goldnum; } public setMaxRecord(num: number, Action: string) { this.$MaxRecord = this.Action(this.$MaxRecord, num, Action) this.save(); cc.systemEvent.emit(EventName.changeMaxRecord); } public getMaxRecord() { return this.$MaxRecord; } public setHasCharacterSkin(num: number) { this.$HasCharacterSkin.push(num) this.save(); } public getHasCharacterSkin() { return this.$HasCharacterSkin; } public setHasParabolaSkin(num: number) { this.$HasParabolaSkin.push(num) this.save(); } public getHasParabolaSkin() { return this.$HasParabolaSkin; } public setCurrentCharacterSkin(num: number) { this.$useCharacterSkin = num this.save(); } public getCurrentCharacterSkin() { return this.$useCharacterSkin; } public setCurrentParabolaSkin(num: number) { this.$useParabolaSkin = num this.save(); } public getCurrentParabolaSkin() { return this.$useParabolaSkin; } ///////////////////////////////////////////////通关次数 private $OverCount: number = 0; public setOverCount(Temp: number) { this.$OverCount = Temp this.save(); } public getOverCount() { return this.$OverCount; } /////////////////////////////////////////////当前玩的关卡 private $Map: number = 1; public setMap(Temp: number) { this.$Map = Temp this.save(); } public getMap() { return this.$Map; } /////////////////////////////////////////////设置 private $Music: boolean = true; private $Effects: boolean = true; public setMusic(Temp: boolean) { this.$Music = Temp this.save(); } public getMusic() { return this.$Music; } public setEffects(Temp: boolean) { this.$Effects = Temp this.save(); } public getEffects() { return this.$Effects; } /////////////////////////////////////////////签到 private $Sign: { [key: number]: number }[] = [] public setSign(Temp: { [key: number]: number }[]) { this.$Sign = Temp this.save(); } public getSign() { return this.$Sign; } /////////////////////////////////////////////游戏内 ///抛物线次数 private $ParabolaCount: number = Global.FirstParabolaCount;//初始抛物线次数 public setParabolaCount(num: number, Action: string) { this.$ParabolaCount = this.Action(this.$ParabolaCount, num, Action) this.save(); cc.systemEvent.emit(EventName.changeParabolaCount); } public getParabolaCount() { return this.$ParabolaCount; } ///超级跳的次数 private $Supjump: number = Global.FirstSupjumpCount;//初始超级跳次数 public setSupjumpCount(num: number, Action: string) { this.$Supjump = this.Action(this.$Supjump, num, Action) this.save(); cc.systemEvent.emit(EventName.changeSupjumpCount); } public getSupjumpCount() { return this.$Supjump; } ///飞的次数 private $Fly: number = Global.FirstFlyCount;//初始超级跳次数 public setFlyCount(num: number, Action: string) { this.$Fly = this.Action(this.$Fly, num, Action) this.save(); cc.systemEvent.emit(EventName.changeFlyCount); } public getFlyCount() { return this.$Fly; } //保存玩家本地数据 public save() { let data: any = {}; for (const key in this) { if (Object.prototype.hasOwnProperty.call(this, key)) { if (key.charAt(0) == '$') { data[key] = this[key]; } } } cc.sys.localStorage.setItem(this.$localkey, JSON.stringify(data)); } //加载玩家本地数据 private load() { let data = cc.sys.localStorage.getItem(this.$localkey); if (data) { data = JSON.parse(data); for (const key in data) { this[key] = data[key]; } } } private Action(data: number, num: number, str: string) { switch (str) { case '+': return data += num case '-': return data -= num case '=': return data = num default: cc.error('出错了') return 0 } } //#region 签到代码 initSign() { if (this.getSign().length <= 0) { console.log('初始化了一次'); let Total_temp: { [key: number]: number }[] = [] for (let index = 0; index < 7; index++) { let temp: { [key: number]: number } = {} const today = new Date(); const tomorrow = new Date(today); tomorrow.setDate(tomorrow.getDate() + index); tomorrow.setHours(0, 0, 0, 0); const todayTimestamp = tomorrow.getTime(); temp[todayTimestamp] = 0 Total_temp.push(temp) } this.setSign(Total_temp) } this.getSign().forEach((e, index) => { for (const key in e) { if (Object.prototype.hasOwnProperty.call(e, key)) { const element = e[key]; console.log('数组排序的值', index);//数组排序的值 console.log('当日零点时间戳值', key);//当日零点时间戳值 console.log('签到次数', element);//签到次数 if (index == 6) { const today = new Date(); today.setHours(0, 0, 0, 0); const todayTimestamp = today.getTime(); //如果以及是7天后了 那么就 重置一下 if (todayTimestamp > parseInt(key)) { //清空 this.setSign([]) //重新赋值 this.initSign() } } } } }) } //返回今天签到次数 getTodaySignCount(): [number, number] { const today = new Date(); today.setHours(0, 0, 0, 0); const todayTimestamp = today.getTime(); for (let i = 0; i < this.getSign().length; i++) { const e = this.getSign()[i]; for (const key in e) { if (Object.prototype.hasOwnProperty.call(e, key)) { const element = e[key]; if (todayTimestamp == parseInt(key)) { return [element, i] } } } } } //增加一次今天签到次数 addTodaySignCount() { const today = new Date(); today.setHours(0, 0, 0, 0); const todayTimestamp = today.getTime(); for (let i = 0; i < this.getSign().length; i++) { const e = this.getSign()[i]; for (const key in e) { if (Object.prototype.hasOwnProperty.call(e, key)) { if (todayTimestamp == parseInt(key)) { e[key]++ } } } } } byIndex2Count(index: number): [number, number] { for (let i = 0; i < this.getSign().length; i++) { const e = this.getSign()[i]; for (const key in e) { if (Object.prototype.hasOwnProperty.call(e, key)) { if (index == i) { return [parseInt(key), e[key]] } } } } } //#endregion }