03ebba3c-fcde-43a5-8ce6-463b5e254dc4.js 14.6 KB
"use strict";
cc._RF.push(module, '03ebbo8/N5DpYzmRjteJU3E', 'SaveDataManager');
// scripts/component/SaveDataManager.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaveDataManager = void 0;
var Global_1 = require("../Global");
var BattleConst_1 = require("../kernel/battle/BattleConst");
var GlobalNode_1 = require("../kernel/GlobalNode");
var TableDefine_1 = require("../kernel/table/TableDefine");
var UIManager_1 = require("../ui/UIManager");
var SoundManager_1 = require("./SoundManager");
var SaveDataManager = /** @class */ (function () {
    function SaveDataManager() {
        this.runtimeData = null;
        /**回到主场景后待添加的星星 */
        this.mainSceneAddStar = 0;
        // private _saveTimeFrq: number = 5;
        // private _saveTimeCount: number = 0;
        // private _coinDirty: number = 0;
        this.devDay = 0;
    }
    Object.defineProperty(SaveDataManager, "ins", {
        get: function () {
            if (this._ins == null) {
                this._ins = new SaveDataManager();
            }
            return this._ins;
        },
        enumerable: false,
        configurable: true
    });
    SaveDataManager.prototype.getNowTime = function () {
        if (CC_DEV) {
            return this.devDay * 3600000 * 24 + Date.now();
        }
        else {
            return Date.now();
        }
    };
    SaveDataManager.prototype.init = function () {
        this.resetData();
        var userDataText = cc.sys.localStorage.getItem('USERDATA_PLAYERDATA' + '_' + SaveDataManager.PKG_NAME);
        if (userDataText == null || userDataText.length == 0) {
            console.log('[Data][本地没有数据,使用初始数据]');
            this.saveData();
        }
        else {
            var userData = JSON.parse(userDataText);
            if (userData.curDataVersion == SaveDataManager.DATA_VERSION) {
                this.runtimeData = this.getDataObj(this.runtimeData, userData);
            }
            else {
                console.log('[Data][数据版本不同,使用初始数据]');
                this.saveData();
            }
            // this.runtimeData = JSON.parse(userDataText);
            // console.log('[Data][读取结束]结果为:')
            // console.log(this.runtimeData);
        }
        // 重新计算每日任务
        // this.Action_RefreshDailyMission();
        // 保存数据
        // this.saveData();
        // DataManager.inited = true;
        // if (_finishCallback != null) {
        //     _finishCallback();
        // }
        // this._coinDirty = this.runtimeData.curCoin;
        cc.game.on(cc.game.EVENT_HIDE, this.onGameHideOrClose, this);
    };
    SaveDataManager.prototype.onGameHideOrClose = function () {
        cc.log('game hide');
        this.saveData();
    };
    SaveDataManager.prototype.getDataObj = function (originalObj, newObj) {
        if (typeof (originalObj) == 'string') {
            return newObj;
        }
        var keys_runtimeData = Object.keys(originalObj);
        var keys_loaded = Object.keys(newObj);
        for (var i = 0; i < keys_runtimeData.length; i++) {
            var key = keys_runtimeData[i];
            // if (key == 'unlockSkinList') {
            //     log();
            // }
            // 找到储存的数据一致的key,赋值给本地
            for (var j = 0; j < keys_loaded.length; j++) {
                if (keys_loaded[j] == key) {
                    if (originalObj[key] instanceof Array) {
                        originalObj[key] = newObj[key];
                    }
                    else {
                        var tempKeys = Object.keys(originalObj[key]);
                        if (tempKeys.length > 0) {
                            originalObj[key] = this.getDataObj(originalObj[key], newObj[key]);
                        }
                        else {
                            originalObj[key] = newObj[key];
                        }
                    }
                    break;
                }
            }
        }
        return originalObj;
    };
    SaveDataManager.prototype.resetData = function () {
        this.runtimeData = {
            curDataVersion: SaveDataManager.DATA_VERSION,
            star: 0,
            diamond: 0,
            curGameScene: 1,
            unlockObj: [],
            userSkillNum: { 1: 0, 2: 0, 3: 0, 4: 0 },
            curLevel: 0,
            gender: -1,
            freeWheelNum: 3,
            loginRewardData: {
                step: 0,
                freshTime: 0,
            },
            musicMute: false,
            SEMute: false,
            vibration: true,
            goldenAimEndTime: 0,
            goldenAimTry: false,
            ballSkinUnlocked: [4],
            ballSkinCurrent: 4,
            shopDailyPackRefreshTime: 0,
            ballSkinTry: 0,
            guideStep: 0,
            levelScoreMap: {}
        };
    };
    SaveDataManager.prototype.saveData = function () {
        // console.log("[Data][保存数据成功]")
        // console.log(DataManager.RuntimeData)
        cc.sys.localStorage.setItem('USERDATA_PLAYERDATA' + '_' + SaveDataManager.PKG_NAME, JSON.stringify(this.runtimeData));
        // this._coinDirty = this.runtimeData.curCoin;
    };
    /**
     *
     * @param delta
     * @param useAni
     * @param wPos 星星动画的目标点,世界坐标,当useAni为true时且delta小于0时需要传入
     * @returns 是否成功,当delta小于0时,会检测当前钱是否足够
     */
    SaveDataManager.prototype.adjustStar = function (delta, useAni, wPos) {
        var _this = this;
        if (useAni === void 0) { useAni = false; }
        if (delta < 0 && this.runtimeData.star < Math.abs(delta)) {
            return false;
        }
        if (useAni) {
            if (delta < 0) {
                UIManager_1.UIManager.ins.widgetLayer.convertToNodeSpaceAR(wPos, wPos); //扣钱时使用传入的位置
            }
            else {
                var widgetNode = UIManager_1.UIManager.ins.getWidgetNode(Global_1.ResType.STAR);
                wPos = cc.v3(widgetNode.position.x - widgetNode.width / 2, widgetNode.y, widgetNode.z); //加钱时使用widget位置
            }
            BattleConst_1.getGlobalNode().getComponent(GlobalNode_1.default).schedule(function (dt) {
                _this.makeStar(wPos, delta / Math.abs(delta));
            }, 0.1, Math.abs(delta) - 1, 0);
        }
        else {
            this.runtimeData.star += delta;
            this.saveData();
            BattleConst_1.getGlobalNode().emit(Global_1.GlobalEvent.CHANGE_EVENT_STAR, delta);
        }
        return true;
    };
    /**
     *
     * @param delta
     * @returns 是否成功,当delta小于0时,会检测当前钱是否足够
     */
    SaveDataManager.prototype.adjustDiamond = function (delta, useAni, wPos) {
        var _this = this;
        if (useAni === void 0) { useAni = false; }
        if (delta < 0 && this.runtimeData.diamond < Math.abs(delta)) {
            UIManager_1.UIManager.ins.openWindow(Global_1.WindowName.FREE_DIAMOND);
            return false;
        }
        if (useAni) {
            if (delta < 0) {
                UIManager_1.UIManager.ins.widgetLayer.convertToNodeSpaceAR(wPos, wPos); //扣钱时使用传入的位置
            }
            else {
                var widgetNode = UIManager_1.UIManager.ins.getWidgetNode(Global_1.ResType.DIAMOND);
                wPos = cc.v3(widgetNode.position.x - widgetNode.width / 2, widgetNode.y, widgetNode.z); //加钱时使用widget位置
            }
            if (Math.abs(delta) < 5) {
                this.makeDiamond(wPos, delta); //小于5只播一次
            }
            else {
                var step_1 = Math.ceil(delta / 5);
                var lastStep_1 = delta - step_1 * 4;
                //console.log('step', step, 'last step', lastStep);
                BattleConst_1.getGlobalNode().getComponent(GlobalNode_1.default).schedule(function () {
                    _this.makeDiamond(wPos, step_1);
                }, 0.1, 3, 0);
                if (lastStep_1 != 0) {
                    BattleConst_1.getGlobalNode().getComponent(GlobalNode_1.default).scheduleOnce(function () {
                        _this.makeDiamond(wPos, lastStep_1);
                    }, 0.5);
                }
            }
            // getGlobalNode().getComponent(GlobalNode).schedule((dt) => {
            //     this.makeDiamond(wPos, delta / Math.abs(delta));
            // }, 0.1, Math.abs(delta) - 1, 0);
        }
        else {
            this.runtimeData.diamond += delta;
            this.saveData();
            BattleConst_1.getGlobalNode().emit(Global_1.GlobalEvent.CHANGE_EVENT_DIAMOND, delta);
        }
        return true;
    };
    SaveDataManager.prototype.unlockGameSceneObj = function (ID) {
        if (!this.runtimeData.unlockObj.includes(ID)) {
            var config = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_SCENE_OBJ, ID);
            if (config) {
                this.runtimeData.unlockObj.push(ID);
                BattleConst_1.getGlobalNode().emit(Global_1.GlobalEvent.OBJ_UNLOCK, ID);
                SoundManager_1.SoundManager.ins.playEffect(Global_1.SoundName.BUILD);
            }
        }
    };
    SaveDataManager.prototype.setUserSkillNumDelta = function (skillId, delta) {
        if (!this.runtimeData.userSkillNum[skillId]) {
            this.runtimeData.userSkillNum[skillId] = delta;
        }
        else {
            this.runtimeData.userSkillNum[skillId] += delta;
        }
        if (this.runtimeData.userSkillNum[skillId] < 0) {
            this.runtimeData.userSkillNum[skillId] = 0;
        }
        BattleConst_1.getGlobalNode().emit(Global_1.GlobalEvent.SKILL_NUM_CHANGE, skillId);
    };
    SaveDataManager.prototype.getUserSkillNum = function (skillId) {
        if (!this.runtimeData.userSkillNum[skillId]) {
            return 0;
        }
        else {
            return this.runtimeData.userSkillNum[skillId];
        }
    };
    SaveDataManager.prototype.setMusicMute = function (value) {
        this.runtimeData.musicMute = value;
        this.saveData();
        BattleConst_1.getGlobalNode().emit(Global_1.GlobalEvent.SOUND_MUSIC_CONFIG_CHANGE, this.runtimeData.musicMute);
    };
    SaveDataManager.prototype.setSoundEffectMute = function (value) {
        this.runtimeData.SEMute = value;
        this.saveData();
        BattleConst_1.getGlobalNode().emit(Global_1.GlobalEvent.SOUND_EFFECT_CONFIG_CHANGE, this.runtimeData.SEMute);
    };
    SaveDataManager.prototype.unlockBallSkin = function (id) {
        if (!this.runtimeData.ballSkinUnlocked.includes(id)) {
            this.runtimeData.ballSkinUnlocked.push(id);
            this.saveData();
            BattleConst_1.getGlobalNode().emit(Global_1.GlobalEvent.BALL_SKIN_UNLOCK);
        }
    };
    SaveDataManager.prototype.useBallSkin = function (id) {
        if (id != SaveDataManager.ins.runtimeData.ballSkinCurrent) {
            SaveDataManager.ins.runtimeData.ballSkinCurrent = id;
            this.saveData();
            BattleConst_1.getGlobalNode().emit(Global_1.GlobalEvent.BALL_SKIN_USE);
        }
    };
    SaveDataManager.prototype.update = function (dt) {
        // if (this._addStarNum > 0) {
        //     this._addStarCount += dt;
        //     if (this._addStarCount > 0.1) {
        //         this._addStarCount = 0;
        // this.makeStar();
        // }
        // }
        // this._saveTimeCount += dt;
        // if (this._saveTimeCount >= this._saveTimeFrq) {
        //     this._saveTimeCount = 0;
        //     if (this.runtimeData.curCoin != this._coinDirty) { //有脏数据时才保存
        //         // this._coinDirty = this.runtimeData.curCoin;
        //         this.saveData();
        //     }
        // }
    };
    SaveDataManager.prototype.makeStar = function (wPos, delta) {
        var _this = this;
        var node = new cc.Node();
        var sp = node.addComponent(cc.Sprite);
        sp.spriteFrame = cc.resources.get('ui/general/UI_Title_star', cc.SpriteFrame);
        UIManager_1.UIManager.ins.widgetLayer.addChild(node);
        if (delta < 0) {
            //如果扣钱,起点设在widget上
            var widgetNode = UIManager_1.UIManager.ins.getWidgetNode(Global_1.ResType.STAR);
            node.setPosition(widgetNode.position.x - widgetNode.width / 2, widgetNode.y, widgetNode.z);
        }
        cc.tween(node).to(0.2, { position: wPos }, { easing: 'sineOut' }).call(function () {
            node.destroy();
            _this.adjustStar(delta, false);
        }).start();
        SoundManager_1.SoundManager.ins.playEffect(Global_1.SoundName.USE_STAR);
    };
    SaveDataManager.prototype.makeDiamond = function (wPos, delta) {
        var _this = this;
        var node = new cc.Node();
        var sp = node.addComponent(cc.Sprite);
        sp.spriteFrame = cc.resources.get('ui/general/UI_Title_diamond', cc.SpriteFrame);
        UIManager_1.UIManager.ins.widgetLayer.addChild(node);
        if (delta < 0) {
            //如果扣钱,起点设在widget上
            var widgetNode = UIManager_1.UIManager.ins.getWidgetNode(Global_1.ResType.DIAMOND);
            node.setPosition(widgetNode.position.x - widgetNode.width / 2, widgetNode.y, widgetNode.z);
        }
        else {
            SoundManager_1.SoundManager.ins.playEffect(Global_1.SoundName.DIAMOND);
        }
        cc.tween(node).to(0.2, { position: wPos }, { easing: 'sineOut' }).call(function () {
            node.destroy();
            _this.adjustDiamond(delta, false);
        }).start();
    };
    /**设置某一关的过关成绩(星星数) */
    SaveDataManager.prototype.setLevelStarNum = function (level, star) {
        if (star < 0 || star > 3) {
            return;
        }
        if (this.runtimeData.levelScoreMap[level]) {
            //已有数据,只有在取得更好成绩时才更改
            if (star > this.runtimeData.levelScoreMap[level]) {
                this.runtimeData.levelScoreMap[level] = star;
                BattleConst_1.getGlobalNode().emit(Global_1.GlobalEvent.LEVEL_STAR_CHANGE, level);
                this.saveData();
            }
        }
        else {
            this.runtimeData.levelScoreMap[level] = star;
            BattleConst_1.getGlobalNode().emit(Global_1.GlobalEvent.LEVEL_STAR_CHANGE, level);
            this.saveData();
        }
    };
    SaveDataManager.prototype.getLevelStarNum = function (levelId) {
        if (this.runtimeData.levelScoreMap[levelId]) {
            return this.runtimeData.levelScoreMap[levelId];
        }
        return 0;
    };
    SaveDataManager._ins = null;
    SaveDataManager.DATA_VERSION = "1.0.1";
    SaveDataManager.PKG_NAME = 'com.BaiYing.Marbles';
    return SaveDataManager;
}());
exports.SaveDataManager = SaveDataManager;

cc._RF.pop();