import { _decorator, Component, Node, Prefab, instantiate, Vec3, Sprite, SpriteFrame, resources, Texture2D, Vec2, Size, Rect, Color } from 'cc'; import { ConfigManager, WeaponConfig } from '../Core/ConfigManager'; const { ccclass, property } = _decorator; /** * 武器方块示例脚本 * 展示如何使用ConfigManager来创建带有武器配置的方块 */ @ccclass('WeaponBlockExample') export class WeaponBlockExample extends Component { @property({ type: Prefab, tooltip: '基础方块预制体' }) public blockPrefab: Prefab = null; @property({ type: Node, tooltip: '方块容器节点' }) public blockContainer: Node = null; private configManager: ConfigManager = null; start() { console.log('WeaponBlockExample start()'); // 获取配置管理器 this.configManager = ConfigManager.getInstance(); if (!this.configManager) { return; } // 等待配置加载完成后创建方块 this.scheduleOnce(() => { this.initializeBlocks(); }, 0.5); } private initializeBlocks() { // 检查配置管理器是否已初始化 if (!this.configManager) { return; } // 检查配置是否已加载完成 if (!this.configManager.isConfigLoaded()) { // 如果配置尚未加载完成,延迟创建 this.scheduleOnce(() => { this.initializeBlocks(); }, 1.0); return; } // 检查方块预制体是否已设置 if (!this.blockPrefab) { return; } // 创建5个不同稀有度的武器方块 this.createWeaponBlock('common'); this.createWeaponBlock('uncommon'); this.createWeaponBlock('rare'); this.createWeaponBlock('epic'); this.createRandomWeaponBlock(); } private createWeaponBlock(rarity: string) { // 获取指定稀有度的武器 const weapons = this.configManager.getWeaponsByRarity(rarity); if (weapons.length === 0) { return; } // 随机选择一个武器 const weapon = weapons[Math.floor(Math.random() * weapons.length)]; this.createBlockWithWeapon(weapon); } private createRandomWeaponBlock() { // 获取随机武器 const weapon = this.configManager.getRandomWeapon(); if (!weapon) { return; } this.createBlockWithWeapon(weapon); } private createBlockWithWeapon(weaponConfig: any) { // 检查方块预制体 if (!this.blockPrefab) { return; } // 实例化方块 const blockNode = instantiate(this.blockPrefab); if (!blockNode) { return; } // 设置方块名称 blockNode.name = `WeaponBlock_${weaponConfig.id}`; // 添加到场景中 this.node.addChild(blockNode); // 设置方块位置(简单的网格布局) const index = this.node.children.length - 1; const x = (index % 5) * 120 - 240; // 5列布局,间距120 const y = Math.floor(index / 5) * 120; // 行间距120 blockNode.position = new Vec3(x, y, 0); // 设置武器配置到方块 this.setupBlockWeapon(blockNode, weaponConfig); console.log(`生成武器方块: ${weaponConfig.name} (${weaponConfig.rarity})`); } // 设置方块的武器配置 private setupBlockWeapon(blockNode: Node, weaponConfig: WeaponConfig) { // 设置方块的武器ID blockNode['weaponId'] = weaponConfig.id; // 设置方块的稀有度颜色 this.setBlockRarityColor(blockNode, weaponConfig.rarity); // 加载武器图标 this.loadWeaponIcon(blockNode, weaponConfig); } // 设置方块稀有度颜色 private setBlockRarityColor(blockNode: Node, rarity: string) { const sprite = blockNode.getComponent(Sprite); if (!sprite) { return; } // 根据稀有度设置颜色 let color: Color; switch (rarity) { case 'common': color = new Color(255, 255, 255); // 白色 break; case 'uncommon': color = new Color(0, 255, 0); // 绿色 break; case 'rare': color = new Color(0, 100, 255); // 蓝色 break; case 'epic': color = new Color(160, 32, 240); // 紫色 break; case 'legendary': color = new Color(255, 165, 0); // 橙色 break; default: color = new Color(255, 255, 255); // 默认白色 } sprite.color = color; } // 加载武器图标 private loadWeaponIcon(blockNode: Node, weaponConfig: WeaponConfig) { // 这里应该根据weaponConfig加载对应的武器图标 // 由于没有实际的图标资源,这里只是示例代码 // 寻找方块中的图标节点 const iconNode = blockNode.getChildByName('Icon'); if (!iconNode) { return; } const iconSprite = iconNode.getComponent(Sprite); if (!iconSprite) { return; } // 根据武器类型加载不同的图标 // 这里需要根据实际的资源路径来加载 const iconPath = `images/PlantsSprite/${weaponConfig.id}`; resources.load(iconPath, SpriteFrame, (err, spriteFrame) => { if (err) { // 如果加载失败,使用默认图标或保持原样 return; } if (iconSprite && spriteFrame) { iconSprite.spriteFrame = spriteFrame; } }); } // 根据武器ID获取武器配置 public getWeaponConfigById(weaponId: string): WeaponConfig | null { if (!this.configManager) { return null; } const weaponConfig = this.configManager.getWeaponById(weaponId); if (!weaponConfig) { return null; } return weaponConfig; } // 获取方块的武器配置(供其他脚本调用) public static getBlockWeaponConfig(blockNode: Node): WeaponConfig | null { return (blockNode as any).weaponConfig || null; } // 创建指定ID的武器方块 public createWeaponBlockById(weaponId: string, position: Vec3): Node | null { const weaponConfig = this.configManager.getWeaponById(weaponId); if (!weaponConfig) { console.warn(`找不到ID为 ${weaponId} 的武器配置`); return null; } this.createBlockWithWeapon(weaponConfig); return null; // 实际项目中应该返回创建的节点 } // 获取所有可用的武器ID列表 public getAvailableWeaponIds(): string[] { if (!this.configManager || !this.configManager.isConfigLoaded()) { return []; } return this.configManager.getAllWeapons().map(weapon => weapon.id); } // 获取指定稀有度的武器数量 public getWeaponCountByRarity(rarity: string): number { if (!this.configManager || !this.configManager.isConfigLoaded()) { return 0; } return this.configManager.getWeaponsByRarity(rarity).length; } }