152 lines
4.7 KiB
C#
152 lines
4.7 KiB
C#
using Framework;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Gameplay.Level
|
|
{
|
|
public partial class LevelManager
|
|
{
|
|
private Dictionary<int, LevelInfo> _levelDic = new();
|
|
|
|
private Dictionary<int, LevelGroupInfo> _levelGroupDic = new();
|
|
|
|
private Dictionary<int, ChapterInfo> _chapterInfoDic = new();
|
|
|
|
private void InitData()
|
|
{
|
|
RegisterEventData();
|
|
}
|
|
|
|
private void ReleaseData()
|
|
{
|
|
UnregisterEventData();
|
|
_levelDic = null;
|
|
_levelGroupDic = null;
|
|
_chapterInfoDic = null;
|
|
}
|
|
|
|
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()
|
|
{
|
|
LoadAllLevel();
|
|
LoadAllChapter();
|
|
LoadAllLevelGroup();
|
|
}
|
|
|
|
private void LoadAllLevel()
|
|
{
|
|
_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);
|
|
}
|
|
}
|
|
|
|
private void LoadAllChapter()
|
|
{
|
|
_chapterInfoDic.Clear();
|
|
foreach (var levelInfo in _levelDic.Values)
|
|
{
|
|
var chapterID = levelInfo.ChapterID;
|
|
ChapterInfo chapterInfo = null;
|
|
if (!_chapterInfoDic.TryGetValue(chapterID, out chapterInfo))
|
|
{
|
|
var config = TableManager.Instance.Tables.Chapter.Get(chapterID);
|
|
if (config != null)
|
|
{
|
|
chapterInfo = new ChapterInfo(config);
|
|
_chapterInfoDic.Add(chapterID, chapterInfo);
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError($"cant find chapter config,{chapterID}");
|
|
}
|
|
}
|
|
|
|
if (chapterInfo != null)
|
|
chapterInfo.AddLevel(levelInfo);
|
|
}
|
|
}
|
|
|
|
private void LoadAllLevelGroup()
|
|
{
|
|
_levelGroupDic.Clear();
|
|
foreach (var chapterInfo in _chapterInfoDic.Values)
|
|
{
|
|
var levelGroupID = chapterInfo.GroupID;
|
|
LevelGroupInfo levelGroupInfo = null;
|
|
if (!_levelGroupDic.TryGetValue(levelGroupID, out levelGroupInfo))
|
|
{
|
|
var config = TableManager.Instance.Tables.LevelGroupConfig.Get(levelGroupID);
|
|
if (config != null)
|
|
{
|
|
levelGroupInfo = new LevelGroupInfo(config);
|
|
_levelGroupDic.Add(levelGroupID, levelGroupInfo);
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError($"cant find LevelGroup config,{levelGroupID}");
|
|
}
|
|
}
|
|
|
|
if (levelGroupInfo != null)
|
|
levelGroupInfo.AddChapter(chapterInfo);
|
|
}
|
|
}
|
|
|
|
private void OnNewLevelServerData(int levelID)
|
|
{
|
|
var levelInfo = GetLevelInfoByID(levelID);
|
|
if (levelInfo != null)
|
|
{
|
|
levelInfo.RefreshServerData();
|
|
}
|
|
}
|
|
|
|
#region API
|
|
|
|
public LevelInfo GetLevelInfoByID(int id)
|
|
{
|
|
if (!_levelDic.TryGetValue(id, out var levelInfo))
|
|
{
|
|
DebugUtil.LogError($"找不到levelInfo,id:{id}");
|
|
}
|
|
|
|
return levelInfo;
|
|
}
|
|
|
|
public ChapterInfo GetChapterInfoByID(int id)
|
|
{
|
|
if (!_chapterInfoDic.TryGetValue(id, out var chapterInfo))
|
|
{
|
|
DebugUtil.LogError($"找不到chapterInfo,id:{id}");
|
|
}
|
|
|
|
return chapterInfo;
|
|
}
|
|
|
|
public LevelGroupInfo GetLevelGroupInfoByID(int id)
|
|
{
|
|
if (!_levelGroupDic.TryGetValue(id, out var levelGroupInfo))
|
|
{
|
|
DebugUtil.LogError($"找不到levelGroupInfo,id:{id}");
|
|
}
|
|
|
|
return levelGroupInfo;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |