// 代码生成器 - 生成ProcessEvents代码 function CodeGenerator() { this.indentLevel = 0; this.indentString = " "; // 4个空格缩进 } // 生成ProcessEvents代码 CodeGenerator.prototype.generateProcessEventsCode = function(fileName, steps) { if (!steps || steps.length === 0) { throw new Error("当前流程文件为空,无法生成代码"); } var fileNameWithoutExtension = fileName.replace('.json', ''); var className = fileNameWithoutExtension + "ProcessEvents"; var code = ""; // 添加using语句 code += "using UnityEngine;\n"; code += "using UnityEngine.SceneManagement;\n"; code += "using Framework.ProcessMode;\n"; code += "using Framework.Dto;\n"; code += "\n"; // 添加类注释 code += "/// \n"; code += "/// " + fileNameWithoutExtension + " 流程事件处理类\n"; code += "/// \n"; // 添加类定义 code += "public class " + className + " : MonoBehaviour\n"; code += "{\n"; // 添加RegisterProcessEvents方法 code += " [ProcessAction]\n"; code += " public void RegisterProcessEvents()\n"; code += " {\n"; // 遍历步骤和动作生成事件注册代码 for (var i = 0; i < steps.length; i++) { var step = steps[i]; for (var j = 0; j < step.Actions.length; j++) { var action = step.Actions[j]; if (action.ActionType === ProcessActionType.DEFAULT) { // 默认类型:为每个目标对象生成事件 for (var k = 0; k < action.TargetObjects.length; k++) { var target = action.TargetObjects[k]; code += this.generateEventRegistration(i, j, step, action, target.ObjectName); } } else if (action.ActionType === ProcessActionType.JUDGMENT) { // 判断题类型:为每个题目生成事件 for (var k = 0; k < action.JudgmentQuestions.length; k++) { var question = action.JudgmentQuestions[k]; var targetName = "判断题_" + (k + 1); code += this.generateEventRegistration(i, j, step, action, targetName, question); } } else if (action.ActionType === ProcessActionType.MULTIPLE_CHOICE) { // 多选题类型:为每个题目生成事件 for (var k = 0; k < action.MultipleChoiceQuestions.length; k++) { var question = action.MultipleChoiceQuestions[k]; var targetName = "多选题_" + (k + 1); code += this.generateEventRegistration(i, j, step, action, targetName, question); } } } } code += " }\n"; code += "}\n"; return code; }; // 生成单个事件注册代码 CodeGenerator.prototype.generateEventRegistration = function(stepIndex, actionIndex, step, action, targetName, questionData) { var code = ""; // 添加方法注释 code += " /// \n"; code += " /// 步骤 " + (stepIndex + 1) + ": " + step.StepDescription + "\n"; code += " /// 动作 " + (actionIndex + 1) + ": " + action.Title + "\n"; if (questionData) { if (action.ActionType === ProcessActionType.JUDGMENT) { code += " /// 判断题: " + questionData.Question + "\n"; code += " /// 正确答案: " + questionData.CorrectAnswer + "\n"; } else if (action.ActionType === ProcessActionType.MULTIPLE_CHOICE) { code += " /// 多选题: " + questionData.Question + "\n"; code += " /// 选项: " + questionData.Options.join(', ') + "\n"; code += " /// 正确答案: " + questionData.CorrectAnswers.join(', ') + "\n"; } } else { code += " /// 目标对象: " + targetName + "\n"; } code += " /// \n"; // 生成事件注册代码 code += " EventRegistrationCenter.AddEventsToAllStepEvents(new EventStepInfo(" + stepIndex + ", " + actionIndex + ", \"" + targetName + "\", () =>\n"; code += " {\n"; code += " // 在这里加入事件处理逻辑\n"; if (questionData) { if (action.ActionType === ProcessActionType.JUDGMENT) { code += " Debug.Log(\"执行判断题:步骤 " + (stepIndex + 1) + " -> 动作 " + (actionIndex + 1) + " -> " + questionData.Question + "\");\n"; code += " // 正确答案: " + questionData.CorrectAnswer + "\n"; } else if (action.ActionType === ProcessActionType.MULTIPLE_CHOICE) { code += " Debug.Log(\"执行多选题:步骤 " + (stepIndex + 1) + " -> 动作 " + (actionIndex + 1) + " -> " + questionData.Question + "\");\n"; code += " // 正确答案: " + questionData.CorrectAnswers.join(', ') + "\n"; } } else { code += " Debug.Log(\"执行事件:步骤 " + (stepIndex + 1) + " -> 动作 " + (actionIndex + 1) + " -> " + targetName + "\");\n"; } code += " }));\n"; code += "\n"; return code; };