GameFlowManager.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { _decorator, Component, Node, resources, sp, Asset, Button } from 'cc';
  2. import { DataManager } from './DataManager';
  3. import { CharacterManager } from './CharacterManager';
  4. import { QuestionAnswerManager } from './QuestionAnswerManager';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('GameFlowManager')
  7. export class GameFlowManager extends Component {
  8. @property({
  9. type: DataManager,
  10. tooltip: '数据管理器引用'
  11. })
  12. dataManager: DataManager = null;
  13. @property({
  14. type: CharacterManager,
  15. tooltip: '角色管理器引用'
  16. })
  17. characterManager: CharacterManager = null;
  18. @property({
  19. type: QuestionAnswerManager,
  20. tooltip: '问答管理器引用'
  21. })
  22. questionAnswerManager: QuestionAnswerManager = null;
  23. @property({
  24. type: Button,
  25. tooltip: '放行按钮'
  26. })
  27. allowButton: Button = null;
  28. @property({
  29. type: Button,
  30. tooltip: '赶走按钮'
  31. })
  32. dismissButton: Button = null;
  33. // 当前关卡中的NPC索引
  34. private currentNpcIndex: number = -1;
  35. // 当前关卡的NPC列表
  36. private currentNpcs: any[] = [];
  37. start() {
  38. // 注册按钮事件
  39. this.registerButtonEvents();
  40. // 开始游戏流程
  41. this.startGameFlow();
  42. }
  43. /**
  44. * 注册按钮事件
  45. */
  46. private registerButtonEvents(): void {
  47. // 注册放行按钮点击事件
  48. if (this.allowButton) {
  49. this.allowButton.node.on(Button.EventType.CLICK, this.handleCharacterPassed, this);
  50. } else {
  51. console.error('放行按钮未设置');
  52. }
  53. // 注册赶走按钮点击事件
  54. if (this.dismissButton) {
  55. this.dismissButton.node.on(Button.EventType.CLICK, this.handleCharacterDismissed, this);
  56. } else {
  57. console.error('赶走按钮未设置');
  58. }
  59. }
  60. /**
  61. * 开始游戏流程
  62. */
  63. private startGameFlow(): void {
  64. // 获取当前关卡数据
  65. const currentLevel = this.dataManager.getCurrentLevel();
  66. if (!currentLevel) {
  67. console.error('无法获取当前关卡数据');
  68. return;
  69. }
  70. console.log(`开始关卡: ${currentLevel.name}`);
  71. // 获取当前关卡的NPC列表
  72. this.currentNpcs = currentLevel.npcs;
  73. // 重置NPC索引
  74. this.currentNpcIndex = -1;
  75. // 显示第一个NPC
  76. this.showNextNpc();
  77. }
  78. /**
  79. * 显示下一个NPC
  80. */
  81. public showNextNpc(): void {
  82. console.log('showNextNpc');
  83. this.currentNpcIndex++;
  84. // 检查是否还有NPC
  85. if (this.currentNpcIndex >= this.currentNpcs.length) {
  86. console.log('当前关卡所有NPC已处理完毕');
  87. // 可以在这里添加关卡完成逻辑
  88. return;
  89. }
  90. // 获取当前NPC数据
  91. const npc = this.currentNpcs[this.currentNpcIndex];
  92. // 配置问答对
  93. this.setupQuestionAnswers(npc.qaPairs);
  94. // 配置角色外观,并在完成后让角色入场
  95. this.characterManager.setupCharacterAppearance(npc.characterId, npc.skinName, () => {
  96. // 让角色入场(只有在外观设置完成后才执行)
  97. this.characterManager.characterEnter();
  98. // 启用按钮
  99. this.enableButtons();
  100. });
  101. }
  102. /**
  103. * 启用决策按钮
  104. */
  105. private enableButtons(): void {
  106. if (this.allowButton) {
  107. this.allowButton.interactable = true;
  108. }
  109. if (this.dismissButton) {
  110. this.dismissButton.interactable = true;
  111. }
  112. }
  113. /**
  114. * 禁用决策按钮
  115. */
  116. private disableButtons(): void {
  117. if (this.allowButton) {
  118. this.allowButton.interactable = false;
  119. }
  120. if (this.dismissButton) {
  121. this.dismissButton.interactable = false;
  122. }
  123. }
  124. /**
  125. * 设置问答对
  126. * @param qaPairs 问答对数组
  127. */
  128. private setupQuestionAnswers(qaPairs: any[]): void {
  129. if (!this.questionAnswerManager) {
  130. console.error('问答管理器未设置');
  131. return;
  132. }
  133. // 替换所有问答对(而不是添加)
  134. this.questionAnswerManager.replaceAllQuestionAnswers(qaPairs);
  135. }
  136. /**
  137. * 处理放行角色
  138. */
  139. public handleCharacterPassed(): void {
  140. // 禁用按钮,防止连续点击
  141. this.disableButtons();
  142. // 让角色向右移动,动画完成后显示下一个NPC
  143. this.characterManager.moveCharacterRight(() => {
  144. this.showNextNpc();
  145. });
  146. }
  147. /**
  148. * 处理赶走角色
  149. */
  150. public handleCharacterDismissed(): void {
  151. // 禁用按钮,防止连续点击
  152. this.disableButtons();
  153. // 让角色向左移动,动画完成后显示下一个NPC
  154. this.characterManager.moveCharacterLeft(() => {
  155. this.showNextNpc();
  156. });
  157. }
  158. /**
  159. * 组件销毁时清理事件监听
  160. */
  161. onDestroy() {
  162. // 移除按钮事件监听
  163. if (this.allowButton) {
  164. this.allowButton.node.off(Button.EventType.CLICK, this.handleCharacterPassed, this);
  165. }
  166. if (this.dismissButton) {
  167. this.dismissButton.node.off(Button.EventType.CLICK, this.handleCharacterDismissed, this);
  168. }
  169. }
  170. }