using System.Collections.Generic; //using System.IO; using UnityEngine; /// /// 资源类型 /// public enum EResourceType { Environment, Item, Actor, Enemy, } /// /// 地图资源信息 /// [System.Serializable] public class MapResourceInfo { public int Level; public EResourceType Type; public int ID; public Vector2 Point; } /// /// 资源信息 /// [System.Serializable] public class ResourceInfo { public EResourceType Type; public int ID; public string Name; public string Info; public string Path; public string IconPath; } /// /// 游戏信息 用于存档 /// [System.Serializable] public class GameInfo { public string MapArchive; public string PlayerInfo; public int LevelInfo; public int MaxLevelInfo; public string BackpackInfo; public string PlotInfo; //每个层商店购买次数 public int StoreBuyNum_F4 = 0; public int StoreBuyNum_F12 = 0; public int StoreBuyNum_F32 = 0; public int StoreBuyNum_F46 = 0; } public class ResourceManager : Singleton { //private string _mapInfoPath = Application.dataPath + "/Settings/地图信息.txt"; //private string _propertyInfoPath = Application.dataPath + "/Settings/属性列表.txt"; private List _mapResourceInfoList = new List(); private Dictionary> _resourceInfoDic = new Dictionary>(); public ResourceManager() { LoadPropertyFile(); } /// /// 绑定事件 /// public void BindEvent() { GameManager.Instance.EventManager.OnSaveGameInput += SaveGameInfoEvent; GameManager.Instance.EventManager.OnLevelChanged += LoadLevelEvent; } /// /// 新建游戏信息 /// public void NewGameInfo() { //// 打开 txt 设置地图信息 //using (FileStream fs = new FileStream(_mapInfoPath, FileMode.Open)) //{ // // 创建 txt 字节长度的 byte 数组 // byte[] bytes = new byte[fs.Length]; // // 读取 txt 字节到 byte 数组 // fs.Read(bytes, 0, bytes.Length); // // 用 utf-8 解码 // string mapStr = System.Text.Encoding.UTF8.GetString(bytes); // // json 转对象 list // _mapResourceInfoList = JsonUtility.FromJson>(mapStr).ToList(); //} //Debug.Log("mapinfo" + Resources.Load("cfg/mapinfo").text); _mapResourceInfoList = JsonUtility.FromJson>(Resources.Load("cfg/mapinfo").text).ToList(); // 初始化剧情信息 GameManager.Instance.PlotManager.Init(); // 设置层数信息 GameManager.Instance.LevelManager.Level = 1; // 设置玩家信息 GameManager.Instance.PlayerManager.PlayerInfo = new PlayerInfo { Health = 1000, Attack = 100, Defence = 100, Gold = 0, WeaponID = 32, ArmorID = 33, NotepadInfo = "", }; // 删除标识符 PlayerPrefs.DeleteKey("NewGame"); } /// /// 加载资源 /// /// 资源类型 /// 资源 id /// 资源物体 public GameObject LoadResource(EResourceType type, int id) { // 获取资源属性 ResourceInfo tempInfo = _resourceInfoDic[type][id]; if (null == tempInfo) return null; //Debug.Log("加载资源:"+ tempInfo.Path); // 创建资源并返回 return Object.Instantiate(Resources.Load(tempInfo.Path), Vector3.zero, Quaternion.identity); } /// /// 获取资源信息 /// /// 资源类型 /// 资源 id /// 资源信息 public ResourceInfo GetResourceInfo(EResourceType type, int id) { if (!_resourceInfoDic.ContainsKey(type)) return null; if (!_resourceInfoDic[type].ContainsKey(id)) return null; return _resourceInfoDic[type][id]; } /// /// 获取新游戏状态 /// /// public bool GetNewGameStatus() { return PlayerPrefs.HasKey("NewGame"); } /// /// 获取游戏存档状态 /// /// 是否有存档 public bool GetGameArchiveStatus() { return PlayerPrefs.HasKey("GameInfo"); } /// /// 加载游戏存档 /// /// 是否能够加载 public void LoadGameArchive() { // 判断存档是否存在 if (!PlayerPrefs.HasKey("GameInfo")) return; // json 对象转 GameInfo GameInfo gameInfo = JsonUtility.FromJson(PlayerPrefs.GetString("GameInfo")); // 加载存档 _mapResourceInfoList = JsonUtility.FromJson>(gameInfo.MapArchive).ToList(); // 加载背包信息 GameManager.Instance.BackpackManager.BackpackDictionary = JsonUtility.FromJson>(gameInfo.BackpackInfo).ToDictionary(); // 加载玩家信息 GameManager.Instance.PlayerManager.PlayerInfo = JsonUtility.FromJson(gameInfo.PlayerInfo); // 加载剧情信息 GameManager.Instance.PlotManager.PlotDictionary = JsonUtility.FromJson>(gameInfo.PlotInfo).ToDictionary(); // 加载层数信息 GameManager.Instance.LevelManager.Level = gameInfo.LevelInfo; GameManager.Instance.LevelManager.MaxLevel = gameInfo.MaxLevelInfo; } /// /// 移动玩家位置 /// /// 关卡 /// 位置 public void MovePlayerPointForLevel(int level, Vector2 point) { _mapResourceInfoList.ForEach(mri => { if (mri.Level == level && mri.Type == EResourceType.Actor && mri.ID == 1) { mri.Point = point; return; } }); } /// /// 创建资源到关卡 /// /// /// /// /// public void MakeResourceForLevel(int level, EResourceType type, int id, Vector2 point) { _mapResourceInfoList.Add(new MapResourceInfo() { Level = level, Type = type, ID = id, Point = point, }); } /// /// 加载属性文件 /// private void LoadPropertyFile() { //Debug.Log("attinfo" + Resources.Load("cfg/attinfo").text); List tempList = JsonUtility.FromJson>(Resources.Load("cfg/attinfo").text).ToList(); // 将 list 加入字典 tempList.ForEach(ri => { if (!_resourceInfoDic.ContainsKey(ri.Type)) _resourceInfoDic.Add(ri.Type, new Dictionary()); if (!_resourceInfoDic[ri.Type].ContainsKey(ri.ID)) _resourceInfoDic[ri.Type].Add(ri.ID, ri); }); /*// 判断属性文件是否存在 if (!File.Exists(_propertyInfoPath)) return; // 打开 txt using (FileStream fs = new FileStream(_propertyInfoPath, FileMode.Open)) { // 创建 txt 字节长度的 byte 数组 byte[] bytes = new byte[fs.Length]; // 读取 txt 字节到 byte 数组 fs.Read(bytes, 0, bytes.Length); // 用 utf-8 解码 string infoStr = System.Text.Encoding.UTF8.GetString(bytes); // json 转对象 list List tempList = JsonUtility.FromJson>(infoStr).ToList(); // 将 list 加入字典 tempList.ForEach(ri => { if (!_resourceInfoDic.ContainsKey(ri.Type)) _resourceInfoDic.Add(ri.Type, new Dictionary()); if (!_resourceInfoDic[ri.Type].ContainsKey(ri.ID)) _resourceInfoDic[ri.Type].Add(ri.ID, ri); }); }*/ } /// /// 加载关卡事件 /// /// 旧关卡序号 /// 新关卡序号 private void LoadLevelEvent(int oldIndex, int newIndex) { // 判断地图信息是否存在 if (_mapResourceInfoList.Count == 0) return; // 获取当前关卡在用资源 for (int i = _mapResourceInfoList.Count - 1; i >= 0; i--) { if (_mapResourceInfoList[i].Level == oldIndex) _mapResourceInfoList.RemoveAt(i); } // 更新到资源信息中 GameManager.Instance.PoolManager.UseList.ForEach(u => { EResourceType type = EResourceType.Actor; switch (u.tag) { case "Item": type = EResourceType.Item; break; case "Enemy": type = EResourceType.Enemy; break; case "Environment": type = EResourceType.Environment; break; default: break; } _mapResourceInfoList.Add(new MapResourceInfo { Level = oldIndex, Type = type, ID = u.GetComponent().ID, Point = u.transform.position, }); }); // 回收资源 GameManager.Instance.PoolManager.RecycleResource(); // 加载资源 foreach (var info in _mapResourceInfoList) { // 判断关卡一致 if (info.Level == newIndex) { GameObject tempObj = GameManager.Instance.PoolManager.GetResourceInFreePool(info.Type, info.ID); tempObj.transform.position = info.Point; } } // 加载完毕执行事件 GameManager.Instance.EventManager.OnResourceLoaded?.Invoke(); } /// /// 保存游戏信息事件 /// private void SaveGameInfoEvent() { // 删除当前关卡资源 for (int i = _mapResourceInfoList.Count - 1; i >= 0; i--) { if (_mapResourceInfoList[i].Level == GameManager.Instance.LevelManager.Level) _mapResourceInfoList.RemoveAt(i); } // 将在用对象池资源加入资源信息中 GameManager.Instance.PoolManager.UseList.ForEach(u => { EResourceType type = EResourceType.Actor; switch (u.tag) { case "Item": type = EResourceType.Item; break; case "Enemy": type = EResourceType.Enemy; break; case "Environment": type = EResourceType.Environment; break; } _mapResourceInfoList.Add(new MapResourceInfo { Level = GameManager.Instance.LevelManager.Level, Type = type, ID = u.GetComponent().ID, Point = u.transform.position, }); }); // 获取游戏信息 GameInfo gameInfo = new GameInfo { MapArchive = JsonUtility.ToJson(new Serialization(_mapResourceInfoList)), PlayerInfo = JsonUtility.ToJson(GameManager.Instance.PlayerManager.PlayerInfo), LevelInfo = GameManager.Instance.LevelManager.Level, MaxLevelInfo = GameManager.Instance.LevelManager.MaxLevel, BackpackInfo = JsonUtility.ToJson(new Serialization(GameManager.Instance.BackpackManager.BackpackDictionary)), PlotInfo = JsonUtility.ToJson(new Serialization(GameManager.Instance.PlotManager.PlotDictionary)), }; // 保存资源信息 PlayerPrefs.SetString("GameInfo", JsonUtility.ToJson(gameInfo)); // UI 提示 GameManager.Instance.UIManager.ShowInfo("游戏保存成功!"); // 音频播放 GameManager.Instance.SoundManager.PlaySound(ESoundType.Effect, "Save"); } }