GameFlowManager.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  100. * 设置问答对
  101. * @param qaPairs 问答对数组
  102. */
  103. private setupQuestionAnswers(qaPairs: any[]): void {
  104. if (!this.questionAnswerManager) {
  105. console.error('问答管理器未设置');
  106. return;
  107. }
  108. // 替换所有问答对(而不是添加)
  109. this.questionAnswerManager.replaceAllQuestionAnswers(qaPairs);
  110. }
  111. /**
  112. * 处理放行角色
  113. */
  114. public handleCharacterPassed(): void {
  115. // 让角色向右移动
  116. this.characterManager.moveCharacterRight();
  117. // 延迟显示下一个NPC
  118. this.scheduleOnce(() => {
  119. this.showNextNpc();
  120. }, this.characterManager.moveDuration + 0.5);
  121. }
  122. /**
  123. * 处理赶走角色
  124. */
  125. public handleCharacterDismissed(): void {
  126. // 让角色向左移动
  127. this.characterManager.moveCharacterLeft();
  128. // 延迟显示下一个NPC
  129. this.scheduleOnce(() => {
  130. this.showNextNpc();
  131. }, this.characterManager.moveDuration + 0.5);
  132. }
  133. /**
  134. * 组件销毁时清理事件监听
  135. */
  136. onDestroy() {
  137. // 移除按钮事件监听
  138. if (this.allowButton) {
  139. this.allowButton.node.off(Button.EventType.CLICK, this.handleCharacterPassed, this);
  140. }
  141. if (this.dismissButton) {
  142. this.dismissButton.node.off(Button.EventType.CLICK, this.handleCharacterDismissed, this);
  143. }
  144. }
  145. }