123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- //本地数据管理
- 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;
- private $lastLoginTime: number = 0;
- 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;
- }
- //保存玩家本地数据
- 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
- }
- }
- }
|