using System; using System.Collections; using System.Collections.Generic; using TableConfig; using UnityEngine; using UnityEngine.Tilemaps; namespace Ether { public class TaskSystem : SaveBase { private MultiList allTaskMultiList = new MultiList(); [Save] public Dictionary taskStateDic = new Dictionary(); [Save] public int CurMainTaskId = 0; public void Init() { //TODO:从存档中读取当前任务数据 allTaskMultiList.Clear(); List taskTables = TableProvider.Tables.TbTask.DataList; List parentTaskIdList = new List(); foreach (var taskTable in taskTables) { if (taskTable.PreTask == -1 && taskTable.ReceiveCondition == null) { allTaskMultiList.AddNode(taskTable.Id, taskTable); continue; } parentTaskIdList.Clear(); if (taskTable.PreTask != -1) { parentTaskIdList.Add(taskTable.PreTask); } if (taskTable.ReceiveCondition != null) { foreach (var conditionId in taskTable.ReceiveCondition) { ConditionTable conditionTable = TableProvider.Tables.TbCondition[conditionId]; if (conditionTable == null) { continue; } if (conditionTable.Type == ConditionType.ReceiveTask || conditionTable.Type == ConditionType.SuccessTask) { parentTaskIdList.Add(conditionTable.TaskId); } } } allTaskMultiList.AddNode(taskTable.Id, taskTable, parentTaskIdList.ToArray()); } } public TaskTable GetTaskData(int taskId) { return allTaskMultiList[taskId]?.Data; } public TaskState GetTaskState(int taskId) { if (taskStateDic.ContainsKey(taskId)) { return taskStateDic[taskId]; } return TaskState.None; } /// /// 接任务 /// /// /// public bool ReceiveTask(int taskId) { var taskData = GetTaskData(taskId); if (taskData == null) { return false; } int receiveSuccIndex = 0; //if (taskData.ReceiveCondition == null) //{ // taskData.ReceiveCondition = new List(); //} foreach (var conditionId in taskData.ReceiveCondition) { bool isSuccess = ConditionSystem.Inst.GetConditionState(conditionId); if (isSuccess) { receiveSuccIndex++; } } if (receiveSuccIndex == taskData.ReceiveCondition.Count) { if (taskStateDic.ContainsKey(taskId)) { taskStateDic[taskId] = TaskState.Received; } else { taskStateDic.Add(taskId, TaskState.Received); } return true; } return false; } /// /// 完成任务 /// /// /// public bool SuccessTask(int taskId) { var taskData = GetTaskData(taskId); if (taskData == null) { return false; } int succIndex = 0; foreach (var conditionId in taskData.SuccessCondition) { bool isSuccess = ConditionSystem.Inst.GetConditionState(conditionId); if (isSuccess) { succIndex++; } } //if (taskData.SuccessCondition == null) //{ // taskData.SuccessCondition = new List(); //} if (succIndex == taskData.SuccessCondition.Count) { if (taskStateDic.ContainsKey(taskId)) { taskStateDic[taskId] = TaskState.Success; } else { taskStateDic.Add(taskId, TaskState.Success); } return true; } return false; } } /// /// 任务状态 /// public enum TaskState { /// /// 未接取 /// None = 0, /// /// 已接取进行中 /// Received = 1, /// /// 已完成 /// Success = 2, } }