QuestionAnswerManager.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { _decorator, Component, Node, Button, Label } from 'cc';
  2. import { DialogueManager } from './DialogueManager';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 问答对接口
  6. */
  7. interface QuestionAnswer {
  8. question: string; // 问题文本
  9. answer: string; // 回答文本
  10. }
  11. @ccclass('QuestionAnswerManager')
  12. export class QuestionAnswerManager extends Component {
  13. @property({
  14. type: DialogueManager,
  15. tooltip: '对话管理器引用'
  16. })
  17. dialogueManager: DialogueManager = null;
  18. @property({
  19. type: [Button],
  20. tooltip: '问题按钮数组',
  21. readonly: true
  22. })
  23. questionButtons: Button[] = [];
  24. @property({
  25. type: [Node],
  26. tooltip: '问题文本节点数组(Label组件所在的节点)',
  27. readonly: true
  28. })
  29. questionLabels: Node[] = [];
  30. // 问答对数组
  31. private questionAnswerPairs: QuestionAnswer[] = [];
  32. // 当前显示的问题索引
  33. private currentQuestionIndices: number[] = [0, 1, 2];
  34. start() {
  35. // 初始化默认问答对
  36. this.initDefaultQuestionAnswers();
  37. // 注册按钮事件
  38. this.registerButtons();
  39. // 更新问题按钮文本
  40. this.updateQuestionButtonTexts();
  41. }
  42. /**
  43. * 初始化默认问答对
  44. */
  45. private initDefaultQuestionAnswers(): void {
  46. // 添加一些默认的问答对
  47. this.questionAnswerPairs = [
  48. {
  49. question: "询问为何不在名单内?",
  50. answer: "我没见过你的资料。你确定你应该在这个区域吗?"
  51. },
  52. {
  53. question: "询问通行证?",
  54. answer: "你需要有效的通行证才能进入这个区域。没有通行证是不允许的。"
  55. },
  56. {
  57. question: "询问外貌?",
  58. answer: "我对你的外貌没有任何评价。我只负责确认身份。"
  59. },
  60. {
  61. question: "你是谁?",
  62. answer: "我是这个区域的安全管理员,负责身份验证和访问控制。"
  63. },
  64. {
  65. question: "这是什么地方?",
  66. answer: "这是一个受限制的区域,需要特殊许可才能进入。"
  67. },
  68. {
  69. question: "我可以离开吗?",
  70. answer: "如果你没有通行证,建议你尽快离开,否则可能会有麻烦。"
  71. }
  72. ];
  73. }
  74. /**
  75. * 注册所有问题按钮
  76. */
  77. private registerButtons(): void {
  78. // 注册每个按钮的点击事件
  79. for (let i = 0; i < this.questionButtons.length; i++) {
  80. const buttonIndex = i;
  81. if (this.questionButtons[i]) {
  82. this.questionButtons[i].node.on(Button.EventType.CLICK, () => {
  83. this.onQuestionButtonClicked(buttonIndex);
  84. }, this);
  85. }
  86. }
  87. }
  88. /**
  89. * 更新问题按钮文本
  90. */
  91. private updateQuestionButtonTexts(): void {
  92. // 为每个按钮设置对应的问题文本
  93. for (let i = 0; i < Math.min(this.questionLabels.length, this.currentQuestionIndices.length); i++) {
  94. const questionIndex = this.currentQuestionIndices[i];
  95. const labelNode = this.questionLabels[i];
  96. if (labelNode && questionIndex >= 0 && questionIndex < this.questionAnswerPairs.length) {
  97. // 获取Label组件并设置文本
  98. const label = labelNode.getComponent(Label);
  99. if (label) {
  100. label.string = this.questionAnswerPairs[questionIndex].question;
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * 问题按钮点击回调
  107. * @param buttonIndex 按钮索引
  108. */
  109. private onQuestionButtonClicked(buttonIndex: number): void {
  110. if (buttonIndex < 0 || buttonIndex >= this.currentQuestionIndices.length) {
  111. return;
  112. }
  113. const questionIndex = this.currentQuestionIndices[buttonIndex];
  114. if (questionIndex < 0 || questionIndex >= this.questionAnswerPairs.length) {
  115. return;
  116. }
  117. // 获取问答对
  118. const qa = this.questionAnswerPairs[questionIndex];
  119. // 使用对话管理器显示回答
  120. if (this.dialogueManager && qa) {
  121. this.dialogueManager.showDialogue(qa.answer);
  122. }
  123. }
  124. /**
  125. * 添加新的问答对
  126. * @param questionAnswers 要添加的问答对数组
  127. */
  128. public addQuestionAnswers(questionAnswers: QuestionAnswer[]): void {
  129. if (!questionAnswers || questionAnswers.length === 0) {
  130. return;
  131. }
  132. // 添加新的问答对
  133. this.questionAnswerPairs = [...this.questionAnswerPairs, ...questionAnswers];
  134. // 更新按钮文本(如果需要)
  135. this.updateQuestionButtonTexts();
  136. }
  137. /**
  138. * 设置问题按钮显示的问题索引
  139. * @param indices 问题索引数组,长度应与按钮数量相同
  140. */
  141. public setQuestionIndices(indices: number[]): void {
  142. // 验证索引有效性
  143. const validIndices = indices.filter(index =>
  144. index >= 0 && index < this.questionAnswerPairs.length
  145. );
  146. // 更新当前显示的问题索引
  147. this.currentQuestionIndices = validIndices.slice(0, this.questionButtons.length);
  148. // 如果索引数量少于按钮数量,使用默认索引填充
  149. while (this.currentQuestionIndices.length < this.questionButtons.length) {
  150. const defaultIndex = this.currentQuestionIndices.length % this.questionAnswerPairs.length;
  151. this.currentQuestionIndices.push(defaultIndex);
  152. }
  153. // 更新按钮文本
  154. this.updateQuestionButtonTexts();
  155. }
  156. /**
  157. * 获取当前所有问答对
  158. * @returns 所有问答对数组
  159. */
  160. public getAllQuestionAnswers(): QuestionAnswer[] {
  161. return [...this.questionAnswerPairs];
  162. }
  163. /**
  164. * 清除所有问答对并重置为默认值
  165. */
  166. public resetToDefault(): void {
  167. this.initDefaultQuestionAnswers();
  168. this.currentQuestionIndices = [0, 1, 2];
  169. this.updateQuestionButtonTexts();
  170. }
  171. /**
  172. * 更新特定索引的问答对
  173. * @param index 要更新的问答对索引
  174. * @param newQA 新的问答对
  175. */
  176. public updateQuestionAnswer(index: number, newQA: QuestionAnswer): void {
  177. if (index >= 0 && index < this.questionAnswerPairs.length && newQA) {
  178. this.questionAnswerPairs[index] = newQA;
  179. this.updateQuestionButtonTexts();
  180. }
  181. }
  182. }