NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Level/LevelManager.Data.cs

372 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Framework;
using System.Collections.Generic;
using System.Linq;
using cfg.LevelCfg;
using UnityEngine;
namespace Gameplay.Level
{
/// <summary>
/// 关卡管理器
/// </summary>
public partial class LevelManager
{
private const string LAST_FIGHT_LEVEL_PREFIX = "last_fight_level_";
/// <summary>
/// 关卡信息字典
/// </summary>
private Dictionary<int, LevelInfo> dicLevelInfo = new();
/// <summary>
/// 章节信息字典
/// </summary>
private Dictionary<int, ChapterInfo> dicChapterInfo = new();
/// <summary>
/// 关卡组信息字典,key:关卡组idvalue:关卡组信息
/// </summary>
private Dictionary<int, LevelGroupInfo> dicLevelGroupInfo = new();
private readonly int[] _levelGroupTypeRedPointIDArray = new[] { 101, 102, 103, 104 };
/// <summary>
/// 章节类型字典
/// </summary>
private Dictionary<E_ChapterType, List<ChapterInfo>> dicChapterType = new();
private void InitData()
{
EventManager.Instance.Register(EventManager.EventName.AllConfigLoad, OnAllConfigLoad);
EventManager.Instance.Register<int>(EventManager.EventName.NewLevelServerData, OnNewLevelServerData);
}
private void ReleaseData()
{
EventManager.Instance.Unregister<int>(EventManager.EventName.NewLevelServerData, OnNewLevelServerData);
EventManager.Instance.Unregister(EventManager.EventName.AllConfigLoad, OnAllConfigLoad);
DisposeRedPointTree();
dicLevelInfo = null;
dicChapterInfo = null;
dicLevelGroupInfo = null;
}
#region API
/// <summary>
/// 刷新所有关卡信息
/// </summary>
public void RefreshAllLevelInfo()
{
foreach (LevelInfo levelInfo in dicLevelInfo.Values)
{
levelInfo?.RefreshServerData(true);
}
}
/// <summary>
/// 通关关卡id获取关卡信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool TryGetLevelInfoByID(int id, out LevelInfo levelInfo)
{
if (!dicLevelInfo.TryGetValue(id, out levelInfo))
{
DebugUtil.LogError($"找不到levelInfo,id:{id}");
return false;
}
return true;
}
public LevelGroupInfo GetLevelGroupInfoByID(int id)
{
if (!dicLevelGroupInfo.TryGetValue(id, out var chapterInfo))
{
DebugUtil.LogError($"找不到chapterInfo,id:{id}");
}
return chapterInfo;
}
public ChapterInfo GetChapterInfoByID(int id)
{
if (!dicChapterInfo.TryGetValue(id, out var levelGroupInfo))
{
DebugUtil.LogError($"找不到levelGroupInfo,id:{id}");
}
return levelGroupInfo;
}
public List<ChapterInfo> GetAllChapterInfo()
{
return dicChapterInfo.Values.ToList();
}
/// <summary>
/// 根据章节类型获取章节信息
/// </summary>
/// <param name="levelChapterType"></param>
/// <returns></returns>
public bool TryGetChapterInfosByType(E_ChapterType type, out List<ChapterInfo> listChapterInfo)
{
if (dicChapterType.TryGetValue(type, out listChapterInfo) && null != listChapterInfo)
return true;
return false;
}
public List<LevelInfo> GetAllLevelInfo()
{
return dicLevelInfo.Values.ToList();
}
public List<LevelGroupInfo> GetAllLevelGroupInfo()
{
return dicLevelGroupInfo.Values.ToList();
}
//获取某种关卡类型上一次战斗的关卡
public LevelInfo GetLastFightLevelInfo(E_ChapterType type)
{
var key = GetChapterTypeSaveKey(type);
if (!PlayerPrefs.HasKey(key))
{
return null;
}
var levelID = PlayerPrefs.GetInt(key);
TryGetLevelInfoByID(levelID, out LevelInfo levelInfo);
return levelInfo;
}
/// <summary>
/// 根据关卡ID获取关卡所属章节类型
/// </summary>
/// <param name="levelID"></param>
/// <returns></returns>
public E_ChapterType GetLevelChapterType(int levelID)
{
if (!dicLevelInfo.ContainsKey(levelID))
{
DebugUtil.LogError("error level ID! ");
return E_ChapterType.Main;
}
if (!TryGetLevelInfoByID(levelID, out LevelInfo levelInfo))
return E_ChapterType.Count;
int levelGroupID = levelInfo.Cfg.LevelGroupID;
DataLevelGroup config = TableManager.Instance.Tables.LevelGroupConfig.Get(levelGroupID);
DataChapter chapterCfg = TableManager.Instance.Tables.Chapter.Get(config.ChapterID);
var chapterInfo = new ChapterInfo(chapterCfg);
return chapterInfo.Cfg.Type;
}
/// <summary>
/// 判断关卡是否解锁
/// </summary>
/// <param name="levelID"></param>
/// <returns></returns>
public bool CheckLevelUnlock(int levelID)
{
if(!TryGetLevelInfoByID(levelID, out LevelInfo levelInfo))
{
DebugUtil.LogError($"cant find levelInfo,id:{levelID}");
return false;
}
return !levelInfo.IsLock;
}
public string LevelIDToNum(int levelID)
{
if(!TryGetLevelInfoByID(levelID, out LevelInfo levelInfo))
{
DebugUtil.LogError($"cant find levelInfo,id:{levelID}");
return string.Empty;
}
return levelInfo.ChapterIndex + "-" + levelInfo.LevelIndex;
}
public string LevelIDToName(int levelID)
{
if(!TryGetLevelInfoByID(levelID, out LevelInfo levelInfo))
{
DebugUtil.LogError($"cant find levelInfo,id:{levelID}");
return string.Empty;
}
return levelInfo.Name;
}
public string GetLevelNum(LevelInfo info)
{
if (info == null)
{
DebugUtil.LogError("info is null");
return string.Empty;
}
return info.ChapterIndex + "-" + info.LevelIndex;
}
public string GetLevelName(LevelInfo info)
{
if (info == null)
{
DebugUtil.LogError("info is null");
return string.Empty;
}
return info.Name;
}
#endregion
#region 私有方法
/// <summary>
/// 加载配置
/// </summary>
private void OnAllConfigLoad()
{
LoadAllLevelInfo();
LoadAllLevelGroup();
LoadAllChapter();
ConstructRedPointTree();
}
/// <summary>
/// 加载所有关卡信息
/// </summary>
private void LoadAllLevelInfo()
{
dicLevelInfo.Clear();
List<DataLevel> listAllLevelConfig = TableManager.Instance.Tables.Level.DataList;
foreach (DataLevel config in listAllLevelConfig)
{
dicLevelInfo.Add(config.Id, new LevelInfo(config));
}
}
/// <summary>
/// 加载所有关卡组信息
/// </summary>
private void LoadAllLevelGroup()
{
dicLevelGroupInfo.Clear();
foreach (LevelInfo levelInfo in dicLevelInfo.Values)
{
int levelGroupID = levelInfo.Cfg.LevelGroupID;
if (!dicLevelGroupInfo.TryGetValue(levelGroupID, out LevelGroupInfo levelGroupInfo))
{
DataLevelGroup config = TableManager.Instance.Tables.LevelGroupConfig.Get(levelGroupID);
if (null != config)
{
levelGroupInfo = new LevelGroupInfo(config);
dicLevelGroupInfo.Add(levelGroupID, levelGroupInfo);
}
else
DebugUtil.LogError($"找不到关卡组配置, id: {levelGroupID}");
}
levelGroupInfo?.AddLevel(levelInfo);
}
}
/// <summary>
/// 加载所有章节
/// </summary>
private void LoadAllChapter()
{
dicChapterInfo.Clear();
foreach (LevelGroupInfo levelGroupInfo in dicLevelGroupInfo.Values)
{
int chapterID = levelGroupInfo.Cfg.ChapterID;
if (!dicChapterInfo.TryGetValue(chapterID, out ChapterInfo chapterInfo))
{
DataChapter config = TableManager.Instance.Tables.Chapter.Get(chapterID);
if (null != config)
{
chapterInfo = new ChapterInfo(config);
dicChapterInfo.Add(chapterID, chapterInfo);
}
else
DebugUtil.LogError($"找不到章节配置,id{chapterID}");
}
chapterInfo?.AddLevelGroupInfo(levelGroupInfo);
}
foreach (ChapterInfo chapterInfo in dicChapterInfo.Values)
{
E_ChapterType chapterType = chapterInfo.Cfg.Type;
if (!dicChapterType.TryGetValue(chapterType, out List<ChapterInfo> list))
{
list = new();
dicChapterType.Add(chapterType, list);
}
list.Add(chapterInfo);
}
}
private void ConstructRedPointTree()
{
foreach (ChapterInfo chapterInfo in dicChapterInfo.Values)
{
int index = (int)chapterInfo.Cfg.Type;
if (index < 0 || index > _levelGroupTypeRedPointIDArray.Length)
{
DebugUtil.LogError($"index is out of range!index:{index}");
continue;
}
int redPointParentID = _levelGroupTypeRedPointIDArray[index];
chapterInfo.ConstructRedPointTree(redPointParentID);
}
}
private void DisposeRedPointTree()
{
foreach (ChapterInfo chapterInfo in dicChapterInfo.Values)
{
if (null != chapterInfo.RedPoint)
RedPointManager.Instance.RemoveDynamicNode(chapterInfo.RedPoint);
}
}
/// <summary>
/// 新的关卡服务器数据
/// </summary>
/// <param name="levelID"></param>
private void OnNewLevelServerData(int levelID)
{
if (TryGetLevelInfoByID(levelID, out LevelInfo levelInfo))
levelInfo.RefreshServerData(false);
}
/// <summary>
/// 保存战斗关卡
/// </summary>
/// <param name="levelInfo"></param>
private void SaveFightLevel(LevelInfo levelInfo)
{
if(null == levelInfo)
return;
LevelGroupInfo levelGroupInfo = levelInfo.GroupInfo;
if(null == levelGroupInfo)
return;
ChapterInfo chapterInfo = levelGroupInfo.ChapterInfo;
if(null == chapterInfo)
return;
PlayerPrefs.SetInt(GetChapterTypeSaveKey(chapterInfo.Cfg.Type), levelInfo.Cfg.Id);
}
private string GetChapterTypeSaveKey(E_ChapterType chapterType) => LAST_FIGHT_LEVEL_PREFIX + chapterType;
#endregion
}
}