using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEngine.SceneManagement; using System.IO; using System.Linq; public class SceneTaskManager : MonoBehaviour { private const string DYNAMIC_ELEMENT = "/DynamicElements/"; private const string STATIC_ELEMENT = "/StaticElements/"; [SerializeField] public List TaskElements; public bool isNewGame = true; public bool isContinued = false; public string MapName; public string ElementFolderName = "Quest"; public string ItemsListName = "QuestsList"; public static SceneTaskManager Instance; public void Awake() { if(Instance == null) { this.MapName = SceneManager.GetActiveScene().name; Instance = this; }else if (Instance != this) { Destroy(gameObject); } } public void Start() { LoadQuests(); // load user quests BuildQuests(TaskElements); // pass them to TaskManager } /// /// Pass quest to taskManager after load data /// public void BuildQuests(List questList) { // pass to taskManager singleton foreach(Task quest in questList) TaskManager.Instance.AddTask(quest); } public int AddQuest(GameObject dynamicObject) { Task quest = dynamicObject.GetComponent().task; this.TaskElements.Add(quest); return this.TaskElements.Count - 1; } public void RemoveQuest(string _title) { // 1. Fetch all matched quests - we search by title which shoudl be unique but who know :D List questList = this.TaskElements.Where(quest => quest.title == _title).ToList(); // 2. Remove them this.TaskElements.RemoveAll(quest => quest.title == _title); } /// /// Use TaskManaager list to save user quests - TaskManager.Instance.taskList /// public void SaveQuests() { // Case I - if we remember all list // 1) if after removed item form DynamicList is empty - remove all file // 2) if after removed item form DynamciList there are another one - save updated list again if(TaskManager.Instance.taskList.Count > 0) { SaveQuestSystem.SaveQuestsList(TaskManager.Instance.taskList, this.ItemsListName); } else { string _path = SaveSystem.GetSavePath() + "/" + this.ItemsListName + ".fun"; try { Debug.Log("File to remove: " + _path); if(File.Exists(_path)) { File.Delete(_path); } } catch (IOException ioExp) { Debug.LogError(ioExp.Message); } } // Case II - if we rememenber object per file // 1) remove specyfic file // // Unfortunatelly we don't use this way of saving items yet :D } /// /// Load quests to local list /// Pass this to taskManager in outside method /// public void LoadQuests() { string path = SaveSystem.GetSavePath(); if (!Directory.Exists(path)) // if not exists thats mean there was nothing saved yet - nothing to load return; FileInfo[] fileInfo = new DirectoryInfo(path).GetFiles(); foreach(FileInfo file in fileInfo) { if(file.Name != this.ItemsListName + ".fun") continue; List taskList = SaveQuestSystem.LoadQuestsList(this.ItemsListName); foreach(Task task in taskList) TaskElements.Add(task); } } }