_xiaofang/xiaofang/Assets/Prefabs/HYLPrefabs/TaskItem.cs
2024-12-16 15:28:35 +08:00

75 lines
1.8 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;
Debug.Log(info.Targets1);
UpdateTxt();
}
// 更新任务描述文本
public void UpdateTxt()
{
string progressInfo = "";
foreach (var target in Targets)
{
progressInfo += $"{target.Description}: {target.CurrentProgress}/{target.RequiredProgress}\n";
}
Debug.Log(taskName);
Debug.Log(progressInfo);
taskTxt.text = $"{taskName}";
}
// 更新任务进度
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);
}
}
}
}