SaveDataManager.ts 38 KB

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