QuestionAnswerManager.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. console.log('updateQuestionButtonTexts');
  93. // 为每个按钮设置对应的问题文本
  94. for (let i = 0; i < Math.min(this.questionLabels.length, this.currentQuestionIndices.length); i++) {
  95. const questionIndex = this.currentQuestionIndices[i];
  96. const labelNode = this.questionLabels[i];
  97. if (labelNode && questionIndex >= 0 && questionIndex < this.questionAnswerPairs.length) {
  98. // 获取Label组件并设置文本
  99. const label = labelNode.getComponent(Label);
  100. if (label) {
  101. label.string = this.questionAnswerPairs[questionIndex].question;
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * 问题按钮点击回调
  108. * @param buttonIndex 按钮索引
  109. */
  110. private onQuestionButtonClicked(buttonIndex: number): void {
  111. if (buttonIndex < 0 || buttonIndex >= this.currentQuestionIndices.length) {
  112. return;
  113. }
  114. const questionIndex = this.currentQuestionIndices[buttonIndex];
  115. if (questionIndex < 0 || questionIndex >= this.questionAnswerPairs.length) {
  116. return;
  117. }
  118. // 获取问答对
  119. const qa = this.questionAnswerPairs[questionIndex];
  120. // 使用对话管理器显示回答
  121. if (this.dialogueManager && qa) {
  122. this.dialogueManager.showDialogue(qa.answer);
  123. }
  124. }
  125. /**
  126. * 添加新的问答对
  127. * @param questionAnswers 要添加的问答对数组
  128. */
  129. public addQuestionAnswers(questionAnswers: QuestionAnswer[]): void {
  130. if (!questionAnswers || questionAnswers.length === 0) {
  131. return;
  132. }
  133. // 添加新的问答对
  134. this.questionAnswerPairs = [...this.questionAnswerPairs, ...questionAnswers];
  135. // 更新按钮文本(如果需要)
  136. this.updateQuestionButtonTexts();
  137. }
  138. /**
  139. * 设置问题按钮显示的问题索引
  140. * @param indices 问题索引数组,长度应与按钮数量相同
  141. */
  142. public setQuestionIndices(indices: number[]): void {
  143. // 验证索引有效性
  144. const validIndices = indices.filter(index =>
  145. index >= 0 && index < this.questionAnswerPairs.length
  146. );
  147. // 更新当前显示的问题索引
  148. this.currentQuestionIndices = validIndices.slice(0, this.questionButtons.length);
  149. // 如果索引数量少于按钮数量,使用默认索引填充
  150. while (this.currentQuestionIndices.length < this.questionButtons.length) {
  151. const defaultIndex = this.currentQuestionIndices.length % this.questionAnswerPairs.length;
  152. this.currentQuestionIndices.push(defaultIndex);
  153. }
  154. // 更新按钮文本
  155. this.updateQuestionButtonTexts();
  156. }
  157. /**
  158. * 获取当前所有问答对
  159. * @returns 所有问答对数组
  160. */
  161. public getAllQuestionAnswers(): QuestionAnswer[] {
  162. return [...this.questionAnswerPairs];
  163. }
  164. /**
  165. * 清除所有问答对并重置为默认值
  166. */
  167. public resetToDefault(): void {
  168. this.initDefaultQuestionAnswers();
  169. this.currentQuestionIndices = [0, 1, 2];
  170. this.updateQuestionButtonTexts();
  171. }
  172. /**
  173. * 更新特定索引的问答对
  174. * @param index 要更新的问答对索引
  175. * @param newQA 新的问答对
  176. */
  177. public updateQuestionAnswer(index: number, newQA: QuestionAnswer): void {
  178. if (index >= 0 && index < this.questionAnswerPairs.length && newQA) {
  179. this.questionAnswerPairs[index] = newQA;
  180. this.updateQuestionButtonTexts();
  181. }
  182. }
  183. /**
  184. * 替换所有问答对并更新显示
  185. * @param newQAPairs 新的问答对数组
  186. */
  187. public replaceAllQuestionAnswers(newQAPairs: QuestionAnswer[]): void {
  188. if (!newQAPairs) {
  189. return;
  190. }
  191. // 替换问答对
  192. this.questionAnswerPairs = [...newQAPairs];
  193. // 重置问题索引为前三个
  194. this.currentQuestionIndices = [0, 1, 2];
  195. // 如果新问答对数量少于3个,调整索引
  196. while (this.currentQuestionIndices.length > 0 &&
  197. this.currentQuestionIndices[this.currentQuestionIndices.length - 1] >= this.questionAnswerPairs.length) {
  198. this.currentQuestionIndices.pop();
  199. }
  200. // 更新按钮文本
  201. this.updateQuestionButtonTexts();
  202. }
  203. }