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 Debug = DebugUtil;
using System;
using cfg.LevelCfg;
using static MapData;
///
/// 关卡信息
///
public class LevelInfo : IRedPointTreeNode
{
#region 私有字段
///
/// 关卡配置信息
///
private readonly DataLevel cfg;
///
/// 关卡数据
///
private LevelData levelData;
///
/// 地图数据
///
private MapData mapData;
///
/// 服务器关卡数据
///
private Gameplay.Net.LevelInfo serverData;
///
/// 红点
///
private RedPointNode redPointNode;
///
/// 章节索引
///
private int _chapterIndex = -1;
///
/// 关卡索引
///
private int _levelIndex = -1;
///
/// 解锁条件id
///
private int unLockConditionId;
#endregion
#region config
///
/// 配置数据
///
public DataLevel Cfg
{
get { return cfg; }
}
///
/// 所在关卡组信息
///
public LevelGroupInfo GroupInfo
{
get { return LevelManager.Instance.GetLevelGroupInfoByID(cfg.LevelGroupID); }
}
public ChapterInfo ChapterInfo => GroupInfo?.ChapterInfo;
//解锁条件
public int[] UnLockCondition => cfg.Unlock;
//消耗
public List CostItems => cfg.CostItems;
public int[] NomralRewardID => cfg.NormalRewardID;
public int[] FirstRewardID => cfg.FristRewardID;
//一星奖励
public int[] StarRewardID_1 => cfg.StarRewardID1;
//二星奖励
public int[] StarRewardID_2 => cfg.StarRewardID2;
//三星奖励
public int[] StarRewardID_3 => cfg.StarRewardID3;
public int SettleExp => cfg.SettleExp;
public int SettleLikability => cfg.SettleLikability;
public string LevelDataPath => cfg.LevelData;
public string Name => StringManager.Instance.GetLocalizeTextByKey(cfg.Name);
public string Desc => StringManager.Instance.GetLocalizeTextByKey(cfg.Desc);
public string LevelScene => cfg.LevelScene;
public string MapDataPath => cfg.MapData;
public float ShadowDistance => cfg.ShadowDistance;
public string StoryID => cfg.StoryID;
//章节序号
public int ChapterIndex
{
get
{
if (_chapterIndex == -1)
{
if (cfg.Id < 100000)
{
DebugUtil.LogError("关卡id为{0}的关卡id没到6位", cfg.Id);
return 0;
}
var resultStr = cfg.Id.ToString().Substring(1, 2);
_chapterIndex = int.Parse(resultStr);
}
return _chapterIndex;
}
}
//关卡序号
public int LevelIndex
{
get
{
if (_levelIndex == -1)
{
if (cfg.Id < 100000)
{
DebugUtil.LogError("关卡id为{0}的关卡id没到6位", cfg.Id);
return 0;
}
var resultStr = cfg.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 cfg.LevelWeather;
}
}
public DayNightType DayNightType
{
get
{
if (LevelData == null)
return DayNightType.Day;
return cfg.LevelTime;
}
}
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(cfg.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)
{
cfg = config;
RefreshServerData(true);
}
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:{cfg.Id}");
break;
}
}
return result;
}
public void RefreshServerData(bool isReset)
{
serverData = Actor.Instance.Level.GetLevelInfo((uint)cfg.Id);
if (isReset)
{
IsLock = true;
AddLockConditionReaction();
}
}
public void ConstructRedPointTree(RedPointNode parent)
{
redPointNode = RedPointManager.Instance.AddDynamicNode(NodeType.Level, parent, cfg.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.RemoveReaction(unLockConditionId);
unLockConditionId = ConditionReactor.Instance.AddReaction(UnLockCondition, OnUnlockConditionChanged);
}
private void OnUnlockConditionChanged(bool result, object callBackParam, int reactionID)
{
if (result)
{
IsLock = false;
DebugUtil.Log($"[Level] level unlock,id:{cfg.Id}");
EventManager.Instance.Send(EventManager.EventName.LevelUnlock, cfg.Id);
ConditionReactor.Instance.RemoveReaction(reactionID);
}
}
public void ConstructRedPointTree(int parentID)
{
throw new NotImplementedException();
}
}