using cfg.LevelCfg; using Gameplay; using Gameplay.Level; using Gameplay.Net; using System.Collections.Generic; using cfg.RedPoint; using Framework; using Framework.Condition; using UnityEngine; using static Gameplay.Level.LevelData; using static MapData; using Debug = DebugUtil; public class LevelInfo { private DataLevel _config; private LevelData _levelData; private MapData _mapData; private Gameplay.Net.LevelInfo _serverData; private RedPointNode _redPointNode; #region config public int ID => _config.Id; public int Index => _config.Index; public int ChapterID => _config.ChapterID; //解锁条件 public int[] UnLockCondition => _config.Unlock; //消耗 public List 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 int StoryID => _config.StoryID; #endregion #region Data public WeatherType WeatherType => _levelData?.weatherType?? WeatherType.Fog; public DayNightType DayNightType => _levelData?.dayNightType?? DayNightType.Day; public EnvironmentType EnvironmentType => _mapData?.environmentType?? EnvironmentType.Desert; public bool IsLock { get; private set; } = true; public bool CostEnough => IsConstEnough(); public bool CanEnter => !IsLock && CostEnough; public MapData MapData => _mapData; public LevelData LevelData => _levelData; public bool IsStory => StoryID > 0; #endregion #region Server Data public int StarCount => (int)(_serverData == null ? 0 : _serverData.star); public bool HasPass => _serverData == null ? false : _serverData.isPassed; public RedPointNode RedPointNode => _redPointNode; #endregion public LevelInfo(DataLevel config) { _config = config; Load(); AddLockConditionReaction(); } private async void Load() { RefreshServerData(); _levelData = await LevelUtils.LoadLevelData(ID); _mapData = await MapUtils.LoadMapData(MapDataPath); } 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); } 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(EventManager.EventName.LevelUnlock, ID); ConditionReactor.Instance.RemoveReaction(reactionID); } } }