IndieGame/client/Assets/Scripts/System/Task/TaskSystem.cs

192 lines
5.1 KiB
C#
Raw Permalink Normal View History

2024-10-11 10:12:15 +08:00

using System;
using System.Collections;
using System.Collections.Generic;
using TableConfig;
using UnityEngine;
using UnityEngine.Tilemaps;
namespace Ether
{
public class TaskSystem : SaveBase<TaskSystem>
{
private MultiList<int, TaskTable> allTaskMultiList = new MultiList<int, TaskTable>();
[Save]
public Dictionary<int, TaskState> taskStateDic = new Dictionary<int, TaskState>();
[Save]
public int CurMainTaskId = 0;
public void Init()
{
//TODO:从存档中读取当前任务数据
allTaskMultiList.Clear();
List<TaskTable> taskTables = TableProvider.Tables.TbTask.DataList;
List<int> parentTaskIdList = new List<int>();
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;
}
/// <summary>
/// 接任务
/// </summary>
/// <param name="taskId"></param>
/// <returns></returns>
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<int>();
//}
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;
}
/// <summary>
/// 完成任务
/// </summary>
/// <param name="taskId"></param>
/// <returns></returns>
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<int>();
//}
if (succIndex == taskData.SuccessCondition.Count)
{
if (taskStateDic.ContainsKey(taskId))
{
taskStateDic[taskId] = TaskState.Success;
}
else
{
taskStateDic.Add(taskId, TaskState.Success);
}
return true;
}
return false;
}
}
/// <summary>
/// 任务状态
/// </summary>
public enum TaskState
{
/// <summary>
/// 未接取
/// </summary>
None = 0,
/// <summary>
/// 已接取进行中
/// </summary>
Received = 1,
/// <summary>
/// 已完成
/// </summary>
Success = 2,
}
}