Scriptum/Assets/Scripts/SceneManager/SceneTaskManager.cs

136 lines
4.0 KiB
C#
Raw Normal View History

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<Task> 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
}
///<summary>
/// Pass quest to taskManager after load data
///</summary>
public void BuildQuests(List<Task> questList)
{
// pass to taskManager singleton
foreach(Task quest in questList)
TaskManager.Instance.AddTask(quest);
}
public int AddQuest(GameObject dynamicObject)
{
Task quest = dynamicObject.GetComponent<TaskBox>().task;
this.TaskElements.Add(quest);
return this.TaskElements.Count - 1;
}
// NIE UŻYWANA !!!
// TODO - usuwane mają być elementy z listy tasków z Taskmanagera - pozbyć sie tej listy najepiej tu
// TA LISTA SŁUŻY TYLKO DO ODCZYTU Z PLIKÓW I PRZEKAZANIU WARTOŚCI DO MANAGERA - NIR DO KASOWANIA
public void RemoveQuest(string _title)
{
// 1. Fetch all matched quests - we search by title which shoudl be unique but who know :D
List<Task> questList = this.TaskElements.Where(quest => quest.title == _title).ToList();
// 2. Remove them
this.TaskElements.RemoveAll(quest => quest.title == _title);
}
///<summary>
/// Use TaskManaager list to save user quests - TaskManager.Instance.taskList
///</summary>
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
}
///<summary>
/// Load quests to local list
/// Pass this to taskManager in outside method
///</summary>
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<Task> taskList = SaveQuestSystem.LoadQuestsList(this.ItemsListName);
foreach(Task task in taskList)
TaskElements.Add(task);
}
}
}