SaveDataManager.ts 39 KB

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