GameFlowManager.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.characterManager.setupCharacterAppearance(npc.characterId, npc.skinName);
  94. // 配置问答对
  95. this.setupQuestionAnswers(npc.qaPairs);
  96. // 让角色入场
  97. this.characterManager.characterEnter();
  98. // 启用按钮
  99. this.enableButtons();
  100. }
  101. /**
  102. * 启用决策按钮
  103. */
  104. private enableButtons(): void {
  105. if (this.allowButton) {
  106. this.allowButton.interactable = true;
  107. }
  108. if (this.dismissButton) {
  109. this.dismissButton.interactable = true;
  110. }
  111. }
  112. /**
  113. * 禁用决策按钮
  114. */
  115. private disableButtons(): void {
  116. if (this.allowButton) {
  117. this.allowButton.interactable = false;
  118. }
  119. if (this.dismissButton) {
  120. this.dismissButton.interactable = false;
  121. }
  122. }
  123. /**
  124. * 设置问答对
  125. * @param qaPairs 问答对数组
  126. */
  127. private setupQuestionAnswers(qaPairs: any[]): void {
  128. if (!this.questionAnswerManager) {
  129. console.error('问答管理器未设置');
  130. return;
  131. }
  132. // 替换所有问答对(而不是添加)
  133. this.questionAnswerManager.replaceAllQuestionAnswers(qaPairs);
  134. }
  135. /**
  136. * 处理放行角色
  137. */
  138. public handleCharacterPassed(): void {
  139. // 禁用按钮,防止连续点击
  140. this.disableButtons();
  141. // 让角色向右移动
  142. this.characterManager.moveCharacterRight();
  143. // 延迟显示下一个NPC
  144. this.scheduleOnce(() => {
  145. this.showNextNpc();
  146. }, this.characterManager.moveDuration + 0.5);
  147. }
  148. /**
  149. * 处理赶走角色
  150. */
  151. public handleCharacterDismissed(): void {
  152. // 禁用按钮,防止连续点击
  153. this.disableButtons();
  154. // 让角色向左移动
  155. this.characterManager.moveCharacterLeft();
  156. // 延迟显示下一个NPC
  157. this.scheduleOnce(() => {
  158. this.showNextNpc();
  159. }, this.characterManager.moveDuration + 0.5);
  160. }
  161. /**
  162. * 组件销毁时清理事件监听
  163. */
  164. onDestroy() {
  165. // 移除按钮事件监听
  166. if (this.allowButton) {
  167. this.allowButton.node.off(Button.EventType.CLICK, this.handleCharacterPassed, this);
  168. }
  169. if (this.dismissButton) {
  170. this.dismissButton.node.off(Button.EventType.CLICK, this.handleCharacterDismissed, this);
  171. }
  172. }
  173. }