| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- 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);
- }
- }
|