import { _decorator, Component, Node, JsonAsset, resources } from 'cc'; const { ccclass, property } = _decorator; // 问答对接口 interface QAPair { question: string; answers: string[]; } // 通行证信息 interface PassInfo { name: string; room: string; identityId: string; reason: string; avatarPath: string; hasStamp: boolean; } // 个人信息 interface PersonalInfo { id: string; info: string; avatarPath: string; hasRoommate: boolean; roommate: PersonalInfo | null; } // 错误信息 interface ErrorInfo { errorTypes: number[]; description: string; } // NPC接口 interface NPC { type: 'real' | 'fake'; characterId: number; characterName: string; skinId: number; skinName: string; qaPairs: QAPair[]; pass: PassInfo; personal: PersonalInfo; error: ErrorInfo; } // 今日名单项 export interface TodayListItem { name: string; avatarPath: string; } // 关卡数据接口 interface LevelData { name: string; description: string; timeLimit: number; randomNpcCount: number; defaultQuestions: string[]; npcs: NPC[]; todayList: TodayListItem[]; } @ccclass('DataManager') export class DataManager extends Component { // 允许策划将JSON资产拖放到此数组中 @property({ type: [JsonAsset], tooltip: '关卡JSON数据文件,顺序决定关卡顺序' }) levelJsonAssets: JsonAsset[] = []; // 存储所有关卡数据 private levels: LevelData[] = []; // 当前关卡索引 private currentLevelIndex: number = 0; // 单例模式 private static _instance: DataManager = null; public static get instance(): DataManager { return this._instance; } onLoad() { // 设置单例 if (DataManager._instance === null) { DataManager._instance = this; } else { this.destroy(); return; } // 加载所有关卡数据 this.loadAllLevels(); } /** * 加载所有关卡数据 */ private loadAllLevels(): void { this.levels = []; // 处理拖放到组件的JSON资产 for (const jsonAsset of this.levelJsonAssets) { if (jsonAsset) { try { const levelData = jsonAsset.json as LevelData; this.levels.push(levelData); console.log(`成功加载关卡: ${levelData.name}`); } catch (error) { console.error(`加载关卡数据失败: ${jsonAsset.name}`, error); } } } console.log(`总共加载了 ${this.levels.length} 个关卡`); } /** * 获取当前关卡数据 */ public getCurrentLevel(): LevelData | null { if (this.levels.length === 0) { console.warn('没有加载任何关卡数据'); return null; } if (this.currentLevelIndex < 0 || this.currentLevelIndex >= this.levels.length) { console.warn(`当前关卡索引 ${this.currentLevelIndex} 超出范围`); return null; } return this.levels[this.currentLevelIndex]; } /** * 切换到下一个关卡 * @returns 是否成功切换到下一关卡 */ public nextLevel(): boolean { if (this.currentLevelIndex < this.levels.length - 1) { this.currentLevelIndex++; console.log(`切换到关卡 ${this.currentLevelIndex + 1}: ${this.getCurrentLevel()?.name}`); return true; } console.log('已经是最后一个关卡'); return false; } /** * 切换到上一个关卡 * @returns 是否成功切换到上一关卡 */ public previousLevel(): boolean { if (this.currentLevelIndex > 0) { this.currentLevelIndex--; console.log(`切换到关卡 ${this.currentLevelIndex + 1}: ${this.getCurrentLevel()?.name}`); return true; } console.log('已经是第一个关卡'); return false; } /** * 切换到指定索引的关卡 * @param index 关卡索引 * @returns 是否成功切换 */ public setLevel(index: number): boolean { if (index >= 0 && index < this.levels.length) { this.currentLevelIndex = index; console.log(`切换到关卡 ${index + 1}: ${this.getCurrentLevel()?.name}`); return true; } console.warn(`关卡索引 ${index} 超出范围`); return false; } /** * 获取当前关卡中的所有NPC */ public getCurrentLevelNPCs(): NPC[] { const level = this.getCurrentLevel(); return level ? level.npcs : []; } /** * 获取当前关卡中的真人NPC */ public getRealNPCs(): NPC[] { const npcs = this.getCurrentLevelNPCs(); return npcs.filter(npc => npc.type === 'real'); } /** * 获取当前关卡中的伪人NPC */ public getFakeNPCs(): NPC[] { const npcs = this.getCurrentLevelNPCs(); return npcs.filter(npc => npc.type === 'fake'); } /** * 根据角色ID获取NPC * @param characterId 角色ID */ public getNPCById(characterId: number): NPC | null { const npcs = this.getCurrentLevelNPCs(); return npcs.find(npc => npc.characterId === characterId) || null; } /** * 获取所有关卡数量 */ public getLevelCount(): number { return this.levels.length; } /** * 获取当前关卡索引 */ public getCurrentLevelIndex(): number { return this.currentLevelIndex; } /** * 重新加载所有关卡数据 */ public reloadAllLevels(): void { this.loadAllLevels(); } /** * 获取当前关卡的时间限制(秒) */ public getCurrentLevelTimeLimit(): number { const level = this.getCurrentLevel(); return level ? level.timeLimit : 0; } /** * 获取当前关卡的随机NPC数量 */ public getRandomNpcCount(): number { const level = this.getCurrentLevel(); return level ? level.randomNpcCount : 0; } /** * 获取当前关卡的默认问题列表 */ public getDefaultQuestions(): string[] { const level = this.getCurrentLevel(); return level ? level.defaultQuestions : []; } /** * 获取当前关卡的今日名单 */ public getTodayList(): TodayListItem[] { const level = this.getCurrentLevel(); return level ? level.todayList : []; } /** * 根据角色ID获取NPC的通行证信息 * @param characterId 角色ID */ public getNPCPassInfo(characterId: number): PassInfo | null { const npc = this.getNPCById(characterId); return npc ? npc.pass : null; } /** * 根据角色ID获取NPC的个人信息 * @param characterId 角色ID */ public getNPCPersonalInfo(characterId: number): PersonalInfo | null { const npc = this.getNPCById(characterId); return npc ? npc.personal : null; } /** * 根据角色ID获取NPC的错误信息 * @param characterId 角色ID */ public getNPCErrorInfo(characterId: number): ErrorInfo | null { const npc = this.getNPCById(characterId); return npc ? npc.error : null; } /** * 检查指定名称的人是否在今日名单上 * @param name 人物名称 */ public isPersonInTodayList(name: string): boolean { const todayList = this.getTodayList(); return todayList.some(item => item.name.trim() === name.trim()); } }