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

338 lines
11 KiB
C#
Raw Normal View History

2023-10-19 15:00:19 +08:00
using Framework;
using System.Collections.Generic;
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>
private Dictionary<int, LevelInfo> dicLevelInfo = new();
2024-05-06 16:46:00 +08:00
/// <summary>
/// 章节信息字典
/// </summary>
private Dictionary<int, ChapterInfo> dicChapterInfo = new();
2024-05-06 16:46:00 +08:00
/// <summary>
/// 关卡组信息字典,key:关卡组idvalue:关卡组信息
/// </summary>
private Dictionary<int, LevelGroupInfo> dicLevelGroupInfo = new();
2024-09-06 18:32:17 +08:00
/// <summary>
/// 关卡组红点id
/// </summary>
private readonly int[] levelGroupRedPointIDs = new[] { 101, 102, 103, 104 };
/// <summary>
/// 章节类型字典
/// </summary>
2024-09-02 14:08:35 +08:00
private Dictionary<E_ChapterType, List<ChapterInfo>> dicChapterType = new();
2024-01-19 14:00:42 +08:00
2023-10-19 15:00:19 +08:00
private void InitData()
{
2024-09-02 14:55:43 +08:00
EventManager.Instance.Register(EventManager.EventName.AllConfigLoad, OnAllConfigLoad);
EventManager.Instance.Register<int>(EventManager.EventName.NewLevelServerData, OnNewLevelServerData);
2023-10-19 15:00:19 +08:00
}
private void ReleaseData()
{
2024-09-02 14:55:43 +08:00
EventManager.Instance.Unregister<int>(EventManager.EventName.NewLevelServerData, OnNewLevelServerData);
EventManager.Instance.Unregister(EventManager.EventName.AllConfigLoad, OnAllConfigLoad);
DisposeRedPointTree();
dicLevelInfo = null;
dicChapterInfo = null;
dicLevelGroupInfo = null;
2023-10-19 15:00:19 +08:00
}
2024-09-05 13:41:39 +08:00
#region API
/// <summary>
2024-09-05 13:41:39 +08:00
/// 刷新所有关卡信息
/// </summary>
public void RefreshAllLevelInfo()
{
foreach (LevelInfo levelInfo in dicLevelInfo.Values)
{
levelInfo?.RefreshServerData(true);
}
}
2023-10-24 14:58:21 +08:00
2024-09-05 13:41:39 +08:00
/// <summary>
/// 通关关卡id获取关卡信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool TryGetLevelInfoByID(int id, out LevelInfo levelInfo)
2023-10-19 15:00:19 +08:00
{
2024-09-05 13:41:39 +08:00
if (!dicLevelInfo.TryGetValue(id, out levelInfo))
2023-10-19 15:00:19 +08:00
{
DebugUtil.LogError($"找不到levelInfo,id:{id}");
2024-09-05 13:41:39 +08:00
return false;
2023-10-19 15:00:19 +08:00
}
2023-10-24 14:58:21 +08:00
2024-09-05 13:41:39 +08:00
return true;
2023-10-19 15:00:19 +08:00
}
2023-10-24 14:58:21 +08:00
2024-09-05 13:52:02 +08:00
/// <summary>
/// 通过关卡组id获取关卡组信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool TryGetLevelGroupInfoByID(int id, out LevelGroupInfo levelGroupInfo)
2023-10-24 14:58:21 +08:00
{
2024-09-05 13:52:02 +08:00
if (!dicLevelGroupInfo.TryGetValue(id, out levelGroupInfo))
2023-10-24 14:58:21 +08:00
{
DebugUtil.LogError($"找不到chapterInfo,id:{id}");
2024-09-05 13:52:02 +08:00
return false;
2023-10-24 14:58:21 +08:00
}
2024-09-05 13:52:02 +08:00
return true;
2023-10-24 14:58:21 +08:00
}
2024-09-05 13:52:02 +08:00
/// <summary>
/// 通过章节id获取章节信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool TryGetChapterInfoByID(int id, out ChapterInfo chapterInfo)
2023-10-24 14:58:21 +08:00
{
2024-09-05 13:52:02 +08:00
if (!dicChapterInfo.TryGetValue(id, out chapterInfo))
2023-10-24 14:58:21 +08:00
{
DebugUtil.LogError($"找不到levelGroupInfo,id:{id}");
2024-09-05 13:52:02 +08:00
return false;
2023-10-24 14:58:21 +08:00
}
2024-09-05 13:52:02 +08:00
return true;
2023-10-26 20:20:11 +08:00
}
2024-04-07 16:22:58 +08:00
/// <summary>
/// 根据章节类型获取章节信息
/// </summary>
/// <param name="levelChapterType"></param>
/// <returns></returns>
2024-09-03 19:22:47 +08:00
public bool TryGetChapterInfosByType(E_ChapterType type, out List<ChapterInfo> listChapterInfo)
2024-04-07 16:22:58 +08:00
{
2024-09-03 19:22:47 +08:00
if (dicChapterType.TryGetValue(type, out listChapterInfo) && null != listChapterInfo)
return true;
2024-04-07 16:22:58 +08:00
2024-09-03 19:22:47 +08:00
return false;
2024-04-07 16:22:58 +08:00
}
2024-09-05 13:52:02 +08:00
/// <summary>
/// 获取某种关卡类型上一次战斗的关卡
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
2024-09-02 14:08:35 +08:00
public LevelInfo GetLastFightLevelInfo(E_ChapterType type)
2024-01-19 19:20:05 +08:00
{
2024-09-05 13:52:02 +08:00
string key = GetChapterTypeSaveKey(type);
2024-01-19 19:20:05 +08:00
if (!PlayerPrefs.HasKey(key))
return null;
2024-09-05 13:52:02 +08:00
int levelID = PlayerPrefs.GetInt(key);
2024-09-05 13:41:39 +08:00
TryGetLevelInfoByID(levelID, out LevelInfo levelInfo);
2024-01-19 19:20:05 +08:00
return levelInfo;
}
/// <summary>
2024-09-05 13:52:02 +08:00
/// 通过id判断关卡是否解锁
/// </summary>
/// <param name="levelID"></param>
/// <returns></returns>
public bool CheckLevelUnlock(int levelID)
{
2024-09-05 13:41:39 +08:00
if(!TryGetLevelInfoByID(levelID, out LevelInfo levelInfo))
{
DebugUtil.LogError($"cant find levelInfo,id:{levelID}");
return false;
}
2024-09-05 13:41:39 +08:00
return !levelInfo.IsLock;
}
2024-09-05 13:52:02 +08:00
/// <summary>
/// 通过关卡id转数字
/// </summary>
/// <param name="levelID"></param>
/// <returns></returns>
public string LevelIDToNum(int levelID)
{
2024-09-05 13:41:39 +08:00
if(!TryGetLevelInfoByID(levelID, out LevelInfo levelInfo))
{
DebugUtil.LogError($"cant find levelInfo,id:{levelID}");
return string.Empty;
}
2024-09-05 13:41:39 +08:00
return levelInfo.ChapterIndex + "-" + levelInfo.LevelIndex;
}
2024-09-05 13:52:02 +08:00
/// <summary>
/// 通过关卡id转名字
/// </summary>
/// <param name="levelID"></param>
/// <returns></returns>
public string LevelIDToName(int levelID)
{
2024-09-05 13:41:39 +08:00
if(!TryGetLevelInfoByID(levelID, out LevelInfo levelInfo))
{
DebugUtil.LogError($"cant find levelInfo,id:{levelID}");
return string.Empty;
}
2024-09-05 13:41:39 +08:00
return levelInfo.Name;
}
2024-09-05 13:41:39 +08:00
#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));
}
}
2024-09-05 13:41:39 +08:00
/// <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;
2024-09-06 18:32:17 +08:00
if (index < 0 || index > levelGroupRedPointIDs.Length)
2024-09-05 13:41:39 +08:00
{
DebugUtil.LogError($"index is out of range!index:{index}");
continue;
}
2024-09-06 18:32:17 +08:00
int redPointParentID = levelGroupRedPointIDs[index];
2024-09-06 19:03:33 +08:00
chapterInfo.ConstructRedPointTree(redPointParentID);
2024-09-05 13:41:39 +08:00
}
}
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;
2023-10-19 15:00:19 +08:00
#endregion
}
2023-10-24 14:58:21 +08:00
}