import { _decorator, Component, math, Node, EventTarget } from 'cc'; import { EventCenter } from '../../EventCenter'; import { AudioMgr } from '../audio/AudioMgr'; import { Config } from '../../data/GameConfig'; const { ccclass, property } = _decorator; const eventTarget = new EventTarget(); @ccclass('LocalSaveMgr') export class LocalSaveMgr extends Component { static instance: LocalSaveMgr; playerData: PlayerData = null protected onLoad(): void { LocalSaveMgr.instance = this let value = localStorage.getItem("playerData") if (value != null || value != "") { this.playerData = new PlayerData() try { this.playerData.jsonDataInit(JSON.parse(value)) } catch (err) { //… } } else { this.playerData = new PlayerData() } this.initSound() } passLevel() { this.playerData.levelIndex += 1 this.save() } addCoin(count: number) { console.log("addCoin", count) this.playerData.coin += count EventCenter.coinCountChange.emit("coinCountChange") this.save() } save() { localStorage.setItem("playerData", JSON.stringify(this.playerData)) } clearData() { localStorage.clear() } initSound() { AudioMgr.inst.activeSound(!this.playerData.muteSound) AudioMgr.inst.activeMusic(!this.playerData.muteMusic) } activeSound(active: boolean) { this.playerData.muteSound = !active AudioMgr.inst.activeSound(active) this.save() } activeMusic(active: boolean) { this.playerData.muteMusic = !active AudioMgr.inst.activeMusic(active) this.save() } equipWeapon(index: number) { this.playerData.equipedWeapon = index this.save() } buyWeapon(index: number) { this.playerData.ownWeapon.push(index) console.log("购买武器", index, this.playerData.ownWeapon[this.playerData.ownWeapon.length - 1]) this.save() } timerAddPower(count: number) { this.playerData.power += count if (this.playerData.power > Config.maxHp) { this.playerData.power = Config.maxHp } EventCenter.powerChange.emit("powerChange") this.save() } usePower() { this.playerData.power -= 1 EventCenter.powerChange.emit("powerChange") this.save() } setSideTip() { this.playerData.haveGetSideReward = true this.save() } adsAddPower() { this.playerData.power += 3 EventCenter.powerChange.emit("powerChange") this.save() } completeTutorial() { this.playerData.haveTutorial = true this.save() } getPowerCD() { let passTime = (Date.now() - this.playerData.lastAddPowerTimer) / 1000 return Config.powerCD - passTime } setShareTime() { const date: Date = new Date(); const dayOfMonth: number = date.getDate(); this.playerData.day = dayOfMonth this.save() console.log(dayOfMonth) } protected update(dt: number): void { if (this.playerData.power < Config.maxHp) { if (this.playerData.isOnTimerRun == false) { this.playerData.isOnTimerRun = true this.playerData.lastAddPowerTimer = Date.now() this.save() } if (this.playerData.isOnTimerRun) { let passTime = (Date.now() - this.playerData.lastAddPowerTimer) / 1000 // console.log(passTime) if ((passTime > Config.powerCD)) { console.log("倒计时足够") this.playerData.lastAddPowerTimer = Date.now() this.timerAddPower(Math.floor(passTime / Config.powerCD)) } if (this.playerData.power >= Config.maxHp) { console.log("体力超过上线") this.playerData.isOnTimerRun = false this.save() } } } } } class PlayerData { muteMusic: boolean = false muteSound: boolean = false levelIndex: number = 1 equipedWeapon: number = 1 power: number = Config.maxHp coin: number = 100 ownWeapon: number[] = [1] lastAddPowerTimer: number = null isOnTimerRun: boolean = false haveGetSideReward: boolean = false haveShare: boolean = false haveTutorial: boolean = false day: number = -1 jsonDataInit(obj) { if (obj == null) return if (obj.muteMusic != null) this.muteMusic = obj.muteMusic if (obj.muteSound != null) this.muteSound = obj.muteSound if (obj.levelIndex != null) this.levelIndex = obj.levelIndex if (obj.equipedWeapon != null) this.equipedWeapon = obj.equipedWeapon if (obj.ownWeapon != null) this.ownWeapon = obj.ownWeapon if (obj.coin != null) this.coin = obj.coin if (obj.lastAddPowerTimer != null) this.lastAddPowerTimer = obj.lastAddPowerTimer if (obj.power != null) this.power = obj.power if (obj.isOnTimerRun != null) this.isOnTimerRun = obj.isOnTimerRun if (obj.haveGetSideReward != null) this.haveGetSideReward = obj.haveGetSideReward if (obj.haveShare != null) this.haveShare = obj.haveShare if (obj.day != null) this.day = obj.day if (obj.haveTutorial != null) this.haveTutorial = obj.haveTutorial } }