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

78 lines
1.9 KiB
C#
Raw Normal View History

2024-12-11 14:24:06 +08:00
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public enum TaskState
2024-12-11 20:16:45 +08:00
{
NotStarted, // δ<><CEB4>ʼ
InProgress, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Completed, // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Failed // ʧ<><CAA7>
2024-12-11 20:16:45 +08:00
}
public class TaskItem : MonoBehaviour
{
public int taskId; // <20><><EFBFBD><EFBFBD>ID
public string taskName; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public TaskState state; // <20><><EFBFBD><EFBFBD>״̬
public Text taskTxt; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
public List<TaskTarget> Targets = new List<TaskTarget>(); // <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>1<EFBFBD>б<EFBFBD>
private JSONReader jsonReader; // JSON <20><><EFBFBD>ݶ<EFBFBD>ȡ<EFBFBD><C8A1>
2024-12-11 20:16:45 +08:00
public void SetInfo(int id, JSONReader reader)
2024-12-11 20:16:45 +08:00
{
taskId = id;
jsonReader = reader;
LoadTaskData();
2024-12-11 20:16:45 +08:00
}
private void LoadTaskData()
2024-12-11 14:24:06 +08:00
{
Task_ info = jsonReader.GetTaskByID(taskId);
taskName = info.Note;
state = TaskState.NotStarted;
2024-12-11 14:24:06 +08:00
//// <20><>JSON<4F><4E><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>
//foreach (var target in info.Targets)
//{
// Targets.Add(new TaskTarget((TargetType)target.TargetType, target.TargetID, target.Description, target.RequiredProgress));
//}
UpdateTxt();
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
public void UpdateTxt()
{
string progressInfo = "";
foreach (var target in Targets)
{
progressInfo += $"{target.Description}: {target.CurrentProgress}/{target.RequiredProgress}\n";
}
taskTxt.text = $"{taskName}\n{progressInfo}";
2024-12-12 14:50:53 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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();
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (Targets.TrueForAll(t => t.IsCompleted))
{
state = TaskState.Completed;
TaskPanel.instance.RemoveTask(taskId);
}
}
}
}