EnergyEfficiencyManagement/Assets/SKFramework/Core/Task/TaskData.cs

196 lines
5.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using UnityEngine;
namespace SK.Framework
{
public class TaskData
{
public string TaskId;
public string TaskName;
public string TaskDescription;
public List<TaskStep> Steps = new List<TaskStep>();
public TaskState State { get; private set; } = TaskState.NotStarted;
public event Action<TaskData> OnTaskStarted;
public event Action<TaskData> OnTaskCompleted;
public event Action<TaskData, string> OnTaskFailed;
public event Action<TaskStep> OnStepChanged;
private int _currentStepIndex;
private TaskMode _mode;
public TaskStep CurrentStep =>
(_currentStepIndex >= 0 && _currentStepIndex < Steps.Count)
? Steps[_currentStepIndex]
: null;
/// <summary>
/// 启动任务
/// </summary>
public void Start(TaskMode mode)
{
_mode = mode;
State = TaskState.Running;
_currentStepIndex = 0;
Debug.Log($"[Task] 开始: {TaskId} - {TaskName}");
OnTaskStarted?.Invoke(this);
StartCurrentStep();
}
private void StartCurrentStep()
{
if (CurrentStep == null)
return;
bool allowHint =
_mode == TaskMode.Teach ||
_mode == TaskMode.Practice;
CurrentStep.Start(allowHint);
}
/// <summary>
/// 核心接口
/// 外部调用此方法,即视为当前 Step 已完成
/// </summary>
public void CompleteCurrentStep(bool isError)
{
if (State != TaskState.Running)
return;
if (CurrentStep == null)
return;
CurrentStep.Complete(isError);
OnStepChanged?.Invoke(CurrentStep);
_currentStepIndex++;
if (_currentStepIndex >= Steps.Count)
{
CompleteTask();
}
else
{
StartCurrentStep();
}
}
/// <summary>
/// 事件判定预留
/// </summary>
public void Tick()
{
// 预留:自动判定/超时/提示倒计时等
}
/// <summary>
/// 完成任务方法
/// </summary>
private void CompleteTask()
{
State = TaskState.Completed;
Debug.Log($"[Task] 完成: {TaskId} - {TaskName}");
OnTaskCompleted?.Invoke(this);
}
/// <summary>
/// 任务失败
/// </summary>
/// <param name="reason"></param>
public void Fail(string reason)
{
State = TaskState.Failed;
Debug.LogError($"[Task] 失败: {reason}");
OnTaskFailed?.Invoke(this, reason);
}
/// <summary>
/// 跳过当前步骤并启动下一个步骤
/// </summary>
public void SkipCurrentStep()
{
if (State != TaskState.Running)
return;
if (CurrentStep == null)
return;
CurrentStep.Skip();
OnStepChanged?.Invoke(CurrentStep);
_currentStepIndex++;
if (_currentStepIndex >= Steps.Count)
{
CompleteTask();
}
else
{
StartCurrentStep();
}
}
private void JumpToStep(int targetStepIndex)
{
if (State != TaskState.Running)
return;
if (targetStepIndex < 0 || targetStepIndex >= Steps.Count)
{
Debug.LogError($"[Task] JumpToStep 失败,非法索引 {targetStepIndex}");
return;
}
// 1. 结束当前 Step标记为 Skipped
if (CurrentStep != null && CurrentStep.Result == StepResult.None)
{
CurrentStep.Skip();
OnStepChanged?.Invoke(CurrentStep);
}
// 2. 跳转索引
_currentStepIndex = targetStepIndex;
// 3. 启动目标 Step
StartCurrentStep();
Debug.Log($"[Task] 跳转到 Step {_currentStepIndex + 1}");
}
/// <summary>
/// 根据stepID跳转到目标步骤
/// </summary>
/// <param name="stepId"></param>
public void JumpToStepById(int stepId)
{
int index = Steps.FindIndex(s => s.StepId == stepId);
if (index == -1)
{
Debug.LogError($"[Task] JumpToStepById 失败,未找到 StepId={stepId}");
return;
}
JumpToStep(index);
}
public void Reset()
{
State = TaskState.NotStarted;
_currentStepIndex = 0;
foreach (var step in Steps)
{
step.Reset();
}
}
}
}