GameFlowManager.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. console.log('showNextNpc');
  89. this.currentNpcIndex++;
  90. // 检查是否还有NPC
  91. if (this.currentNpcIndex >= this.currentNpcs.length) {
  92. console.log('当前关卡所有NPC已处理完毕');
  93. // 可以在这里添加关卡完成逻辑
  94. return;
  95. }
  96. // 获取当前NPC数据
  97. const npc = this.currentNpcs[this.currentNpcIndex];
  98. // 配置问答对
  99. this.setupQuestionAnswers(npc.qaPairs);
  100. // 如果有头像管理器,先加载头像
  101. if (this.avatarManager) {
  102. this.avatarManager.loadAvatar(npc.characterId);
  103. }
  104. // 配置角色外观,并在完成后让角色入场
  105. this.characterManager.setupCharacterAppearance(npc.characterId, npc.skinName, () => {
  106. // 让角色入场(只有在外观设置完成后才执行)
  107. this.characterManager.characterEnter(undefined);
  108. // 启用按钮
  109. this.enableButtons();
  110. });
  111. }
  112. /**
  113. * 启用决策按钮
  114. */
  115. private enableButtons(): void {
  116. if (this.allowButton) {
  117. this.allowButton.interactable = true;
  118. }
  119. if (this.dismissButton) {
  120. this.dismissButton.interactable = true;
  121. }
  122. }
  123. /**
  124. * 禁用决策按钮
  125. */
  126. private disableButtons(): void {
  127. if (this.allowButton) {
  128. this.allowButton.interactable = false;
  129. }
  130. if (this.dismissButton) {
  131. this.dismissButton.interactable = false;
  132. }
  133. }
  134. /**
  135. * 设置问答对
  136. * @param qaPairs 问答对数组
  137. */
  138. private setupQuestionAnswers(qaPairs: any[]): void {
  139. if (!this.questionAnswerManager) {
  140. console.error('问答管理器未设置');
  141. return;
  142. }
  143. // 替换所有问答对(而不是添加)
  144. this.questionAnswerManager.replaceAllQuestionAnswers(qaPairs);
  145. }
  146. /**
  147. * 处理放行角色
  148. */
  149. public handleCharacterPassed(): void {
  150. // 禁用按钮,防止连续点击
  151. this.disableButtons();
  152. // 让角色向右移动,动画完成后显示下一个NPC
  153. this.characterManager.moveCharacterRight(() => {
  154. this.showNextNpc();
  155. });
  156. }
  157. /**
  158. * 处理赶走角色
  159. */
  160. public handleCharacterDismissed(): void {
  161. // 禁用按钮,防止连续点击
  162. this.disableButtons();
  163. // 让角色向左移动,动画完成后显示下一个NPC
  164. this.characterManager.moveCharacterLeft(() => {
  165. this.showNextNpc();
  166. });
  167. }
  168. /**
  169. * 获取当前NPC数据
  170. * @returns 当前NPC数据,如果没有则返回null
  171. */
  172. public getCurrentNpcData(): any {
  173. if (this.currentNpcIndex < 0 || this.currentNpcIndex >= this.currentNpcs.length) {
  174. return null;
  175. }
  176. return this.currentNpcs[this.currentNpcIndex];
  177. }
  178. /**
  179. * 组件销毁时清理事件监听
  180. */
  181. onDestroy() {
  182. // 移除按钮事件监听
  183. if (this.allowButton) {
  184. this.allowButton.node.off(Button.EventType.CLICK, this.handleCharacterPassed, this);
  185. }
  186. if (this.dismissButton) {
  187. this.dismissButton.node.off(Button.EventType.CLICK, this.handleCharacterDismissed, this);
  188. }
  189. }
  190. }