123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import { _decorator, Component, Node, resources, sp, Asset, Button } from 'cc';
- import { DataManager } from './DataManager';
- import { CharacterManager } from './CharacterManager';
- import { QuestionAnswerManager } from './QuestionAnswerManager';
- const { ccclass, property } = _decorator;
- @ccclass('GameFlowManager')
- export class GameFlowManager extends Component {
- @property({
- type: DataManager,
- tooltip: '数据管理器引用'
- })
- dataManager: DataManager = null;
- @property({
- type: CharacterManager,
- tooltip: '角色管理器引用'
- })
- characterManager: CharacterManager = null;
- @property({
- type: QuestionAnswerManager,
- tooltip: '问答管理器引用'
- })
- questionAnswerManager: QuestionAnswerManager = null;
- @property({
- type: Button,
- tooltip: '放行按钮'
- })
- allowButton: Button = null;
- @property({
- type: Button,
- tooltip: '赶走按钮'
- })
- dismissButton: Button = null;
- // 当前关卡中的NPC索引
- private currentNpcIndex: number = -1;
-
- // 当前关卡的NPC列表
- private currentNpcs: any[] = [];
- start() {
- // 注册按钮事件
- this.registerButtonEvents();
-
- // 开始游戏流程
- this.startGameFlow();
- }
- /**
- * 注册按钮事件
- */
- private registerButtonEvents(): void {
- // 注册放行按钮点击事件
- if (this.allowButton) {
- this.allowButton.node.on(Button.EventType.CLICK, this.handleCharacterPassed, this);
- } else {
- console.error('放行按钮未设置');
- }
-
- // 注册赶走按钮点击事件
- if (this.dismissButton) {
- this.dismissButton.node.on(Button.EventType.CLICK, this.handleCharacterDismissed, this);
- } else {
- console.error('赶走按钮未设置');
- }
- }
- /**
- * 开始游戏流程
- */
- private startGameFlow(): void {
- // 获取当前关卡数据
- const currentLevel = this.dataManager.getCurrentLevel();
- if (!currentLevel) {
- console.error('无法获取当前关卡数据');
- return;
- }
- console.log(`开始关卡: ${currentLevel.name}`);
-
- // 获取当前关卡的NPC列表
- this.currentNpcs = currentLevel.npcs;
-
- // 重置NPC索引
- this.currentNpcIndex = -1;
-
- // 显示第一个NPC
- this.showNextNpc();
- }
- /**
- * 显示下一个NPC
- */
- public showNextNpc(): void {
- console.log('showNextNpc');
- this.currentNpcIndex++;
-
- // 检查是否还有NPC
- if (this.currentNpcIndex >= this.currentNpcs.length) {
- console.log('当前关卡所有NPC已处理完毕');
- // 可以在这里添加关卡完成逻辑
- return;
- }
-
- // 获取当前NPC数据
- const npc = this.currentNpcs[this.currentNpcIndex];
-
- // 配置角色外观
- this.characterManager.setupCharacterAppearance(npc.characterId, npc.skinName);
-
- // 配置问答对
- this.setupQuestionAnswers(npc.qaPairs);
-
- // 让角色入场
- this.characterManager.characterEnter();
- }
- /**
- * 设置问答对
- * @param qaPairs 问答对数组
- */
- private setupQuestionAnswers(qaPairs: any[]): void {
- if (!this.questionAnswerManager) {
- console.error('问答管理器未设置');
- return;
- }
-
- // 替换所有问答对(而不是添加)
- this.questionAnswerManager.replaceAllQuestionAnswers(qaPairs);
- }
- /**
- * 处理放行角色
- */
- public handleCharacterPassed(): void {
- // 让角色向右移动
- this.characterManager.moveCharacterRight();
-
- // 延迟显示下一个NPC
- this.scheduleOnce(() => {
- this.showNextNpc();
- }, this.characterManager.moveDuration + 0.5);
- }
- /**
- * 处理赶走角色
- */
- public handleCharacterDismissed(): void {
- // 让角色向左移动
- this.characterManager.moveCharacterLeft();
-
- // 延迟显示下一个NPC
- this.scheduleOnce(() => {
- this.showNextNpc();
- }, this.characterManager.moveDuration + 0.5);
- }
- /**
- * 组件销毁时清理事件监听
- */
- onDestroy() {
- // 移除按钮事件监听
- if (this.allowButton) {
- this.allowButton.node.off(Button.EventType.CLICK, this.handleCharacterPassed, this);
- }
-
- if (this.dismissButton) {
- this.dismissButton.node.off(Button.EventType.CLICK, this.handleCharacterDismissed, this);
- }
- }
- }
|