_xiaofang/xiaofang/Assets/Prefabs/HYLPrefabs/TaskItem.cs

78 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public enum TaskState
{
NotStarted, // 未开始
InProgress, // 进行中
Completed, // 已完成
Failed // 失败
}
public class TaskItem : MonoBehaviour
{
public int taskId; // 任务ID
public string taskName; // 任务名称
public TaskState state; // 任务状态
public Text taskTxt; // 任务描述文本
public List<TaskTarget> Targets = new List<TaskTarget>(); // 任务目标1列表
private JSONReader jsonReader; // JSON 数据读取器
public void SetInfo(int id, JSONReader reader)
{
taskId = id;
jsonReader = reader;
LoadTaskData();
}
private void LoadTaskData()
{
Task_ info = jsonReader.GetTaskByID(taskId);
taskName = info.Note;
state = TaskState.NotStarted;
//// 从JSON加载目标
//foreach (var target in info.Targets)
//{
// Targets.Add(new TaskTarget((TargetType)target.TargetType, target.TargetID, target.Description, target.RequiredProgress));
//}
UpdateTxt();
}
// 更新任务描述文本
public void UpdateTxt()
{
string progressInfo = "";
foreach (var target in Targets)
{
progressInfo += $"{target.Description}: {target.CurrentProgress}/{target.RequiredProgress}\n";
}
taskTxt.text = $"{taskName}\n{progressInfo}";
}
// 更新任务进度
public void UpdateProgress(TargetType targetType, string targetId, int value)
{
TaskTarget target = Targets.Find(t => t.Type == targetType && t.TargetID == targetId);
if (target != null)
{
target.UpdateProgress(value);
UpdateTxt();
// 检查是否所有目标已完成
if (Targets.TrueForAll(t => t.IsCompleted))
{
state = TaskState.Completed;
TaskPanel.instance.RemoveTask(taskId);
}
}
}
}