| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- import { _decorator, Component, Node, Prefab, instantiate, Vec3, Sprite, SpriteFrame, resources, Texture2D, Vec2, Size, Rect } 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) {
- console.error('ConfigManager实例获取失败');
- return;
- }
-
- // 等待配置加载完成后创建示例方块
- this.scheduleOnce(() => {
- this.createExampleWeaponBlocks();
- }, 2.0); // 增加等待时间
- }
- // 创建示例武器方块
- private createExampleWeaponBlocks() {
- if (!this.configManager) {
- console.error('配置管理器未初始化');
- return;
- }
-
- if (!this.configManager.isConfigLoaded()) {
- console.warn('配置尚未加载完成,延迟创建');
- this.scheduleOnce(() => {
- this.createExampleWeaponBlocks();
- }, 1.0);
- return;
- }
- // 检查必要的组件
- if (!this.blockPrefab) {
- console.error('方块预制体未设置,请在编辑器中设置blockPrefab');
- return;
- }
-
- // 创建几个不同稀有度的武器方块
- this.createWeaponBlock('common', new Vec3(-200, 0, 0));
- this.createWeaponBlock('uncommon', new Vec3(-100, 0, 0));
- this.createWeaponBlock('rare', new Vec3(0, 0, 0));
- this.createWeaponBlock('epic', new Vec3(100, 0, 0));
-
- // 创建随机武器方块
- this.createRandomWeaponBlock(new Vec3(200, 0, 0));
- }
- // 创建指定稀有度的武器方块
- private createWeaponBlock(rarity: string, position: Vec3) {
- const weaponConfig = this.configManager.getRandomWeapon(rarity);
- if (!weaponConfig) {
- console.warn(`无法获取稀有度为 ${rarity} 的武器配置`);
- return;
- }
- this.createBlockWithWeapon(weaponConfig, position);
- }
- // 创建随机武器方块
- private createRandomWeaponBlock(position: Vec3) {
- const weaponConfig = this.configManager.getRandomWeapon();
- if (!weaponConfig) {
- console.warn('无法获取随机武器配置');
- return;
- }
- this.createBlockWithWeapon(weaponConfig, position);
- }
- // 根据武器配置创建方块
- private createBlockWithWeapon(weaponConfig: WeaponConfig, position: Vec3) {
- if (!this.blockPrefab) {
- console.error('方块预制体未设置');
- return;
- }
- // 实例化方块
- const blockNode = instantiate(this.blockPrefab);
- if (!blockNode) {
- console.error('方块实例化失败');
- return;
- }
- // 设置位置
- blockNode.setPosition(position);
- // 添加到容器
- if (this.blockContainer) {
- this.blockContainer.addChild(blockNode);
- } else {
- this.node.addChild(blockNode);
- }
- // 设置方块的武器配置
- this.setupBlockWeapon(blockNode, weaponConfig);
- }
- // 设置方块的武器配置
- private setupBlockWeapon(blockNode: Node, weaponConfig: WeaponConfig) {
- // 暂时不添加WeaponComponent,避免组件重复问题
- // 直接在节点上存储武器配置数据
- (blockNode as any).weaponConfig = weaponConfig;
- // 设置方块外观
- this.setupBlockVisual(blockNode, weaponConfig);
- // 设置方块名称
- blockNode.name = `WeaponBlock_${weaponConfig.id}`;
- }
- // 设置方块外观
- private setupBlockVisual(blockNode: Node, weaponConfig: WeaponConfig) {
- const spriteComponent = blockNode.getComponent(Sprite);
- if (!spriteComponent) {
- console.warn('方块节点没有Sprite组件');
- return;
- }
- // 根据稀有度设置颜色
- const rarityColors = {
- 'common': { r: 200, g: 200, b: 200, a: 255 }, // 灰色
- 'uncommon': { r: 0, g: 255, b: 0, a: 255 }, // 绿色
- 'rare': { r: 0, g: 100, b: 255, a: 255 }, // 蓝色
- 'epic': { r: 160, g: 32, b: 240, a: 255 } // 紫色
- };
- const color = rarityColors[weaponConfig.rarity] || rarityColors['common'];
- spriteComponent.color = spriteComponent.color.set(color.r, color.g, color.b, color.a);
- // 添加武器名称标识到节点名称中
- blockNode.name = `WeaponBlock_${weaponConfig.id}_${weaponConfig.name}`;
-
- // 查找Weapon子节点并加载武器图标
- this.loadWeaponSpriteToWeaponNode(blockNode, weaponConfig);
- }
- // 将武器图标加载到Weapon子节点
- private loadWeaponSpriteToWeaponNode(blockNode: Node, weaponConfig: WeaponConfig) {
- // 查找B1子节点
- const b1Node = blockNode.getChildByName('B1');
- if (!b1Node) {
- return;
- }
- // 在B1子节点下查找Weapon子节点
- const weaponNode = b1Node.getChildByName('Weapon');
- if (!weaponNode) {
- return;
- }
- const weaponSprite = weaponNode.getComponent(Sprite);
- if (!weaponSprite) {
- return;
- }
- // 加载武器图标
- this.loadWeaponSprite(weaponSprite, weaponConfig);
- }
- // 加载武器图标
- private loadWeaponSprite(spriteComponent: Sprite, weaponConfig: WeaponConfig) {
- // 默认使用1x1尺寸的图标
- const spritePath = weaponConfig.visualConfig.weaponSprites['1x1'];
- if (!spritePath) {
- return;
- }
- // 从路径中提取文件名
- const fileName = spritePath.replace('images/PlantsSprite/', '');
- // 通过loadDir获取资源并直接分配
- resources.loadDir('images/PlantsSprite', (err, assets) => {
- if (err) return;
-
- const spriteFrames = assets.filter(asset => asset instanceof SpriteFrame);
- const matchingAsset = spriteFrames.find(asset => asset.name === fileName);
-
- if (matchingAsset) {
- spriteComponent.spriteFrame = matchingAsset;
- }
- });
- }
- // 获取方块的武器配置(供其他脚本调用)
- 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, position);
- 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;
- }
- }
|