SaveDataManager.ts 36 KB

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