import { _decorator, Component, Node, Button, Label } from 'cc'; import { DialogueManager } from './DialogueManager'; import { PassCardManager } from './PassCardManager'; import { GameFlowManager } from './GameFlowManager'; const { ccclass, property } = _decorator; /** * 问答对接口,添加表情支持 */ interface QuestionAnswer { question: string; // 问题文本 answer: string; // 回答文本 showPassCard?: boolean; // 是否显示通行证 } // 将固定问题的索引定义为常量 const ASK_PASS_QUESTION_INDEX = 1; // 询问通行证的问题固定为第二个问题 @ccclass('QuestionAnswerManager') export class QuestionAnswerManager extends Component { @property({ type: DialogueManager, tooltip: '对话管理器引用' }) dialogueManager: DialogueManager = null; @property({ type: [Button], tooltip: '问题按钮数组', readonly: true }) questionButtons: Button[] = []; @property({ type: [Node], tooltip: '问题文本节点数组(Label组件所在的节点)', readonly: true }) questionLabels: Node[] = []; @property({ type: PassCardManager, tooltip: '通行证管理器引用' }) passCardManager: PassCardManager = null; @property({ type: Node, tooltip: '游戏流程管理器所在的节点' }) gameFlowManagerNode: Node = null; // 游戏流程管理器引用 private gameFlowManager: GameFlowManager = null; // 问答对数组 private questionAnswerPairs: QuestionAnswer[] = []; // 当前显示的问题索引 private currentQuestionIndices: number[] = [0, 1, 2]; start() { // 初始化默认问答对 // this.initDefaultQuestionAnswers(); // 注册按钮事件 this.registerButtons(); // 更新问题按钮文本 this.updateQuestionButtonTexts(); // 从节点获取GameFlowManager组件 if (this.gameFlowManagerNode) { this.gameFlowManager = this.gameFlowManagerNode.getComponent(GameFlowManager); if (!this.gameFlowManager) { console.error('游戏流程管理器节点上没有GameFlowManager组件'); } else { console.log('成功获取GameFlowManager组件'); } } else { console.error('游戏流程管理器节点未设置'); // 尝试在场景中查找 this.gameFlowManager = this.node.scene.getComponentInChildren(GameFlowManager); if (this.gameFlowManager) { console.log('在场景中找到GameFlowManager组件'); } else { console.error('无法在场景中找到GameFlowManager组件'); } } } /** * 初始化默认问答对 */ private initDefaultQuestionAnswers(): void { // 添加一些默认的问答对 this.questionAnswerPairs = [ { question: "询问为何不在名单内?", answer: "我没见过你的资料。你确定你应该在这个区域吗?" }, { question: "询问通行证?", answer: "这是我的通行证,请过目。", showPassCard: true // 这个问题会显示通行证 }, { question: "询问外貌?", answer: "我对你的外貌没有任何评价。我只负责确认身份。" }, { question: "你是谁?", answer: "我是这个区域的安全管理员,负责身份验证和访问控制。" }, { question: "这是什么地方?", answer: "这是一个受限制的区域,需要特殊许可才能进入。" }, { question: "我可以离开吗?", answer: "如果你没有通行证,建议你尽快离开,否则可能会有麻烦。" } ]; } /** * 注册按钮事件 */ private registerButtons(): void { for (let i = 0; i < this.questionButtons.length; i++) { const button = this.questionButtons[i]; const index = i; // 保存当前索引 if (button) { // 注册点击事件 button.node.on(Button.EventType.CLICK, () => { this.onQuestionButtonClicked(index); }, this); } } } /** * 更新问题按钮文本 */ private updateQuestionButtonTexts(): void { console.log('updateQuestionButtonTexts'); // 为每个按钮设置对应的问题文本 for (let i = 0; i < Math.min(this.questionLabels.length, this.currentQuestionIndices.length); i++) { const questionIndex = this.currentQuestionIndices[i]; const labelNode = this.questionLabels[i]; if (labelNode && questionIndex >= 0 && questionIndex < this.questionAnswerPairs.length) { // 获取Label组件并设置文本 const label = labelNode.getComponent(Label); if (label) { label.string = this.questionAnswerPairs[questionIndex].question; } } } } /** * 问题按钮点击回调 * @param buttonIndex 按钮索引 */ private onQuestionButtonClicked(buttonIndex: number): void { console.log(`问题按钮点击: index=${buttonIndex}`); if (buttonIndex < 0 || buttonIndex >= this.currentQuestionIndices.length) { console.error(`无效的按钮索引: ${buttonIndex}`); return; } const questionIndex = this.currentQuestionIndices[buttonIndex]; console.log(`对应问题索引: ${questionIndex}`); if (questionIndex < 0 || questionIndex >= this.questionAnswerPairs.length) { console.error(`无效的问题索引: ${questionIndex}`); return; } // 获取问答对 const qa = this.questionAnswerPairs[questionIndex]; console.log(`问题: "${qa.question}", 答案: "${qa.answer}", 显示通行证: ${qa.showPassCard}`); // 检查是否需要显示通行证 if (qa.showPassCard && this.passCardManager) { console.log('准备显示通行证'); // 获取当前NPC数据 const npcData = this.gameFlowManager ? this.gameFlowManager.getCurrentNpcData() : null; console.log('当前NPC数据:', npcData); if (npcData) { // 显示通行证,使用NPC数据 try { this.passCardManager.showPassCard({ name: npcData.characterName || '未知', room: `${Math.floor(Math.random() * 900) + 100}`, // 随机房间号,可以替换为实际数据 id: `ID-${npcData.characterId}-${Math.floor(Math.random() * 1000)}`, // 随机ID,可以替换为实际数据 reason: npcData.type === 'real' ? '合法居民' : '访客', characterId: npcData.characterId }); console.log('通行证显示成功'); } catch (error) { console.error('显示通行证时出错:', error); } } else { console.error('无法获取NPC数据'); // 尝试使用默认数据显示通行证 if (this.passCardManager) { try { this.passCardManager.showPassCard({ name: '测试角色', room: '101', id: 'ID-TEST-001', reason: '测试', characterId: 1 }); console.log('使用默认数据显示通行证'); } catch (error) { console.error('使用默认数据显示通行证时出错:', error); } } } } else { console.log(`不显示通行证,原因: showPassCard=${qa.showPassCard}, passCardManager=${!!this.passCardManager}`); } // 使用对话管理器显示回答,包含表情 if (this.dialogueManager && qa) { this.dialogueManager.showDialogue(qa.answer); } } /** * 添加新的问答对 * @param questionAnswers 要添加的问答对数组 */ public addQuestionAnswers(questionAnswers: QuestionAnswer[]): void { if (!questionAnswers || questionAnswers.length === 0) { return; } // 添加新的问答对 this.questionAnswerPairs = [...this.questionAnswerPairs, ...questionAnswers]; // 更新按钮文本(如果需要) this.updateQuestionButtonTexts(); } /** * 设置问题按钮显示的问题索引 * @param indices 问题索引数组,长度应与按钮数量相同 */ public setQuestionIndices(indices: number[]): void { // 验证索引有效性 const validIndices = indices.filter(index => index >= 0 && index < this.questionAnswerPairs.length ); // 更新当前显示的问题索引 this.currentQuestionIndices = validIndices.slice(0, this.questionButtons.length); // 如果索引数量少于按钮数量,使用默认索引填充 while (this.currentQuestionIndices.length < this.questionButtons.length) { const defaultIndex = this.currentQuestionIndices.length % this.questionAnswerPairs.length; this.currentQuestionIndices.push(defaultIndex); } // 更新按钮文本 this.updateQuestionButtonTexts(); } /** * 获取当前所有问答对 * @returns 所有问答对数组 */ public getAllQuestionAnswers(): QuestionAnswer[] { return [...this.questionAnswerPairs]; } /** * 清除所有问答对并重置为默认值 */ public resetToDefault(): void { this.initDefaultQuestionAnswers(); this.currentQuestionIndices = [0, 1, 2]; this.updateQuestionButtonTexts(); } /** * 更新特定索引的问答对 * @param index 要更新的问答对索引 * @param newQA 新的问答对 */ public updateQuestionAnswer(index: number, newQA: QuestionAnswer): void { if (index >= 0 && index < this.questionAnswerPairs.length && newQA) { this.questionAnswerPairs[index] = newQA; this.updateQuestionButtonTexts(); } } /** * 替换所有问答对 * @param newQAPairs 新的问答对数组 */ public replaceAllQuestionAnswers(newQAPairs: QuestionAnswer[]): void { console.log('替换问答对:', newQAPairs); // 清空现有问答对 this.questionAnswerPairs = []; // 添加固定的"询问通行证"问题 const passCardQuestion = { question: "询问通行证?", answer: "这是我的通行证,请过目。", showPassCard: true }; console.log('添加通行证问题:', passCardQuestion); // 添加新的问答对 for (let i = 0; i < newQAPairs.length; i++) { if (i === ASK_PASS_QUESTION_INDEX) { // 在第二个位置插入通行证问题 this.questionAnswerPairs.push(passCardQuestion); console.log(`在位置${i}添加通行证问题`); } else { this.questionAnswerPairs.push(newQAPairs[i]); console.log(`在位置${i}添加问题:`, newQAPairs[i]); } // 确保不超过三个问题 if (this.questionAnswerPairs.length >= 3) break; } // 如果问题不足三个,补充通行证问题 if (this.questionAnswerPairs.length < 3) { // 如果还没有添加通行证问题,则添加 const hasPassQuestion = this.questionAnswerPairs.some(qa => qa.showPassCard); if (!hasPassQuestion) { this.questionAnswerPairs.push(passCardQuestion); console.log('添加缺失的通行证问题'); } // 依然不足三个,可以使用默认问题补充 while (this.questionAnswerPairs.length < 3) { const backupQuestion = { question: `备用问题${this.questionAnswerPairs.length + 1}`, answer: "这是一个标准回答。" }; this.questionAnswerPairs.push(backupQuestion); console.log('添加备用问题:', backupQuestion); } } // 确保第二个问题是通行证问题 if (this.questionAnswerPairs.length >= 2 && !this.questionAnswerPairs[1].showPassCard) { console.log('强制第二个问题为通行证问题'); this.questionAnswerPairs[1] = passCardQuestion; } console.log('最终问答对:', this.questionAnswerPairs); // 重置当前显示的问题索引 this.currentQuestionIndices = [0, 1, 2]; // 更新问题按钮文本 this.updateQuestionButtonTexts(); } }