Unity 升级系统 经验值管理器 AES 加密 经验配置表存档

ParseLevelRequiredExperienceJson 函数在 管理器初始化先解密经验值配置表 > 为什么需要 加密?
游戏数值系统 必然会加密的

using System.Collections.Generic;
using System.IO;
using ARPGDemo.Character;
using Common.Encryption;
using LitJson;
using UnityEngine;

namespace Common
{
    /// 
    /// 经验值管理器
    /// 
    public class ExperienceValueManager : MonoSingleton<ExperienceValueManager>
    {
        public Dictionary<int, int> levelRequiredExperienceValueDict;
        private PlayerStatus playerStatus;

        protected override void Init()
        {
            base.Init();
            levelRequiredExperienceValueDict = new Dictionary<int, int>();
            playerStatus = GameObject.Find("Player").GetComponent<PlayerStatus>();
            ParseLevelRequiredExperienceJson();
        }


        /// 
        /// 根据当前等级返回经验值
        /// 
        /// 
        /// 
        private int GetRequiredExperienceValue(int level)
        {
            return levelRequiredExperienceValueDict.TryGet(level);
        }

        public void AddPlayerExp(int value)
        {
            var currentValue = GetCurrentExpValue();
            var requiredExpValue = GetRequiredExperienceValue(playerStatus.level);

            if (currentValue + value >= requiredExpValue)
            {
                //先增加一级
                SetLevel(playerStatus.level + 1);
                //当前剩余经验值
                var residualEmpiricalValue = currentValue + value - requiredExpValue;
                //升级后的所需经验值
                var requiredExperienceValue = GetRequiredExperienceValue(playerStatus.level + 1);

                //更新UI 
                LevelPanel.Instance.UpdateLevelText(playerStatus.level, residualEmpiricalValue,
                    requiredExperienceValue);
                
                if (residualEmpiricalValue >= requiredExperienceValue)
                {
                    SetCurrentExpValue(requiredExperienceValue);
                    AddPlayerExp(residualEmpiricalValue - requiredExperienceValue);
                }
                else
                {
                    SetCurrentExpValue(residualEmpiricalValue);
                }
            }
            else
            {
                //只增加经验值
                SetCurrentExpValue(GetCurrentExpValue() + value);
                //更新UI 
                LevelPanel.Instance.UpdateLevelText(playerStatus.level, GetCurrentExpValue() + value,
                    GetRequiredExperienceValue(playerStatus.level));
            }
        }


        private void SetLevel(int level)
        {
            playerStatus.level = level;
        }


        /// 
        /// 解密AES 算法   得到明文的 经验所需配置表 信息,反序列化成对象存储进字典里
        /// 
        private void ParseLevelRequiredExperienceJson()
        {
            JsonData jsonData =
                JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Resources/LevelRequiredExperience.data")
                    .Decipher());
            foreach (JsonData item in jsonData)
            {
                levelRequiredExperienceValueDict.Add(int.Parse(item["level"].ToString()),
                    int.Parse(item["requiredExperience"].ToString()));
            }
        }

        /// 
        /// 获取当前经验值
        /// 
        /// 
        private int GetCurrentExpValue()
        {
            return playerStatus.experience;
        }

        /// 
        /// 设置当前经验值
        /// 
        /// 
        private void SetCurrentExpValue(int exp)
        {
            playerStatus.experience = exp;
        }
    }
}

你可能感兴趣的:(教程,unity,游戏,游戏引擎,c#)