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 Steps = new List(); public TaskState State { get; private set; } = TaskState.NotStarted; public event Action OnTaskStarted; public event Action OnTaskCompleted; public event Action OnTaskFailed; public event Action OnStepChanged; private int _currentStepIndex; private TaskMode _mode; public TaskStep CurrentStep => (_currentStepIndex >= 0 && _currentStepIndex < Steps.Count) ? Steps[_currentStepIndex] : null; /// /// 启动任务 /// 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); } /// /// 核心接口 /// 外部调用此方法,即视为当前 Step 已完成 /// 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(); } } /// /// 事件判定预留 /// public void Tick() { // 预留:自动判定/超时/提示倒计时等 } /// /// 完成任务方法 /// private void CompleteTask() { State = TaskState.Completed; Debug.Log($"[Task] 完成: {TaskId} - {TaskName}"); OnTaskCompleted?.Invoke(this); } /// /// 任务失败 /// /// public void Fail(string reason) { State = TaskState.Failed; Debug.LogError($"[Task] 失败: {reason}"); OnTaskFailed?.Invoke(this, reason); } /// /// 跳过当前步骤并启动下一个步骤 /// 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}"); } /// /// 根据stepID跳转到目标步骤 /// /// 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(); } } } }