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

199 lines
6.1 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;
2023-10-19 15:00:19 +08:00
namespace Gameplay.Level
{
public partial class LevelManager
{
private Dictionary<int, LevelInfo> _levelDic = new();
2024-01-05 19:15:21 +08:00
private Dictionary<int, ChapterInfo> _chapterInfoDic = new();
2023-10-24 14:58:21 +08:00
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 };
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;
2023-10-24 14:58:21 +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();
ConstructRedPointTree();
2023-10-24 14:58:21 +08:00
}
private void LoadAllLevel()
2023-10-19 15:00:19 +08:00
{
_levelDic.Clear();
var allLevelConfig = TableManager.Instance.Tables.Level.DataMap;
foreach (var config in allLevelConfig.Values)
{
var levelInfo = new LevelInfo(config);
_levelDic.Add(levelInfo.ID, levelInfo);
}
}
2023-10-24 14:58:21 +08:00
private void LoadAllChapter()
{
_chapterInfoDic.Clear();
2024-01-05 19:15:21 +08:00
foreach (var levelGroupInfo in _levelGroupDic.Values)
2023-10-24 14:58:21 +08:00
{
2024-01-05 19:15:21 +08:00
var chapterID = levelGroupInfo.ChapterID;
ChapterInfo chapterInfo = null;
if (!_chapterInfoDic.TryGetValue(chapterID, out chapterInfo))
2023-10-24 14:58:21 +08:00
{
2024-01-05 19:15:21 +08:00
var 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);
_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-01-05 19:15:21 +08:00
if (chapterInfo != null)
2024-01-05 19:35:51 +08:00
chapterInfo.AddLevelGroup(levelGroupInfo);
2023-10-24 14:58:21 +08:00
}
}
private void LoadAllLevelGroup()
{
_levelGroupDic.Clear();
2024-01-05 18:26:18 +08:00
foreach (var levelInfo in _levelDic.Values)
2023-10-24 14:58:21 +08:00
{
2024-01-05 19:15:21 +08:00
var levelGroupID = levelInfo.LevelGroupID;
LevelGroupInfo levelGroupInfo = null;
if (!_levelGroupDic.TryGetValue(levelGroupID, out levelGroupInfo))
2023-10-24 14:58:21 +08:00
{
2024-01-05 19:15:21 +08:00
var 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-01-05 19:15:21 +08:00
if (levelGroupInfo != null)
levelGroupInfo.AddLevel(levelInfo);
2023-10-24 14:58:21 +08:00
}
}
private void ConstructRedPointTree()
{
2024-01-05 19:15:21 +08:00
foreach (var levelGroupInfo in _chapterInfoDic.Values)
{
var index = (int)levelGroupInfo.GroupType;
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-01-05 19:15:21 +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();
}
}
#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-05 19:15:21 +08:00
public LevelGroupInfo GetChapterInfoByID(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-05 19:15:21 +08:00
public ChapterInfo GetLevelGroupInfoByID(int id)
2023-10-24 14:58:21 +08:00
{
2024-01-05 19:15:21 +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-01-05 19:15:21 +08:00
return _chapterInfoDic.Values.ToList();
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
}
2023-10-19 15:00:19 +08:00
#endregion
}
2023-10-24 14:58:21 +08:00
}