using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; using UnityEngine.SceneManagement; [Serializable] class ConditionManager : MonoBehaviour { public static ConditionManager Instance { get; protected set; } [SerializeField] public string CurrentScene; /// /// Key: missionUniqueName || old -> map name && npc name /// [SerializeField] public List>> Conditions; public void Awake() { if (Instance == null) { CurrentScene = SceneManager.GetActiveScene().name; Instance = this; } else { Destroy(gameObject); } } public void RegisterCondition(string missionName, List conditionsToContinue) { if(HasMissionActiveCondition(missionName)) { Debug.LogError($"Mission '{missionName}' has currently active Condition"); return; } Conditions.Add( new IndexValuePair>( missionName, conditionsToContinue ) ); Debug.Log("Condition registered :D"); } /// /// Update all regfistered conditions (for all missions) which refer to passed object (item, enemy) name /// public void UpdateKillCondition(string objectName) { Debug.Log($"UpdateKillCondition - {objectName}"); Conditions .SelectMany(conditions => conditions.Value) .Where(missionCondition => missionCondition.Type == MissionTypeEnum.Kill) .ToList() .ForEach(killCondition => killCondition.RequiredElements .Where(element => element.RequiredObject.name == objectName) .ToList() .ForEach(element => ((KillRequiredSubject)element).IncreaseAmount()) ); // SaveChanges // SaveConditions(); } /// /// If mission step has no condition (nothing registered) return TRUE defaultly /// /// /// public bool CheckCondition(string missionName) { if(!HasMissionActiveCondition(missionName)) { Debug.LogError($"Data inconsistency!!! Mission dont have any registered condition to check"); return true; } int unmeetedConditions = Conditions .Where(condition => condition.Key == missionName) .FirstOrDefault() .Value .ToList() .SelectMany(missionCondition => missionCondition.RequiredElements) .ToList() .Where(el => !el.CheckCondition()) .Count() ; return !(unmeetedConditions > 0); } public void RemoveCondition(string missionName) { Conditions.RemoveAll(condition => condition.Key == missionName); } /// /// Function to check if lists contain any condition for selected mission /// If contain - reject - prev condition must be removed after his fulified! ! ! /// If dont contain - add /// /// /// /// public bool HasMissionActiveCondition(string missionName) { return Conditions.Where(conditon => conditon.Key == missionName).Any(); } }