DataManager.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { _decorator, Component, Node, JsonAsset, resources } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. // 问答对接口
  4. interface QAPair {
  5. question: string;
  6. answer: string;
  7. }
  8. // NPC接口
  9. interface NPC {
  10. type: 'real' | 'fake';
  11. characterId: number;
  12. characterName: string;
  13. skinId: number;
  14. skinName: string;
  15. qaPairs: QAPair[];
  16. }
  17. // 关卡数据接口
  18. interface LevelData {
  19. name: string;
  20. description: string;
  21. npcs: NPC[];
  22. }
  23. @ccclass('DataManager')
  24. export class DataManager extends Component {
  25. // 允许策划将JSON资产拖放到此数组中
  26. @property({
  27. type: [JsonAsset],
  28. tooltip: '关卡JSON数据文件,顺序决定关卡顺序'
  29. })
  30. levelJsonAssets: JsonAsset[] = [];
  31. // 存储所有关卡数据
  32. private levels: LevelData[] = [];
  33. // 当前关卡索引
  34. private currentLevelIndex: number = 0;
  35. // 单例模式
  36. private static _instance: DataManager = null;
  37. public static get instance(): DataManager {
  38. return this._instance;
  39. }
  40. onLoad() {
  41. // 设置单例
  42. if (DataManager._instance === null) {
  43. DataManager._instance = this;
  44. } else {
  45. this.destroy();
  46. return;
  47. }
  48. // 加载所有关卡数据
  49. this.loadAllLevels();
  50. }
  51. /**
  52. * 加载所有关卡数据
  53. */
  54. private loadAllLevels(): void {
  55. this.levels = [];
  56. // 处理拖放到组件的JSON资产
  57. for (const jsonAsset of this.levelJsonAssets) {
  58. if (jsonAsset) {
  59. try {
  60. const levelData = jsonAsset.json as LevelData;
  61. this.levels.push(levelData);
  62. console.log(`成功加载关卡: ${levelData.name}`);
  63. } catch (error) {
  64. console.error(`加载关卡数据失败: ${jsonAsset.name}`, error);
  65. }
  66. }
  67. }
  68. console.log(`总共加载了 ${this.levels.length} 个关卡`);
  69. }
  70. /**
  71. * 获取当前关卡数据
  72. */
  73. public getCurrentLevel(): LevelData | null {
  74. if (this.levels.length === 0) {
  75. console.warn('没有加载任何关卡数据');
  76. return null;
  77. }
  78. if (this.currentLevelIndex < 0 || this.currentLevelIndex >= this.levels.length) {
  79. console.warn(`当前关卡索引 ${this.currentLevelIndex} 超出范围`);
  80. return null;
  81. }
  82. return this.levels[this.currentLevelIndex];
  83. }
  84. /**
  85. * 切换到下一个关卡
  86. * @returns 是否成功切换到下一关卡
  87. */
  88. public nextLevel(): boolean {
  89. if (this.currentLevelIndex < this.levels.length - 1) {
  90. this.currentLevelIndex++;
  91. console.log(`切换到关卡 ${this.currentLevelIndex + 1}: ${this.getCurrentLevel()?.name}`);
  92. return true;
  93. }
  94. console.log('已经是最后一个关卡');
  95. return false;
  96. }
  97. /**
  98. * 切换到上一个关卡
  99. * @returns 是否成功切换到上一关卡
  100. */
  101. public previousLevel(): boolean {
  102. if (this.currentLevelIndex > 0) {
  103. this.currentLevelIndex--;
  104. console.log(`切换到关卡 ${this.currentLevelIndex + 1}: ${this.getCurrentLevel()?.name}`);
  105. return true;
  106. }
  107. console.log('已经是第一个关卡');
  108. return false;
  109. }
  110. /**
  111. * 切换到指定索引的关卡
  112. * @param index 关卡索引
  113. * @returns 是否成功切换
  114. */
  115. public setLevel(index: number): boolean {
  116. if (index >= 0 && index < this.levels.length) {
  117. this.currentLevelIndex = index;
  118. console.log(`切换到关卡 ${index + 1}: ${this.getCurrentLevel()?.name}`);
  119. return true;
  120. }
  121. console.warn(`关卡索引 ${index} 超出范围`);
  122. return false;
  123. }
  124. /**
  125. * 获取当前关卡中的所有NPC
  126. */
  127. public getCurrentLevelNPCs(): NPC[] {
  128. const level = this.getCurrentLevel();
  129. return level ? level.npcs : [];
  130. }
  131. /**
  132. * 获取当前关卡中的真人NPC
  133. */
  134. public getRealNPCs(): NPC[] {
  135. const npcs = this.getCurrentLevelNPCs();
  136. return npcs.filter(npc => npc.type === 'real');
  137. }
  138. /**
  139. * 获取当前关卡中的伪人NPC
  140. */
  141. public getFakeNPCs(): NPC[] {
  142. const npcs = this.getCurrentLevelNPCs();
  143. return npcs.filter(npc => npc.type === 'fake');
  144. }
  145. /**
  146. * 根据角色ID获取NPC
  147. * @param characterId 角色ID
  148. */
  149. public getNPCById(characterId: number): NPC | null {
  150. const npcs = this.getCurrentLevelNPCs();
  151. return npcs.find(npc => npc.characterId === characterId) || null;
  152. }
  153. /**
  154. * 获取所有关卡数量
  155. */
  156. public getLevelCount(): number {
  157. return this.levels.length;
  158. }
  159. /**
  160. * 获取当前关卡索引
  161. */
  162. public getCurrentLevelIndex(): number {
  163. return this.currentLevelIndex;
  164. }
  165. /**
  166. * 重新加载所有关卡数据
  167. */
  168. public reloadAllLevels(): void {
  169. this.loadAllLevels();
  170. }
  171. }