SaveDataManager.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. import { _decorator, sys, resources, JsonAsset } from 'cc';
  2. import { BundleLoader } from '../Core/BundleLoader';
  3. import { LevelConfigManager } from './LevelConfigManager';
  4. import EventBus, { GameEvents } from '../Core/EventBus';
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * 武器配置数据接口
  8. */
  9. interface WeaponConfig {
  10. id: string;
  11. name: string;
  12. description: string;
  13. rarity: string;
  14. unlockLevel: number;
  15. baseDamage: number;
  16. baseSpeed: number;
  17. upgradeCosts: number[];
  18. maxLevel: number;
  19. }
  20. /**
  21. * 玩家数据结构
  22. */
  23. export interface PlayerData {
  24. // 基本信息
  25. playerId: string;
  26. playerName: string;
  27. createTime: number;
  28. lastPlayTime: number;
  29. totalPlayTime: number;
  30. // 货币系统
  31. money: number; // 局外钞票
  32. diamonds: number; // 钻石
  33. // 墙体等级系统
  34. wallLevel: number; // 墙体等级
  35. // 关卡进度
  36. currentLevel: number;
  37. maxUnlockedLevel: number;
  38. levelProgress: { [levelId: number]: LevelProgress };
  39. // 道具和装备
  40. inventory: InventoryData;
  41. // 统计数据
  42. statistics: GameStatistics;
  43. // 设置
  44. settings: PlayerSettings;
  45. }
  46. /**
  47. * 关卡进度数据
  48. */
  49. export interface LevelProgress {
  50. levelId: number;
  51. completed: boolean;
  52. bestScore: number;
  53. bestTime: number;
  54. attempts: number;
  55. firstClearTime: number;
  56. lastPlayTime: number;
  57. rewards: RewardData[];
  58. }
  59. /**
  60. * 背包数据
  61. */
  62. export interface InventoryData {
  63. weapons: { [weaponId: string]: WeaponData };
  64. items: { [itemId: string]: ItemData };
  65. materials: { [materialId: string]: number };
  66. capacity: number;
  67. usedSlots: number;
  68. }
  69. /**
  70. * 武器数据
  71. */
  72. export interface WeaponData {
  73. weaponId: string;
  74. level: number;
  75. rarity: string;
  76. obtainTime: number;
  77. upgradeCount: number;
  78. isEquipped: boolean;
  79. }
  80. /**
  81. * 道具数据
  82. */
  83. export interface ItemData {
  84. itemId: string;
  85. count: number;
  86. obtainTime: number;
  87. lastUsedTime: number;
  88. }
  89. /**
  90. * 奖励数据
  91. */
  92. export interface RewardData {
  93. type: 'money' | 'coins' | 'diamonds' | 'weapon' | 'item';
  94. id?: string;
  95. amount: number;
  96. obtainTime: number;
  97. source: string; // 获得来源:level_complete, daily_reward, shop_purchase等
  98. }
  99. /**
  100. * 游戏统计数据
  101. */
  102. export interface GameStatistics {
  103. totalGamesPlayed: number;
  104. totalWins: number;
  105. totalLosses: number;
  106. totalScore: number;
  107. totalEnemiesDefeated: number;
  108. totalShotsfired: number;
  109. totalDamageDealt: number;
  110. totalTimePlayed: number;
  111. totalPlayTime: number;
  112. highestLevel: number;
  113. consecutiveWins: number;
  114. bestWinStreak: number;
  115. favoriteWeapon: string;
  116. weaponsUnlocked: number;
  117. itemsCollected: number;
  118. }
  119. /**
  120. * 玩家设置
  121. */
  122. export interface PlayerSettings {
  123. soundEnabled: boolean;
  124. musicEnabled: boolean;
  125. soundVolume: number;
  126. musicVolume: number;
  127. vibrationEnabled: boolean;
  128. autoSaveEnabled: boolean;
  129. language: string;
  130. graphics: 'low' | 'medium' | 'high';
  131. newbieGuideCompleted: boolean;
  132. }
  133. /**
  134. * 存档管理器
  135. * 负责玩家数据的保存、加载和管理
  136. */
  137. @ccclass('SaveDataManager')
  138. export class SaveDataManager {
  139. private static instance: SaveDataManager = null;
  140. private playerData: PlayerData = null;
  141. private initialized: boolean = false;
  142. private autoSaveInterval: number = 30; // 自动保存间隔(秒)
  143. private lastSaveTime: number = 0;
  144. // 私有属性
  145. private weaponsConfig: { weapons: any[] } = null;
  146. private wallConfig: any = null;
  147. /**
  148. * 设置墙体配置(从MainUIController传递)
  149. */
  150. public setWallConfig(config: any): void {
  151. this.wallConfig = config;
  152. console.log('[SaveDataManager] 接收到墙体配置:', this.wallConfig);
  153. }
  154. // 最近的奖励记录(用于UI显示)
  155. private lastRewards: {money: number, diamonds: number} = {money: 0, diamonds: 0};
  156. // 存档文件键名
  157. private readonly SAVE_KEY = 'pong_game_save_data';
  158. private readonly BACKUP_KEY = 'pong_game_save_data_backup';
  159. private readonly SETTINGS_KEY = 'pong_game_settings';
  160. private constructor() {
  161. // 私有构造函数,确保单例模式
  162. }
  163. public static getInstance(): SaveDataManager {
  164. if (SaveDataManager.instance === null) {
  165. SaveDataManager.instance = new SaveDataManager();
  166. }
  167. return SaveDataManager.instance;
  168. }
  169. /**
  170. * 初始化存档管理器
  171. */
  172. public async initialize(): Promise<void> {
  173. if (this.initialized) return;
  174. this.loadPlayerData();
  175. await this.loadWeaponsConfig();
  176. this.initialized = true;
  177. }
  178. /**
  179. * 加载武器配置
  180. */
  181. private async loadWeaponsConfig(): Promise<void> {
  182. try {
  183. const bundleLoader = BundleLoader.getInstance();
  184. const weaponData = await bundleLoader.loadDataJson('weapons');
  185. if (!weaponData) {
  186. throw new Error('武器配置文件内容为空');
  187. }
  188. this.weaponsConfig = weaponData.json as { weapons: any[] };
  189. console.log('[SaveDataManager] 武器配置加载成功');
  190. } catch (error) {
  191. console.error('[SaveDataManager] 加载武器配置失败:', error);
  192. this.weaponsConfig = { weapons: [] };
  193. }
  194. }
  195. /**
  196. * 加载玩家数据
  197. */
  198. private loadPlayerData(): void {
  199. try {
  200. const savedData = sys.localStorage.getItem(this.SAVE_KEY);
  201. if (savedData) {
  202. const data = JSON.parse(savedData);
  203. this.playerData = this.validateAndMigrateData(data);
  204. this.playerData.lastPlayTime = Date.now();
  205. } else {
  206. this.createNewPlayerData();
  207. }
  208. } catch (error) {
  209. console.error('❌ 存档数据加载失败,尝试加载备份:', error);
  210. this.loadBackupData();
  211. }
  212. }
  213. /**
  214. * 加载备份数据
  215. */
  216. private loadBackupData(): void {
  217. try {
  218. const backupData = sys.localStorage.getItem(this.BACKUP_KEY);
  219. if (backupData) {
  220. const data = JSON.parse(backupData);
  221. this.playerData = this.validateAndMigrateData(data);
  222. this.playerData.lastPlayTime = Date.now();
  223. } else {
  224. this.createNewPlayerData();
  225. }
  226. } catch (error) {
  227. console.error('❌ 备份数据加载失败,创建新存档:', error);
  228. this.createNewPlayerData();
  229. }
  230. }
  231. /**
  232. * 创建新的玩家数据
  233. */
  234. private createNewPlayerData(): void {
  235. this.playerData = {
  236. playerId: this.generatePlayerId(),
  237. playerName: 'Player',
  238. createTime: Date.now(),
  239. lastPlayTime: Date.now(),
  240. totalPlayTime: 0,
  241. money: 0, // 初始局外金币
  242. diamonds: 0, // 初始钻石
  243. wallLevel: 1, // 初始墙体等级
  244. currentLevel: 1,
  245. maxUnlockedLevel: 1,
  246. levelProgress: {},
  247. inventory: {
  248. weapons: {},
  249. items: {},
  250. materials: {},
  251. capacity: 50,
  252. usedSlots: 0
  253. },
  254. statistics: {
  255. totalGamesPlayed: 0,
  256. totalWins: 0,
  257. totalLosses: 0,
  258. totalScore: 0,
  259. totalEnemiesDefeated: 0,
  260. totalShotsfired: 0,
  261. totalDamageDealt: 0,
  262. totalTimePlayed: 0,
  263. totalPlayTime: 0,
  264. highestLevel: 1,
  265. consecutiveWins: 0,
  266. bestWinStreak: 0,
  267. favoriteWeapon: '',
  268. weaponsUnlocked: 0,
  269. itemsCollected: 0
  270. },
  271. settings: {
  272. soundEnabled: true,
  273. musicEnabled: true,
  274. soundVolume: 0.8,
  275. musicVolume: 0.6,
  276. vibrationEnabled: true,
  277. autoSaveEnabled: true,
  278. language: 'zh-CN',
  279. graphics: 'medium',
  280. newbieGuideCompleted: false
  281. }
  282. };
  283. this.savePlayerData();
  284. }
  285. /**
  286. * 验证和迁移数据(用于版本兼容)
  287. */
  288. private validateAndMigrateData(data: any): PlayerData {
  289. // 数据迁移:将旧的coins字段迁移到money字段
  290. if (data.coins !== undefined && data.money === undefined) {
  291. data.money = data.coins;
  292. console.log(`[SaveDataManager] 数据迁移: coins(${data.coins}) -> money(${data.money})`);
  293. delete data.coins; // 删除旧字段
  294. }
  295. // 确保所有必要字段存在
  296. const defaultData = this.createDefaultPlayerData();
  297. return {
  298. ...defaultData,
  299. ...data,
  300. // 确保嵌套对象也被正确合并
  301. inventory: { ...defaultData.inventory, ...data.inventory },
  302. statistics: { ...defaultData.statistics, ...data.statistics },
  303. settings: { ...defaultData.settings, ...data.settings }
  304. };
  305. }
  306. /**
  307. * 创建默认玩家数据模板
  308. */
  309. private createDefaultPlayerData(): PlayerData {
  310. return {
  311. playerId: this.generatePlayerId(),
  312. playerName: 'Player',
  313. createTime: Date.now(),
  314. lastPlayTime: Date.now(),
  315. totalPlayTime: 0,
  316. money: 0, // 初始局外金钱
  317. diamonds: 0, // 初始钻石
  318. wallLevel: 1, // 初始墙体等级
  319. currentLevel: 1,
  320. maxUnlockedLevel: 1,
  321. levelProgress: {},
  322. inventory: {
  323. weapons: {
  324. // 默认解锁第一个武器(毛豆射手)
  325. 'pea_shooter': {
  326. weaponId: 'pea_shooter',
  327. level: 1,
  328. rarity: 'common',
  329. obtainTime: Date.now(),
  330. upgradeCount: 0,
  331. isEquipped: true
  332. }
  333. },
  334. items: {},
  335. materials: {},
  336. capacity: 50,
  337. usedSlots: 0
  338. },
  339. statistics: {
  340. totalGamesPlayed: 0,
  341. totalWins: 0,
  342. totalLosses: 0,
  343. totalScore: 0,
  344. totalEnemiesDefeated: 0,
  345. totalShotsfired: 0,
  346. totalDamageDealt: 0,
  347. totalTimePlayed: 0,
  348. totalPlayTime: 0,
  349. highestLevel: 1,
  350. consecutiveWins: 0,
  351. bestWinStreak: 0,
  352. favoriteWeapon: 'pea_shooter',
  353. weaponsUnlocked: 1,
  354. itemsCollected: 0
  355. },
  356. settings: {
  357. soundEnabled: true,
  358. musicEnabled: true,
  359. soundVolume: 0.8,
  360. musicVolume: 0.6,
  361. vibrationEnabled: true,
  362. autoSaveEnabled: true,
  363. language: 'zh-CN',
  364. graphics: 'medium',
  365. newbieGuideCompleted: false
  366. }
  367. };
  368. }
  369. /**
  370. * 保存玩家数据
  371. */
  372. public savePlayerData(force: boolean = false): void {
  373. if (!this.playerData) return;
  374. const currentTime = Date.now();
  375. // 检查是否需要保存(避免频繁保存)
  376. if (!force && currentTime - this.lastSaveTime < 5000) {
  377. return;
  378. }
  379. try {
  380. // 更新最后保存时间
  381. this.playerData.lastPlayTime = currentTime;
  382. // 创建备份
  383. const currentData = sys.localStorage.getItem(this.SAVE_KEY);
  384. if (currentData) {
  385. sys.localStorage.setItem(this.BACKUP_KEY, currentData);
  386. }
  387. // 保存当前数据
  388. const dataToSave = JSON.stringify(this.playerData);
  389. sys.localStorage.setItem(this.SAVE_KEY, dataToSave);
  390. this.lastSaveTime = currentTime;
  391. } catch (error) {
  392. console.error('❌ 玩家数据保存失败:', error);
  393. }
  394. }
  395. /**
  396. * 自动保存检查
  397. */
  398. public checkAutoSave(): void {
  399. if (!this.playerData || !this.playerData.settings.autoSaveEnabled) return;
  400. const currentTime = Date.now();
  401. if (currentTime - this.lastSaveTime > this.autoSaveInterval * 1000) {
  402. this.savePlayerData();
  403. }
  404. }
  405. // === 玩家基本信息管理 ===
  406. public getPlayerData(): PlayerData {
  407. return this.playerData;
  408. }
  409. public getWallLevel(): number {
  410. return this.playerData?.wallLevel || 1;
  411. }
  412. public getWallHealth(): number {
  413. const wallLevel = this.getWallLevel();
  414. if (this.wallConfig && this.wallConfig.wallConfig && this.wallConfig.wallConfig.healthByLevel) {
  415. return this.wallConfig.wallConfig.healthByLevel[wallLevel.toString()] || 200;
  416. }
  417. // 如果没有配置,返回默认值
  418. return 200;
  419. }
  420. public getCurrentLevel(): number {
  421. return this.playerData?.currentLevel || 1;
  422. }
  423. /**
  424. * 设置当前关卡
  425. */
  426. public setCurrentLevel(level: number): void {
  427. if (!this.playerData) return;
  428. // 确保关卡在有效范围内
  429. if (level >= 1 && level <= this.playerData.maxUnlockedLevel) {
  430. this.playerData.currentLevel = level;
  431. this.savePlayerData();
  432. console.log(`[SaveDataManager] 当前关卡设置为: ${level}`);
  433. } else {
  434. console.warn(`[SaveDataManager] 无效的关卡设置: ${level},当前最大解锁关卡: ${this.playerData.maxUnlockedLevel}`);
  435. }
  436. }
  437. public getMaxUnlockedLevel(): number {
  438. return this.playerData?.maxUnlockedLevel || 1;
  439. }
  440. public getMoney(): number {
  441. return this.playerData?.money || 0;
  442. }
  443. // 保持向后兼容的方法
  444. public getCoins(): number {
  445. return this.getMoney();
  446. }
  447. public getDiamonds(): number {
  448. return this.playerData?.diamonds || 0;
  449. }
  450. // === 关卡进度管理 ===
  451. /**
  452. * 完成关卡
  453. */
  454. public completeLevel(levelId: number, score: number, time: number): void {
  455. if (!this.playerData) return;
  456. // 更新关卡进度
  457. if (!this.playerData.levelProgress[levelId]) {
  458. this.playerData.levelProgress[levelId] = {
  459. levelId: levelId,
  460. completed: false,
  461. bestScore: 0,
  462. bestTime: 0,
  463. attempts: 0,
  464. firstClearTime: 0,
  465. lastPlayTime: 0,
  466. rewards: []
  467. };
  468. }
  469. const progress = this.playerData.levelProgress[levelId];
  470. progress.attempts += 1;
  471. progress.lastPlayTime = Date.now();
  472. // 首次完成
  473. if (!progress.completed) {
  474. progress.completed = true;
  475. progress.firstClearTime = Date.now();
  476. // 解锁下一关
  477. if (levelId === this.playerData.maxUnlockedLevel) {
  478. this.playerData.maxUnlockedLevel = levelId + 1;
  479. }
  480. }
  481. // 更新最佳记录
  482. if (score > progress.bestScore) {
  483. progress.bestScore = score;
  484. }
  485. if (time > 0 && (progress.bestTime === 0 || time < progress.bestTime)) {
  486. progress.bestTime = time;
  487. }
  488. // 更新统计数据
  489. this.playerData.statistics.totalGamesPlayed += 1;
  490. this.playerData.statistics.totalWins += 1;
  491. this.playerData.statistics.totalScore += score;
  492. this.playerData.statistics.consecutiveWins += 1;
  493. if (this.playerData.statistics.consecutiveWins > this.playerData.statistics.bestWinStreak) {
  494. this.playerData.statistics.bestWinStreak = this.playerData.statistics.consecutiveWins;
  495. }
  496. if (levelId > this.playerData.statistics.highestLevel) {
  497. this.playerData.statistics.highestLevel = levelId;
  498. }
  499. // 奖励处理已迁移到GameEnd.ts中
  500. // this.giveCompletionRewards(levelId);
  501. this.savePlayerData();
  502. }
  503. /**
  504. * 关卡失败
  505. */
  506. public failLevel(levelId: number): void {
  507. if (!this.playerData) return;
  508. // 更新关卡进度
  509. if (!this.playerData.levelProgress[levelId]) {
  510. this.playerData.levelProgress[levelId] = {
  511. levelId: levelId,
  512. completed: false,
  513. bestScore: 0,
  514. bestTime: 0,
  515. attempts: 0,
  516. firstClearTime: 0,
  517. lastPlayTime: 0,
  518. rewards: []
  519. };
  520. }
  521. const progress = this.playerData.levelProgress[levelId];
  522. progress.attempts += 1;
  523. progress.lastPlayTime = Date.now();
  524. // 更新统计数据
  525. this.playerData.statistics.totalGamesPlayed += 1;
  526. this.playerData.statistics.totalLosses += 1;
  527. this.playerData.statistics.consecutiveWins = 0;
  528. this.savePlayerData();
  529. }
  530. /**
  531. * 获取关卡进度
  532. */
  533. public getLevelProgress(levelId: number): LevelProgress | null {
  534. return this.playerData?.levelProgress[levelId] || null;
  535. }
  536. /**
  537. * 检查关卡是否已解锁
  538. */
  539. public isLevelUnlocked(levelId: number): boolean {
  540. return levelId <= (this.playerData?.maxUnlockedLevel || 1);
  541. }
  542. /**
  543. * 检查关卡是否已完成
  544. */
  545. public isLevelCompleted(levelId: number): boolean {
  546. const progress = this.getLevelProgress(levelId);
  547. return progress?.completed || false;
  548. }
  549. // === 货币管理 ===
  550. /**
  551. * 添加局外钞票
  552. */
  553. public addMoney(amount: number, source: string = 'unknown'): boolean {
  554. if (!this.playerData || amount <= 0) return false;
  555. this.playerData.money += amount;
  556. this.addReward('money', '', amount, source);
  557. // 触发货币变化事件
  558. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  559. return true;
  560. }
  561. // 保持向后兼容的方法
  562. public addCoins(amount: number, source: string = 'unknown'): boolean {
  563. return this.addMoney(amount, source);
  564. }
  565. /**
  566. * 消费局外钞票
  567. */
  568. public spendMoney(amount: number): boolean {
  569. if (!this.playerData || amount <= 0) {
  570. console.log(`[SaveDataManager] spendMoney失败 - 无效参数: amount=${amount}, playerData=${!!this.playerData}`);
  571. return false;
  572. }
  573. if (this.playerData.money < amount) {
  574. console.log(`[SaveDataManager] spendMoney失败 - 钞票不足: 需要=${amount}, 当前=${this.playerData.money}`);
  575. return false;
  576. }
  577. const moneyBefore = this.playerData.money;
  578. this.playerData.money -= amount;
  579. console.log(`[SaveDataManager] spendMoney成功 - 消费: ${amount}, 之前: ${moneyBefore}, 之后: ${this.playerData.money}`);
  580. // 触发货币变化事件
  581. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  582. return true;
  583. }
  584. /**
  585. * 添加钻石
  586. */
  587. public addDiamonds(amount: number, source: string = 'unknown'): boolean {
  588. if (!this.playerData || amount <= 0) return false;
  589. this.playerData.diamonds += amount;
  590. this.addReward('diamonds', '', amount, source);
  591. // 触发货币变化事件
  592. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  593. return true;
  594. }
  595. /**
  596. * 消费钻石
  597. */
  598. public spendDiamonds(amount: number): boolean {
  599. if (!this.playerData || amount <= 0 || this.playerData.diamonds < amount) {
  600. return false;
  601. }
  602. this.playerData.diamonds -= amount;
  603. // 触发货币变化事件
  604. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  605. return true;
  606. }
  607. // === 墙体等级管理 ===
  608. /**
  609. * 升级墙体等级
  610. */
  611. public upgradeWallLevel(): boolean {
  612. if (!this.playerData) return false;
  613. this.playerData.wallLevel += 1;
  614. this.savePlayerData(true);
  615. return true;
  616. }
  617. // === 道具和武器管理 ===
  618. /**
  619. * 添加武器
  620. */
  621. public addWeapon(weaponId: string, rarity: string = 'common', initialLevel: number = 0): boolean {
  622. if (!this.playerData) return false;
  623. if (!this.playerData.inventory.weapons[weaponId]) {
  624. this.playerData.inventory.weapons[weaponId] = {
  625. weaponId: weaponId,
  626. level: initialLevel, // 初始等级可配置,0表示未解锁,1表示已解锁
  627. rarity: rarity,
  628. obtainTime: Date.now(),
  629. upgradeCount: 0,
  630. isEquipped: false
  631. };
  632. // 只有解锁的武器才计入统计
  633. if (initialLevel > 0) {
  634. this.playerData.statistics.weaponsUnlocked += 1;
  635. this.addReward('weapon', weaponId, 1, 'drop');
  636. }
  637. return true;
  638. }
  639. return false;
  640. }
  641. /**
  642. * 获取武器数据
  643. */
  644. public getWeapon(weaponId: string): WeaponData | null {
  645. if (!this.playerData) return null;
  646. return this.playerData.inventory.weapons[weaponId] || null;
  647. }
  648. /**
  649. * 获取所有武器数据
  650. */
  651. public getAllWeapons(): { [weaponId: string]: WeaponData } {
  652. if (!this.playerData) return {};
  653. return this.playerData.inventory.weapons;
  654. }
  655. /**
  656. * 升级武器
  657. */
  658. public upgradeWeapon(weaponId: string): boolean {
  659. if (!this.playerData) return false;
  660. const weapon = this.playerData.inventory.weapons[weaponId];
  661. if (!weapon || weapon.level === 0) return false; // 0级武器需要先解锁
  662. const cost = this.getWeaponUpgradeCost(weaponId);
  663. if (!this.spendMoney(cost)) return false;
  664. weapon.level += 1;
  665. weapon.upgradeCount += 1;
  666. return true;
  667. }
  668. /**
  669. * 获取武器升级费用
  670. */
  671. public getWeaponUpgradeCost(weaponId: string): number {
  672. const weapon = this.getWeapon(weaponId);
  673. if (!weapon || weapon.level === 0) return 0; // 0级武器需要解锁,不是升级
  674. // 从武器配置中获取升级费用
  675. if (this.weaponsConfig && this.weaponsConfig.weapons) {
  676. const weaponConfig = this.weaponsConfig.weapons.find(w => w.id === weaponId);
  677. if (weaponConfig && weaponConfig.upgradeConfig && weaponConfig.upgradeConfig.levels) {
  678. const levelConfig = weaponConfig.upgradeConfig.levels[weapon.level.toString()];
  679. if (levelConfig && levelConfig.cost) {
  680. return levelConfig.cost;
  681. }
  682. }
  683. }
  684. // 如果配置不存在,使用默认公式作为后备
  685. return 25 * weapon.level;
  686. }
  687. /**
  688. * 检查是否可以升级武器
  689. */
  690. public canUpgradeWeapon(weaponId: string): boolean {
  691. const weapon = this.getWeapon(weaponId);
  692. if (!weapon || weapon.level === 0) return false; // 0级武器需要先解锁
  693. const cost = this.getWeaponUpgradeCost(weaponId);
  694. return this.getMoney() >= cost;
  695. }
  696. /**
  697. * 解锁武器(将等级从0提升到1)
  698. */
  699. public unlockWeapon(weaponId: string): boolean {
  700. if (!this.playerData) return false;
  701. // 如果武器不存在,先添加(添加时设置为解锁状态)
  702. if (!this.playerData.inventory.weapons[weaponId]) {
  703. this.addWeapon(weaponId, 'common', 1);
  704. return true; // 添加武器即表示解锁成功
  705. }
  706. const weapon = this.playerData.inventory.weapons[weaponId];
  707. if (!weapon) return false;
  708. // 如果武器存在但未解锁(level为0),则解锁它
  709. if (weapon.level === 0) {
  710. weapon.level = 1;
  711. this.playerData.statistics.weaponsUnlocked += 1;
  712. this.addReward('weapon', weaponId, 1, 'unlock');
  713. return true;
  714. }
  715. // 武器已经存在且等级>=1,表示已解锁
  716. return weapon.level >= 1;
  717. }
  718. /**
  719. * 检查武器是否已解锁
  720. */
  721. public isWeaponUnlocked(weaponId: string): boolean {
  722. // 根据关卡进度判断武器是否应该解锁
  723. const requiredLevel = this.getWeaponUnlockLevel(weaponId);
  724. const maxUnlockedLevel = this.getMaxUnlockedLevel();
  725. console.log(`[PreviewInEditor] 检查武器解锁: ${weaponId}, 需要关卡: ${requiredLevel}, 当前最大解锁关卡: ${maxUnlockedLevel}`);
  726. return maxUnlockedLevel >= requiredLevel;
  727. }
  728. /**
  729. * 检查指定关卡是否会触发新武器解锁(基于配置)
  730. */
  731. public hasNewWeaponUnlockAtLevel(level: number): boolean {
  732. if (!this.weaponsConfig || !this.weaponsConfig.weapons) return false;
  733. for (const weapon of this.weaponsConfig.weapons) {
  734. if (weapon.unlockLevel === level) {
  735. const weaponData = this.getWeapon(weapon.id);
  736. const isUnlocked = weaponData && weaponData.level > 0;
  737. if (!isUnlocked) {
  738. return true;
  739. }
  740. }
  741. }
  742. return false;
  743. }
  744. /**
  745. * 获取武器解锁所需的关卡等级
  746. */
  747. private getWeaponUnlockLevel(weaponId: string): number {
  748. // 从武器配置中读取解锁等级
  749. if (this.weaponsConfig && this.weaponsConfig.weapons) {
  750. const weaponConfig = this.weaponsConfig.weapons.find(w => w.id === weaponId);
  751. if (weaponConfig && weaponConfig.unlockLevel !== undefined) {
  752. return weaponConfig.unlockLevel;
  753. }
  754. }
  755. // 如果配置不存在,返回默认值1
  756. return 1;
  757. }
  758. /**
  759. * 添加道具
  760. */
  761. public addItem(itemId: string, count: number = 1): boolean {
  762. if (!this.playerData || count <= 0) return false;
  763. if (!this.playerData.inventory.items[itemId]) {
  764. this.playerData.inventory.items[itemId] = {
  765. itemId: itemId,
  766. count: 0,
  767. obtainTime: Date.now(),
  768. lastUsedTime: 0
  769. };
  770. }
  771. this.playerData.inventory.items[itemId].count += count;
  772. this.playerData.statistics.itemsCollected += count;
  773. this.addReward('item', itemId, count, 'drop');
  774. return true;
  775. }
  776. /**
  777. * 使用道具
  778. */
  779. public useItem(itemId: string, count: number = 1): boolean {
  780. if (!this.playerData || count <= 0) return false;
  781. const item = this.playerData.inventory.items[itemId];
  782. if (!item || item.count < count) {
  783. return false;
  784. }
  785. item.count -= count;
  786. item.lastUsedTime = Date.now();
  787. if (item.count === 0) {
  788. delete this.playerData.inventory.items[itemId];
  789. }
  790. return true;
  791. }
  792. // === 奖励管理 ===
  793. /**
  794. * 添加奖励记录
  795. */
  796. private addReward(type: RewardData['type'], id: string, amount: number, source: string): void {
  797. if (!this.playerData) return;
  798. const reward: RewardData = {
  799. type: type,
  800. id: id,
  801. amount: amount,
  802. obtainTime: Date.now(),
  803. source: source
  804. };
  805. // 这里可以添加到全局奖励历史记录中
  806. // 暂时不实现,避免数据过大
  807. }
  808. /**
  809. * 从JSON配置文件获取关卡奖励数据
  810. */
  811. public async getLevelRewardsFromConfig(levelId: number): Promise<{money: number, diamonds: number} | null> {
  812. try {
  813. console.log(`[SaveDataManager] 开始获取关卡${levelId}的奖励配置`);
  814. // 由于不支持动态导入,改为直接导入
  815. const configManager = LevelConfigManager.getInstance();
  816. if (!configManager) {
  817. console.warn(`LevelConfigManager未初始化,使用默认奖励`);
  818. return null;
  819. }
  820. const levelConfig = await configManager.getLevelConfig(levelId);
  821. if (levelConfig && levelConfig.levelSettings && (levelConfig.levelSettings as any).rewards) {
  822. const rewards = (levelConfig.levelSettings as any).rewards;
  823. console.log(`[SaveDataManager] 从LevelConfigManager获取到奖励:`, rewards);
  824. return {
  825. money: rewards.coins || 0,
  826. diamonds: rewards.diamonds || 0
  827. };
  828. }
  829. // 如果JSON中没有奖励配置,尝试使用BundleLoader从data Bundle加载
  830. const jsonPath = `levels/Level${levelId}`;
  831. console.log(`[SaveDataManager] 尝试使用BundleLoader从data Bundle加载: ${jsonPath}`);
  832. try {
  833. const bundleLoader = BundleLoader.getInstance();
  834. const asset = await bundleLoader.loadDataJson(jsonPath);
  835. if (!asset || !asset.json) {
  836. console.warn(`无法加载关卡${levelId}的JSON配置`);
  837. return null;
  838. }
  839. const jsonData = asset.json;
  840. console.log(`[SaveDataManager] JSON数据:`, jsonData);
  841. // 修复字段名称映射:直接从JSON根级别读取coinReward和diamondReward
  842. if (jsonData && (jsonData.coinReward !== undefined || jsonData.diamondReward !== undefined)) {
  843. const rewardData = {
  844. money: jsonData.coinReward || 0,
  845. diamonds: jsonData.diamondReward || 0
  846. };
  847. return rewardData;
  848. } else if (jsonData && jsonData.rewards) {
  849. // 兼容嵌套在rewards对象中的情况
  850. const rewardData = {
  851. money: jsonData.rewards.coins || jsonData.rewards.coinReward || 0,
  852. diamonds: jsonData.rewards.diamonds || jsonData.rewards.diamondReward || 0
  853. };
  854. console.log(`[SaveDataManager] 从JSON.rewards获取到奖励:`, rewardData);
  855. return rewardData;
  856. } else {
  857. console.warn(`[SaveDataManager] JSON中未找到奖励配置`);
  858. return null;
  859. }
  860. } catch (error) {
  861. console.warn(`使用BundleLoader加载关卡${levelId}配置失败:`, error);
  862. return null;
  863. }
  864. } catch (error) {
  865. console.error(`获取关卡${levelId}奖励配置失败:`, error);
  866. return null;
  867. }
  868. }
  869. /**
  870. * 给予关卡完成奖励(从JSON配置获取)
  871. */
  872. public async giveCompletionRewards(levelId: number): Promise<void> {
  873. console.log(`[SaveDataManager] 开始给予关卡${levelId}完成奖励`);
  874. // 尝试从JSON配置文件获取奖励数据
  875. const configRewards = await this.getLevelRewardsFromConfig(levelId);
  876. console.log(`[SaveDataManager] 获取到的配置奖励:`, configRewards);
  877. let actualCoins = 0;
  878. let actualDiamonds = 0;
  879. if (configRewards) {
  880. // 使用JSON配置中的奖励数据
  881. if (configRewards.money > 0) {
  882. this.addMoney(configRewards.money, `level_${levelId}_complete`);
  883. actualCoins = configRewards.money;
  884. console.log(`[SaveDataManager] 添加钞票: ${configRewards.money}`);
  885. }
  886. if (configRewards.diamonds > 0) {
  887. this.addDiamonds(configRewards.diamonds, `level_${levelId}_complete`);
  888. actualDiamonds = configRewards.diamonds;
  889. console.log(`[SaveDataManager] 添加钻石: ${configRewards.diamonds}`);
  890. }
  891. } else {
  892. console.warn(`[SaveDataManager] 未获取到配置奖励,使用默认值0`);
  893. }
  894. // 特殊关卡额外奖励(里程碑奖励)
  895. // if (levelId % 10 === 0) {
  896. // this.addGems(1, `level_${levelId}_milestone`);
  897. // }
  898. // 存储最近的奖励记录
  899. this.lastRewards = {money: actualCoins, diamonds: actualDiamonds};
  900. console.log(`[SaveDataManager] 最终奖励记录:`, this.lastRewards);
  901. }
  902. /**
  903. * 根据波数比例给予失败奖励
  904. */
  905. public async giveFailureRewards(levelId: number, completedWaves: number, totalWaves: number): Promise<void> {
  906. console.log(`[SaveDataManager] 计算失败奖励 - 关卡: ${levelId}, 完成波数: ${completedWaves}/${totalWaves}`);
  907. if (totalWaves <= 0) {
  908. console.warn('[SaveDataManager] 总波数无效,重置奖励为0');
  909. this.lastRewards = {money: 0, diamonds: 0};
  910. return;
  911. }
  912. // 如果完成波数为0,则不给予任何奖励
  913. if (completedWaves <= 0) {
  914. console.log('[SaveDataManager] 完成波数为0,不给予任何奖励');
  915. this.lastRewards = {money: 0, diamonds: 0};
  916. return;
  917. }
  918. const actualCompletedWaves = Math.max(completedWaves, 0);
  919. const waveRatio = actualCompletedWaves / totalWaves;
  920. console.log(`[SaveDataManager] 波数完成情况: ${actualCompletedWaves}/${totalWaves} = ${(waveRatio * 100).toFixed(1)}%`);
  921. // 获取完整奖励数据
  922. const configRewards = await this.getLevelRewardsFromConfig(levelId);
  923. let actualCoins = 0;
  924. let actualDiamonds = 0;
  925. if (configRewards) {
  926. // 基于JSON配置计算比例奖励(取整)
  927. const partialCoins = Math.floor(configRewards.money * waveRatio);
  928. const partialDiamonds = Math.floor(configRewards.diamonds * waveRatio);
  929. console.log(`[SaveDataManager] 基于配置计算奖励 - 原始钞票: ${configRewards.money}, 原始钻石: ${configRewards.diamonds}`);
  930. console.log(`[SaveDataManager] 计算出的失败奖励 - 钞票: ${partialCoins}, 钻石: ${partialDiamonds}`);
  931. if (partialCoins > 0) {
  932. this.addMoney(partialCoins, `level_${levelId}_partial_${actualCompletedWaves}/${totalWaves}`);
  933. actualCoins = partialCoins;
  934. }
  935. if (partialDiamonds > 0) {
  936. this.addDiamonds(partialDiamonds, `level_${levelId}_partial_${actualCompletedWaves}/${totalWaves}`);
  937. actualDiamonds = partialDiamonds;
  938. }
  939. } else {
  940. // 回退到默认计算
  941. const baseCoins = levelId * 50;
  942. const partialCoins = Math.floor(baseCoins * waveRatio);
  943. console.log(`[SaveDataManager] 使用默认计算 - 基础钞票: ${baseCoins}, 计算出的失败奖励: ${partialCoins}`);
  944. if (partialCoins > 0) {
  945. this.addMoney(partialCoins, `level_${levelId}_partial_${actualCompletedWaves}/${totalWaves}`);
  946. actualCoins = partialCoins;
  947. }
  948. }
  949. console.log(`[SaveDataManager] 最终失败奖励 - 钞票: ${actualCoins}, 钻石: ${actualDiamonds}`);
  950. // 存储最近的奖励记录
  951. this.lastRewards = {money: actualCoins, diamonds: actualDiamonds};
  952. }
  953. /**
  954. * 获取最近的奖励记录(用于UI显示)
  955. */
  956. public getLastRewards(): {money: number, diamonds: number} {
  957. return {...this.lastRewards};
  958. }
  959. // === 统计数据更新 ===
  960. public updateStatistic(key: keyof GameStatistics, value: number): void {
  961. if (!this.playerData) return;
  962. if (typeof this.playerData.statistics[key] === 'number') {
  963. (this.playerData.statistics[key] as number) += value;
  964. }
  965. }
  966. // === 设置管理 ===
  967. public updateSetting<K extends keyof PlayerSettings>(key: K, value: PlayerSettings[K]): void {
  968. if (!this.playerData) return;
  969. this.playerData.settings[key] = value;
  970. this.savePlayerData();
  971. }
  972. public getSetting<K extends keyof PlayerSettings>(key: K): PlayerSettings[K] {
  973. return this.playerData?.settings[key];
  974. }
  975. // === 工具方法 ===
  976. /**
  977. * 生成唯一玩家ID
  978. */
  979. private generatePlayerId(): string {
  980. return `player_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  981. }
  982. /**
  983. * 重置所有数据
  984. */
  985. public resetAllData(): void {
  986. sys.localStorage.removeItem(this.SAVE_KEY);
  987. sys.localStorage.removeItem(this.BACKUP_KEY);
  988. this.createNewPlayerData();
  989. }
  990. /**
  991. * 导出存档数据(用于调试)
  992. */
  993. public exportSaveData(): string {
  994. return JSON.stringify(this.playerData, null, 2);
  995. }
  996. /**
  997. * 导入存档数据(用于调试)
  998. */
  999. public importSaveData(dataString: string): boolean {
  1000. try {
  1001. const data = JSON.parse(dataString);
  1002. this.playerData = this.validateAndMigrateData(data);
  1003. this.savePlayerData(true);
  1004. return true;
  1005. } catch (error) {
  1006. console.error('❌ 存档数据导入失败:', error);
  1007. return false;
  1008. }
  1009. }
  1010. /**
  1011. * 获取存档摘要信息
  1012. */
  1013. public getSaveSummary(): string {
  1014. if (!this.playerData) return '无存档数据';
  1015. return `墙体等级: ${this.playerData.wallLevel} | ` +
  1016. `当前关卡: ${this.playerData.currentLevel} | ` +
  1017. `最高关卡: ${this.playerData.maxUnlockedLevel} | ` +
  1018. `局外钞票: ${this.playerData.money} | ` +
  1019. `钻石: ${this.playerData.diamonds} | ` +
  1020. `游戏次数: ${this.playerData.statistics.totalGamesPlayed}`;
  1021. }
  1022. }