GameFlowManager.ts 6.1 KB

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