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

224 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Framework;
using System.Collections.Generic;
using PhxhSDK.Phxh;
using static Gameplay.Level.LevelData;
using Constants = Framework.Constants;
using cfg.LevelCfg;
using Cysharp.Threading.Tasks;
using PhxhSDK;
using UnityEngine;
namespace Gameplay.Level
{
public static class LevelUtils
{
private const string SCENE_ORIGIN_PATH = "Assets/Scenes/Maps";
private const string SCENE_POSTPROCESS_PATH = "Assets/Art_Out/AutoGen/Maps";
private const string SCENE_POST_PROCESS_NAME = "PostProcess";
// public static bool CheckEnterLevel(int levelId)
// {
// var configFilePath = GetLevelFilePath(levelId);
// if (AssetManager.Instance.CanLocateAsset(configFilePath))
// {
// return true;
// }
//
// else
// {
// return false;
// }
// }
public static string GetLevelFilePath(int id)
{
var cfg = TableManager.Instance.Tables.Level.DataMap.GetValueOrDefault(id);
if (cfg == null)
{
DebugUtil.LogError($"找不到level配置id:{id}");
return null;
}
return string.Format(Constants.LEVEL_CONFIG_FORMAT_PATH, cfg.LevelData);
}
public async static UniTask<LevelData> LoadLevelData(int id)
{
string levelFilePath = GetLevelFilePath(id);
if (string.IsNullOrEmpty(levelFilePath))
return null;
TextAsset textAsset = await AssetManager.Instance.LoadAssetAsync<TextAsset>(levelFilePath);
LevelData levelData = null;
if (null != textAsset)
levelData = JsonUtility.FromJson<LevelData>(textAsset.text);
if (null == levelData)
DebugUtil.LogError("加载关卡配置失败,路径: {0}", levelFilePath);
AssetManager.Instance.Unload(levelFilePath);
return levelData;
}
public static float SetCharaterToward(CharacterToward npcToward)
{
switch (npcToward)
{
case CharacterToward.TopLeft:
{
return 0f;
}
case CharacterToward.TopRight:
{
return 60f;
}
case CharacterToward.Right:
{
return 120f;
}
case CharacterToward.BottomRight:
{
return 180f;
}
case CharacterToward.BottomLeft:
{
return -120f;
}
default:
{
return -60f;
}
}
}
// public static string GetPostProcessLevelScenePath(string scenePath)
// {
// var oldSceneDir = scenePath;
// var oldSceneName = Path.GetFileNameWithoutExtension(scenePath);
// var tempSceneDir = oldSceneDir.Replace( SCENE_ORIGIN_PATH, SCENE_POSTPROCESS_PATH);
// var subStr = tempSceneDir.Substring(SCENE_POSTPROCESS_PATH.Length + 1);
// var folderName = Path.GetFileNameWithoutExtension(subStr.Replace("/", "_"));
// var result = $"{SCENE_POSTPROCESS_PATH}/{folderName}/{oldSceneName}_{SCENE_POST_PROCESS_NAME}.unity";
// return result;
// }
/// <summary>
/// 获取战役关卡评级
/// </summary>
/// <returns></returns>
public static E_WarRating GetWarLevelRatingByLevelId(int levelId)
{
if (!LevelManager.Instance.TryGetLevelInfoByID(levelId, out LevelInfo levelInfo))
{
DebugUtil.LogError($"获取关卡信息错误,关卡id{levelId}");
return E_WarRating.C;
}
return GetWarLevelRatingByLevelInfo(levelInfo);
}
/// <summary>
/// 获取战役关卡评级
/// </summary>
/// <returns></returns>
public static E_WarRating GetWarLevelRatingByLevelInfo(LevelInfo levelInfo)
{
if (null == levelInfo)
{
DebugUtil.LogError($"获取评级错误,空数据");
return E_WarRating.C;
}
if (E_ChapterType.War != levelInfo.ChapterInfo.Cfg.Type)
{
DebugUtil.LogError($"获取评级错误,不是战役关卡,关卡id{levelInfo.Cfg.Id}");
return E_WarRating.C;
}
return GetRatingByScore(levelInfo.Score, levelInfo.ScoreMax);
}
/// <summary>
/// 获取战役关卡组评级
/// </summary>
/// <returns></returns>
public static E_WarRating GetWarLevelRatingByLeveGrouplId(int levelGroupId)
{
if (!LevelManager.Instance.TryGetLevelGroupInfoByID(levelGroupId, out LevelGroupInfo levelGroupInfo))
{
DebugUtil.LogError($"获取关卡组信息错误,关卡id{levelGroupId}");
return E_WarRating.C;
}
return GetWarLevelRatingByLevelGroupInfo(levelGroupInfo);
}
/// <summary>
/// 获取战役关卡组评级
/// </summary>
/// <returns></returns>
public static E_WarRating GetWarLevelRatingByLevelGroupInfo(LevelGroupInfo levelGroupInfo)
{
if (null == levelGroupInfo)
{
DebugUtil.LogError($"获取评级错误,空数据");
return E_WarRating.C;
}
if (E_ChapterType.War != levelGroupInfo.ChapterInfo.Cfg.Type)
{
DebugUtil.LogError($"获取评级错误,不是战役关卡组,关卡组id{levelGroupInfo.Cfg.Id}");
return E_WarRating.C;
}
return GetRatingByScore(levelGroupInfo.Score, levelGroupInfo.ScoreMax);
}
/// <summary>
/// 获取评级根据分数
/// </summary>
/// <param name="score"></param>
/// <param name="maxScore"></param>
/// <returns></returns>
public static E_WarRating GetRatingByScore(int score, int maxScore)
{
float percentage = 0 >= maxScore ? 0f : (float)score / maxScore;
List<float> listRating = TableManager.Instance.Tables.GlobalConfig.CampaignScoreClass;
for (int i = listRating.Count - 1; i >= 0; --i)
{
if (percentage < listRating[i])
continue;
return (E_WarRating)i;
}
return E_WarRating.C;
}
/// <summary>
/// 是否战役关卡
/// </summary>
/// <param name="levelInfo"></param>
/// <returns></returns>
public static bool IsWarLevel(LevelInfo levelInfo)
{
return E_ChapterType.War == levelInfo.ChapterInfo.Cfg.Type;
}
/// <summary>
/// 是否战役关卡
/// </summary>
/// <param name="levelInfo"></param>
/// <returns></returns>
public static bool IsWarLevel(int levelId)
{
if (!LevelManager.Instance.TryGetLevelInfoByID(levelId, out LevelInfo levelInfo))
return false;
return IsWarLevel(levelInfo);
}
}
}