|
|
@@ -1,348 +0,0 @@
|
|
|
-import { _decorator, sys } from 'cc';
|
|
|
-import { ConfigManager, WeaponConfig } from '../Core/ConfigManager';
|
|
|
-import { BaseSingleton } from '../Core/BaseSingleton';
|
|
|
-const { ccclass, property } = _decorator;
|
|
|
-
|
|
|
-/**
|
|
|
- * 商店物品接口
|
|
|
- */
|
|
|
-export interface ShopItem {
|
|
|
- id: string;
|
|
|
- name: string;
|
|
|
- type: 'weapon' | 'upgrade' | 'consumable';
|
|
|
- price: number;
|
|
|
- currency: 'coin' | 'gem';
|
|
|
- weaponId?: string; // 如果是武器类型
|
|
|
- description: string;
|
|
|
- icon: string;
|
|
|
- available: boolean;
|
|
|
- purchased: boolean;
|
|
|
- maxPurchases?: number; // 最大购买次数,-1为无限
|
|
|
- currentPurchases?: number; // 当前购买次数
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * 商店管理器
|
|
|
- * 负责商店物品的购买、货币管理等功能
|
|
|
- */
|
|
|
-@ccclass('ShopManager')
|
|
|
-export class ShopManager extends BaseSingleton {
|
|
|
- // 仅用于类型声明,实例由 BaseSingleton 维护
|
|
|
- public static _instance: ShopManager;
|
|
|
-
|
|
|
- private coins: number = 0;
|
|
|
- private gems: number = 0;
|
|
|
- private purchaseHistory: any = {};
|
|
|
- private itemPurchaseCounts: any = {};
|
|
|
- private initialized: boolean = false;
|
|
|
- private shopItems: ShopItem[] = [];
|
|
|
- private purchasedItems: string[] = [];
|
|
|
-
|
|
|
- /**
|
|
|
- * BaseSingleton 首次实例化回调
|
|
|
- */
|
|
|
- protected init() {
|
|
|
- if (!this.initialized) {
|
|
|
- this.loadShopData();
|
|
|
- this.initializeShopItems();
|
|
|
- this.initialized = true;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 初始化商店物品
|
|
|
- private initializeShopItems() {
|
|
|
- // 基础武器商店物品
|
|
|
- this.shopItems = [
|
|
|
- {
|
|
|
- id: 'weapon_basic_gun',
|
|
|
- name: '基础手枪',
|
|
|
- type: 'weapon',
|
|
|
- price: 50,
|
|
|
- currency: 'coin',
|
|
|
- weaponId: 'basic_gun',
|
|
|
- description: '基础的射击武器,伤害适中',
|
|
|
- icon: 'shop/weapon_basic_gun',
|
|
|
- available: true,
|
|
|
- purchased: false,
|
|
|
- maxPurchases: 1,
|
|
|
- currentPurchases: 0
|
|
|
- },
|
|
|
- {
|
|
|
- id: 'weapon_sniper',
|
|
|
- name: '狙击枪',
|
|
|
- type: 'weapon',
|
|
|
- price: 150,
|
|
|
- currency: 'coin',
|
|
|
- weaponId: 'sniper_rifle',
|
|
|
- description: '高伤害远程武器',
|
|
|
- icon: 'shop/weapon_sniper',
|
|
|
- available: true,
|
|
|
- purchased: false,
|
|
|
- maxPurchases: 1,
|
|
|
- currentPurchases: 0
|
|
|
- },
|
|
|
- {
|
|
|
- id: 'weapon_rocket',
|
|
|
- name: '火箭筒',
|
|
|
- type: 'weapon',
|
|
|
- price: 5,
|
|
|
- currency: 'gem',
|
|
|
- weaponId: 'rocket_launcher',
|
|
|
- description: '范围爆炸伤害武器',
|
|
|
- icon: 'shop/weapon_rocket',
|
|
|
- available: true,
|
|
|
- purchased: false,
|
|
|
- maxPurchases: 1,
|
|
|
- currentPurchases: 0
|
|
|
- },
|
|
|
- {
|
|
|
- id: 'upgrade_damage',
|
|
|
- name: '伤害提升',
|
|
|
- type: 'upgrade',
|
|
|
- price: 100,
|
|
|
- currency: 'coin',
|
|
|
- description: '永久增加所有武器10%伤害',
|
|
|
- icon: 'shop/upgrade_damage',
|
|
|
- available: true,
|
|
|
- purchased: false,
|
|
|
- maxPurchases: 5,
|
|
|
- currentPurchases: 0
|
|
|
- },
|
|
|
- {
|
|
|
- id: 'consumable_health_potion',
|
|
|
- name: '生命药水',
|
|
|
- type: 'consumable',
|
|
|
- price: 20,
|
|
|
- currency: 'coin',
|
|
|
- description: '恢复50点生命值',
|
|
|
- icon: 'shop/health_potion',
|
|
|
- available: true,
|
|
|
- purchased: false,
|
|
|
- maxPurchases: -1, // 无限购买
|
|
|
- currentPurchases: 0
|
|
|
- }
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- // 加载商店数据
|
|
|
- private loadShopData() {
|
|
|
- const savedData = sys.localStorage.getItem('shopData');
|
|
|
- if (savedData) {
|
|
|
- try {
|
|
|
- const data = JSON.parse(savedData);
|
|
|
- this.coins = data.coins || 0;
|
|
|
- this.gems = data.gems || 0;
|
|
|
- this.purchaseHistory = data.purchaseHistory || {};
|
|
|
- this.itemPurchaseCounts = data.itemPurchaseCounts || {};
|
|
|
- this.purchasedItems = data.purchasedItems || [];
|
|
|
- } catch (error) {
|
|
|
- this.resetShopData();
|
|
|
- }
|
|
|
- } else {
|
|
|
- this.resetShopData();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 保存商店数据
|
|
|
- private saveShopData() {
|
|
|
- const data = {
|
|
|
- coins: this.coins,
|
|
|
- gems: this.gems,
|
|
|
- purchaseHistory: this.purchaseHistory,
|
|
|
- itemPurchaseCounts: this.itemPurchaseCounts,
|
|
|
- purchasedItems: this.purchasedItems,
|
|
|
- shopItems: this.shopItems.map(item => ({
|
|
|
- id: item.id,
|
|
|
- purchased: item.purchased,
|
|
|
- currentPurchases: item.currentPurchases
|
|
|
- }))
|
|
|
- };
|
|
|
-
|
|
|
- try {
|
|
|
- sys.localStorage.setItem('shopData', JSON.stringify(data));
|
|
|
- } catch (error) {
|
|
|
- // 静默处理保存错误
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 重置商店数据
|
|
|
- private resetShopData() {
|
|
|
- this.coins = 100;
|
|
|
- this.gems = 10;
|
|
|
- this.purchaseHistory = {};
|
|
|
- this.itemPurchaseCounts = {};
|
|
|
- this.purchasedItems = [];
|
|
|
- this.saveShopData();
|
|
|
- }
|
|
|
-
|
|
|
- // 获取金币数量
|
|
|
- public getCoins(): number {
|
|
|
- return this.coins;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取宝石数量
|
|
|
- public getGems(): number {
|
|
|
- return this.gems;
|
|
|
- }
|
|
|
-
|
|
|
- // 添加金币
|
|
|
- public addCoins(amount: number) {
|
|
|
- this.coins += amount;
|
|
|
- this.saveShopData();
|
|
|
- }
|
|
|
-
|
|
|
- // 添加宝石
|
|
|
- public addGems(amount: number) {
|
|
|
- this.gems += amount;
|
|
|
- this.saveShopData();
|
|
|
- }
|
|
|
-
|
|
|
- // 消费金币
|
|
|
- public spendCoins(amount: number): boolean {
|
|
|
- if (this.coins >= amount) {
|
|
|
- this.coins -= amount;
|
|
|
- this.saveShopData();
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 消费宝石
|
|
|
- public spendGems(amount: number): boolean {
|
|
|
- if (this.gems >= amount) {
|
|
|
- this.gems -= amount;
|
|
|
- this.saveShopData();
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取所有商店物品
|
|
|
- public getShopItems(): ShopItem[] {
|
|
|
- return this.shopItems.filter(item => item.available);
|
|
|
- }
|
|
|
-
|
|
|
- // 获取特定类型的商店物品
|
|
|
- public getShopItemsByType(type: 'weapon' | 'upgrade' | 'consumable'): ShopItem[] {
|
|
|
- return this.shopItems.filter(item => item.type === type && item.available);
|
|
|
- }
|
|
|
-
|
|
|
- // 获取单个商店物品
|
|
|
- public getShopItem(itemId: string): ShopItem | null {
|
|
|
- return this.shopItems.find(item => item.id === itemId) || null;
|
|
|
- }
|
|
|
-
|
|
|
- // 购买物品
|
|
|
- public purchaseItem(itemId: string, price: number, currency: 'coins' | 'gems' = 'coins', maxPurchases: number = -1): boolean {
|
|
|
- // 检查购买次数限制
|
|
|
- if (maxPurchases > 0) {
|
|
|
- const currentCount = this.itemPurchaseCounts[itemId] || 0;
|
|
|
- if (currentCount >= maxPurchases) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 检查货币并扣除
|
|
|
- let success = false;
|
|
|
- if (currency === 'coins') {
|
|
|
- success = this.spendCoins(price);
|
|
|
- } else {
|
|
|
- success = this.spendGems(price);
|
|
|
- }
|
|
|
-
|
|
|
- if (success) {
|
|
|
- // 记录购买历史
|
|
|
- if (!this.purchaseHistory[itemId]) {
|
|
|
- this.purchaseHistory[itemId] = [];
|
|
|
- }
|
|
|
- this.purchaseHistory[itemId].push({
|
|
|
- price: price,
|
|
|
- currency: currency,
|
|
|
- timestamp: Date.now()
|
|
|
- });
|
|
|
-
|
|
|
- // 更新购买次数
|
|
|
- this.itemPurchaseCounts[itemId] = (this.itemPurchaseCounts[itemId] || 0) + 1;
|
|
|
-
|
|
|
- this.saveShopData();
|
|
|
- }
|
|
|
-
|
|
|
- return success;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取物品购买次数
|
|
|
- public getItemPurchaseCount(itemId: string): number {
|
|
|
- return this.itemPurchaseCounts[itemId] || 0;
|
|
|
- }
|
|
|
-
|
|
|
- // 检查是否可以购买物品
|
|
|
- public canPurchaseItem(itemId: string, price: number, currency: 'coins' | 'gems' = 'coins', maxPurchases: number = -1): boolean {
|
|
|
- // 检查购买次数限制
|
|
|
- if (maxPurchases > 0) {
|
|
|
- const currentCount = this.itemPurchaseCounts[itemId] || 0;
|
|
|
- if (currentCount >= maxPurchases) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 检查货币是否足够
|
|
|
- if (currency === 'coins') {
|
|
|
- return this.coins >= price;
|
|
|
- } else {
|
|
|
- return this.gems >= price;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 获取购买历史
|
|
|
- public getPurchaseHistory(itemId?: string): any {
|
|
|
- if (itemId) {
|
|
|
- return this.purchaseHistory[itemId] || [];
|
|
|
- }
|
|
|
- return this.purchaseHistory;
|
|
|
- }
|
|
|
-
|
|
|
- // 重置所有购买数据
|
|
|
- public resetAllPurchases() {
|
|
|
- this.resetShopData();
|
|
|
- }
|
|
|
-
|
|
|
- // 调试用:添加大量货币
|
|
|
- public addDebugCurrency() {
|
|
|
- this.coins += 10000;
|
|
|
- this.gems += 1000;
|
|
|
- this.saveShopData();
|
|
|
- }
|
|
|
-
|
|
|
- // 检查物品是否已购买
|
|
|
- public isItemPurchased(itemId: string): boolean {
|
|
|
- return this.purchasedItems.indexOf(itemId) !== -1;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取已购买的武器列表
|
|
|
- public getPurchasedWeapons(): string[] {
|
|
|
- const purchasedWeaponItems = this.shopItems.filter(item =>
|
|
|
- item.type === 'weapon' && this.isItemPurchased(item.id)
|
|
|
- );
|
|
|
- return purchasedWeaponItems.map(item => item.weaponId).filter(id => id);
|
|
|
- }
|
|
|
-
|
|
|
- // 重置所有购买记录(调试用)
|
|
|
- public resetPurchases() {
|
|
|
- this.shopItems.forEach(item => {
|
|
|
- item.purchased = false;
|
|
|
- item.currentPurchases = 0;
|
|
|
- });
|
|
|
- this.purchasedItems = [];
|
|
|
- this.saveShopData();
|
|
|
- }
|
|
|
-
|
|
|
- // 添加免费金币(调试用)
|
|
|
- public addFreeCoins(amount: number = 1000) {
|
|
|
- this.addCoins(amount);
|
|
|
- }
|
|
|
-
|
|
|
- // 添加免费宝石(调试用)
|
|
|
- public addFreeGems(amount: number = 100) {
|
|
|
- this.addGems(amount);
|
|
|
- }
|
|
|
-}
|