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

279 lines
8.9 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
{
public partial class LevelManager
{
2024-01-19 19:20:05 +08:00
private const string LAST_FIGHT_LEVEL_PREFIX = "last_fight_level_";
2023-10-19 15:00:19 +08:00
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 };
2024-01-19 14:00:42 +08:00
private Dictionary<LevelChapterType, List<ChapterInfo>> _chapterTypeDic = new();
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();
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);
}
}
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
}
2024-01-19 14:00:42 +08:00
foreach (var chapterInfo in _chapterInfoDic.Values)
{
var chapterType = chapterInfo.ChapterType;
if (!_chapterTypeDic.TryGetValue(chapterType, out var list))
{
list = new();
_chapterTypeDic.Add(chapterType, list);
}
list.Add(chapterInfo);
}
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
}
}
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-01-05 19:15:21 +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-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();
}
}
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);
}
private string GetChapterTypeSaveKey(LevelChapterType chapterType) =>
LAST_FIGHT_LEVEL_PREFIX + chapterType;
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-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
}
2024-01-19 19:20:05 +08:00
//获取某种关卡类型的最新进度
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;
}
2023-10-19 15:00:19 +08:00
#endregion
}
2023-10-24 14:58:21 +08:00
}