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

260 lines
6.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 cfg.LevelCfg;
using Gameplay;
using Gameplay.Level;
using Gameplay.Net;
using System.Collections.Generic;
using System.Linq;
using cfg.RedPoint;
using Cysharp.Threading.Tasks;
using Framework;
using Framework.Condition;
using UnityEngine;
using static Gameplay.Level.LevelData;
using static MapData;
using Debug = DebugUtil;
using System;
public class LevelInfo : IRedPointTreeNode
{
private DataLevel _config;
private LevelData _levelData;
private MapData _mapData;
private Gameplay.Net.LevelInfo _serverData;
private RedPointNode _redPointNode;
private int _chapterIndex = -1;
private int _levelIndex = -1;
#region config
public int ID => _config.Id;
public int LevelGroupID => _config.LevelGroupID;
public LevelGroupInfo LevelGroupInfo => LevelManager.Instance.GetLevelGroupInfoByID(LevelGroupID);
public ChapterInfo ChapterInfo => LevelGroupInfo?.ChapterInfo;
//解锁条件
public int[] UnLockCondition => _config.Unlock;
//消耗
public List<cfg.item.ItemCounts> CostItems => _config.CostItems;
public int[] NomralRewardID => _config.NormalRewardID;
public int[] FirstRewardID => _config.FristRewardID;
//一星奖励
public int[] StarRewardID_1 => _config.StarRewardID1;
//二星奖励
public int[] StarRewardID_2 => _config.StarRewardID2;
//三星奖励
public int[] StarRewardID_3 => _config.StarRewardID3;
public int SettleExp => _config.SettleExp;
public int SettleLikability => _config.SettleLikability;
public string LevelDataPath => _config.LevelData;
public string Name => StringManager.Instance.GetLocalizeTextByKey(_config.Name);
public string Desc => StringManager.Instance.GetLocalizeTextByKey(_config.Desc);
public string LevelScene => _config.LevelScene;
public string MapDataPath => _config.MapData;
public float ShadowDistance => _config.ShadowDistance;
public string StoryID => _config.StoryID;
//章节序号
public int ChapterIndex
{
get
{
if (_chapterIndex == -1)
{
if (ID < 100000)
{
DebugUtil.LogError("关卡id为{0}的关卡id没到6位", ID);
return 0;
}
var resultStr = ID.ToString().Substring(1, 2);
_chapterIndex = int.Parse(resultStr);
}
return _chapterIndex;
}
}
//关卡序号
public int LevelIndex
{
get
{
if (_levelIndex == -1)
{
if (ID < 100000)
{
DebugUtil.LogError("关卡id为{0}的关卡id没到6位", ID);
return 0;
}
var resultStr = ID.ToString().Substring(3, 3);
_levelIndex = int.Parse(resultStr);
}
return _levelIndex;
}
}
#endregion
#region Data
public WeatherType WeatherType
{
get
{
if (LevelData == null)
return WeatherType.Fog;
return LevelData.weatherType;
}
}
public DayNightType DayNightType
{
get
{
if (LevelData == null)
return DayNightType.Day;
return LevelData.dayNightType;
}
}
public EnvironmentType EnvironmentType
{
get
{
if (MapData == null)
return EnvironmentType.Desert;
return _mapData.environmentType;
}
}
public bool IsLock { get; set; } = true;
public bool CostEnough => IsConstEnough();
public bool CanEnter => !IsLock && CostEnough;
public MapData MapData => _mapData ??= MapUtils.LoadMapData(MapDataPath);
public LevelData LevelData => _levelData ??= LevelUtils.LoadLevelData(ID);
public bool IsStory => !string.IsNullOrWhiteSpace(StoryID);
//返回环境buff
public string EnvironmentBuffString => CommonUtils.GetLocalizeText(TableManager.Instance.Tables.EnviormentConfig.Get((int)EnvironmentType).DescLocal);
#endregion
#region Server Data
public int StarCount => (int)(_serverData == null ? 0 : _serverData.StarCount);
public bool[] StarResult => _serverData == null ? new bool[3] : _serverData.star.Select(s => s > 0).ToArray();
public bool HasPass => _serverData == null ? false : _serverData.isPassed;
public RedPointNode RedPointNode => _redPointNode;
#endregion
public LevelInfo(DataLevel config)
{
_config = config;
Load();
AddLockConditionReaction();
}
private void Load()
{
RefreshServerData();
}
private bool IsConstEnough()
{
bool result = true;
if (CostItems == null || CostItems.Count == 0)
return result;
foreach (var costItem in CostItems)
{
var currCount = Actor.Instance.Item.GetItemCount(costItem.Id);
result = result && currCount >= costItem.Count;
if (!result)
{
Debug.LogError($"关卡消耗品不足level id:{ID}");
break;
}
}
return result;
}
public void RefreshServerData()
{
_serverData = Actor.Instance.Level.GetLevelInfo((uint)ID);
}
public void ConstructRedPointTree(RedPointNode parent)
{
_redPointNode = RedPointManager.Instance.AddDynamicNode(NodeType.Level, parent, ID);
}
//获得星级目标的文本
public string GetStarTargetLocalizationText(int targetIndex)
{
var starTarget = LevelData.starTarget;
if (targetIndex < 0 || targetIndex >= starTarget.Count)
return null;
return starTarget[targetIndex].GetConditionLocalization();
}
private void AddLockConditionReaction()
{
ConditionReactor.Instance.AddReaction(UnLockCondition, OnUnlockConditionChanged);
}
private void OnUnlockConditionChanged(bool result, object callBackParam, int reactionID)
{
if (result)
{
IsLock = false;
DebugUtil.Log($"[Level] level unlock,id:{ID}");
EventManager.Instance.Send<int>(EventManager.EventName.LevelUnlock, ID);
ConditionReactor.Instance.RemoveReaction(reactionID);
}
}
public void ConstructRedPointTree(int parentID)
{
throw new NotImplementedException();
}
}