123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- 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;
- }
- // 今日名单项
- 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());
- }
- }
|