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

398 lines
13 KiB
C#
Raw Normal View History

2023-10-19 15:00:19 +08:00
using Framework;
using System.Collections.Generic;
2023-10-26 20:20:11 +08:00
using System.Linq;
2024-01-18 16:02:45 +08:00
using cfg.LevelCfg;
2024-01-19 19:20:05 +08:00
using UnityEngine;
2023-10-19 15:00:19 +08:00
namespace Gameplay.Level
{
2024-05-06 16:46:00 +08:00
/// <summary>
/// 关卡管理器
/// </summary>
2023-10-19 15:00:19 +08:00
public partial class LevelManager
{
2024-01-19 19:20:05 +08:00
private const string LAST_FIGHT_LEVEL_PREFIX = "last_fight_level_";
2024-05-06 16:46:00 +08:00
/// <summary>
/// 关卡信息字典
/// </summary>
2023-10-19 15:00:19 +08:00
private Dictionary<int, LevelInfo> _levelDic = new();
2024-05-06 16:46:00 +08:00
/// <summary>
/// 章节信息字典
/// </summary>
private Dictionary<int, ChapterInfo> chapterInfoDic = new();
2023-10-24 14:58:21 +08:00
2024-05-06 16:46:00 +08:00
/// <summary>
/// 关卡组信息字典,key:关卡组idvalue:关卡组信息
/// </summary>
2024-01-05 19:15:21 +08:00
private Dictionary<int, LevelGroupInfo> _levelGroupDic = new();
2023-10-24 14:58:21 +08:00
private readonly int[] _levelGroupTypeRedPointIDArray = new[] { 101, 102, 103 };
2024-01-19 14:00:42 +08:00
private Dictionary<LevelChapterType, List<ChapterInfo>> _chapterTypeDic = new();
2024-07-09 13:37:27 +08:00
public List<LevelInfo> ListLevelInfo
{
get { return _levelDic.Values.ToList(); }
}
2023-10-19 15:00:19 +08:00
private void InitData()
{
RegisterEventData();
}
private void ReleaseData()
{
UnregisterEventData();
DisposeRedPointTree();
2023-10-19 15:00:19 +08:00
_levelDic = null;
2024-05-06 16:46:00 +08:00
chapterInfoDic = null;
2024-01-05 19:15:21 +08:00
_levelGroupDic = null;
2023-10-19 15:00:19 +08:00
}
private void RegisterEventData()
{
EventManager.Instance.Register(EventManager.EventName.AllConfigLoad, OnAllConfigLoad);
EventManager.Instance.Register<int>(EventManager.EventName.NewLevelServerData, OnNewLevelServerData);
}
private void UnregisterEventData()
{
EventManager.Instance.Unregister(EventManager.EventName.AllConfigLoad, OnAllConfigLoad);
EventManager.Instance.Unregister<int>(EventManager.EventName.NewLevelServerData, OnNewLevelServerData);
}
private void OnAllConfigLoad()
2023-10-24 14:58:21 +08:00
{
LoadAllLevel();
LoadAllLevelGroup();
2024-01-05 18:26:18 +08:00
LoadAllChapter();
2024-02-06 13:29:01 +08:00
//InitSortIndex();
ConstructRedPointTree();
2023-10-24 14:58:21 +08:00
}
private void LoadAllLevel()
2023-10-19 15:00:19 +08:00
{
_levelDic.Clear();
2024-01-18 16:02:45 +08:00
var allLevelConfig = TableManager.Instance.Tables.Level.DataList;
int sortIndex = 0;
foreach (var config in allLevelConfig)
2023-10-19 15:00:19 +08:00
{
var levelInfo = new LevelInfo(config);
_levelDic.Add(levelInfo.ID, levelInfo);
}
}
2024-05-06 16:46:00 +08:00
/// <summary>
/// 加载所有章节
/// </summary>
2023-10-24 14:58:21 +08:00
private void LoadAllChapter()
{
2024-05-06 16:46:00 +08:00
chapterInfoDic.Clear();
foreach (LevelGroupInfo levelGroupInfo in _levelGroupDic.Values)
2023-10-24 14:58:21 +08:00
{
2024-05-06 16:46:00 +08:00
int chapterID = levelGroupInfo.ChapterID;
if (!chapterInfoDic.TryGetValue(chapterID, out ChapterInfo chapterInfo))
2023-10-24 14:58:21 +08:00
{
2024-05-06 16:46:00 +08:00
DataChapter config = TableManager.Instance.Tables.Chapter.Get(chapterID);
2023-10-24 14:58:21 +08:00
if (config != null)
{
2024-01-05 19:15:21 +08:00
chapterInfo = new ChapterInfo(config);
2024-05-06 16:46:00 +08:00
chapterInfoDic.Add(chapterID, chapterInfo);
2023-10-24 14:58:21 +08:00
}
else
{
2024-01-05 19:15:21 +08:00
DebugUtil.LogError($"cant find LevelGroup config,{chapterID}");
2023-10-24 14:58:21 +08:00
}
}
2024-05-06 16:46:00 +08:00
chapterInfo?.AddLevelGroup(levelGroupInfo);
2023-10-24 14:58:21 +08:00
}
2024-01-19 14:00:42 +08:00
2024-05-06 16:46:00 +08:00
foreach (ChapterInfo chapterInfo in chapterInfoDic.Values)
2024-01-19 14:00:42 +08:00
{
2024-05-06 16:46:00 +08:00
LevelChapterType chapterType = chapterInfo.ChapterType;
if (!_chapterTypeDic.TryGetValue(chapterType, out List<ChapterInfo> list))
2024-01-19 14:00:42 +08:00
{
list = new();
_chapterTypeDic.Add(chapterType, list);
}
list.Add(chapterInfo);
}
2023-10-24 14:58:21 +08:00
}
2024-05-06 16:46:00 +08:00
/// <summary>
/// 加载所有关卡组信息
/// </summary>
2023-10-24 14:58:21 +08:00
private void LoadAllLevelGroup()
{
_levelGroupDic.Clear();
2024-05-06 16:46:00 +08:00
foreach (LevelInfo levelInfo in _levelDic.Values)
2023-10-24 14:58:21 +08:00
{
2024-05-06 16:46:00 +08:00
int levelGroupID = levelInfo.LevelGroupID;
if (!_levelGroupDic.TryGetValue(levelGroupID, out LevelGroupInfo levelGroupInfo))
2023-10-24 14:58:21 +08:00
{
2024-05-06 16:46:00 +08:00
DataLevelGroup config = TableManager.Instance.Tables.LevelGroupConfig.Get(levelGroupID);
2023-10-24 14:58:21 +08:00
if (config != null)
{
2024-01-05 19:15:21 +08:00
levelGroupInfo = new LevelGroupInfo(config);
_levelGroupDic.Add(levelGroupID, levelGroupInfo);
2023-10-24 14:58:21 +08:00
}
else
{
2024-01-05 19:15:21 +08:00
DebugUtil.LogError($"cant find chapter config,{levelGroupID}");
2023-10-24 14:58:21 +08:00
}
}
2024-05-06 16:46:00 +08:00
levelGroupInfo?.AddLevel(levelInfo);
2023-10-24 14:58:21 +08:00
}
}
2024-02-06 13:29:01 +08:00
// private void InitSortIndex()
// {
// foreach (var list in _chapterTypeDic.Values)
// {
// list.Sort((a, b) => a.ID.CompareTo(b.ID));
// for (int i = 0; i < list.Count; i++)
// {
// var chapterInfo = list[i];
// chapterInfo.SortIndex = i;
// chapterInfo.InitAllLevelSortIndex();
// }
// }
// }
2024-01-19 14:00:42 +08:00
private void ConstructRedPointTree()
{
2024-05-06 16:46:00 +08:00
foreach (var levelGroupInfo in chapterInfoDic.Values)
{
2024-01-18 16:02:45 +08:00
var index = (int)levelGroupInfo.ChapterType;
if (index < 0 || index > _levelGroupTypeRedPointIDArray.Length)
{
DebugUtil.LogError($"index is out of range!index:{index}");
continue;
}
var redPointParentID = _levelGroupTypeRedPointIDArray[index];
levelGroupInfo.ConstructRedPointTree(redPointParentID);
}
}
private void DisposeRedPointTree()
{
2024-05-06 16:46:00 +08:00
foreach (var levelGroupInfo in chapterInfoDic.Values)
{
if (levelGroupInfo.RedPointNode != null)
{
RedPointManager.Instance.RemoveDynamicNode(levelGroupInfo.RedPointNode);
}
}
}
2023-10-19 15:00:19 +08:00
private void OnNewLevelServerData(int levelID)
{
var levelInfo = GetLevelInfoByID(levelID);
if (levelInfo != null)
{
levelInfo.RefreshServerData();
}
}
2024-01-19 19:20:05 +08:00
private void SaveFightLevel(LevelInfo levelInfo)
{
if(levelInfo == null)
return;
var levelGroupInfo = levelInfo.LevelGroupInfo;
if(levelGroupInfo == null)
return;
var chapterInfo = levelGroupInfo.ChapterInfo;
if(chapterInfo == null)
return;
PlayerPrefs.SetInt(GetChapterTypeSaveKey(chapterInfo.ChapterType), levelInfo.ID);
}
2024-05-06 16:46:00 +08:00
private string GetChapterTypeSaveKey(LevelChapterType chapterType) => LAST_FIGHT_LEVEL_PREFIX + chapterType;
2024-01-19 19:20:05 +08:00
2023-10-19 15:00:19 +08:00
#region API
2023-10-24 14:58:21 +08:00
2023-10-19 15:00:19 +08:00
public LevelInfo GetLevelInfoByID(int id)
{
if (!_levelDic.TryGetValue(id, out var levelInfo))
{
DebugUtil.LogError($"找不到levelInfo,id:{id}");
}
2023-10-24 14:58:21 +08:00
2023-10-19 15:00:19 +08:00
return levelInfo;
}
2023-10-24 14:58:21 +08:00
2024-01-19 14:00:42 +08:00
public LevelGroupInfo GetLevelGroupInfoByID(int id)
2023-10-24 14:58:21 +08:00
{
2024-01-05 19:15:21 +08:00
if (!_levelGroupDic.TryGetValue(id, out var chapterInfo))
2023-10-24 14:58:21 +08:00
{
DebugUtil.LogError($"找不到chapterInfo,id:{id}");
}
return chapterInfo;
}
2024-01-19 14:00:42 +08:00
public ChapterInfo GetChapterInfoByID(int id)
2023-10-24 14:58:21 +08:00
{
2024-05-06 16:46:00 +08:00
if (!chapterInfoDic.TryGetValue(id, out var levelGroupInfo))
2023-10-24 14:58:21 +08:00
{
DebugUtil.LogError($"找不到levelGroupInfo,id:{id}");
}
return levelGroupInfo;
}
2023-10-26 20:20:11 +08:00
2024-01-05 19:35:51 +08:00
public List<ChapterInfo> GetAllChapterInfo()
2023-10-26 20:20:11 +08:00
{
2024-05-06 16:46:00 +08:00
return chapterInfoDic.Values.ToList();
2023-10-26 20:20:11 +08:00
}
2024-04-07 16:22:58 +08:00
/// <summary>
/// 根据章节类型获取章节信息
/// </summary>
/// <param name="levelChapterType"></param>
/// <returns></returns>
public void GetChapterInfosByType(LevelChapterType type, ref List<ChapterInfo> listChapterInfo)
{
listChapterInfo ??= new();
listChapterInfo.Clear();
2024-05-06 16:46:00 +08:00
foreach (ChapterInfo chapterInfo in chapterInfoDic.Values)
2024-04-07 16:22:58 +08:00
{
if (chapterInfo.ChapterType == type)
listChapterInfo.Add(chapterInfo);
}
}
2023-10-26 20:20:11 +08:00
public List<LevelInfo> GetAllLevelInfo()
{
return _levelDic.Values.ToList();
}
2024-01-05 19:35:51 +08:00
public List<LevelGroupInfo> GetAllLevelGroupInfo()
2023-10-26 20:20:11 +08:00
{
2024-01-05 19:15:21 +08:00
return _levelGroupDic.Values.ToList();
2023-10-26 20:20:11 +08:00
}
2024-04-07 16:22:58 +08:00
/// <summary>
/// 获取某种关卡类型的最新进度
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
2024-01-19 14:00:42 +08:00
public LevelInfo GetLatestLevelInfo(LevelChapterType type)
{
if (_chapterTypeDic.TryGetValue(type, out var list))
{
var latestChapterInfo = list.Where(chapterInfo => !chapterInfo.IsLock).ToList()[^1];
if (latestChapterInfo == null)
return null;
var allUnlockLevels = latestChapterInfo.AllLevelInfo.Where(level => !level.IsLock).ToList();
allUnlockLevels.Sort((a, b) => a.ID.CompareTo(b.ID));
return allUnlockLevels[^1];
}
return null;
}
2024-01-19 19:20:05 +08:00
//获取某种关卡类型上一次战斗的关卡
public LevelInfo GetLastFightLevelInfo(LevelChapterType type)
{
var key = GetChapterTypeSaveKey(type);
if (!PlayerPrefs.HasKey(key))
{
return null;
}
var levelID = PlayerPrefs.GetInt(key);
var levelInfo = GetLevelInfoByID(levelID);
return levelInfo;
}
/// <summary>
/// 根据关卡ID获取关卡所属章节类型
/// </summary>
/// <param name="levelID"></param>
/// <returns></returns>
public LevelChapterType GetLevelChapterType(int levelID)
{
if (!_levelDic.ContainsKey(levelID))
{
DebugUtil.LogError("error level ID! ");
return LevelChapterType.Main;
}
var levelInfo = GetLevelInfoByID(levelID);
int levelGroupID = levelInfo.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.ChapterType;
}
/// <summary>
/// 判断关卡是否解锁
/// </summary>
/// <param name="levelID"></param>
/// <returns></returns>
public bool CheckLevelUnlock(int levelID)
{
var levelInfo = GetLevelInfoByID(levelID);
if(levelInfo == null)
{
DebugUtil.LogError($"cant find levelInfo,id:{levelID}");
return false;
}
return !levelInfo.IsLock;
}
public string LevelIDToNum(int levelID)
{
var levelInfo = GetLevelInfoByID(levelID);
if(levelInfo == null)
{
DebugUtil.LogError($"cant find levelInfo,id:{levelID}");
return string.Empty;
}
return levelInfo.ChapterIndex + "-" + levelInfo.LevelIndex;
}
public string LevelIDToName(int levelID)
{
var levelInfo = GetLevelInfoByID(levelID);
if (levelInfo == null)
{
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;
}
2023-10-19 15:00:19 +08:00
#endregion
}
2023-10-24 14:58:21 +08:00
}