using System; using System.Collections.Generic; using System.Linq; using HighlightPlus; using MotionFramework; using UnityEngine; using UnityEngine.UI; using Type = System.Type; namespace DefaultNamespace.ProcessMode { [ScriptDescription("流程模式管理器")] public class AnimationProcessManager : ModuleSingleton, IModule { private Dictionary processes = new Dictionary(); public ProcessMode currentMode; // 当前模式 private int currentStepIndex; // 当前步骤索引 private int currentActionIndex; // 当前动作索引,确保动作顺序 private int currentActionGameIndex; // 当前动作索引,确保动作顺序 private List incorrectClicks; // 错误点击的记录列表 public AnimationProcess CurrentProcess => processes[currentMode.ToString()]; public delegate void CompleteEventHandler(); // 定义一个事件,使用委托类型 public event CompleteEventHandler OnCompleteEvent; public void AddProcess(string type) { if (!processes.ContainsKey(type)) { processes[type] = new AnimationProcess(type); Enum.TryParse(type, true, out currentMode); } } public void ClearProcess() { processes.Clear(); } public void AddStepToProcess(string type, AnimationStep step) { if (processes.ContainsKey(type)) { processes[type].AddStep(step); } } public void HandleClick(GameObject clickedObject) { string type = currentMode.ToString(); if (processes.ContainsKey(type)) { AnimationProcess process = processes[type]; if (currentStepIndex < process.Steps.Count) { AnimationStep step = process.Steps[currentStepIndex]; ActionWithDescription currentAction = step.Actions[currentActionIndex]; if (currentAction.IsSequential) { // 按顺序点击的逻辑 if (currentAction.CurrentObjectIndex < currentAction.TargetObjects.Count && currentAction.TargetObjects[currentAction.CurrentObjectIndex] == clickedObject) { Debug.Log($"正确点击了:{clickedObject.name}"); currentAction.CurrentObjectIndex++; // 正确点击,递增对象索引 currentAction.ClickedObjects.Add(clickedObject); // 添加到已点击对象集合 HandleModeSpecificFeedback(currentMode, currentAction); // 处理模式特定的反馈 if (currentAction.CurrentObjectIndex >= currentAction.TargetObjects.Count) { CompleteAction(step, currentAction); // 完成当前动作 } } else { string correctObjectName = currentAction.TargetObjects[currentAction.CurrentObjectIndex].name; Debug.Log($"错误点击或顺序错误:{clickedObject.name}。正确的物体是:{correctObjectName}"); } } else { // 不按顺序点击的逻辑 if (currentAction.TargetObjects.Any(obj => obj.name == clickedObject.name)) { if (currentAction.ClickedObjects.Any(obj => obj.name == clickedObject.name)) { Debug.Log($"错误点击:{clickedObject.name}。这个物体已经点击过。"); return; // 如果物体已经点击过,不继续处理 } if (!currentAction.ClickedObjects.Contains(clickedObject)) { Debug.Log($"正确点击了:{clickedObject.name}"); currentAction.ClickedObjects.Add(clickedObject); // 添加到已点击对象集合 HandleModeSpecificFeedback(currentMode, currentAction); // 处理模式特定的反馈 if (currentAction.ClickedObjects.Count >= currentAction.TargetObjects.Count) { CompleteAction(step, currentAction); // 完成当前动作 } } } else { Debug.Log($"错误点击:{clickedObject.name}"); List correctObjectNames = new List(); foreach (var obj in currentAction.TargetObjects) { if (!currentAction.ClickedObjects.Contains(obj)) { correctObjectNames.Add(obj.name); } } string correctObjects = string.Join(", ", correctObjectNames); Debug.Log($"正确的物体是:{correctObjects}"); } } } } } /// /// 处理完成当前动作的逻辑 /// /// 当前步骤 /// 当前动作 private void CompleteAction(AnimationStep step, ActionWithDescription currentAction) { Debug.Log($"完成了动作 {currentActionIndex + 1}"); step.PlayAnimation(currentActionIndex); // 播放当前动作的动画 DisplayActionFeedback(currentMode, currentAction); // 显示模式特定的反馈 currentActionIndex++; currentAction.CurrentObjectIndex = 0; // 重置当前动作对象索引 currentAction.ClickedObjects.Clear(); // 重置已点击对象集合 currentAction.FeedbackDisplayed = false; // 重置反馈显示标志 if (currentActionIndex >= step.Actions.Count) { Debug.Log("所有动作完成!"); currentActionIndex = 0; // 重置动作索引或进入下一个大步骤 currentStepIndex++; if (currentStepIndex < processes[currentMode.ToString()].Steps.Count) { var nextStep = processes[currentMode.ToString()].Steps[currentStepIndex]; PrepareNextStep(currentMode, nextStep.Actions[0]); // 准备下一个步骤 } else { if (MotionEngine.GetModule().GetToolsPackScene() == ToolsPackScene.其他) { OnCompleteEvent(); } Debug.Log("全部完成了!!!!"); } } else { Debug.Log("开始下一个动作!"); PrepareNextStep(currentMode, step.Actions[currentActionIndex]); // 传递下一个动作对象 } } // Start is called before the first frame update /// /// 根据当前模式显示相应的动作反馈 /// /// 当前的流程模式 /// 当前的动作 private void DisplayActionFeedback(ProcessMode mode, ActionWithDescription action) { switch (mode) { case ProcessMode.Teaching: HighlightNextObject(action); // 高亮下一个需要点击的物体 break; case ProcessMode.Training: //ShowTrainingStep(action); // 在右上角显示流程步骤 break; case ProcessMode.Practice: ShowPracticeStep(action); // 只显示当前步骤 break; case ProcessMode.Assessment: // 考核模式无提示 break; } } /// /// 准备下一个步骤或动作,并根据模式处理相应的提示或反馈 /// /// 当前的流程模式 /// 下一个步骤或动作 public void PrepareNextStep(ProcessMode mode, object nextStepOrAction) { switch (mode) { case ProcessMode.Teaching: HighlightNextObject(nextStepOrAction as ActionWithDescription); // 高亮显示下一个需要点击的物体 break; case ProcessMode.Training: // 在培训模式下只打印下一步骤或动作的描述 if (nextStepOrAction is ActionWithDescription action) { Debug.Log($"培训模式:{action.Description}"); } else if (nextStepOrAction is AnimationStep step) { if (step.Actions.Count > 0) { Debug.Log($"培训模式:{step.Actions[0].Description}"); } } break; case ProcessMode.Practice: // 练习模式下不需要提前显示下一个步骤的描述 break; case ProcessMode.Assessment: // 考核模式无提示 break; } } /// /// 处理模式特定的反馈逻辑 /// /// 当前的流程模式 /// 当前的动作 private void HandleModeSpecificFeedback(ProcessMode mode, ActionWithDescription action) { if (action.FeedbackDisplayed) { return; // 已经显示过反馈,不再重复显示 } switch (mode) { case ProcessMode.Teaching: HighlightNextObject(action); // 高亮下一个需要点击的物体 break; case ProcessMode.Training: //ShowTrainingStep(action.Description); // 在右上角显示流程步骤 break; case ProcessMode.Practice: ShowPracticeStep(action.Description); // 只显示当前步骤 break; case ProcessMode.Assessment: // 考核模式无提示 break; } action.FeedbackDisplayed = true; // 标记反馈已显示 } /// /// 高亮显示下一个需要点击的物体 /// /// 当前的动作 private void HighlightNextObject(ActionWithDescription action) { if (action != null) { for (int i = action.CurrentObjectIndex; i < action.TargetObjects.Count; i++) { GameObject nextObject = action.TargetObjects[i]; if (!action.ClickedObjects.Contains(nextObject)) { // 添加高亮效果的逻辑 GameObject.Find(nextObject.name).GetComponent().highlighted = true; Debug.Log($"高亮显示:{nextObject.name}"); break; } } } } /// /// 设置当前模式 /// /// 模式 public void SetCurrentMode(ProcessMode mode) { currentMode = mode; currentStepIndex = 0; currentActionIndex = 0; } private void ShowTrainingStep(object nextStepOrAction) { if (nextStepOrAction is AnimationStep step) { Debug.Log($"培训模式:{step.StepDescription}"); } else if (nextStepOrAction is ActionWithDescription action) { Debug.Log($"培训模式:{action.Description}"); } } private void ShowPracticeStep(object nextStepOrAction) { if (nextStepOrAction is AnimationStep step) { Debug.Log($"练习模式:{step.StepDescription}"); } else if (nextStepOrAction is ActionWithDescription action) { Debug.Log($"练习模式:{action.Description}"); } } public void OnCreate(object createParam) { processes = new Dictionary(); incorrectClicks = new List(); } public void OnUpdate() { } public void OnDestroy() { } public void OnGUI() { } } }