SaveDataManager.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. import { _decorator, sys } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * 玩家数据结构
  5. */
  6. export interface PlayerData {
  7. // 基本信息
  8. playerId: string;
  9. playerName: string;
  10. createTime: number;
  11. lastPlayTime: number;
  12. totalPlayTime: number;
  13. // 等级和经验
  14. playerLevel: number;
  15. experience: number;
  16. experienceToNext: number;
  17. // 货币系统
  18. coins: number; // 金币
  19. diamonds: number; // 钻石
  20. gems: number; // 宝石
  21. // 关卡进度
  22. currentLevel: number;
  23. maxUnlockedLevel: number;
  24. levelProgress: { [levelId: number]: LevelProgress };
  25. // 道具和装备
  26. inventory: InventoryData;
  27. // 统计数据
  28. statistics: GameStatistics;
  29. // 设置
  30. settings: PlayerSettings;
  31. }
  32. /**
  33. * 关卡进度数据
  34. */
  35. export interface LevelProgress {
  36. levelId: number;
  37. completed: boolean;
  38. bestScore: number;
  39. bestTime: number;
  40. stars: number;
  41. attempts: number;
  42. firstClearTime: number;
  43. lastPlayTime: number;
  44. rewards: RewardData[];
  45. }
  46. /**
  47. * 背包数据
  48. */
  49. export interface InventoryData {
  50. weapons: { [weaponId: string]: WeaponData };
  51. items: { [itemId: string]: ItemData };
  52. materials: { [materialId: string]: number };
  53. capacity: number;
  54. usedSlots: number;
  55. }
  56. /**
  57. * 武器数据
  58. */
  59. export interface WeaponData {
  60. weaponId: string;
  61. level: number;
  62. experience: number;
  63. rarity: string;
  64. obtainTime: number;
  65. upgradeCount: number;
  66. isEquipped: boolean;
  67. }
  68. /**
  69. * 道具数据
  70. */
  71. export interface ItemData {
  72. itemId: string;
  73. count: number;
  74. obtainTime: number;
  75. lastUsedTime: number;
  76. }
  77. /**
  78. * 奖励数据
  79. */
  80. export interface RewardData {
  81. type: 'coins' | 'diamonds' | 'gems' | 'weapon' | 'item' | 'experience';
  82. id?: string;
  83. amount: number;
  84. obtainTime: number;
  85. source: string; // 获得来源:level_complete, daily_reward, shop_purchase等
  86. }
  87. /**
  88. * 游戏统计数据
  89. */
  90. export interface GameStatistics {
  91. totalGamesPlayed: number;
  92. totalWins: number;
  93. totalLosses: number;
  94. totalScore: number;
  95. totalEnemiesDefeated: number;
  96. totalShotsfired: number;
  97. totalDamageDealt: number;
  98. totalTimePlayed: number;
  99. highestLevel: number;
  100. consecutiveWins: number;
  101. bestWinStreak: number;
  102. favoriteWeapon: string;
  103. weaponsUnlocked: number;
  104. itemsCollected: number;
  105. }
  106. /**
  107. * 玩家设置
  108. */
  109. export interface PlayerSettings {
  110. soundEnabled: boolean;
  111. musicEnabled: boolean;
  112. soundVolume: number;
  113. musicVolume: number;
  114. vibrationEnabled: boolean;
  115. autoSaveEnabled: boolean;
  116. language: string;
  117. graphics: 'low' | 'medium' | 'high';
  118. }
  119. /**
  120. * 存档管理器
  121. * 负责玩家数据的保存、加载和管理
  122. */
  123. @ccclass('SaveDataManager')
  124. export class SaveDataManager {
  125. private static instance: SaveDataManager = null;
  126. private playerData: PlayerData = null;
  127. private initialized: boolean = false;
  128. private autoSaveInterval: number = 30; // 自动保存间隔(秒)
  129. private lastSaveTime: number = 0;
  130. // 存档文件键名
  131. private readonly SAVE_KEY = 'pong_game_save_data';
  132. private readonly BACKUP_KEY = 'pong_game_save_data_backup';
  133. private readonly SETTINGS_KEY = 'pong_game_settings';
  134. private constructor() {
  135. // 私有构造函数,确保单例模式
  136. }
  137. public static getInstance(): SaveDataManager {
  138. if (SaveDataManager.instance === null) {
  139. SaveDataManager.instance = new SaveDataManager();
  140. }
  141. return SaveDataManager.instance;
  142. }
  143. /**
  144. * 初始化存档管理器
  145. */
  146. public initialize(): void {
  147. if (this.initialized) return;
  148. this.loadPlayerData();
  149. this.initialized = true;
  150. }
  151. /**
  152. * 加载玩家数据
  153. */
  154. private loadPlayerData(): void {
  155. try {
  156. const savedData = sys.localStorage.getItem(this.SAVE_KEY);
  157. if (savedData) {
  158. const data = JSON.parse(savedData);
  159. this.playerData = this.validateAndMigrateData(data);
  160. this.playerData.lastPlayTime = Date.now();
  161. } else {
  162. this.createNewPlayerData();
  163. }
  164. } catch (error) {
  165. console.error('❌ 存档数据加载失败,尝试加载备份:', error);
  166. this.loadBackupData();
  167. }
  168. }
  169. /**
  170. * 加载备份数据
  171. */
  172. private loadBackupData(): void {
  173. try {
  174. const backupData = sys.localStorage.getItem(this.BACKUP_KEY);
  175. if (backupData) {
  176. const data = JSON.parse(backupData);
  177. this.playerData = this.validateAndMigrateData(data);
  178. this.playerData.lastPlayTime = Date.now();
  179. } else {
  180. this.createNewPlayerData();
  181. }
  182. } catch (error) {
  183. console.error('❌ 备份数据加载失败,创建新存档:', error);
  184. this.createNewPlayerData();
  185. }
  186. }
  187. /**
  188. * 创建新的玩家数据
  189. */
  190. private createNewPlayerData(): void {
  191. this.playerData = {
  192. playerId: this.generatePlayerId(),
  193. playerName: 'Player',
  194. createTime: Date.now(),
  195. lastPlayTime: Date.now(),
  196. totalPlayTime: 0,
  197. playerLevel: 1,
  198. experience: 0,
  199. experienceToNext: 100,
  200. coins: 1000, // 初始金币
  201. diamonds: 50, // 初始钻石
  202. gems: 0,
  203. currentLevel: 1,
  204. maxUnlockedLevel: 1,
  205. levelProgress: {},
  206. inventory: {
  207. weapons: {},
  208. items: {},
  209. materials: {},
  210. capacity: 50,
  211. usedSlots: 0
  212. },
  213. statistics: {
  214. totalGamesPlayed: 0,
  215. totalWins: 0,
  216. totalLosses: 0,
  217. totalScore: 0,
  218. totalEnemiesDefeated: 0,
  219. totalShotsfired: 0,
  220. totalDamageDealt: 0,
  221. totalTimePlayed: 0,
  222. highestLevel: 1,
  223. consecutiveWins: 0,
  224. bestWinStreak: 0,
  225. favoriteWeapon: '',
  226. weaponsUnlocked: 0,
  227. itemsCollected: 0
  228. },
  229. settings: {
  230. soundEnabled: true,
  231. musicEnabled: true,
  232. soundVolume: 0.8,
  233. musicVolume: 0.6,
  234. vibrationEnabled: true,
  235. autoSaveEnabled: true,
  236. language: 'zh-CN',
  237. graphics: 'medium'
  238. }
  239. };
  240. this.savePlayerData();
  241. }
  242. /**
  243. * 验证和迁移数据(用于版本兼容)
  244. */
  245. private validateAndMigrateData(data: any): PlayerData {
  246. // 确保所有必要字段存在
  247. const defaultData = this.createDefaultPlayerData();
  248. return {
  249. ...defaultData,
  250. ...data,
  251. // 确保嵌套对象也被正确合并
  252. inventory: { ...defaultData.inventory, ...data.inventory },
  253. statistics: { ...defaultData.statistics, ...data.statistics },
  254. settings: { ...defaultData.settings, ...data.settings }
  255. };
  256. }
  257. /**
  258. * 创建默认玩家数据模板
  259. */
  260. private createDefaultPlayerData(): PlayerData {
  261. return {
  262. playerId: this.generatePlayerId(),
  263. playerName: 'Player',
  264. createTime: Date.now(),
  265. lastPlayTime: Date.now(),
  266. totalPlayTime: 0,
  267. playerLevel: 1,
  268. experience: 0,
  269. experienceToNext: 100,
  270. coins: 1000,
  271. diamonds: 50,
  272. gems: 0,
  273. currentLevel: 1,
  274. maxUnlockedLevel: 1,
  275. levelProgress: {},
  276. inventory: {
  277. weapons: {},
  278. items: {},
  279. materials: {},
  280. capacity: 50,
  281. usedSlots: 0
  282. },
  283. statistics: {
  284. totalGamesPlayed: 0,
  285. totalWins: 0,
  286. totalLosses: 0,
  287. totalScore: 0,
  288. totalEnemiesDefeated: 0,
  289. totalShotsfired: 0,
  290. totalDamageDealt: 0,
  291. totalTimePlayed: 0,
  292. highestLevel: 1,
  293. consecutiveWins: 0,
  294. bestWinStreak: 0,
  295. favoriteWeapon: '',
  296. weaponsUnlocked: 0,
  297. itemsCollected: 0
  298. },
  299. settings: {
  300. soundEnabled: true,
  301. musicEnabled: true,
  302. soundVolume: 0.8,
  303. musicVolume: 0.6,
  304. vibrationEnabled: true,
  305. autoSaveEnabled: true,
  306. language: 'zh-CN',
  307. graphics: 'medium'
  308. }
  309. };
  310. }
  311. /**
  312. * 保存玩家数据
  313. */
  314. public savePlayerData(force: boolean = false): void {
  315. if (!this.playerData) return;
  316. const currentTime = Date.now();
  317. // 检查是否需要保存(避免频繁保存)
  318. if (!force && currentTime - this.lastSaveTime < 5000) {
  319. return;
  320. }
  321. try {
  322. // 更新最后保存时间
  323. this.playerData.lastPlayTime = currentTime;
  324. // 创建备份
  325. const currentData = sys.localStorage.getItem(this.SAVE_KEY);
  326. if (currentData) {
  327. sys.localStorage.setItem(this.BACKUP_KEY, currentData);
  328. }
  329. // 保存当前数据
  330. const dataToSave = JSON.stringify(this.playerData);
  331. sys.localStorage.setItem(this.SAVE_KEY, dataToSave);
  332. this.lastSaveTime = currentTime;
  333. } catch (error) {
  334. console.error('❌ 玩家数据保存失败:', error);
  335. }
  336. }
  337. /**
  338. * 自动保存检查
  339. */
  340. public checkAutoSave(): void {
  341. if (!this.playerData || !this.playerData.settings.autoSaveEnabled) return;
  342. const currentTime = Date.now();
  343. if (currentTime - this.lastSaveTime > this.autoSaveInterval * 1000) {
  344. this.savePlayerData();
  345. }
  346. }
  347. // === 玩家基本信息管理 ===
  348. public getPlayerData(): PlayerData {
  349. return this.playerData;
  350. }
  351. public getPlayerLevel(): number {
  352. return this.playerData?.playerLevel || 1;
  353. }
  354. public getCurrentLevel(): number {
  355. return this.playerData?.currentLevel || 1;
  356. }
  357. public getMaxUnlockedLevel(): number {
  358. return this.playerData?.maxUnlockedLevel || 1;
  359. }
  360. public getCoins(): number {
  361. return this.playerData?.coins || 0;
  362. }
  363. public getDiamonds(): number {
  364. return this.playerData?.diamonds || 0;
  365. }
  366. public getGems(): number {
  367. return this.playerData?.gems || 0;
  368. }
  369. // === 关卡进度管理 ===
  370. /**
  371. * 完成关卡
  372. */
  373. public completeLevel(levelId: number, score: number, time: number, stars: number): void {
  374. if (!this.playerData) return;
  375. // 更新关卡进度
  376. if (!this.playerData.levelProgress[levelId]) {
  377. this.playerData.levelProgress[levelId] = {
  378. levelId: levelId,
  379. completed: false,
  380. bestScore: 0,
  381. bestTime: 0,
  382. stars: 0,
  383. attempts: 0,
  384. firstClearTime: 0,
  385. lastPlayTime: 0,
  386. rewards: []
  387. };
  388. }
  389. const progress = this.playerData.levelProgress[levelId];
  390. progress.attempts += 1;
  391. progress.lastPlayTime = Date.now();
  392. // 首次完成
  393. if (!progress.completed) {
  394. progress.completed = true;
  395. progress.firstClearTime = Date.now();
  396. // 解锁下一关
  397. if (levelId === this.playerData.maxUnlockedLevel) {
  398. this.playerData.maxUnlockedLevel = levelId + 1;
  399. }
  400. }
  401. // 更新最佳记录
  402. if (score > progress.bestScore) {
  403. progress.bestScore = score;
  404. }
  405. if (time > 0 && (progress.bestTime === 0 || time < progress.bestTime)) {
  406. progress.bestTime = time;
  407. }
  408. if (stars > progress.stars) {
  409. progress.stars = stars;
  410. }
  411. // 更新统计数据
  412. this.playerData.statistics.totalGamesPlayed += 1;
  413. this.playerData.statistics.totalWins += 1;
  414. this.playerData.statistics.totalScore += score;
  415. this.playerData.statistics.consecutiveWins += 1;
  416. if (this.playerData.statistics.consecutiveWins > this.playerData.statistics.bestWinStreak) {
  417. this.playerData.statistics.bestWinStreak = this.playerData.statistics.consecutiveWins;
  418. }
  419. if (levelId > this.playerData.statistics.highestLevel) {
  420. this.playerData.statistics.highestLevel = levelId;
  421. }
  422. // 给予经验值
  423. this.addExperience(score / 10 + stars * 20);
  424. // 计算并给予奖励
  425. this.giveCompletionRewards(levelId, stars);
  426. this.savePlayerData();
  427. }
  428. /**
  429. * 关卡失败
  430. */
  431. public failLevel(levelId: number): void {
  432. if (!this.playerData) return;
  433. // 更新关卡进度
  434. if (!this.playerData.levelProgress[levelId]) {
  435. this.playerData.levelProgress[levelId] = {
  436. levelId: levelId,
  437. completed: false,
  438. bestScore: 0,
  439. bestTime: 0,
  440. stars: 0,
  441. attempts: 0,
  442. firstClearTime: 0,
  443. lastPlayTime: 0,
  444. rewards: []
  445. };
  446. }
  447. const progress = this.playerData.levelProgress[levelId];
  448. progress.attempts += 1;
  449. progress.lastPlayTime = Date.now();
  450. // 更新统计数据
  451. this.playerData.statistics.totalGamesPlayed += 1;
  452. this.playerData.statistics.totalLosses += 1;
  453. this.playerData.statistics.consecutiveWins = 0;
  454. this.savePlayerData();
  455. }
  456. /**
  457. * 获取关卡进度
  458. */
  459. public getLevelProgress(levelId: number): LevelProgress | null {
  460. return this.playerData?.levelProgress[levelId] || null;
  461. }
  462. /**
  463. * 检查关卡是否已解锁
  464. */
  465. public isLevelUnlocked(levelId: number): boolean {
  466. return levelId <= (this.playerData?.maxUnlockedLevel || 1);
  467. }
  468. /**
  469. * 检查关卡是否已完成
  470. */
  471. public isLevelCompleted(levelId: number): boolean {
  472. const progress = this.getLevelProgress(levelId);
  473. return progress?.completed || false;
  474. }
  475. // === 货币管理 ===
  476. /**
  477. * 添加金币
  478. */
  479. public addCoins(amount: number, source: string = 'unknown'): boolean {
  480. if (!this.playerData || amount <= 0) return false;
  481. this.playerData.coins += amount;
  482. this.addReward('coins', '', amount, source);
  483. return true;
  484. }
  485. /**
  486. * 消费金币
  487. */
  488. public spendCoins(amount: number): boolean {
  489. if (!this.playerData || amount <= 0 || this.playerData.coins < amount) {
  490. return false;
  491. }
  492. this.playerData.coins -= amount;
  493. return true;
  494. }
  495. /**
  496. * 添加钻石
  497. */
  498. public addDiamonds(amount: number, source: string = 'unknown'): boolean {
  499. if (!this.playerData || amount <= 0) return false;
  500. this.playerData.diamonds += amount;
  501. this.addReward('diamonds', '', amount, source);
  502. return true;
  503. }
  504. /**
  505. * 消费钻石
  506. */
  507. public spendDiamonds(amount: number): boolean {
  508. if (!this.playerData || amount <= 0 || this.playerData.diamonds < amount) {
  509. return false;
  510. }
  511. this.playerData.diamonds -= amount;
  512. return true;
  513. }
  514. /**
  515. * 添加宝石
  516. */
  517. public addGems(amount: number, source: string = 'unknown'): boolean {
  518. if (!this.playerData || amount <= 0) return false;
  519. this.playerData.gems += amount;
  520. this.addReward('gems', '', amount, source);
  521. return true;
  522. }
  523. // === 经验和等级管理 ===
  524. /**
  525. * 添加经验值
  526. */
  527. public addExperience(amount: number): boolean {
  528. if (!this.playerData || amount <= 0) return false;
  529. this.playerData.experience += amount;
  530. // 检查是否升级
  531. while (this.playerData.experience >= this.playerData.experienceToNext) {
  532. this.levelUp();
  533. }
  534. return true;
  535. }
  536. /**
  537. * 玩家升级
  538. */
  539. private levelUp(): void {
  540. this.playerData.experience -= this.playerData.experienceToNext;
  541. this.playerData.playerLevel += 1;
  542. // 计算下一级所需经验值
  543. this.playerData.experienceToNext = this.calculateExperienceToNext(this.playerData.playerLevel);
  544. // 升级奖励
  545. const coinReward = this.playerData.playerLevel * 100;
  546. const diamondReward = Math.floor(this.playerData.playerLevel / 5);
  547. this.addCoins(coinReward, 'level_up');
  548. if (diamondReward > 0) {
  549. this.addDiamonds(diamondReward, 'level_up');
  550. }
  551. }
  552. /**
  553. * 计算升级所需经验值
  554. */
  555. private calculateExperienceToNext(level: number): number {
  556. return 100 + (level - 1) * 50;
  557. }
  558. // === 道具和武器管理 ===
  559. /**
  560. * 添加武器
  561. */
  562. public addWeapon(weaponId: string, rarity: string = 'common'): boolean {
  563. if (!this.playerData) return false;
  564. if (!this.playerData.inventory.weapons[weaponId]) {
  565. this.playerData.inventory.weapons[weaponId] = {
  566. weaponId: weaponId,
  567. level: 1,
  568. experience: 0,
  569. rarity: rarity,
  570. obtainTime: Date.now(),
  571. upgradeCount: 0,
  572. isEquipped: false
  573. };
  574. this.playerData.statistics.weaponsUnlocked += 1;
  575. this.addReward('weapon', weaponId, 1, 'drop');
  576. return true;
  577. }
  578. return false;
  579. }
  580. /**
  581. * 添加道具
  582. */
  583. public addItem(itemId: string, count: number = 1): boolean {
  584. if (!this.playerData || count <= 0) return false;
  585. if (!this.playerData.inventory.items[itemId]) {
  586. this.playerData.inventory.items[itemId] = {
  587. itemId: itemId,
  588. count: 0,
  589. obtainTime: Date.now(),
  590. lastUsedTime: 0
  591. };
  592. }
  593. this.playerData.inventory.items[itemId].count += count;
  594. this.playerData.statistics.itemsCollected += count;
  595. this.addReward('item', itemId, count, 'drop');
  596. return true;
  597. }
  598. /**
  599. * 使用道具
  600. */
  601. public useItem(itemId: string, count: number = 1): boolean {
  602. if (!this.playerData || count <= 0) return false;
  603. const item = this.playerData.inventory.items[itemId];
  604. if (!item || item.count < count) {
  605. return false;
  606. }
  607. item.count -= count;
  608. item.lastUsedTime = Date.now();
  609. if (item.count === 0) {
  610. delete this.playerData.inventory.items[itemId];
  611. }
  612. return true;
  613. }
  614. // === 奖励管理 ===
  615. /**
  616. * 添加奖励记录
  617. */
  618. private addReward(type: RewardData['type'], id: string, amount: number, source: string): void {
  619. if (!this.playerData) return;
  620. const reward: RewardData = {
  621. type: type,
  622. id: id,
  623. amount: amount,
  624. obtainTime: Date.now(),
  625. source: source
  626. };
  627. // 这里可以添加到全局奖励历史记录中
  628. // 暂时不实现,避免数据过大
  629. }
  630. /**
  631. * 给予关卡完成奖励
  632. */
  633. private giveCompletionRewards(levelId: number, stars: number): void {
  634. // 基础金币奖励
  635. const baseCoins = levelId * 50;
  636. const starBonus = stars * 25;
  637. const totalCoins = baseCoins + starBonus;
  638. this.addCoins(totalCoins, `level_${levelId}_complete`);
  639. // 钻石奖励(3星才给)
  640. if (stars >= 3) {
  641. const diamonds = Math.max(1, Math.floor(levelId / 3));
  642. this.addDiamonds(diamonds, `level_${levelId}_perfect`);
  643. }
  644. // 特殊关卡额外奖励
  645. if (levelId % 10 === 0) {
  646. this.addGems(1, `level_${levelId}_milestone`);
  647. }
  648. }
  649. // === 统计数据更新 ===
  650. public updateStatistic(key: keyof GameStatistics, value: number): void {
  651. if (!this.playerData) return;
  652. if (typeof this.playerData.statistics[key] === 'number') {
  653. (this.playerData.statistics[key] as number) += value;
  654. }
  655. }
  656. // === 设置管理 ===
  657. public updateSetting<K extends keyof PlayerSettings>(key: K, value: PlayerSettings[K]): void {
  658. if (!this.playerData) return;
  659. this.playerData.settings[key] = value;
  660. this.savePlayerData();
  661. }
  662. public getSetting<K extends keyof PlayerSettings>(key: K): PlayerSettings[K] {
  663. return this.playerData?.settings[key];
  664. }
  665. // === 工具方法 ===
  666. /**
  667. * 生成唯一玩家ID
  668. */
  669. private generatePlayerId(): string {
  670. return `player_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  671. }
  672. /**
  673. * 重置所有数据
  674. */
  675. public resetAllData(): void {
  676. sys.localStorage.removeItem(this.SAVE_KEY);
  677. sys.localStorage.removeItem(this.BACKUP_KEY);
  678. this.createNewPlayerData();
  679. }
  680. /**
  681. * 导出存档数据(用于调试)
  682. */
  683. public exportSaveData(): string {
  684. return JSON.stringify(this.playerData, null, 2);
  685. }
  686. /**
  687. * 导入存档数据(用于调试)
  688. */
  689. public importSaveData(dataString: string): boolean {
  690. try {
  691. const data = JSON.parse(dataString);
  692. this.playerData = this.validateAndMigrateData(data);
  693. this.savePlayerData(true);
  694. return true;
  695. } catch (error) {
  696. console.error('❌ 存档数据导入失败:', error);
  697. return false;
  698. }
  699. }
  700. /**
  701. * 获取存档摘要信息
  702. */
  703. public getSaveSummary(): string {
  704. if (!this.playerData) return '无存档数据';
  705. return `玩家等级: ${this.playerData.playerLevel} | ` +
  706. `当前关卡: ${this.playerData.currentLevel} | ` +
  707. `最高关卡: ${this.playerData.maxUnlockedLevel} | ` +
  708. `金币: ${this.playerData.coins} | ` +
  709. `钻石: ${this.playerData.diamonds} | ` +
  710. `游戏次数: ${this.playerData.statistics.totalGamesPlayed}`;
  711. }
  712. }